Skip to content

Commit c52cdaf

Browse files
committed
change: Add implementation
1 parent b2c0a2f commit c52cdaf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1655
-0
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ proguard/
2525

2626
# Log Files
2727
*.log
28+
29+
# Gradle files
30+
.gradle/
31+
build/
32+
33+
# Android Studio files
34+
.idea/
35+
*.iml

.gitmodules

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "submodules/AndroidLib"]
2+
path = submodules/AndroidLib
3+
url = https://github.com/yongce/AndroidLib.git
4+
[submodule "submodules/libsuperuser"]
5+
path = submodules/libsuperuser
6+
url = https://github.com/yongce/libsuperuser.git

build.gradle

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
buildscript {
2+
repositories {
3+
jcenter()
4+
}
5+
dependencies {
6+
classpath 'com.android.tools.build:gradle:1.1.3'
7+
}
8+
}
9+
10+
ext {
11+
compileSdkVersion = 21
12+
buildToolsVersion = "21.1.2"
13+
}
14+
15+
allprojects {
16+
repositories {
17+
jcenter()
18+
}
19+
}
20+
21+
apply plugin: 'android-reporting'

demo/build.gradle

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion rootProject.ext.compileSdkVersion
5+
buildToolsVersion rootProject.ext.buildToolsVersion
6+
7+
defaultConfig {
8+
applicationId "me.ycdev.android.lib.ssproxy.demo"
9+
minSdkVersion 9
10+
targetSdkVersion 21
11+
12+
versionCode 1
13+
versionName "1.0"
14+
}
15+
buildTypes {
16+
release {
17+
minifyEnabled false
18+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19+
}
20+
}
21+
}
22+
23+
dependencies {
24+
compile project(':lib')
25+
compile fileTree(dir: 'libs', include: ['*.jar'])
26+
compile 'com.android.support:appcompat-v7:21.0.3'
27+
}

demo/proguard-rules.pro

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/pub/tools/android-sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package me.ycdev.android.lib.ssproxy.demo;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}

