OSDN Git Service

Initial implementation of Shizuku for enabling additional desktop mode settings
authorBraden Farmer <farmerbb@gmail.com>
Mon, 13 Dec 2021 06:33:21 +0000 (23:33 -0700)
committerBraden Farmer <farmerbb@gmail.com>
Mon, 13 Dec 2021 06:36:28 +0000 (23:36 -0700)
app/build.gradle
app/src/main/java/com/farmerbb/taskbar/activity/EnableAdditionalSettingsActivity.java
app/src/nonplaystore/java/com/farmerbb/taskbar/activity/EnableAdditionalSettingsActivityBase.java [new file with mode: 0644]
app/src/playstore/AndroidManifest.xml
app/src/playstore/java/com/farmerbb/taskbar/activity/EnableAdditionalSettingsActivityBase.java [new file with mode: 0644]
dependencies.gradle

index fac8d0d..eed6413 100644 (file)
@@ -143,6 +143,8 @@ dependencies {
     //noinspection GradleDependency
     implementation 'com.mikepenz:iconics-core:3.2.5'
     implementation 'com.mikepenz:foundation-icons-typeface:3.0.0.5'
+    implementation "dev.rikka.shizuku:api:$SHIZUKU_VERSION"
+    implementation "dev.rikka.shizuku:provider:$SHIZUKU_VERSION"
     implementation 'moe.banana:toast-compat:1.0.5'
     implementation group:'com.twofortyfouram', name:'android-plugin-api-for-locale', version:'[1.0.2,2.0['
     implementation "org.lsposed.hiddenapibypass:hiddenapibypass:$HIDDEN_API_BYPASS_VERSION"
index 6151d5b..f154d2f 100644 (file)
 package com.farmerbb.taskbar.activity;
 
 import android.Manifest;
-import android.os.Bundle;
-import androidx.appcompat.app.AppCompatActivity;
 import android.widget.Button;
 import android.widget.TextView;
 
 import com.farmerbb.taskbar.R;
 
-public class EnableAdditionalSettingsActivity extends AppCompatActivity {
+public class EnableAdditionalSettingsActivity extends EnableAdditionalSettingsActivityBase {
 
     @Override
-    protected void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-
+    protected void proceedWithOnCreate() {
         setContentView(R.layout.tb_enable_additional_settings);
         setTitle(R.string.tb_enable_additional_settings);
 
diff --git a/app/src/nonplaystore/java/com/farmerbb/taskbar/activity/EnableAdditionalSettingsActivityBase.java b/app/src/nonplaystore/java/com/farmerbb/taskbar/activity/EnableAdditionalSettingsActivityBase.java
new file mode 100644 (file)
index 0000000..4127176
--- /dev/null
@@ -0,0 +1,32 @@
+/* Copyright 2021 Braden Farmer
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.farmerbb.taskbar.activity;
+
+import android.os.Bundle;
+
+import androidx.appcompat.app.AppCompatActivity;
+
+public abstract class EnableAdditionalSettingsActivityBase extends AppCompatActivity {
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        proceedWithOnCreate();
+    }
+
+    protected abstract void proceedWithOnCreate();
+}
\ No newline at end of file
index 680ed39..2beae94 100644 (file)
@@ -52,6 +52,9 @@
 
     <uses-permission-sdk-23 android:name="com.farmerbb.taskbar.support.USE_SUPPORT_LIBRARY" />
 
+    <uses-sdk
+        tools:overrideLibrary="rikka.shizuku.provider,rikka.shizuku.api,rikka.shizuku.shared,rikka.shizuku.aidl"/>
+
     <uses-feature
         android:name="android.hardware.touchscreen"
         android:required="false"/>
                 android:name="android.support.FILE_PROVIDER_PATHS"
                 android:resource="@xml/tb_file_paths" />
         </provider>
+        <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>
 
diff --git a/app/src/playstore/java/com/farmerbb/taskbar/activity/EnableAdditionalSettingsActivityBase.java b/app/src/playstore/java/com/farmerbb/taskbar/activity/EnableAdditionalSettingsActivityBase.java
new file mode 100644 (file)
index 0000000..a3d1058
--- /dev/null
@@ -0,0 +1,103 @@
+/* Copyright 2021 Braden Farmer
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.farmerbb.taskbar.activity;
+
+import android.annotation.SuppressLint;
+import android.content.pm.PackageManager;
+import android.os.Build;
+import android.os.Bundle;
+
+import androidx.annotation.NonNull;
+import androidx.appcompat.app.AppCompatActivity;
+
+import android.os.IBinder;
+
+import java.lang.reflect.Method;
+
+import rikka.shizuku.Shizuku;
+import rikka.shizuku.ShizukuBinderWrapper;
+import rikka.shizuku.ShizukuProvider;
+import rikka.shizuku.SystemServiceHelper;
+
+public abstract class EnableAdditionalSettingsActivityBase extends AppCompatActivity implements Shizuku.OnRequestPermissionResultListener {
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Shizuku.pingBinder()) {
+            boolean isGranted;
+            if(Shizuku.isPreV11() || Shizuku.getVersion() < 11) {
+                isGranted = checkSelfPermission(ShizukuProvider.PERMISSION) == PackageManager.PERMISSION_GRANTED;
+            } else {
+                isGranted = Shizuku.checkSelfPermission() == PackageManager.PERMISSION_GRANTED;
+            }
+
+            if(isGranted) {
+                grantWriteSecureSettingsPermission();
+            } else {
+                int SHIZUKU_CODE = 123;
+                if(Shizuku.isPreV11() || Shizuku.getVersion() < 11) {
+                    requestPermissions(new String[] { ShizukuProvider.PERMISSION }, SHIZUKU_CODE);
+                } else {
+                    Shizuku.requestPermission(SHIZUKU_CODE);
+                }
+            }
+        } else {
+            proceedWithOnCreate();
+        }
+    }
+
+    @Override
+    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
+        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
+        for(int i = 0; i < permissions.length; i++) {
+            String permission = permissions[i];
+            int result = grantResults[i];
+
+            if(permission.equals(ShizukuProvider.PERMISSION)) {
+                onRequestPermissionResult(requestCode, result);
+            }
+        }
+    }
+
+    @Override
+    public void onRequestPermissionResult(int requestCode, int grantResult) {
+        boolean isGranted = grantResult == PackageManager.PERMISSION_GRANTED;
+        if(isGranted) {
+            grantWriteSecureSettingsPermission();
+        } else {
+            proceedWithOnCreate();
+        }
+    }
+
+    @SuppressLint("PrivateApi")
+    private void grantWriteSecureSettingsPermission() {
+        try {
+            Class<?> iPmClass = Class.forName("android.content.pm.IPackageManager");
+            Class<?> iPmStub = Class.forName("android.content.pm.IPackageManager$Stub");
+            Method asInterfaceMethod = iPmStub.getMethod("asInterface", IBinder.class);
+            Method grantRuntimePermissionMethod = iPmClass.getMethod("grantRuntimePermission", String.class, String.class, int.class);
+
+            Object iPmInstance = asInterfaceMethod.invoke(null, new ShizukuBinderWrapper(SystemServiceHelper.getSystemService("package")));
+            grantRuntimePermissionMethod.invoke(iPmInstance, getPackageName(), android.Manifest.permission.WRITE_SECURE_SETTINGS, 0);
+        } catch (Exception ignored) {}
+
+        finish();
+    }
+
+    protected abstract void proceedWithOnCreate();
+}
\ No newline at end of file
index 31835d5..fd2b89d 100644 (file)
@@ -15,5 +15,6 @@ allprojects {
         POWERMOCK_VERSION = "2.0.9"
         AGP_VERSION = "7.0.4"
         HIDDEN_API_BYPASS_VERSION = "3.0"
+        SHIZUKU_VERSION = "11.0.3"
     }
 }