Skip to content

Commit

Permalink
feat: Add support for Shizuku as activation method
Browse files Browse the repository at this point in the history
  • Loading branch information
Xtr126 committed Feb 22, 2024
1 parent daa209c commit e974243
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 21 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,7 @@ dependencies {
implementation 'androidx.webkit:webkit:1.10.0'
implementation "com.github.topjohnwu.libsu:core:5.2.2"
implementation "com.github.topjohnwu.libsu:service:5.2.2"
implementation "dev.rikka.shizuku:api:13.1.5"
implementation "dev.rikka.shizuku:provider:13.1.5"
compileOnly project(path: ':app:hidden-api')
}
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@
<meta-data android:name="android.view.im"
android:resource="@xml/method"/>
</service>

<provider
android:name="rikka.shizuku.ShizukuProvider"
android:authorities="${applicationId}.shizuku"
android:multiprocess="false"
android:enabled="true"
android:exported="true"
android:permission="android.permission.INTERACT_ACROSS_USERS_FULL" />
</application>

</manifest>
5 changes: 4 additions & 1 deletion app/src/main/java/xtr/keymapper/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import xtr.keymapper.databinding.ActivityMainBinding;
import xtr.keymapper.editor.EditorActivity;
import xtr.keymapper.fragment.SettingsFragment;
import xtr.keymapper.keymap.KeymapConfig;
import xtr.keymapper.server.RemoteServiceHelper;

public class MainActivity extends AppCompatActivity {
Expand Down Expand Up @@ -55,11 +56,13 @@ protected void onCreate(Bundle savedInstanceState) {
intent = new Intent(context, TouchPointer.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);

RemoteServiceHelper.useShizuku = new KeymapConfig(context).useShizuku;

Shell.getShell(shell -> {
Boolean rootAccess = Shell.isAppGrantedRoot();
if (rootAccess == null || !rootAccess) {
Server.setupServer(this, mCallback);
alertRootAccessNotFound();
if(!RemoteServiceHelper.useShizuku) alertRootAccessNotFound();
}
setupButtons();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
binding.mouseAimKeyGrave.setChecked(keymapConfig.keyGraveMouseAim);
binding.mouseAimRightClick.setChecked(keymapConfig.rightClickMouseAim);
binding.autoProfileSwitch.setChecked(keymapConfig.disableAutoProfiling);
binding.useShizuku.setChecked(keymapConfig.useShizuku);

loadKeyboardShortcuts();
binding.launchEditor.setOnKeyListener(this::onKey);
Expand Down Expand Up @@ -197,6 +198,7 @@ public void onDestroyView() {
keymapConfig.rightClickMouseAim = binding.mouseAimRightClick.isChecked();
keymapConfig.keyGraveMouseAim = binding.mouseAimKeyGrave.isChecked();
keymapConfig.disableAutoProfiling = binding.autoProfileSwitch.isChecked();
keymapConfig.useShizuku = binding.useShizuku.isChecked();

keymapConfig.dpadRadiusMultiplier = binding.sliderDpad.getValue();

Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/xtr/keymapper/keymap/KeymapConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class KeymapConfig implements Parcelable {
public Float mouseSensitivity, scrollSpeed;
public Float dpadRadiusMultiplier;
public boolean ctrlMouseWheelZoom, ctrlDragMouseGesture, rightClickMouseAim, keyGraveMouseAim;
public boolean disableAutoProfiling;
public boolean disableAutoProfiling , useShizuku;

public int pauseResumeShortcutKey, launchEditorShortcutKey, switchProfileShortcutKey;
public int swipeDelayMs;
Expand Down Expand Up @@ -65,6 +65,7 @@ protected KeymapConfig(Parcel in) {
mouseAimToggle = in.readByte() != 0;
disableAutoProfiling = in.readByte() != 0;
touchpadInputMode = in.readString();
useShizuku = in.readByte() != 0;
}

public static final Creator<KeymapConfig> CREATOR = new Creator<>() {
Expand All @@ -86,6 +87,7 @@ private void loadSharedPrefs() {
ctrlDragMouseGesture = sharedPref.getBoolean("ctrl_drag_mouse_gesture", true);
mouseAimToggle = sharedPref.getBoolean("mouse_aim_shortcut_toggle", true);
disableAutoProfiling = sharedPref.getBoolean("disable_auto_profile", true);
useShizuku = sharedPref.getBoolean("use_shizuku", false);

launchEditorShortcutKey = sharedPref.getInt("launch_editor_shortcut", -1);
pauseResumeShortcutKey = sharedPref.getInt("pause_resume_shortcut", -1);
Expand Down Expand Up @@ -115,6 +117,7 @@ public void applySharedPrefs() {
.putBoolean("right_click_mouse_aim", rightClickMouseAim)
.putBoolean("mouse_aim_shortcut_toggle", mouseAimToggle)
.putBoolean("disable_auto_profile", disableAutoProfiling)
.putBoolean("use_shizuku", useShizuku)
.putInt("pause_resume_shortcut", pauseResumeShortcutKey)
.putInt("launch_editor_shortcut", launchEditorShortcutKey)
.putInt("switch_profile_shortcut", switchProfileShortcutKey)
Expand Down Expand Up @@ -167,5 +170,6 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeByte((byte) (mouseAimToggle ? 1 : 0));
dest.writeByte((byte) (disableAutoProfiling ? 1 : 0));
dest.writeString(touchpadInputMode);
dest.writeByte((byte) (useShizuku ? 1 : 0));
}
}
26 changes: 26 additions & 0 deletions app/src/main/java/xtr/keymapper/server/RemoteService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package xtr.keymapper.server;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.RemoteException;

import java.io.BufferedReader;
Expand All @@ -21,6 +24,29 @@ public class RemoteService extends IRemoteService.Stub {
private ActivityObserverService activityObserverService;
String nativeLibraryDir = System.getProperty("java.library.path");

public RemoteService() {

}

/* For Shizuku UserService */
public RemoteService(Context context) {
loadLibraries();
init(context);
}

public RemoteService init(Context context) {
PackageManager pm = context.getPackageManager();
String packageName = context.getPackageName();
try {
ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
nativeLibraryDir = ai.nativeLibraryDir;
start_getevent();
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(e);
}
return this;
}

public static void loadLibraries() {
System.loadLibrary("mouse_read");
System.loadLibrary("mouse_cursor");
Expand Down
32 changes: 25 additions & 7 deletions app/src/main/java/xtr/keymapper/server/RemoteServiceHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
Expand All @@ -14,12 +15,14 @@

import java.io.IOException;

import rikka.shizuku.Shizuku;
import xtr.keymapper.IRemoteService;

public class RemoteServiceHelper {

private static IRemoteService service = null;
public static boolean isRootService = true;
public static boolean useShizuku = false;

public static void pauseKeymap(Context context){
RemoteServiceHelper.getInstance(context, service -> {
Expand Down Expand Up @@ -89,15 +92,30 @@ public static void getInstance(){
}
}

public static void getInstance(Context context, RootRemoteServiceCallback cb){
private static void bindShizukuService(Context context, RemoteServiceConnection connection) {
Shizuku.UserServiceArgs userServiceArgs =
new Shizuku.UserServiceArgs(new ComponentName(context, RemoteService.class.getName()))
.daemon(false)
.processNameSuffix("service")
.debuggable(false)
.version(11);
Shizuku.bindUserService(userServiceArgs, connection);
}

public static void getInstance(Context context, RootRemoteServiceCallback callback){
getInstance();
if (service != null) cb.onConnection(service);
if (service != null) callback.onConnection(service);
else {
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);
RemoteServiceConnection connection = new RemoteServiceConnection(callback);
if (useShizuku) {
if (Shizuku.pingBinder() && Shizuku.checkSelfPermission() == PackageManager.PERMISSION_GRANTED)
bindShizukuService(context, connection);
} else {
Boolean hasRootAccess = Shell.isAppGrantedRoot();
if (hasRootAccess != null) isRootService = hasRootAccess;
Intent intent = new Intent(context, RootRemoteService.class);
RootService.bind(intent, connection);
}
}
}
}
15 changes: 3 additions & 12 deletions app/src/main/java/xtr/keymapper/server/RootRemoteService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package xtr.keymapper.server;

import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.os.Process;

Expand All @@ -20,19 +18,12 @@ class RootRemoteService extends RootService {
RemoteService.loadLibraries();
}

public final RemoteService mService = new RemoteService();

private RemoteService mService = null;

@Override
public IBinder onBind(@NonNull Intent intent) {
PackageManager pm = this.getPackageManager();
String packageName = this.getPackageName();
try {
ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
mService.nativeLibraryDir = ai.nativeLibraryDir;
mService.start_getevent();
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(e);
if (mService == null) {
mService = new RemoteService().init(this);
}
return mService;
}
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/layout/fragment_settings_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,13 @@

</LinearLayout>

<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/use_shizuku"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:checked="false"
android:text="@string/use_shizuku" />

</LinearLayout>

<com.google.android.material.bottomnavigation.BottomNavigationView
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@
<string name="disable_auto_profile">Disable auto profiling</string>
<string name="touchpad_input">Touchpad Input</string>
<string name="launch_app">Launch App</string>
<string name="use_shizuku">Use Shizuku</string>

</resources>

0 comments on commit e974243

Please sign in to comment.