Skip to content

Commit

Permalink
Initial implementation of Shizuku for enabling additional desktop mod…
Browse files Browse the repository at this point in the history
…e settings
  • Loading branch information
farmerbb committed Dec 13, 2021
1 parent 9f0a85d commit 3bcac05
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 6 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@
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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
10 changes: 10 additions & 0 deletions app/src/playstore/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand Down Expand Up @@ -639,6 +642,13 @@
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>

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
1 change: 1 addition & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit 3bcac05

Please sign in to comment.