-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Although using runtime permission from Android is perfect enough, it still have two "problems": 1. MIUI (or maybe more OEMs) breaks custom runtime permission, users can only change Shizuku permission from Shizuku app 2. Possible system bug (#83)
- Loading branch information
Showing
33 changed files
with
953 additions
and
145 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
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
74 changes: 35 additions & 39 deletions
74
manager/src/main/java/moe/shizuku/manager/authorization/AuthorizationManager.kt
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 |
---|---|---|
@@ -1,46 +1,42 @@ | ||
package moe.shizuku.manager.authorization; | ||
|
||
import android.content.Context; | ||
import android.content.pm.PackageInfo; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class AuthorizationManager { | ||
|
||
private static final AuthorizationManagerImpl IMPL; | ||
|
||
static { | ||
IMPL = new AuthorizationManagerImplV23(); | ||
} | ||
|
||
public static void init(Context context) { | ||
IMPL.init(context); | ||
} | ||
|
||
public static boolean granted(String packageName, int uid) { | ||
return IMPL.granted(packageName, uid); | ||
} | ||
|
||
public static void revoke(String packageName, int uid) { | ||
IMPL.revoke(packageName, uid); | ||
package moe.shizuku.manager.authorization | ||
|
||
import android.content.pm.PackageInfo | ||
import android.content.pm.PackageManager | ||
import android.os.Process | ||
import moe.shizuku.api.ShizukuService | ||
import moe.shizuku.manager.Manifest | ||
import moe.shizuku.manager.utils.ShizukuSystemApis | ||
import java.util.* | ||
|
||
object AuthorizationManager { | ||
|
||
private const val FLAG_ALLOWED = 1 shl 1 | ||
private const val FLAG_DENIED = 1 shl 2 | ||
private const val MASK_PERMISSION = FLAG_ALLOWED or FLAG_DENIED | ||
|
||
fun getPackages(pmFlags: Int): List<PackageInfo> { | ||
val packages: MutableList<PackageInfo> = ArrayList() | ||
for (pi in ShizukuSystemApis.getInstalledPackages(pmFlags or PackageManager.GET_PERMISSIONS, Process.myUid() / 100000)) { | ||
if (pi.requestedPermissions == null) continue | ||
for (p in pi.requestedPermissions) { | ||
if (Manifest.permission.API_V23 == p) { | ||
packages.add(pi) | ||
break | ||
} | ||
} | ||
} | ||
return packages | ||
} | ||
|
||
public static void grant(String packageName, int uid) { | ||
IMPL.grant(packageName, uid); | ||
fun granted(packageName: String, uid: Int): Boolean { | ||
return (ShizukuService.getFlagsForUid(uid, MASK_PERMISSION) and FLAG_ALLOWED) == FLAG_ALLOWED | ||
} | ||
|
||
public static List<PackageInfo> getPackages(int pmFlags) { | ||
return IMPL.getPackages(pmFlags); | ||
fun grant(packageName: String, uid: Int) { | ||
ShizukuService.updateFlagsForUid(uid, MASK_PERMISSION, FLAG_ALLOWED) | ||
} | ||
|
||
public static List<PackageInfo> getGrantedPackages(int pmFlags) { | ||
List<PackageInfo> packages = new ArrayList<>(); | ||
for (PackageInfo pi : IMPL.getPackages(pmFlags)) { | ||
if (granted(pi.packageName, pi.applicationInfo.uid)) { | ||
packages.add(pi); | ||
} | ||
} | ||
return packages; | ||
fun revoke(packageName: String, uid: Int) { | ||
ShizukuService.updateFlagsForUid(uid, MASK_PERMISSION, 0) | ||
} | ||
} | ||
} |
19 changes: 0 additions & 19 deletions
19
manager/src/main/java/moe/shizuku/manager/authorization/AuthorizationManagerImpl.java
This file was deleted.
Oops, something went wrong.
37 changes: 0 additions & 37 deletions
37
manager/src/main/java/moe/shizuku/manager/authorization/AuthorizationManagerImplV23.kt
This file was deleted.
Oops, something went wrong.
69 changes: 69 additions & 0 deletions
69
manager/src/main/java/moe/shizuku/manager/authorization/RequestPermissionActivity.kt
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,69 @@ | ||
package moe.shizuku.manager.authorization | ||
|
||
import android.app.Dialog | ||
import android.content.pm.ApplicationInfo | ||
import android.os.Bundle | ||
import androidx.appcompat.app.AlertDialog | ||
import moe.shizuku.api.ShizukuApiConstants.REQUEST_PERMISSION_REPLY_ALLOWED | ||
import moe.shizuku.api.ShizukuApiConstants.REQUEST_PERMISSION_REPLY_IS_ONETIME | ||
import moe.shizuku.api.ShizukuService | ||
import moe.shizuku.manager.R | ||
import moe.shizuku.manager.app.AppActivity | ||
import moe.shizuku.manager.databinding.ConfirmationDialogBinding | ||
import moe.shizuku.manager.utils.Logger.LOGGER | ||
import rikka.html.text.HtmlCompat | ||
|
||
class RequestPermissionActivity : AppActivity() { | ||
|
||
private lateinit var dialog: Dialog | ||
|
||
private fun setResult(requestUid: Int, requestPid: Int, requestCode: Int, allowed: Boolean, onetime: Boolean) { | ||
val data = Bundle() | ||
data.putBoolean(REQUEST_PERMISSION_REPLY_ALLOWED, allowed) | ||
data.putBoolean(REQUEST_PERMISSION_REPLY_IS_ONETIME, onetime) | ||
try { | ||
ShizukuService.dispatchPermissionConfirmationResult(requestUid, requestPid, requestCode, data) | ||
} catch (e: Throwable) { | ||
LOGGER.e("dispatchPermissionConfirmationResult") | ||
} | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
val uid = intent.getIntExtra("uid", -1) | ||
val pid = intent.getIntExtra("pid", -1) | ||
val requestCode = intent.getIntExtra("requestCode", -1) | ||
val ai = intent.getParcelableExtra<ApplicationInfo>("applicationInfo") | ||
if (uid == -1 || pid == -1 || ai == null) { | ||
return | ||
} | ||
|
||
val label = try { | ||
ai.loadLabel(packageManager) | ||
} catch (e: Exception) { | ||
ai.packageName | ||
} | ||
|
||
val binding = ConfirmationDialogBinding.inflate(layoutInflater).apply { | ||
button1.setOnClickListener { | ||
setResult(uid, pid, requestCode, allowed = true, onetime = false) | ||
dialog.dismiss() | ||
} | ||
button3.setOnClickListener { | ||
setResult(uid, pid, requestCode, allowed = false, onetime = true) | ||
dialog.dismiss() | ||
} | ||
title.text = HtmlCompat.fromHtml(getString(R.string.permission_warning_template, | ||
label, getString(R.string.permission_group_description))) | ||
} | ||
|
||
dialog = AlertDialog.Builder(this) | ||
.setView(binding.root) | ||
.setCancelable(false) | ||
.setOnDismissListener { finish() } | ||
.create() | ||
dialog.setCanceledOnTouchOutside(false) | ||
dialog.show() | ||
} | ||
} |
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,67 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical"> | ||
|
||
<ImageView | ||
android:layout_width="24dp" | ||
android:layout_height="24dp" | ||
android:layout_gravity="center" | ||
android:layout_marginTop="24dp" | ||
android:importantForAccessibility="no" | ||
android:src="@drawable/ic_permission_24dp" | ||
android:tint="?android:colorAccent" | ||
android:tintMode="src_in" /> | ||
|
||
<TextView | ||
android:id="@android:id/title" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center" | ||
android:paddingStart="24dp" | ||
android:paddingTop="8dp" | ||
android:paddingEnd="24dp" | ||
android:paddingBottom="16dp" | ||
android:textColor="?android:textColorPrimary" | ||
android:textSize="20sp" | ||
tools:text="@string/permission_warning_template" /> | ||
|
||
<View | ||
android:layout_width="match_parent" | ||
android:layout_height="1dp" | ||
android:background="?android:dividerVertical" /> | ||
|
||
<TextView | ||
android:id="@android:id/button1" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:background="?android:selectableItemBackground" | ||
android:fontFamily="sans-serif-medium" | ||
android:gravity="center" | ||
android:minHeight="56dp" | ||
android:padding="16dp" | ||
android:text="@string/grant_dialog_button_allow_always" | ||
android:textColor="?android:colorAccent" | ||
android:textSize="14sp" /> | ||
|
||
<View | ||
android:layout_width="match_parent" | ||
android:layout_height="1dp" | ||
android:background="?android:dividerVertical" /> | ||
|
||
<TextView | ||
android:id="@android:id/button3" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:background="?android:selectableItemBackground" | ||
android:fontFamily="sans-serif-medium" | ||
android:gravity="center" | ||
android:minHeight="56dp" | ||
android:padding="16dp" | ||
android:text="@string/grant_dialog_button_deny" | ||
android:textColor="?android:colorAccent" | ||
android:textSize="14sp" /> | ||
|
||
</LinearLayout> |
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
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
Oops, something went wrong.