Skip to content

Commit

Permalink
feat: alert when shizuku not authorized
Browse files Browse the repository at this point in the history
  • Loading branch information
Xtr126 committed Aug 4, 2024
1 parent 67294e7 commit 9a094a7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
56 changes: 35 additions & 21 deletions app/src/main/java/xtr/keymapper/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@
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;
import android.os.IBinder;
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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,6 @@
<string name="disabled">禁用</string>
<string name="direct">直接模式</string>

<string name="shizuku_not_authorized_title">Shizuku not authorized</string>
<string name="shizuku_not_authorized_message">Authorize XtMapper from Shizuku manager app to continue</string>
</resources>
4 changes: 2 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@
<string name="disabled">Disabled</string>
<string name="direct">Direct</string>



<string name="shizuku_not_authorized_title">Shizuku not authorized</string>
<string name="shizuku_not_authorized_message">Authorize XtMapper from Shizuku manager app to continue</string>
</resources>

0 comments on commit 9a094a7

Please sign in to comment.