|
| 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 | +} |
0 commit comments