-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of Shizuku for enabling additional desktop mod…
…e settings
- Loading branch information
Showing
6 changed files
with
150 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...nonplaystore/java/com/farmerbb/taskbar/activity/EnableAdditionalSettingsActivityBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...rc/playstore/java/com/farmerbb/taskbar/activity/EnableAdditionalSettingsActivityBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters