private ActivityInfo tryGetActivity(AccessibilityService service, ComponentName componentName) { try { return service.getPackageManager().getActivityInfo(componentName, 0); } catch (PackageManager.NameNotFoundException e) { return null; } }
@Override public void onCreate() { super.onCreate(); settings = getSharedPreferences("settings", MODE_PRIVATE); registerSmsMiddleware(); clearGoogleVoiceNotifications(); }
public void executeMessage(Message message) { switch (message.what) { case DO_ON_ACCESSIBILITY_EVENT: AccessibilityEvent event = (AccessibilityEvent) message.obj; mTarget.onAccessibilityEvent(event); event.recycle(); return; case DO_ON_INTERRUPT: mTarget.onInterrupt(); return; case DO_SET_SET_CONNECTION: mConnection = ((IAccessibilityServiceConnection) message.obj); mTarget.onServiceConnected(); return; default: Log.w(LOG_TAG, "Unknown message type " + message.what); } }
// set the accessibility filter to // watch only for google voice notifications @Override protected void onServiceConnected() { super.onServiceConnected(); connected = true; AccessibilityServiceInfo info = new AccessibilityServiceInfo(); // We are interested in all types of accessibility events. info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED; // We want to provide specific type of feedback. info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC; // We want to receive events in a certain interval. info.notificationTimeout = 100; // We want to receive accessibility events only from certain packages. info.packageNames = new String[] {Helper.GOOGLE_VOICE_PACKAGE}; setServiceInfo(info); }
@Override protected void onServiceConnected() { super.onServiceConnected(); thisService = this; lastCommand = 0; if (!PreferenceManager.getDefaultSharedPreferences(this) .getBoolean("setup", false)) // make sure this only runs when the user explicitly enables it { PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("setup", true).commit(); Intent openMainActivity = new Intent(this, MainActivity.class); openMainActivity.addFlags( Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(openMainActivity); // go back to setup activity after enabling } }
@Override public void onCreate() { super.onCreate(); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag"); mWakeLock.acquire(); File logf = new File(Environment.getExternalStorageDirectory() + File.separator + "DemoLog.txt"); try { mLogWriter = LogWriter.open(logf.getAbsolutePath()); } catch (IOException e) { // TODO Auto-generatedo catch block log(e.getMessage()); } log("onCreate()"); // 测试杀掉teamview再打开功能 try { Runtime.getRuntime().exec(new String[] {"/system/bin/su", "-c", "ls -al"}); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (!isAppInstalled(WatchService.this, paName)) { Log.d("DemoLog", "no install"); ApkController apkController = new ApkController(WatchService.this); apkController.install(); handler.removeMessages(UPDATE_BEGIN_ZHUANGTAI); handler.sendEmptyMessage(UPDATE_BEGIN_ZHUANGTAI); } else { try { startTeamView(); } catch (Exception e) { } } // 亮度调节 settingUtils = new SettingUtils(WatchService.this); settingUtils.setBrightness(0); }
@Override public int onStartCommand(final Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); if (null != settings.getString("account", null)) { ensureEnabled(); } if (intent == null) return START_STICKY; // handle an outgoing sms on a background thread. if (intent.getAction() == "android.intent.action.NEW_OUTGOING_SMS") { new Thread() { @Override public void run() { handleOutgoingSms(intent); } }.start(); } return START_STICKY; }
@Override public void onDestroy() { super.onDestroy(); KeyguardUtil.unlock(this); }
@Override public void handleMessage(Message message) { // TODO Auto-generated method stub super.handleMessage(message); final String zhuangtai = (String) message.obj; switch (message.what) { case UPDATE_TIME: RequestParams params = new RequestParams(UrlData.URL_UPDATE_TIME); params.addBodyParameter("授权用户", UrlData.ADMIN_UID); params.addBodyParameter("密码", UrlData.ADMIN_PASSWORD); params.addBodyParameter("用户ID", mTeamViewData.mPCIDTEXT); params.addBodyParameter("设备ID", mTeamViewData.mIdText); params.addBodyParameter("使用时间", mTeamViewData.BEGIN_TIME); log( "用户ID=" + mTeamViewData.mPCIDTEXT + ";设备ID=" + mTeamViewData.mIdText + "连接判断=" + "使用时间=" + mTeamViewData.BEGIN_TIME); x.http() .post( params, new CommonCallback<String>() { @Override public void onSuccess(String result) { InputStream sbs = new ByteArrayInputStream(result.getBytes()); try { String response = XMLParse.parseResponseCheck(sbs); log("连接判断 response=" + response); if (response.equals("成功")) { handler.removeMessages(UPDATE_TIME); handler.sendEmptyMessageDelayed(UPDATE_TIME, UPDATE_TIME_TIME); } else { handler.removeMessages(UPDATE_TIME); handler.sendEmptyMessageDelayed(UPDATE_TIME, UPDATE_TIME_TIME); } } catch (Exception e) { } } @Override public void onError(Throwable ex, boolean isOnCallback) { log("连接判断 error" + ex.getMessage()); } @Override public void onCancelled(CancelledException cex) {} @Override public void onFinished() {} }); break; case SHUT_DOWN_TEAM: if (state == STATC_CONNECTION_SUCCESS) { handler.removeMessages(SHUT_DOWN_CONNECTION); handler.sendEmptyMessage(SHUT_DOWN_CONNECTION); } break; case UPDATE_STARTEAM: startTeamView(); break; case OPEN: log("restart state=" + state); if (state != STATC_CONNECTION_SUCCESS) { Intent mIntent = new Intent(); mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ComponentName comp = new ComponentName(paName, MAIN_ACTIVITY); mIntent.setComponent(comp); mIntent.setAction("android.intent.action.VIEW"); startActivity(mIntent); } handler.removeMessages(OPEN); handler.sendEmptyMessageDelayed(OPEN, RESTART); break; case SHUT_DOWN_CONNECTION: state = STATC_CONNECTION_OVER; handler.removeCallbacksAndMessages(null); // 杀死进程,重启teamview log("kill killBackgroundProcesses"); try { Process suProcess = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(suProcess.getOutputStream()); os.writeBytes("adb shell" + "\n"); os.flush(); os.writeBytes("am force-stop " + paName + "\n"); os.flush(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); log(e.getMessage()); } if (processWatcher != null) { processWatcher.stop(); } handler.removeMessages(UPDATE_STARTEAM); handler.sendEmptyMessageDelayed(UPDATE_STARTEAM, 1000 * 8); RequestParams params2 = new RequestParams(UrlData.URL_UPDATE_TIME_END); params2.addBodyParameter("授权用户", UrlData.ADMIN_UID); params2.addBodyParameter("密码", UrlData.ADMIN_PASSWORD); params2.addBodyParameter("用户ID", mTeamViewData.mPCIDTEXT); params2.addBodyParameter("设备ID", mTeamViewData.mIdText); params2.addBodyParameter("使用时间", mTeamViewData.BEGIN_TIME); log( "用户ID=" + mTeamViewData.mPCIDTEXT + ";设备ID=" + mTeamViewData.mIdText + "使用时间=" + mTeamViewData.BEGIN_TIME); x.http() .post( params2, new CommonCallback<String>() { @Override public void onSuccess(String result) { InputStream sbs = new ByteArrayInputStream(result.getBytes()); try { String response = XMLParse.parseResponseCheck(sbs); log("结束时间 response=" + response); if (response.equals("成功")) { // 结束的时候不用更改状态写入结束信息即可 // Message msg = new Message(); // msg.what = UPDATE_ZHUANGTAI; // msg.obj = (String) "空闲"; // handler.removeMessages(UPDATE_ZHUANGTAI); // handler.sendMessage(msg); handler.removeMessages(UPDATE_SHUTDOWN_ZHUANGTAI); handler.sendEmptyMessage(UPDATE_SHUTDOWN_ZHUANGTAI); } } catch (Exception e) { } } @Override public void onError(Throwable ex, boolean isOnCallback) { log("结束时间 error" + ex.getMessage()); } @Override public void onCancelled(CancelledException cex) {} @Override public void onFinished() {} }); break; case UPDATE_ZHUANGTAI: log("连接状态=" + zhuangtai); RequestParams params3 = new RequestParams(UrlData.URL_UPDATE_ZHUANGTAI); params3.addBodyParameter("授权用户", UrlData.ADMIN_UID); params3.addBodyParameter("密码", UrlData.ADMIN_PASSWORD); params3.addBodyParameter("连接状态", zhuangtai); params3.addBodyParameter("设备ID", mTeamViewData.mIdText); log("连接状态=" + zhuangtai + ";设备ID=" + mTeamViewData.mIdText); x.http() .post( params3, new CommonCallback<String>() { @Override public void onSuccess(String result) { InputStream sbs = new ByteArrayInputStream(result.getBytes()); try { String response = XMLParse.parseResponseCheck(sbs); log("更新状态 response=" + response); if (response.equals("成功")) { zhuangtaiTimes = 0; } else { if (zhuangtaiTimes < TIMES) { Message msg = new Message(); msg.what = UPDATE_ZHUANGTAI; msg.obj = zhuangtai; handler.removeMessages(UPDATE_ZHUANGTAI); handler.sendMessageDelayed(msg, TRY_TIME); zhuangtaiTimes++; } } } catch (Exception e) { log("ZHUANGTAI=" + e.getMessage()); } } @Override public void onError(Throwable ex, boolean isOnCallback) { log("更新状态 error" + ex.getMessage()); } @Override public void onCancelled(CancelledException cex) {} @Override public void onFinished() {} }); break; case UPDATE_SHUTDOWN_ZHUANGTAI: RequestParams params4 = new RequestParams(UrlData.URL_SHUT_DOWN_ZHUANGTAI); params4.addBodyParameter("授权用户", UrlData.ADMIN_UID); params4.addBodyParameter("密码", UrlData.ADMIN_PASSWORD); params4.addBodyParameter("设备ID", mTeamViewData.mIdText); log("设备ID=" + mTeamViewData.mIdText); x.http() .post( params4, new CommonCallback<String>() { @Override public void onSuccess(String result) { InputStream sbs = new ByteArrayInputStream(result.getBytes()); try { String response = XMLParse.parseResponseCheck(sbs); log("更新结束信息 response=" + response); if (response.equals("成功")) { shutDownTimes = 0; mTeamViewData.mPCIDTEXT = ""; state = STATE_BEGIN; } else { if (shutDownTimes < TIMES) { handler.removeMessages(UPDATE_SHUTDOWN_ZHUANGTAI); handler.sendEmptyMessageDelayed( UPDATE_SHUTDOWN_ZHUANGTAI, TRY_TIME); shutDownTimes++; } } } catch (Exception e) { } } @Override public void onError(Throwable ex, boolean isOnCallback) { log("更新结束信息 error" + ex.getMessage()); } @Override public void onCancelled(CancelledException cex) {} @Override public void onFinished() {} }); break; case UPDATE_BEGIN_ZHUANGTAI: // 写状态时直接调用状态的handler即可 log("UPDATE_BEGIN_ZHUANGTAI=" + state + ";id=" + mTeamViewData.mIdText); if (state == STATE_BEGIN || state == STATE_ACTIVATIONING) // 没有获取到id,正在激活,离线状态 { // 没有获取到id,写入离线状态,并同时再次进行此handler // 如果没有获取到id,如何写离线状态?怎么找到对应id的记录写呢? mTeamViewData.mIdText = Dbmanager.getUserId(); Message msg = new Message(); msg.what = UPDATE_ZHUANGTAI; msg.obj = (String) "离线"; handler.removeMessages(UPDATE_ZHUANGTAI); handler.sendMessage(msg); handler.removeMessages(UPDATE_BEGIN_ZHUANGTAI); handler.sendEmptyMessageDelayed(UPDATE_BEGIN_ZHUANGTAI, UPDATE_BEGIN_TIME_TIME); } else if (state == STATE_GET_ID_SUCCESS) // 获取到id,正在空闲状态 { // 成功获取到id,写入空闲状态,并停止handler Message msg = new Message(); msg.what = UPDATE_ZHUANGTAI; msg.obj = (String) "空闲"; handler.removeMessages(UPDATE_ZHUANGTAI); handler.sendMessage(msg); handler.removeMessages(UPDATE_BEGIN_ZHUANGTAI); handler.sendEmptyMessageDelayed(UPDATE_BEGIN_ZHUANGTAI, UPDATE_BEGIN_TIME_TIME); } break; case BEGIN_BEGIN: state = STATC_CONNECTION_SUCCESS; log("connection success"); RequestParams params5 = new RequestParams(UrlData.URL_BEGIN_CONNECT); params5.addBodyParameter("授权用户", UrlData.ADMIN_UID); params5.addBodyParameter("密码", UrlData.ADMIN_PASSWORD); params5.addBodyParameter("用户ID", mTeamViewData.mPCIDTEXT); params5.addBodyParameter("设备ID", mTeamViewData.mIdText); params5.addBodyParameter("使用时间", mTeamViewData.BEGIN_TIME); log( "用户ID=" + mTeamViewData.mPCIDTEXT + ";设备ID=" + mTeamViewData.mIdText + "使用时间=" + mTeamViewData.BEGIN_TIME); x.http() .post( params5, new CommonCallback<String>() { @Override public void onSuccess(String result) { InputStream sbs = new ByteArrayInputStream(result.getBytes()); try { String response = XMLParse.parseResponseCheck(sbs); Log.d("DemoLog", "开始时间 response=" + response); if (response.equals("成功")) { // 点击自动允许 if (mTeamViewData.allowButton != null) { mTeamViewData.allowButton.performAction( AccessibilityNodeInfo.ACTION_CLICK); // state = STATE_ALLOW; } else { log("allowButton==null"); } Message msg = new Message(); msg.what = UPDATE_ZHUANGTAI; msg.obj = (String) "忙碌"; handler.removeMessages(UPDATE_ZHUANGTAI); handler.sendMessage(msg); handler.removeMessages(UPDATE_TIME); handler.sendEmptyMessageDelayed(UPDATE_TIME, UPDATE_TIME_TIME); handler.removeMessages(SHUT_DOWN_TEAM); handler.sendEmptyMessageDelayed(SHUT_DOWN_TEAM, SHUT_DOWN_TEAM_TIME); Intent i = new Intent(Intent.ACTION_MAIN); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.addCategory(Intent.CATEGORY_HOME); startActivity(i); } } catch (Exception e) { log(e.getMessage()); } } @Override public void onError(Throwable ex, boolean isOnCallback) { Log.d("DemoLog", "开始时间 error" + ex.getMessage()); } @Override public void onCancelled(CancelledException cex) {} @Override public void onFinished() {} }); break; } }
@Override public void onDestroy() { super.onDestroy(); if (mWakeLock != null) { mWakeLock.release(); } if (!TextUtils.isEmpty(mTeamViewData.mIdText) && !TextUtils.isEmpty(mTeamViewData.mPCIDTEXT)) { RequestParams params = new RequestParams(UrlData.URL_SHUT_DOWN_OWN); params.addBodyParameter("授权用户", UrlData.ADMIN_UID); params.addBodyParameter("密码", UrlData.ADMIN_PASSWORD); params.addBodyParameter("设备ID", mTeamViewData.mIdText); params.addBodyParameter("用户ID", mTeamViewData.mPCIDTEXT); log("违例结束 设备ID=" + mTeamViewData.mIdText + "用户id=" + mTeamViewData.mPCIDTEXT); x.http() .post( params, new CommonCallback<String>() { @Override public void onSuccess(String result) { InputStream sbs = new ByteArrayInputStream(result.getBytes()); String response = ""; try { response = XMLParse.parseResponseCheck(sbs); log("违例结束=" + response); } catch (Exception e) { } } @Override public void onError(Throwable ex, boolean isOnCallback) { log("违例结束 error" + ex.getMessage()); } @Override public void onCancelled(CancelledException cex) {} @Override public void onFinished() {} }); } if (handler != null) { handler.removeCallbacksAndMessages(null); } try { Process suProcess = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(suProcess.getOutputStream()); os.writeBytes("adb shell" + "\n"); os.flush(); os.writeBytes("am force-stop " + paName + "\n"); os.flush(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); log(e.getMessage()); } if (processWatcher != null) { processWatcher.stop(); } if (settingUtils != null) { settingUtils.reset(); } }
@Override public void onAccessibilityEvent(AccessibilityService service, AccessibilityEvent event) { Log.d(TAG, "event: " + event); // Log.d(TAG, "event: " + toString(event)); AccessibilityNodeInfo node = event.getSource(); CharSequence packageName = event.getPackageName(); int type = event.getEventType(); int windowId = event.getWindowId(); if (null == node) { Log.w(TAG, "node is null, ^~^"); return; } if ("com.android.settings".equals(packageName)) { // http://stackoverflow.com/questions/3873659/android-how-can-i-get-the-current-foreground-activity-from-a-service if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { mLastComponentName = new ComponentName(event.getPackageName().toString(), event.getClassName().toString()); Log.i(TAG, "last activity: " + mLastComponentName); // from QueryController if (event.getText() != null && event.getText().size() > 0) { if (event.getText().get(0) != null) { String lastActivityName = event.getText().get(0).toString(); // Log.i(TAG, "last activity name: " + lastActivityName); } } } if (AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED == type) { AccessibilityNodeInfo rootNode = node; rootNode = service.getRootInActiveWindow(); List<AccessibilityNodeInfo> nodes = rootNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/dashboard"); // nodes = rootNode.findAccessibilityNodeInfosByText("设置"); boolean settingActivity = nodes != null && nodes.size() > 0; nodes = rootNode.findAccessibilityNodeInfosByText("开发者选项"); boolean devActivity = nodes != null && nodes.size() == 1; settingActivity = new ComponentName("com.android.settings", "com.android.settings.Settings") .equals(mLastComponentName); devActivity = new ComponentName("com.android.settings", "com.android.settings.SubSettings") .equals(mLastComponentName); Log.d(TAG, "settingActivity: " + settingActivity + " devActivity: " + devActivity); if (settingActivity) { Log.d(TAG, "scroll to bottom"); nodes = rootNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/dashboard"); nodes.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); nodes.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); nodes.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); nodes.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); nodes = rootNode.findAccessibilityNodeInfosByText("开发者选项"); if (null != nodes && nodes.size() > 0) { Log.d(TAG, "open developer's setting"); nodes.get(0).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK); } } else if (devActivity) { nodes = rootNode.findAccessibilityNodeInfosByText("开启"); AccessibilityNodeInfo open = AccessibilityNodeInfoFinder.find(rootNode, new UiSelector().text("开启")); Log.d(TAG, "open: " + open); open = open.getParent().getChild(1); if (!open.isChecked()) { // open.setChecked(true); open.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK); } else { Log.i(TAG, "adb is open yet."); } AccessibilityNodeInfo usb = AccessibilityNodeInfoFinder.find(rootNode, new UiSelector().text("USB调试")); if (null == usb) { AccessibilityNodeInfo listview = AccessibilityNodeInfoFinder.find( rootNode, new UiSelector().className("android.widget.ListView")); listview.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); } usb = AccessibilityNodeInfoFinder.find(rootNode, new UiSelector().text("USB调试")); Log.i(TAG, "usb: " + usb); Log.i(TAG, "p: " + usb.getParent()); Log.i(TAG, "pp: " + usb.getParent().getParent()); AccessibilityNodeInfo debug = usb.getParent().getParent().getChild(1).getChild(0); Log.i(TAG, "debug: " + debug); debug = AccessibilityNodeInfoFinder.find( rootNode, new UiSelector() .className("android.widget.Switch") .fromParent(new UiSelector().className("android.widget.LinearLayout"))); Log.i(TAG, "debug: " + debug); usb = AccessibilityNodeInfoFinder.find(rootNode, new UiSelector().text("调试")); int index = 0; for (int i = 0; i < usb.getParent().getChildCount(); i++) { if ("调试".equals(usb.getParent().getChild(i).getText())) { index = i; break; } } usb = usb.getParent().getChild(index + 1); Log.i(TAG, "debug: " + debug); // if (!debug.isChecked()) { // debug.performAction(AccessibilityNodeInfo.ACTION_CLICK); // } else { // Log.i(TAG, "adb debug is open yet."); // } // if (null != nodes && nodes.size() > 0) { // Log.d(TAG, nodes.size() + " node found."); // Log.d(TAG, "open developer's option"); // // nodes.get(0).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK); // } else { // return; // } } } } // node.recycle(); }
@Override public void onAccessibilityEvent(AccessibilityService service, AccessibilityEvent event) { Log.d(TAG, "event: " + event); // Log.d(TAG, "event: " + toString(event)); AccessibilityNodeInfo node = event.getSource(); CharSequence packageName = event.getPackageName(); int type = event.getEventType(); int windowId = event.getWindowId(); if (null == node) { Log.w(TAG, "node is null, ^~^"); return; } if ("com.android.settings".equals(packageName)) { // http://stackoverflow.com/questions/3873659/android-how-can-i-get-the-current-foreground-activity-from-a-service if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { mLastComponentName = new ComponentName(event.getPackageName().toString(), event.getClassName().toString()); Log.i(TAG, "last activity: " + mLastComponentName); // from QueryController if (event.getText() != null && event.getText().size() > 0) { if (event.getText().get(0) != null) { String lastActivityName = event.getText().get(0).toString(); // Log.i(TAG, "last activity name: " + lastActivityName); } } boolean appDetail = new ComponentName( "com.android.settings", "com.android.settings.applications.InstalledAppDetailsTop") .equals(mLastComponentName); boolean stopConfirm = new ComponentName("com.android.settings", "android.app.AlertDialog") .equals(mLastComponentName); Log.d(TAG, "appDetail: " + appDetail + " stopConfirm: " + stopConfirm); if (appDetail) { AccessibilityNodeInfo stop = AccessibilityNodeInfoFinder.find( service.getRootInActiveWindow(), new UiSelector().text("强行停止")); if (stop.isEnabled()) { stop.performAction(AccessibilityNodeInfo.ACTION_CLICK); } else { Log.i(TAG, "app has stop yet."); } } if (stopConfirm) { AccessibilityNodeInfo confirm = AccessibilityNodeInfoFinder.find( service.getRootInActiveWindow(), new UiSelector().text("确定")); confirm.performAction(AccessibilityNodeInfo.ACTION_CLICK); Intent self = new Intent(service, MainActivity.class); self.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); service.startActivity(self); } } } }