Skip to content

Commit

Permalink
ensure accessibility service enable
Browse files Browse the repository at this point in the history
  • Loading branch information
cmzf committed Jan 17, 2020
1 parent ec16a21 commit 5dae678
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Application;
import android.os.Handler;
import android.os.HandlerThread;
import android.widget.Toast;

public class Global {
private static MainActivity mainActivity;
Expand Down Expand Up @@ -52,4 +53,12 @@ public static HandlerThread getMainHandlerThread() {
}
return mainHandlerThread;
}

public static void toast(String text) {
toast(text, Toast.LENGTH_LONG);
}

public static void toast(String text, int duration) {
Toast.makeText(getMainActivity(), text, duration).show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,35 @@ private void toggleInspectorServer(View view) {
TextView urlView = getInspectorUrlView();

if (button.getText().toString().toLowerCase().equals("start")) {
int port;
try {
port = Integer.valueOf(String.valueOf(portView.getText()));
if (port < 1024 || port > 65535) {
throw new Exception();
button.setEnabled(false);
button.setBackgroundColor(Color.LTGRAY);

Utils.ensureAccessibilityServiceEnabled(() -> {
int port;
try {
port = Integer.valueOf(String.valueOf(portView.getText()));
if (port < 1024 || port > 65535) {
throw new Exception();
}
} catch (Exception e) {
Toast.makeText(this, "port not allowed!", Toast.LENGTH_LONG).show();
return;
}
} catch (Exception e) {
Toast.makeText(this, "port not allowed!", Toast.LENGTH_LONG).show();
return;
}

portView.setEnabled(false);
urlView.setVisibility(View.VISIBLE);
runOnUiThread(() -> {
portView.setEnabled(false);
urlView.setVisibility(View.VISIBLE);
});

Global.getInspectorServer().startServer(port);
Global.getScreenCaptureService().startProjection(REQUESR_SCREEN_CAPTURE);
Global.getInspectorServer().startServer(port);
Global.getScreenCaptureService().startProjection(REQUESR_SCREEN_CAPTURE);

delayToggleButton(button, "STOP");
delayToggleButton(button, "STOP");
}, () -> {
runOnUiThread(() -> {
toggleInspectorServer(view);
});
});
} else {
portView.setEnabled(true);
urlView.setVisibility(View.INVISIBLE);
Expand Down
53 changes: 53 additions & 0 deletions app/src/main/java/com/github/cmzf/androidinspector/Utils.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.github.cmzf.androidinspector;

import android.app.PendingIntent;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.widget.Toast;

class Utils {

Expand All @@ -26,4 +30,53 @@ public static void setClipBoard(CharSequence text) {
ClipData clip = ClipData.newPlainText(Global.getMainApplication().getPackageName(), text);
clipboard.setPrimaryClip(clip);
}

public static void ensureAccessibilityServiceEnabled(Runnable enterCallback, Runnable exitCallback) {
long checkInterval = 2000;
String appName = Global.getMainActivity().getString(R.string.app_name);

final Runnable exitWrapper = new Runnable() {
@Override
public void run() {
if (Global.getAccessibilityService() == null) {
Global.toast("accessibility disconnected");
exitCallback.run();
} else {
Global.getMainHandler().postDelayed(this, checkInterval);
}
}
};
final Runnable enterWrapper = new Runnable() {
@Override
public void run() {
if (Global.getAccessibilityService() != null) {
Global.toast("accessibility connected");
enterCallback.run();
Global.getMainHandler().postDelayed(exitWrapper, checkInterval);
} else {
Global.getMainHandler().postDelayed(this, checkInterval);
Global.toast("turn on accessibility for " + appName, Toast.LENGTH_SHORT);
}
}
};

long enterDelay = 0;
if (Global.getAccessibilityService() == null) {
Global.toast("need accessibility service");
Global.getMainHandler().postDelayed(Utils::gotoAccessibilityService, 2000);
enterDelay = 3000;
}
Global.getMainHandler().postDelayed(enterWrapper, enterDelay);
}

private static void gotoAccessibilityService() {
final Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final PendingIntent pendingIntent = PendingIntent.getActivity(Global.getMainActivity(), 0, intent, 0);
try {
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}

}

0 comments on commit 5dae678

Please sign in to comment.