From 860471d56eb86de8f768232c774a3e145574c494 Mon Sep 17 00:00:00 2001 From: Xtr126 Date: Wed, 21 Feb 2024 22:42:29 +0530 Subject: [PATCH] Ask and exit gracefully when service could not be run with sufficient privileges --- .../main/java/xtr/keymapper/TouchPointer.java | 1 + .../xtr/keymapper/activity/MainActivity.java | 18 +++++++++++++++++- .../keymapper/server/RemoteServiceHelper.java | 4 +++- .../service/InputListenerService.java | 1 + app/src/main/res/values/strings.xml | 7 ++++++- 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/xtr/keymapper/TouchPointer.java b/app/src/main/java/xtr/keymapper/TouchPointer.java index c0d54304..d8222c9b 100644 --- a/app/src/main/java/xtr/keymapper/TouchPointer.java +++ b/app/src/main/java/xtr/keymapper/TouchPointer.java @@ -171,6 +171,7 @@ private void connectRemoteService(KeymapProfile profile) { } Log.e("startServer", e.toString(), e); } + }); } diff --git a/app/src/main/java/xtr/keymapper/activity/MainActivity.java b/app/src/main/java/xtr/keymapper/activity/MainActivity.java index 9d1d185a..9dd3daa8 100644 --- a/app/src/main/java/xtr/keymapper/activity/MainActivity.java +++ b/app/src/main/java/xtr/keymapper/activity/MainActivity.java @@ -25,6 +25,7 @@ import xtr.keymapper.databinding.ActivityMainBinding; import xtr.keymapper.editor.EditorActivity; import xtr.keymapper.fragment.SettingsFragment; +import xtr.keymapper.server.RemoteServiceHelper; public class MainActivity extends AppCompatActivity { public TouchPointer pointerOverlay; @@ -89,6 +90,8 @@ public void startPointer(){ setButtonState(false); requestNotificationPermission(); } + if (!RemoteServiceHelper.isRootService) + alertRootAccessAndExit(); } private void setButtonState(boolean start) { @@ -144,13 +147,26 @@ public void alertRootAccessNotFound() { MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this); builder.setTitle(R.string.root_not_found_title) .setMessage(R.string.root_not_found_message) - .setPositiveButton("OK", (dialog, which) -> { + .setPositiveButton(R.string.ok, (dialog, which) -> { Intent launchIntent = MainActivity.this.getPackageManager().getLaunchIntentForPackage("me.weishu.kernelsu"); if (launchIntent != null) startActivity(launchIntent); }); runOnUiThread(() -> builder.create().show()); } + public void alertRootAccessAndExit() { + MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(MainActivity.this); + builder.setTitle(R.string.root_no_privileges_title) + .setMessage(R.string.root_no_privileges_message) + .setPositiveButton(R.string.ok, (dialog, which) -> { + finishAffinity(); + System.exit(0); + }) + .setNegativeButton(R.string.cancel, (dialog, which) -> {}); + runOnUiThread(() -> builder.create().show()); + } + + @Override protected void onDestroy() { super.onDestroy(); diff --git a/app/src/main/java/xtr/keymapper/server/RemoteServiceHelper.java b/app/src/main/java/xtr/keymapper/server/RemoteServiceHelper.java index faa31553..e500cf83 100644 --- a/app/src/main/java/xtr/keymapper/server/RemoteServiceHelper.java +++ b/app/src/main/java/xtr/keymapper/server/RemoteServiceHelper.java @@ -19,6 +19,7 @@ public class RemoteServiceHelper { private static IRemoteService service = null; + public static boolean isRootService = true; public static void pauseKeymap(Context context){ RemoteServiceHelper.getInstance(context, service -> { @@ -92,7 +93,8 @@ public static void getInstance(Context context, RootRemoteServiceCallback cb){ getInstance(); if (service != null) cb.onConnection(service); else { - if (!Shell.isAppGrantedRoot()) System.exit(1); + Boolean hasRootAccess = Shell.isAppGrantedRoot(); + if (hasRootAccess != null) isRootService = hasRootAccess; RemoteServiceConnection connection = new RemoteServiceConnection(cb); Intent intent = new Intent(context, RootRemoteService.class); RootService.bind(intent, connection); diff --git a/app/src/main/java/xtr/keymapper/service/InputListenerService.java b/app/src/main/java/xtr/keymapper/service/InputListenerService.java index 47561a98..f29d0824 100644 --- a/app/src/main/java/xtr/keymapper/service/InputListenerService.java +++ b/app/src/main/java/xtr/keymapper/service/InputListenerService.java @@ -6,6 +6,7 @@ import xtr.keymapper.server.RemoteServiceHelper; public class InputListenerService extends InputMethodService { + // Input method to detect when user is inputting text @Override public void onDestroy() { super.onDestroy(); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a15ab55d..782bc8e8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -49,7 +49,12 @@ Swipe delay %d milliseconds Profile Name Root access not found - Root access is required for the keymapper to function. \n Please grant root access to XtMapper from KernelSU manager app: + Root access is required for the keymapper to function. \n Please grant superuser to XtMapper from KernelSU manager app: + Service failed to start as root + + Unable to obtain privileges. Do you want to exit the application?\n + If you already granted superuser privileges, it might work after exiting and opening the app. + Disable auto profiling Touchpad Input Launch App