Skip to content

Commit 1200fe6

Browse files
committed
fix: Not activated false warning when launching editor #149
1 parent e9ab4d1 commit 1200fe6

File tree

3 files changed

+38
-32
lines changed

3 files changed

+38
-32
lines changed

app/src/main/java/xtr/keymapper/activity/MainActivity.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ protected void onCreate(Bundle savedInstanceState) {
9090
*/
9191
if(!RemoteServiceHelper.useShizuku) {
9292
Shell.getShell(shell -> {
93-
RemoteServiceHelper.getInstance(this, null);
94-
if (Shizuku.pingBinder() || getPackageManager().getLaunchIntentForPackage("moe.shizuku.privileged.api") != null) { // Ask user to enable shizuku if shizuku app detected
93+
// Ask user to enable shizuku if shizuku app detected
94+
if (Shizuku.pingBinder() || getPackageManager().getLaunchIntentForPackage("moe.shizuku.privileged.api") != null) {
9595
showAlertDialog(R.string.detected_shizuku, R.string.use_shizuku_for_activation, (dialog, which) -> {
9696
RemoteServiceHelper.useShizuku = keymapConfig.useShizuku = true;
9797
keymapConfig.applySharedPrefs();
9898
alertShizukuNotAuthorized();
9999
}, R.string.ok);
100-
} else if (!RemoteServiceHelper.isRootService) {
100+
} else if (Boolean.FALSE.equals(Shell.isAppGrantedRoot())) {
101101
alertRootAccessNotFound();
102102
}
103103
});
@@ -191,7 +191,7 @@ private Unit startPointer(Integer displayId) {
191191
if (RemoteServiceHelper.useShizuku) {
192192
if (!Shizuku.pingBinder() || Shizuku.checkSelfPermission() != PERMISSION_GRANTED)
193193
alertShizukuNotAuthorized();
194-
} else if (!RemoteServiceHelper.isRootService) {
194+
} else if (Boolean.FALSE.equals(Shell.isAppGrantedRoot())) {
195195
alertRootAccessAndExit();
196196
}
197197

app/src/main/java/xtr/keymapper/editor/EditorActivity.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public void onCreate(Bundle savedInstanceState) {
3838
WindowInsetsControllerCompat windowInsetsController = WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView());
3939
windowInsetsController.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
4040
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
41-
RemoteServiceHelper.getInstance(this, service -> mService = service);
4241

4342
if (editor != null) editor.hideView();
4443

@@ -54,23 +53,32 @@ public void onCreate(Bundle savedInstanceState) {
5453
MainActivity.checkOverlayPermission(this);
5554
}
5655

57-
if (getEvent())
58-
// Can receive key events from remote service
59-
try {
60-
mService.registerOnKeyEventListener(editor);
61-
mService.pauseMouse();
62-
} catch (RemoteException e) {
63-
Log.e("editorActivity", e.getMessage(), e);
56+
RemoteServiceHelper.getInstance(this, this::onConnection);
57+
}
58+
59+
private void onConnection(IRemoteService service) {
60+
if (editor != null) {
61+
this.mService = service;
62+
KeymapConfig keymapConfig = new KeymapConfig(this);
63+
64+
if (getEvent())
65+
// Can receive key events from remote service
66+
try {
67+
mService.registerOnKeyEventListener(editor);
68+
mService.pauseMouse();
69+
} catch (RemoteException e) {
70+
Log.e("editorActivity", e.getMessage(), e);
71+
}
72+
else {
73+
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this);
74+
75+
builder.setMessage(R.string.dialog_alert_editor)
76+
.setPositiveButton(R.string.ok, (dialog, which) -> {})
77+
.setTitle(R.string.dialog_alert_editor_title);
78+
AlertDialog dialog = builder.create();
79+
if (keymapConfig.editorOverlay) dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
80+
dialog.show();
6481
}
65-
else {
66-
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this);
67-
68-
builder.setMessage(R.string.dialog_alert_editor)
69-
.setPositiveButton(R.string.ok, (dialog, which) -> {})
70-
.setTitle(R.string.dialog_alert_editor_title);
71-
AlertDialog dialog = builder.create();
72-
if (keymapConfig.editorOverlay) dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
73-
dialog.show();
7482
}
7583
}
7684

app/src/main/java/xtr/keymapper/server/RemoteServiceHelper.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import android.os.ServiceManager;
1212
import android.util.Log;
1313

14-
import androidx.annotation.NonNull;
15-
1614
import com.topjohnwu.superuser.Shell;
1715
import com.topjohnwu.superuser.ipc.RootService;
1816

@@ -23,7 +21,6 @@
2321
public class RemoteServiceHelper {
2422

2523
private static IRemoteService service = null;
26-
public static boolean isRootService = true;
2724
public static boolean useShizuku = false;
2825

2926
public static void pauseKeymap(Context context){
@@ -59,14 +56,14 @@ public static void reloadKeymap(Context context) {
5956
public static void runIfActive(Context context, Runnable runnable) {
6057
getInstance(context, service -> {
6158
try {
62-
if (service.isActive()) runnable.run();
59+
if (service != null && service.isActive()) runnable.run();
6360
} catch (RemoteException ignored) {
6461
}
6562
});
6663
}
6764

6865
public interface RootRemoteServiceCallback {
69-
void onConnection(@NonNull IRemoteService service);
66+
void onConnection(IRemoteService service);
7067
}
7168
public static class RemoteServiceConnection implements ServiceConnection {
7269
RootRemoteServiceCallback cb;
@@ -141,17 +138,18 @@ public static void getInstance(Context context, RootRemoteServiceCallback callba
141138
} else {
142139
getInstance();
143140
if (service != null) {
144-
if (callback != null) callback.onConnection(service);
141+
callback.onConnection(service);
145142
} else {
146143
RemoteServiceConnection connection = new RemoteServiceConnection(callback);
147144
if (useShizuku) {
148145
if (Shizuku.pingBinder() && Shizuku.checkSelfPermission() == PackageManager.PERMISSION_GRANTED)
149-
if (callback != null) bindShizukuService(context, connection);
146+
bindShizukuService(context, connection);
147+
else callback.onConnection(null);
150148
} else {
151-
Boolean hasRootAccess = Shell.isAppGrantedRoot();
152-
if (hasRootAccess != null) isRootService = hasRootAccess;
153-
Intent intent = new Intent(context, RootRemoteService.class);
154-
if (callback != null) RootService.bind(intent, connection);
149+
if (Boolean.TRUE.equals(Shell.isAppGrantedRoot())) {
150+
Intent intent = new Intent(context, RootRemoteService.class);
151+
RootService.bind(intent, connection);
152+
} else callback.onConnection(null);
155153
}
156154
}
157155
}

0 commit comments

Comments
 (0)