From 9a094a7d0130e16e48263c18a002d8f26dbdaa1c Mon Sep 17 00:00:00 2001
From: Xtr126 <80520774+Xtr126@users.noreply.github.com>
Date: Sun, 4 Aug 2024 12:51:50 +0530
Subject: [PATCH] feat: alert when shizuku not authorized
---
.../xtr/keymapper/activity/MainActivity.java | 56 ++++++++++++-------
app/src/main/res/values-zh-rCN/strings.xml | 2 +
app/src/main/res/values/strings.xml | 4 +-
3 files changed, 39 insertions(+), 23 deletions(-)
diff --git a/app/src/main/java/xtr/keymapper/activity/MainActivity.java b/app/src/main/java/xtr/keymapper/activity/MainActivity.java
index 1956b2c8..787a3b41 100644
--- a/app/src/main/java/xtr/keymapper/activity/MainActivity.java
+++ b/app/src/main/java/xtr/keymapper/activity/MainActivity.java
@@ -7,6 +7,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
+import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.os.Build;
import android.os.Bundle;
@@ -14,11 +15,14 @@
import android.provider.Settings;
import android.widget.Button;
+import androidx.annotation.Nullable;
+import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.topjohnwu.superuser.Shell;
+import rikka.shizuku.Shizuku;
import xtr.keymapper.BuildConfig;
import xtr.keymapper.R;
import xtr.keymapper.Server;
@@ -60,13 +64,17 @@ protected void onCreate(Bundle savedInstanceState) {
RemoteServiceHelper.useShizuku = new KeymapConfig(context).useShizuku;
Server.setupServer(this, mCallback);
- Shell.getShell(shell -> {
- Boolean rootAccess = Shell.isAppGrantedRoot();
- if (rootAccess == null || !rootAccess) {
- if(!RemoteServiceHelper.useShizuku) alertRootAccessNotFound();
+ if(!RemoteServiceHelper.useShizuku)
+ Shell.getShell(shell -> {
+ Boolean rootAccess = Shell.isAppGrantedRoot();
+ if (rootAccess == null || !rootAccess) {
+ alertRootAccessNotFound();
}
- setupButtons();
- });
+ });
+ else if (!(Shizuku.pingBinder() && Shizuku.checkSelfPermission() == PackageManager.PERMISSION_GRANTED)) {
+ alertShizukuNotAuthorized();
+ }
+ setupButtons();
}
private void setupButtons() {
@@ -148,29 +156,35 @@ private void requestNotificationPermission(){
}
public void alertRootAccessNotFound() {
- MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this);
- builder.setTitle(R.string.root_not_found_title)
- .setMessage(R.string.root_not_found_message)
- .setPositiveButton(R.string.ok, (dialog, which) -> {
- Intent launchIntent = MainActivity.this.getPackageManager().getLaunchIntentForPackage("me.weishu.kernelsu");
- if (launchIntent != null) startActivity(launchIntent);
- });
- runOnUiThread(() -> builder.create().show());
+ showAlertDialog(R.string.root_not_found_title, R.string.root_not_found_message, (dialog, which) -> {
+ Intent launchIntent = MainActivity.this.getPackageManager().getLaunchIntentForPackage("me.weishu.kernelsu");
+ if (launchIntent != null) startActivity(launchIntent);
+ });
}
public void alertRootAccessAndExit() {
+ showAlertDialog(R.string.root_no_privileges_title, R.string.root_no_privileges_message, (dialog, which) -> {
+ finishAffinity();
+ System.exit(0);
+ });
+ }
+
+ private void alertShizukuNotAuthorized() {
+ showAlertDialog(R.string.shizuku_not_authorized_title, R.string.shizuku_not_authorized_message, (dialog, which) -> {
+ Intent launchIntent = MainActivity.this.getPackageManager().getLaunchIntentForPackage("moe.shizuku.privileged.api");
+ if (launchIntent != null) startActivity(launchIntent);
+ });
+ }
+
+ private void showAlertDialog(@StringRes int titleId, @StringRes int messageId, @Nullable android.content.DialogInterface.OnClickListener listener) {
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);
- })
+ builder.setTitle(titleId)
+ .setMessage(messageId)
+ .setPositiveButton(R.string.ok, listener)
.setNegativeButton(R.string.cancel, (dialog, which) -> {});
runOnUiThread(() -> builder.create().show());
}
-
@Override
protected void onDestroy() {
super.onDestroy();
diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml
index 15a650ec..44a03103 100644
--- a/app/src/main/res/values-zh-rCN/strings.xml
+++ b/app/src/main/res/values-zh-rCN/strings.xml
@@ -83,4 +83,6 @@
禁用
直接模式
+ Shizuku not authorized
+ Authorize XtMapper from Shizuku manager app to continue
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 5dca92be..e3cd47d9 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -84,6 +84,6 @@
Disabled
Direct
-
-
+ Shizuku not authorized
+ Authorize XtMapper from Shizuku manager app to continue
\ No newline at end of file