demo/src/main/AndroidManifest.xml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
package="me.ycdev.android.lib.ssproxy.demo" >
5+
6+
<uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"
7+
tools:ignore="ProtectedPermissions" />
8+
9+
<application
10+
android:allowBackup="true"
11+
android:icon="@drawable/ic_launcher"
12+
android:label="@string/app_name"
13+
android:theme="@style/AppTheme" >
14+
<activity
15+
android:name=".MainActivity"
16+
android:label="@string/app_name" >
17+
<intent-filter>
18+
<action android:name="android.intent.action.MAIN" />
19+
20+
<category android:name="android.intent.category.LAUNCHER" />
21+
</intent-filter>
22+
</activity>
23+
</application>
24+
25+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
package me.ycdev.android.lib.ssproxy.demo;
2+
3+
import android.content.Context;
4+
import android.os.Bundle;
5+
import android.os.Handler;
6+
import android.os.IBinder;
7+
import android.os.Message;
8+
import android.os.SystemClock;
9+
import android.support.v7.app.ActionBarActivity;
10+
import android.view.Menu;
11+
import android.view.MenuItem;
12+
import android.view.View;
13+
import android.widget.Button;
14+
import android.widget.Toast;
15+
16+
import eu.chainfire.libsuperuser.Debug;
17+
import me.ycdev.android.lib.common.internalapi.android.app.ActivityManagerIA;
18+
import me.ycdev.android.lib.common.internalapi.android.os.PowerManagerIA;
19+
import me.ycdev.android.lib.common.internalapi.android.os.ServiceManagerIA;
20+
import me.ycdev.android.lib.common.utils.WeakHandler;
21+
import me.ycdev.android.lib.ssproxy.SysServiceProxy;
22+
import me.ycdev.android.lib.ssproxy.demo.utils.AppLogger;
23+
24+
25+
public class MainActivity extends ActionBarActivity implements View.OnClickListener,
26+
WeakHandler.MessageHandler {
27+
private static final String TAG = "MainActivity";
28+
29+
private static final int MSG_START_DAEMON_DONE = 100;
30+
private static final int MSG_STOP_DAEMON_DONE = 101;
31+
private static final int MSG_DAEMON_NOT_RUNNING = 102;
32+
33+
private Handler mHandler = new WeakHandler(this);
34+
35+
private Button mStartDaemonBtn;
36+
private Button mStopDaemonBtn;
37+
38+
private Button mGotoSleepBtn;
39+
private Button mRebootBtn;
40+
private Button mTestRawInvokingBtn;
41+
42+
@Override
43+
public void handleMessage(Message msg) {
44+
switch (msg.what) {
45+
case MSG_START_DAEMON_DONE: {
46+
boolean result = (Boolean) msg.obj;
47+
if (result) {
48+
Toast.makeText(this, R.string.tips_start_daemon_success, Toast.LENGTH_LONG).show();
49+
} else {
50+
Toast.makeText(this, R.string.tips_start_daemon_failure, Toast.LENGTH_LONG).show();
51+
}
52+
break;
53+
}
54+
55+
case MSG_STOP_DAEMON_DONE: {
56+
boolean result = (Boolean) msg.obj;
57+
if (result) {
58+
Toast.makeText(this, R.string.tips_stop_daemon_success, Toast.LENGTH_LONG).show();
59+
} else {
60+
Toast.makeText(this, R.string.tips_stop_daemon_failure, Toast.LENGTH_LONG).show();
61+
}
62+
break;
63+
}
64+
65+
case MSG_DAEMON_NOT_RUNNING: {
66+
Toast.makeText(this, R.string.tips_daemon_not_running, Toast.LENGTH_SHORT).show();
67+
break;
68+
}
69+
}
70+
}
71+
72+
@Override
73+
protected void onCreate(Bundle savedInstanceState) {
74+
super.onCreate(savedInstanceState);
75+
setContentView(R.layout.activity_main);
76+
AppLogger.i(TAG, "#onCreate()");
77+
78+
mStartDaemonBtn = (Button) findViewById(R.id.start_daemon);
79+
mStartDaemonBtn.setOnClickListener(this);
80+
mStopDaemonBtn = (Button) findViewById(R.id.stop_daemon);
81+
mStopDaemonBtn.setOnClickListener(this);
82+
83+
mGotoSleepBtn = (Button) findViewById(R.id.goto_sleep);
84+
mGotoSleepBtn.setOnClickListener(this);
85+
mRebootBtn = (Button) findViewById(R.id.reboot);
86+
mRebootBtn.setOnClickListener(this);
87+
mTestRawInvokingBtn = (Button) findViewById(R.id.test_raw_invoking);
88+
mTestRawInvokingBtn.setOnClickListener(this);
89+
90+
Debug.setDebug(true);
91+
}
92+
93+
private void startDaemon() {
94+
final Context appContext = getApplicationContext();
95+
new Thread() {
96+
@Override
97+
public void run() {
98+
Boolean result = SysServiceProxy.getInstance(appContext).startDaemon();
99+
mHandler.obtainMessage(MSG_START_DAEMON_DONE, result).sendToTarget();
100+
}
101+
}.start();
102+
}
103+
104+
private void stopDaemon() {
105+
final Context appContext = getApplicationContext();
106+
new Thread() {
107+
@Override
108+
public void run() {
109+
Boolean result = SysServiceProxy.getInstance(appContext).stopDaemon();
110+
mHandler.obtainMessage(MSG_STOP_DAEMON_DONE, result).sendToTarget();
111+
}
112+
}.start();
113+
}
114+
115+
@Override
116+
public void onClick(View v) {
117+
if (v == mStartDaemonBtn) {
118+
startDaemon();
119+
} else if (v == mStopDaemonBtn) {
120+
stopDaemon();
121+
} else if (v == mGotoSleepBtn) {
122+
gotoSleep();
123+
} else if (v == mRebootBtn) {
124+
reboot();
125+
} else if (v == mTestRawInvokingBtn) {
126+
testRawInvoking();
127+
}
128+
}
129+
130+
private void gotoSleep() {
131+
IBinder powerBinder = SysServiceProxy.getInstance(this).getService(Context.POWER_SERVICE);
132+
AppLogger.d(TAG, "power binder: " + powerBinder);
133+
if (powerBinder != null) {
134+
Object powerService = PowerManagerIA.asInterface(powerBinder);
135+
AppLogger.d(TAG, "power service: " + powerService);
136+
if (powerService != null) {
137+
PowerManagerIA.goToSleep(powerService, SystemClock.uptimeMillis());
138+
}
139+
} else {
140+
mHandler.obtainMessage(MSG_DAEMON_NOT_RUNNING).sendToTarget();
141+
}
142+
}
143+
144+
private void reboot() {
145+
IBinder powerBinder = SysServiceProxy.getInstance(this).getService(Context.POWER_SERVICE);
146+
AppLogger.d(TAG, "power binder: " + powerBinder);
147+
if (powerBinder != null) {
148+
Object powerService = PowerManagerIA.asInterface(powerBinder);
149+
AppLogger.d(TAG, "power service: " + powerService);
150+
if (powerService != null) {
151+
PowerManagerIA.reboot(powerService, "reboot");
152+
}
153+
} else {
154+
mHandler.obtainMessage(MSG_DAEMON_NOT_RUNNING).sendToTarget();
155+
}
156+
}
157+
158+
private void testRawInvoking() {
159+
IBinder amBinder = ServiceManagerIA.getService(Context.ACTIVITY_SERVICE);
160+
AppLogger.d(TAG, "am binder: " + amBinder);
161+
if (amBinder != null) {
162+
Object amService = ActivityManagerIA.asInterface(amBinder);
163+
AppLogger.d(TAG, "am service: " + amService);
164+
if (amService != null) {
165+
try {
166+
ActivityManagerIA.forceStopPackage(amService, getPackageName());
167+
} catch (SecurityException e) {
168+
AppLogger.d(TAG, "cannot invoke the sytem services: " + e);
169+
}
170+
}
171+
}
172+
}
173+
174+
@Override
175+
public boolean onCreateOptionsMenu(Menu menu) {
176+
// Inflate the menu; this adds items to the action bar if it is present.
177+
getMenuInflater().inflate(R.menu.menu_main, menu);
178+
return true;
179+
}
180+
181+
@Override
182+
public boolean onOptionsItemSelected(MenuItem item) {
183+
// Handle action bar item clicks here. The action bar will
184+
// automatically handle clicks on the Home/Up button, so long
185+
// as you specify a parent activity in AndroidManifest.xml.
186+
int id = item.getItemId();
187+
188+
//noinspection SimplifiableIfStatement
189+
if (id == R.id.action_settings) {
190+
return true;
191+
}
192+
193+
return super.onOptionsItemSelected(item);
194+
}
195+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package me.ycdev.android.lib.ssproxy.demo.utils;
2+
3+
import android.util.Log;
4+
5+
import java.io.PrintWriter;
6+
import java.io.StringWriter;
7+
8+
public class AppLogger {
9+
public static final String TAG = "SSProxy";
10+
11+
public static void v(String subTag, String msg) {
12+
Log.v(TAG, getLogMsg(subTag, msg));
13+
}
14+
15+
public static void d(String subTag, String msg) {
16+
Log.d(TAG, getLogMsg(subTag, msg));
17+
}
18+
19+
public static void i(String subTag, String msg) {
20+
Log.i(TAG, getLogMsg(subTag, msg));
21+
}
22+
23+
public static void w(String subTag, String msg) {
24+
Log.w(TAG, getLogMsg(subTag, msg));
25+
}
26+
27+
public static void w(String subTag, String msg, Throwable e) {
28+
Log.w(TAG, getLogMsg(subTag, msg + " Exception: " + getExceptionMsg(e)));
29+
}
30+
31+
public static void e(String subTag, String msg) {
32+
Log.e(TAG, getLogMsg(subTag, msg));
33+
}
34+
35+
public static void e(String subTag, String msg, Throwable e) {
36+
Log.e(TAG, getLogMsg(subTag, msg + " Exception: " + getExceptionMsg(e)));
37+
}
38+
39+
private static String getLogMsg(String subTag, String msg) {
40+
return "[" + subTag + "] " + msg;
41+
}
42+
43+
private static String getExceptionMsg(Throwable e) {
44+
StringWriter sw = new StringWriter(1024);
45+
PrintWriter pw = new PrintWriter(sw);
46+
e.printStackTrace(pw);
47+
pw.close();
48+
return sw.toString();
49+
}
50+
}
9.18 KB
Loading
5.11 KB
Loading
Loading
Loading

0 commit comments

Comments
 (0)