├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── themes.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-anydpi
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── drawable
│ │ │ │ ├── ic_refresh.xml
│ │ │ │ ├── ic_launcher_foreground.xml
│ │ │ │ └── ic_launcher_background.xml
│ │ │ ├── xml
│ │ │ │ ├── backup_rules.xml
│ │ │ │ └── data_extraction_rules.xml
│ │ │ ├── values-night
│ │ │ │ └── themes.xml
│ │ │ ├── menu
│ │ │ │ └── main_menu.xml
│ │ │ └── layout
│ │ │ │ ├── item_app.xml
│ │ │ │ ├── item_permission.xml
│ │ │ │ ├── activity_main.xml
│ │ │ │ └── activity_app_detail.xml
│ │ ├── aidl
│ │ │ └── com
│ │ │ │ └── rrk
│ │ │ │ └── managesensors
│ │ │ │ └── IAppOpsService.aidl
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── rrk
│ │ │ │ └── managesensors
│ │ │ │ ├── PermissionAdapter.kt
│ │ │ │ ├── AppAdapter.kt
│ │ │ │ ├── AppOpsInfo.kt
│ │ │ │ ├── AppOpsService.kt
│ │ │ │ ├── AppDetailActivity.kt
│ │ │ │ └── MainActivity.kt
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── rrk
│ │ │ └── managesensors
│ │ │ └── ExampleUnitTest.kt
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── rrk
│ │ └── managesensors
│ │ └── ExampleInstrumentedTest.kt
├── proguard-rules.pro
└── build.gradle.kts
├── .idea
├── .name
├── .gitignore
├── codeStyles
│ ├── codeStyleConfig.xml
│ └── Project.xml
├── compiler.xml
├── kotlinc.xml
├── vcs.xml
├── migrations.xml
├── misc.xml
├── deploymentTargetSelector.xml
├── gradle.xml
└── other.xml
├── gradle
├── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
└── libs.versions.toml
├── .gitignore
├── settings.gradle.kts
├── LICENSE
├── gradle.properties
├── README-zhCN.md
├── README.md
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | ManageSensors
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | ManageSensors
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Carry-rrk/ManageSensors/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Carry-rrk/ManageSensors/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Carry-rrk/ManageSensors/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Carry-rrk/ManageSensors/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Carry-rrk/ManageSensors/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Carry-rrk/ManageSensors/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Carry-rrk/ManageSensors/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Carry-rrk/ManageSensors/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Carry-rrk/ManageSensors/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Carry-rrk/ManageSensors/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Carry-rrk/ManageSensors/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/.idea/codeStyles/codeStyleConfig.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/kotlinc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/aidl/com/rrk/managesensors/IAppOpsService.aidl:
--------------------------------------------------------------------------------
1 | package com.rrk.managesensors;
2 |
3 | interface IAppOpsService {
4 | void setAppOps(String packageName, String permission, boolean enabled) = 1;
5 | boolean getAppOps(String packageName, String permission) = 2;
6 | }
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Jan 01 17:45:07 HKT 2025
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
5 | zipStoreBase=GRADLE_USER_HOME
6 | zipStorePath=wrapper/dists
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 | local.properties
16 |
--------------------------------------------------------------------------------
/.idea/migrations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FFBB86FC
4 | #FF6200EE
5 | #FF3700B3
6 | #FF03DAC5
7 | #FF018786
8 | #FF000000
9 | #FFFFFFFF
10 |
--------------------------------------------------------------------------------
/app/src/test/java/com/rrk/managesensors/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.rrk.managesensors
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_refresh.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/xml/backup_rules.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
13 |
--------------------------------------------------------------------------------
/settings.gradle.kts:
--------------------------------------------------------------------------------
1 | pluginManagement {
2 | repositories {
3 | google {
4 | content {
5 | includeGroupByRegex("com\\.android.*")
6 | includeGroupByRegex("com\\.google.*")
7 | includeGroupByRegex("androidx.*")
8 | }
9 | }
10 | mavenCentral()
11 | gradlePluginPortal()
12 | }
13 | }
14 | dependencyResolutionManagement {
15 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
16 | repositories {
17 | google()
18 | mavenCentral()
19 | }
20 | }
21 |
22 | rootProject.name = "ManageSensors"
23 | include(":app")
24 |
--------------------------------------------------------------------------------
/app/src/main/res/xml/data_extraction_rules.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
12 |
13 |
19 |
--------------------------------------------------------------------------------
/.idea/deploymentTargetSelector.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/rrk/managesensors/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.rrk.managesensors
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("com.rrk.managesensors", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/app/src/main/res/values-night/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/gradle/libs.versions.toml:
--------------------------------------------------------------------------------
1 | [versions]
2 | agp = "8.5.1"
3 | kotlin = "1.9.0"
4 | coreKtx = "1.15.0"
5 | junit = "4.13.2"
6 | junitVersion = "1.2.1"
7 | espressoCore = "3.6.1"
8 | appcompat = "1.7.0"
9 | material = "1.12.0"
10 |
11 | [libraries]
12 | androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
13 | junit = { group = "junit", name = "junit", version.ref = "junit" }
14 | androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
15 | androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
16 | androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
17 | material = { group = "com.google.android.material", name = "material", version.ref = "material" }
18 |
19 | [plugins]
20 | android-application = { id = "com.android.application", version.ref = "agp" }
21 | jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
22 |
23 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/main_menu.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2025 Carry-rrk
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_app.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
12 |
13 |
18 |
19 |
24 |
25 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/app/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
19 |
20 |
23 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_permission.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
20 |
21 |
26 |
27 |
28 |
33 |
34 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. For more details, visit
12 | # https://developer.android.com/r/tools/gradle-multi-project-decoupled-projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Kotlin code style for this project: "official" or "obsolete":
19 | kotlin.code.style=official
20 | # Enables namespacing of each library's R class so that its R class includes only the
21 | # resources declared in the library itself and none from the library's dependencies,
22 | # thereby reducing the size of the R class for that library
23 | android.nonTransitiveRClass=true
--------------------------------------------------------------------------------
/README-zhCN.md:
--------------------------------------------------------------------------------
1 | # ManageSensors
2 | [English Version](./README.md)
3 | ## 目录
4 | - [项目简介](#项目简介)
5 | - [当前状态](#当前状态)
6 | - [功能特性](#功能特性)
7 | - [使用说明](#使用说明)
8 | - [已知问题](#已知问题)
9 | - [特别说明](#特别说明)
10 | - [贡献](#贡献)
11 | - [许可证](#许可证)
12 |
13 | ## 项目简介
14 | **ManageSensors** 是一个基于 **Shizuku** 的 Android 应用程序,通过 **AppOps** 实现对应用程序权限的精细控制,特别是解决某些应用在静音或免打扰模式下仍能触发震动的问题。
15 |
16 | ## 当前状态
17 | ### 测试环境
18 | - **Shizuku**(通过 ADB 或 root 权限)
19 | - **Android 15**
20 | - **Realme 设备**(系统版本:Realme UI 6.0)
21 |
22 | ### 测试结果
23 | - 部分 **AppOps** 设置可能无法生效。
24 | - 主要目标(禁用静音/免打扰模式下的震动)已成功实现。
25 | - 系统版本(Realme UI 6.0)对功能的影响较小,预计在其他基于 Android 15 的设备上也能正常运行。
26 |
27 | ## 功能特性
28 | - 通过 **Shizuku** 调用 **AppOps** API,实现对应用程序权限的精细控制。
29 | - 解决应用程序在静音/免打扰模式下仍能震动的问题,提升用户体验。
30 | - 提供简洁的用户界面,方便用户快速管理应用程序权限。
31 |
32 | ## 使用说明
33 | 1. 确保设备已安装 **Shizuku** 并正常运行(详情请参考 [Shizuku 官方文档](https://github.com/RikkaApps/Shizuku))。
34 | 2. 下载并安装 **ManageSensors**。
35 | 3. 打开应用,授予 **Shizuku** 权限。
36 | 4. 在应用列表中选择目标应用程序,调整相关权限设置。
37 |
38 | ## 已知问题
39 | - 部分 **AppOps** 设置可能无法生效,具体原因可能与系统限制或设备兼容性有关。
40 | - 目前仅在 **Android 15** 和 **Realme 设备**(Realme UI 6.0)上测试,其他设备和系统版本可能存在兼容性问题。
41 |
42 | ## 特别说明
43 | - 我并非专业的 Android 开发者,代码大部分由 **Cursor**(AI 代码生成工具)生成。如果存在任何侵权问题,请通过 Issue 提出,我将及时处理。
44 | - 尽管测试环境有限(Realme 设备,Realme UI 6.0),但预计这不会对功能产生重大影响。
45 |
46 | ## 贡献
47 | 欢迎提交 Issue 或 Pull Request 帮助改进 **ManageSensors**。贡献时请注意以下事项:
48 | - 提交 Issue 时,请详细描述问题或建议。
49 | - 提交 Pull Request 时,请确保代码风格一致,并附带相关测试结果。
50 |
51 | ## 许可证
52 | 本项目采用 **MIT 许可证**,详情请参阅 [LICENSE](LICENSE) 文件。
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
11 |
12 |
19 |
20 |
21 |
22 |
23 |
29 |
30 |
31 |
37 |
38 |
39 |
45 |
46 |
--------------------------------------------------------------------------------
/app/src/main/java/com/rrk/managesensors/PermissionAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.rrk.managesensors
2 |
3 | import android.content.Context
4 | import android.graphics.Color
5 | import android.view.LayoutInflater
6 | import android.view.ViewGroup
7 | import androidx.recyclerview.widget.RecyclerView
8 | import com.rrk.managesensors.databinding.ItemPermissionBinding
9 |
10 | class PermissionAdapter(
11 | private val context: Context,
12 | private val onTogglePermission: (AppOpsInfo) -> Unit
13 | ) : RecyclerView.Adapter() {
14 |
15 | private val permissions = mutableListOf()
16 |
17 | fun updatePermissions(newPermissions: List) {
18 | permissions.clear()
19 | permissions.addAll(newPermissions)
20 | notifyDataSetChanged()
21 | }
22 |
23 | inner class ViewHolder(val binding: ItemPermissionBinding) : RecyclerView.ViewHolder(binding.root)
24 |
25 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
26 | val binding = ItemPermissionBinding.inflate(LayoutInflater.from(parent.context), parent, false)
27 | return ViewHolder(binding)
28 | }
29 |
30 | override fun onBindViewHolder(holder: ViewHolder, position: Int) {
31 | val permission = permissions[position]
32 | holder.binding.apply {
33 | tvPermissionName.text = permission.name
34 | tvPermissionDescription.text = permission.description
35 | btnToggle.apply {
36 | text = if (permission.isEnabled) "已启用" else "已禁用"
37 | setBackgroundColor(if (permission.isEnabled) Color.GREEN else Color.RED)
38 | setOnClickListener {
39 | onTogglePermission(permission)
40 | }
41 | }
42 | }
43 | }
44 |
45 | override fun getItemCount() = permissions.size
46 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/rrk/managesensors/AppAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.rrk.managesensors
2 |
3 |
4 | import android.content.Context
5 | import android.content.Intent
6 | import android.graphics.drawable.Drawable
7 | import android.view.LayoutInflater
8 | import android.view.ViewGroup
9 | import androidx.recyclerview.widget.RecyclerView
10 | import com.rrk.managesensors.databinding.ItemAppBinding
11 |
12 | data class AppInfo(
13 | val packageName: String,
14 | val appName: String,
15 | val icon: Drawable,
16 | var isSelected: Boolean = false
17 | )
18 |
19 | class AppAdapter(private val context: Context) :
20 | RecyclerView.Adapter() {
21 |
22 | private var appList = mutableListOf()
23 |
24 | fun updateList(newList: List) {
25 | appList.clear()
26 | appList.addAll(newList)
27 | notifyDataSetChanged()
28 | }
29 |
30 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
31 | val binding = ItemAppBinding.inflate(LayoutInflater.from(parent.context), parent, false)
32 | return ViewHolder(binding)
33 | }
34 |
35 | override fun onBindViewHolder(holder: ViewHolder, position: Int) {
36 | val app = appList[position]
37 | holder.binding.apply {
38 | ivAppIcon.setImageDrawable(app.icon)
39 | tvAppName.text = app.appName
40 | tvPackageName.text = app.packageName
41 |
42 | root.setOnClickListener {
43 | val intent = Intent(context, AppDetailActivity::class.java).apply {
44 | putExtra("package_name", app.packageName)
45 | putExtra("app_name", app.appName)
46 | }
47 | context.startActivity(intent)
48 | }
49 | }
50 | }
51 |
52 | override fun getItemCount() = appList.size
53 |
54 | class ViewHolder(val binding: ItemAppBinding) : RecyclerView.ViewHolder(binding.root)
55 | }
--------------------------------------------------------------------------------
/app/build.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | id("com.android.application")
3 | id("kotlin-android")
4 | }
5 |
6 | android {
7 | namespace = "com.rrk.managesensors"
8 | compileSdk = 34
9 |
10 | defaultConfig {
11 | applicationId = "com.rrk.managesensors"
12 | minSdk = 26
13 | targetSdk = 34
14 | versionCode = 1
15 | versionName = "1.0"
16 |
17 | testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
18 | }
19 | compileOptions {
20 | sourceCompatibility = JavaVersion.VERSION_17
21 | targetCompatibility = JavaVersion.VERSION_17
22 | }
23 |
24 | kotlinOptions {
25 | jvmTarget = "17"
26 | }
27 | buildTypes {
28 | release {
29 | isMinifyEnabled = false
30 | proguardFiles(
31 | getDefaultProguardFile("proguard-android-optimize.txt"),
32 | "proguard-rules.pro"
33 | )
34 | }
35 | }
36 |
37 | buildFeatures {
38 | aidl = true
39 | dataBinding = true
40 | viewBinding = true
41 | }
42 |
43 | sourceSets {
44 | getByName("main") {
45 | aidl {
46 | srcDirs("src/main/aidl")
47 | }
48 | }
49 | }
50 | }
51 |
52 | dependencies {
53 | implementation("androidx.core:core:1.10.1")
54 | implementation("androidx.core:core-ktx:1.10.1")
55 | implementation("androidx.appcompat:appcompat:1.6.1")
56 | implementation("com.google.android.material:material:1.9.0")
57 | implementation("androidx.constraintlayout:constraintlayout:2.1.4")
58 |
59 | implementation("dev.rikka.shizuku:api:13.1.5")
60 | testImplementation("junit:junit:4.13.2")
61 | implementation("dev.rikka.shizuku:provider:13.1.5")
62 | androidTestImplementation("androidx.test.ext:junit:1.1.5")
63 | androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
64 | implementation("androidx.databinding:databinding-runtime:8.2.2")
65 | implementation("androidx.databinding:databinding-common:8.2.2")
66 |
67 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
15 |
25 |
26 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
38 |
39 |
46 |
47 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ManageSensors
2 | [中文版本](./README-zhCN.md)
3 | ## Table of Contents
4 | - [Project Overview](#project-overview)
5 | - [Current Status](#current-status)
6 | - [Features](#features)
7 | - [Usage Instructions](#usage-instructions)
8 | - [Known Issues](#known-issues)
9 | - [Special Notes](#special-notes)
10 | - [Contribution](#contribution)
11 | - [License](#license)
12 |
13 | ## Project Overview
14 | **ManageSensors** is an Android application based on **Shizuku** that enables fine-grained control over app permissions through **AppOps**, specifically addressing the issue of certain apps still triggering vibrations in silent or Do Not Disturb mode.
15 |
16 | ## Current Status
17 | ### Test Environment
18 | - **Shizuku** (via ADB or root access)
19 | - **Android 15**
20 | - **Realme Devices** (OS Version: Realme UI 6.0)
21 |
22 | ### Test Results
23 | - Some **AppOps** settings may not take effect.
24 | - The primary goal (disabling vibrations in silent/Do Not Disturb mode) has been successfully achieved.
25 | - The OS version (Realme UI 6.0) has minimal impact on functionality, and it is expected to work on other Android 15-based devices as well.
26 |
27 | ## Features
28 | - Utilizes **Shizuku** to call **AppOps** APIs for fine-grained app permission control.
29 | - Resolves the issue of apps vibrating in silent/Do Not Disturb mode, improving user experience.
30 | - Provides a clean and simple user interface for easy app permission management.
31 |
32 | ## Usage Instructions
33 | 1. Ensure that **Shizuku** is installed and running on your device (refer to the [Shizuku Documentation](https://github.com/RikkaApps/Shizuku) for details).
34 | 2. Download and install **ManageSensors**.
35 | 3. Open the app and grant **Shizuku** permissions.
36 | 4. Select the target app from the list and adjust the relevant permission settings.
37 |
38 | ## Known Issues
39 | - Some **AppOps** settings may not take effect, possibly due to system restrictions or device compatibility.
40 | - Currently tested only on **Android 15** and **Realme Devices** (Realme UI 6.0); compatibility issues may exist on other devices and Android versions.
41 |
42 | ## Special Notes
43 | - As a non-professional Android developer, the majority of the code was generated using **Cursor**, an AI-powered code generation tool. If there are any copyright issues, please raise an Issue, and I will address it promptly.
44 | - Although the testing environment is limited (Realme devices, Realme UI 6.0), it is expected that this will not significantly impact functionality.
45 |
46 | ## Contribution
47 | Issues and Pull Requests are welcome to help improve **ManageSensors**. When contributing, please note the following:
48 | - When submitting an Issue, please provide a detailed description of the problem or suggestion.
49 | - When submitting a Pull Request, ensure that the code style is consistent and include relevant test results.
50 |
51 | ## License
52 | This project is licensed under the **MIT License**. For more details, please refer to the [LICENSE](LICENSE) file.
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_app_detail.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
10 |
11 |
16 |
17 |
22 |
23 |
27 |
28 |
33 |
34 |
40 |
41 |
46 |
47 |
48 |
49 |
54 |
55 |
61 |
62 |
63 |
71 |
72 |
77 |
78 |
--------------------------------------------------------------------------------
/app/src/main/java/com/rrk/managesensors/AppOpsInfo.kt:
--------------------------------------------------------------------------------
1 | package com.rrk.managesensors
2 |
3 | data class AppOpsInfo(
4 | val name: String, // AppOps 内部名称
5 | val description: String, // 显示给用户看的描述
6 | var isEnabled: Boolean = false
7 | )
8 |
9 | object AppOpsPermissions {
10 | val permissions = listOf(
11 | // 位置相关
12 | AppOpsInfo("COARSE_LOCATION", "粗略位置"),
13 | AppOpsInfo("FINE_LOCATION", "精确位置"),
14 | AppOpsInfo("GPS", "GPS定位"),
15 | AppOpsInfo("MOCK_LOCATION", "模拟位置"),
16 |
17 | // 传感器相关
18 | AppOpsInfo("ACTIVITY_RECOGNITION", "活动识别"),
19 | AppOpsInfo("SENSORS", "传感器"),
20 | AppOpsInfo("BODY_SENSORS", "身体传感器"),
21 | AppOpsInfo("ACCELEROMETER", "加速度传感器"),
22 | AppOpsInfo("GYROSCOPE", "陀螺仪"),
23 | AppOpsInfo("MAGNETIC_SENSOR", "磁力计"),
24 |
25 | // 存储相关
26 | AppOpsInfo("READ_EXTERNAL_STORAGE", "读取存储"),
27 | AppOpsInfo("WRITE_EXTERNAL_STORAGE", "写入存储"),
28 | AppOpsInfo("READ_MEDIA_AUDIO", "读取音频"),
29 | AppOpsInfo("READ_MEDIA_VIDEO", "读取视频"),
30 | AppOpsInfo("READ_MEDIA_IMAGES", "读取图片"),
31 |
32 | // 个人信息相关
33 | AppOpsInfo("READ_CONTACTS", "读取通讯录"),
34 | AppOpsInfo("WRITE_CONTACTS", "修改通讯录"),
35 | AppOpsInfo("READ_CALL_LOG", "读取通话记录"),
36 | AppOpsInfo("WRITE_CALL_LOG", "写入通话记录"),
37 | AppOpsInfo("READ_CALENDAR", "读取日历"),
38 | AppOpsInfo("WRITE_CALENDAR", "修改日历"),
39 | AppOpsInfo("READ_SMS", "读取短信"),
40 | AppOpsInfo("WRITE_SMS", "发送短信"),
41 | AppOpsInfo("RECEIVE_SMS", "接收短信"),
42 | AppOpsInfo("READ_PHONE_STATE", "读取手机状态"),
43 | AppOpsInfo("CALL_PHONE", "拨打电话"),
44 |
45 | // 媒体相关
46 | AppOpsInfo("CAMERA", "相机"),
47 | AppOpsInfo("RECORD_AUDIO", "录音"),
48 | AppOpsInfo("RECORD_VIDEO", "录像"),
49 | AppOpsInfo("TAKE_AUDIO_FOCUS", "音频焦点"),
50 | AppOpsInfo("TAKE_MEDIA_BUTTONS", "媒体按键"),
51 |
52 | // 系统相关
53 | AppOpsInfo("RUN_IN_BACKGROUND", "后台运行"),
54 | AppOpsInfo("WAKE_LOCK", "保持唤醒"),
55 | AppOpsInfo("SYSTEM_ALERT_WINDOW", "显示悬浮窗"),
56 | AppOpsInfo("POST_NOTIFICATION", "发送通知"),
57 | AppOpsInfo("VIBRATE", "震动"),
58 | AppOpsInfo("READ_CLIPBOARD", "读取剪贴板"),
59 | AppOpsInfo("WRITE_CLIPBOARD", "写入剪贴板"),
60 | AppOpsInfo("BLUETOOTH_SCAN", "蓝牙扫描"),
61 | AppOpsInfo("BLUETOOTH_CONNECT", "蓝牙连接"),
62 | AppOpsInfo("BLUETOOTH_ADVERTISE", "蓝牙广播"),
63 | AppOpsInfo("WIFI_SCAN", "WiFi扫描"),
64 |
65 | // 网络相关
66 | AppOpsInfo("INTERNET", "网络访问"),
67 | AppOpsInfo("ACCESS_WIFI_STATE", "访问WiFi状态"),
68 | AppOpsInfo("CHANGE_WIFI_STATE", "修改WiFi状态"),
69 | AppOpsInfo("ACCESS_NETWORK_STATE", "访问网络状态"),
70 | AppOpsInfo("CHANGE_NETWORK_STATE", "修改网络状态")
71 | )
72 |
73 | // 按类别分组的权限
74 | val permissionGroups = mapOf(
75 | "位置" to permissions.slice(0..3),
76 | "传感器" to permissions.slice(4..9),
77 | "存储" to permissions.slice(10..14),
78 | "个人信息" to permissions.slice(15..25),
79 | "媒体" to permissions.slice(26..30),
80 | "系统" to permissions.slice(31..41),
81 | "网络" to permissions.slice(42..46)
82 | )
83 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/rrk/managesensors/AppOpsService.kt:
--------------------------------------------------------------------------------
1 | package com.rrk.managesensors
2 |
3 | import android.app.Service
4 | import android.content.Intent
5 | import android.os.IBinder
6 | import android.os.RemoteException
7 | import android.util.Log
8 | import java.io.BufferedReader
9 | import rikka.shizuku.Shizuku
10 | import java.io.InputStreamReader
11 | class AppOpsService : IAppOpsService.Stub() {
12 | companion object {
13 | private const val TAG = "AppOpsService"
14 | }
15 |
16 | override fun setAppOps(packageName: String, permission: String, enabled: Boolean) {
17 | Log.d(TAG, "setAppOps 开始执行: pkg=$packageName, perm=$permission, enabled=$enabled")
18 |
19 | if (enabled) {
20 | // 启用权限
21 | executeCommand(packageName, permission, "allow")
22 | if (!getAppOps(packageName, permission)) {
23 | throw RemoteException("启用权限失败")
24 | }
25 | } else {
26 | // 先尝试 ignore
27 | executeCommand(packageName, permission, "ignore")
28 | if (getAppOps(packageName, permission)) {
29 | // ignore 没有生效,尝试 deny
30 | Log.d(TAG, "ignore 模式未生效,尝试使用 deny")
31 | executeCommand(packageName, permission, "deny")
32 | if (getAppOps(packageName, permission)) {
33 | throw RemoteException("禁用权限失败:ignore 和 deny 都无效")
34 | }
35 | }
36 | }
37 | }
38 |
39 | private fun executeCommand(packageName: String, permission: String, mode: String) {
40 | try {
41 | val process = Runtime.getRuntime().exec(arrayOf(
42 | "appops",
43 | "set",
44 | packageName,
45 | permission,
46 | mode
47 | ))
48 |
49 | val error = process.errorStream.bufferedReader().readText()
50 | val exitCode = process.waitFor()
51 |
52 | if (exitCode != 0) {
53 | throw Exception("命令执行失败: $error")
54 | }
55 |
56 | Log.d(TAG, "命令执行完成: appops set $packageName $permission $mode")
57 | } catch (e: Exception) {
58 | Log.e(TAG, "执行命令失败", e)
59 | throw e
60 | }
61 | }
62 |
63 | override fun getAppOps(packageName: String, permission: String): Boolean {
64 | try {
65 | val process = Runtime.getRuntime().exec(arrayOf(
66 | "appops",
67 | "get",
68 | packageName,
69 | permission
70 | ))
71 | val output = process.inputStream.bufferedReader().readText()
72 | val exitCode = process.waitFor()
73 | if (exitCode != 0) {
74 | throw Exception("命令执行失败: $output")
75 | }
76 | Log.d(TAG, "查询结果: $output")
77 |
78 | return when {
79 | output.contains("allow") -> true
80 | output.contains("deny") -> false
81 | output.contains("default") -> true
82 | output.contains("ignore") -> false
83 | else -> false
84 | }
85 | } catch (e: Exception) {
86 | Log.e(TAG, "执行查询命令出错", e)
87 | throw RemoteException(e.message)
88 | }
89 | }
90 |
91 | // override fun onBind(intent: Intent?): IBinder? {
92 | // return this
93 | // }
94 | }
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | xmlns:android
18 |
19 | ^$
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 | xmlns:.*
29 |
30 | ^$
31 |
32 |
33 | BY_NAME
34 |
35 |
36 |
37 |
38 |
39 |
40 | .*:id
41 |
42 | http://schemas.android.com/apk/res/android
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 | .*:name
52 |
53 | http://schemas.android.com/apk/res/android
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 | name
63 |
64 | ^$
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 | style
74 |
75 | ^$
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 | .*
85 |
86 | ^$
87 |
88 |
89 | BY_NAME
90 |
91 |
92 |
93 |
94 |
95 |
96 | .*
97 |
98 | http://schemas.android.com/apk/res/android
99 |
100 |
101 | ANDROID_ATTRIBUTE_ORDER
102 |
103 |
104 |
105 |
106 |
107 |
108 | .*
109 |
110 | .*
111 |
112 |
113 | BY_NAME
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/app/src/main/java/com/rrk/managesensors/AppDetailActivity.kt:
--------------------------------------------------------------------------------
1 | package com.rrk.managesensors
2 |
3 | import android.content.ComponentName
4 | import android.content.Intent
5 | import android.content.ServiceConnection
6 | import android.content.pm.PackageManager
7 | import android.os.Bundle
8 | import android.os.IBinder
9 | import android.util.Log
10 | import android.widget.Toast
11 | import androidx.appcompat.app.AppCompatActivity
12 | import androidx.lifecycle.lifecycleScope
13 | import androidx.recyclerview.widget.LinearLayoutManager
14 | import com.rrk.managesensors.databinding.ActivityAppDetailBinding
15 | import rikka.shizuku.Shizuku
16 | import kotlinx.coroutines.launch
17 | import kotlinx.coroutines.Dispatchers
18 | import kotlinx.coroutines.delay
19 | import kotlinx.coroutines.withContext
20 | import androidx.appcompat.app.AlertDialog
21 | import android.view.View
22 | import android.view.WindowManager
23 | import android.widget.Button
24 | import androidx.recyclerview.widget.RecyclerView
25 | import android.graphics.Color
26 | import rikka.shizuku.ShizukuBinderWrapper
27 | import rikka.shizuku.SystemServiceHelper
28 |
29 | class AppDetailActivity : AppCompatActivity() {
30 | companion object {
31 | private const val TAG = "AppDetailActivity"
32 | const val EXTRA_PACKAGE_NAME = "package_name"
33 | }
34 |
35 | private lateinit var binding: ActivityAppDetailBinding
36 | private lateinit var permissionAdapter: PermissionAdapter
37 | private var currentPackageName: String? = null
38 | private var appOpsService: IAppOpsService? = null
39 | private var permissions = mutableListOf()
40 |
41 | override fun onCreate(savedInstanceState: Bundle?) {
42 | super.onCreate(savedInstanceState)
43 | binding = ActivityAppDetailBinding.inflate(layoutInflater)
44 | setContentView(binding.root)
45 |
46 | currentPackageName = intent.getStringExtra(EXTRA_PACKAGE_NAME)
47 | Log.d(TAG, "收到包名: $currentPackageName")
48 |
49 | if (currentPackageName == null) {
50 | Toast.makeText(this, "包名为空,无法加载权限", Toast.LENGTH_SHORT).show()
51 | finish()
52 | return
53 | }
54 |
55 | // 先初始化空的适配器
56 | setupRecyclerView()
57 |
58 | // 显示加载动画
59 | binding.loadingOverlay.visibility = View.VISIBLE
60 | binding.rvPermissions.visibility = View.GONE
61 |
62 | // 连接服务
63 | initAppOpsService()
64 | }
65 |
66 | private fun initAppOpsService() {
67 | try {
68 | Log.d(TAG, "开始初始化 AppOpsService")
69 | val binder = ShizukuBinderWrapper(SystemServiceHelper.getSystemService("appops"))
70 | appOpsService = IAppOpsService.Stub.asInterface(binder)
71 | Log.d(TAG, "AppOpsService 初始化成功")
72 |
73 | // 服务连接成功后,开始加载权限列表
74 | permissions.clear()
75 | permissions.addAll(AppOpsPermissions.permissions)
76 | Log.d(TAG, "权限列表初始化完成,数量: ${permissions.size}")
77 | permissionAdapter.updatePermissions(permissions)
78 |
79 | // 加载权限状态
80 | Log.d(TAG, "开始加载权限状态")
81 | loadAppOpsStatus()
82 | } catch (e: Exception) {
83 | Log.e(TAG, "初始化 AppOpsService 失败", e)
84 | Toast.makeText(this, "服务连接失败: ${e.message}", Toast.LENGTH_SHORT).show()
85 | binding.loadingOverlay.visibility = View.GONE
86 | binding.rvPermissions.visibility = View.VISIBLE
87 | }
88 | }
89 |
90 | private fun setupRecyclerView() {
91 | permissionAdapter = PermissionAdapter(this) { permission ->
92 | togglePermission(permission)
93 | }
94 | binding.rvPermissions.apply {
95 | layoutManager = LinearLayoutManager(this@AppDetailActivity)
96 | adapter = permissionAdapter
97 | }
98 | }
99 |
100 | private fun loadAppOpsStatus() {
101 | currentPackageName?.let { pkgName ->
102 | Log.d(TAG, "开始加载包 $pkgName 的权限状态")
103 | lifecycleScope.launch {
104 | try {
105 | // 在后台获取所有权限状态
106 | val updatedPermissions = withContext(Dispatchers.IO) {
107 | permissions.map { permission ->
108 | try {
109 | Log.d(TAG, "正在获取权限 ${permission.name} 的状态")
110 | delay(50)
111 | val isEnabled = appOpsService?.getAppOps(pkgName, permission.name)
112 | Log.d(TAG, "权限 ${permission.name} 状态: $isEnabled")
113 | permission.copy(isEnabled = isEnabled ?: false)
114 | } catch (e: Exception) {
115 | Log.e(TAG, "获取权限状态失败: ${permission.name}", e)
116 | permission
117 | }
118 | }
119 | }
120 |
121 | Log.d(TAG, "所有权限状态获取完成")
122 | // 获取完成后,更新UI
123 | permissions.clear()
124 | permissions.addAll(updatedPermissions)
125 |
126 | // 隐藏加载动画,显示列表
127 | Log.d(TAG, "更新UI")
128 | binding.loadingOverlay.visibility = View.GONE
129 | binding.rvPermissions.visibility = View.VISIBLE
130 |
131 | // 更新适配器数据
132 | permissionAdapter.updatePermissions(permissions)
133 | Log.d(TAG, "UI更新完成")
134 |
135 | } catch (e: Exception) {
136 | Log.e(TAG, "加载权限状态失败", e)
137 | binding.loadingOverlay.visibility = View.GONE
138 | binding.rvPermissions.visibility = View.VISIBLE
139 | Toast.makeText(this@AppDetailActivity, "获取权限状态失败: ${e.message}", Toast.LENGTH_SHORT).show()
140 | }
141 | }
142 | } ?: run {
143 | Log.e(TAG, "packageName 为空")
144 | binding.loadingOverlay.visibility = View.GONE
145 | binding.rvPermissions.visibility = View.VISIBLE
146 | Toast.makeText(this, "包名为空", Toast.LENGTH_SHORT).show()
147 | }
148 | }
149 |
150 | private fun togglePermission(permission: AppOpsInfo) {
151 | currentPackageName?.let { pkgName ->
152 | if (appOpsService == null) {
153 | Log.e(TAG, "appOpsService 为空")
154 | Toast.makeText(this, "服务未连接", Toast.LENGTH_SHORT).show()
155 | return@let
156 | }
157 |
158 | lifecycleScope.launch {
159 | try {
160 | binding.loadingOverlay.visibility = View.VISIBLE
161 |
162 | val success = withContext(Dispatchers.IO) {
163 | try {
164 | appOpsService?.setAppOps(pkgName, permission.name, !permission.isEnabled)
165 | delay(100)
166 | val currentState = appOpsService?.getAppOps(pkgName, permission.name)
167 | currentState == !permission.isEnabled
168 | } catch (e: Exception) {
169 | Log.e(TAG, "权限操作失败", e)
170 | false
171 | }
172 | }
173 |
174 | binding.loadingOverlay.visibility = View.GONE
175 |
176 | if (success) {
177 | permission.isEnabled = !permission.isEnabled
178 | val position = permissions.indexOfFirst { it.name == permission.name }
179 | if (position != -1) {
180 | permissionAdapter.notifyItemChanged(position)
181 | }
182 |
183 | Toast.makeText(
184 | this@AppDetailActivity,
185 | "${if (permission.isEnabled) "启用" else "禁用"}${permission.description}成功",
186 | Toast.LENGTH_SHORT
187 | ).show()
188 | } else {
189 | Toast.makeText(
190 | this@AppDetailActivity,
191 | "${if (!permission.isEnabled) "启用" else "禁用"}${permission.description}失败",
192 | Toast.LENGTH_SHORT
193 | ).show()
194 | val currentState = appOpsService?.getAppOps(pkgName, permission.name) ?: false
195 | permission.isEnabled = currentState
196 | val position = permissions.indexOfFirst { it.name == permission.name }
197 | if (position != -1) {
198 | permissionAdapter.notifyItemChanged(position)
199 | }
200 | }
201 | } catch (e: Exception) {
202 | binding.loadingOverlay.visibility = View.GONE
203 | Toast.makeText(
204 | this@AppDetailActivity,
205 | "操作失败: ${e.message}",
206 | Toast.LENGTH_SHORT
207 | ).show()
208 | Log.e(TAG, "切换权限失败", e)
209 | val currentState = appOpsService?.getAppOps(pkgName, permission.name) ?: false
210 | permission.isEnabled = currentState
211 | val position = permissions.indexOfFirst { it.name == permission.name }
212 | if (position != -1) {
213 | permissionAdapter.notifyItemChanged(position)
214 | }
215 | }
216 | }
217 | }
218 | }
219 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/rrk/managesensors/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.rrk.managesensors
2 |
3 | import android.content.BroadcastReceiver
4 | import android.content.Context
5 | import android.content.Intent
6 | import android.content.IntentFilter
7 | import android.content.pm.PackageManager
8 | import android.graphics.Color
9 | import android.os.Bundle
10 | import android.text.Editable
11 | import android.text.TextWatcher
12 | import android.widget.Toast
13 | import androidx.appcompat.app.AppCompatActivity
14 | import androidx.recyclerview.widget.LinearLayoutManager
15 | import com.rrk.managesensors.databinding.ActivityMainBinding
16 | import rikka.shizuku.Shizuku
17 | import android.widget.SearchView
18 | import android.graphics.drawable.Drawable
19 | import android.util.Log
20 | import android.view.Menu
21 | import android.view.MenuItem
22 | import android.content.ComponentName
23 | import android.os.IBinder
24 | import android.os.Handler
25 | import android.os.Looper
26 | import com.rrk.managesensors.IAppOpsService
27 | import android.view.LayoutInflater
28 | import android.view.ViewGroup
29 | import androidx.recyclerview.widget.RecyclerView
30 | import com.rrk.managesensors.databinding.ItemAppBinding
31 | import android.content.pm.ApplicationInfo
32 |
33 | class MainActivity : AppCompatActivity() {
34 |
35 | private lateinit var binding: ActivityMainBinding
36 | private val SHIZUKU_CODE = 1
37 | private lateinit var adapter: AppAdapter
38 | private lateinit var searchView: SearchView
39 | private val appList = mutableListOf()
40 | private var appOpsService: IAppOpsService? = null
41 | private lateinit var serviceArgs: Shizuku.UserServiceArgs
42 |
43 | private val serviceConnection = object : android.content.ServiceConnection {
44 | override fun onServiceConnected(componentName: ComponentName?, binder: IBinder?) {
45 | appOpsService = IAppOpsService.Stub.asInterface(binder)
46 | Log.d("MainActivity", "Service 已连接")
47 | }
48 |
49 | override fun onServiceDisconnected(componentName: ComponentName?) {
50 | appOpsService = null
51 | Log.d("MainActivity", "Service 已断开")
52 | }
53 | }
54 |
55 | // Shizuku 权限结果监听器
56 | private val permissionResultListener = Shizuku.OnRequestPermissionResultListener { requestCode, grantResult ->
57 | if (requestCode == SHIZUKU_CODE) {
58 | if (grantResult == PackageManager.PERMISSION_GRANTED) {
59 | binding.shizukuStatus.text = "Shizuku 已授权"
60 | binding.shizukuStatus.setTextColor(Color.GREEN)
61 | } else {
62 | binding.shizukuStatus.text = "Shizuku 未授权"
63 | binding.shizukuStatus.setTextColor(Color.RED)
64 | }
65 | }
66 | }
67 |
68 | // Shizuku Binder 接收监听器
69 | private val binderReceivedListener = Shizuku.OnBinderReceivedListener {
70 | runOnUiThread { checkShizukuPermission() }
71 | }
72 |
73 | // Shizuku Binder 死亡监听器
74 | private val binderDeadListener = Shizuku.OnBinderDeadListener {
75 | runOnUiThread {
76 | binding.shizukuStatus.text = "Shizuku 服务已断开"
77 | binding.shizukuStatus.setTextColor(Color.RED)
78 | }
79 | }
80 |
81 | private val binderReceiver = object : BroadcastReceiver() {
82 | override fun onReceive(context: Context, intent: Intent) {
83 | when (intent.action) {
84 | "rikka.shizuku.intent.action.BINDER_RECEIVED" -> {
85 | checkShizukuStatus()
86 | }
87 | }
88 | }
89 | }
90 |
91 | override fun onCreate(savedInstanceState: Bundle?) {
92 | super.onCreate(savedInstanceState)
93 | binding = ActivityMainBinding.inflate(layoutInflater)
94 | setContentView(binding.root)
95 |
96 | setSupportActionBar(binding.toolbar)
97 |
98 | // 注册 Shizuku 监听器
99 | Shizuku.addBinderReceivedListener(binderReceivedListener)
100 | Shizuku.addBinderDeadListener(binderDeadListener)
101 | Shizuku.addRequestPermissionResultListener(permissionResultListener)
102 |
103 | registerReceiver(
104 | binderReceiver,
105 | IntentFilter("rikka.shizuku.intent.action.BINDER_RECEIVED"),
106 | Context.RECEIVER_NOT_EXPORTED
107 | )
108 |
109 | initSearchView()
110 |
111 | initRecyclerView()
112 | loadApps()
113 |
114 | checkShizukuPermission()
115 | }
116 |
117 | private fun initSearchView() {
118 | searchView = binding.searchView
119 | searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
120 | override fun onQueryTextSubmit(query: String?): Boolean {
121 | return false
122 | }
123 |
124 | override fun onQueryTextChange(newText: String?): Boolean {
125 | val filteredList = if (newText.isNullOrEmpty()) {
126 | appList
127 | } else {
128 | appList.filter {
129 | it.appName.lowercase().contains(newText.lowercase()) ||
130 | it.packageName.lowercase().contains(newText.lowercase())
131 | }
132 | }
133 | adapter.updateList(filteredList)
134 | return true
135 | }
136 | })
137 | }
138 |
139 | private fun initRecyclerView() {
140 | adapter = AppAdapter(this)
141 | binding.recyclerView.adapter = adapter
142 | binding.recyclerView.layoutManager = LinearLayoutManager(this)
143 | }
144 |
145 | private fun loadApps() {
146 | val pm = packageManager
147 | val packages = pm.getInstalledPackages(PackageManager.GET_META_DATA)
148 |
149 | appList.clear()
150 | for (packageInfo in packages) {
151 | val appInfo = AppInfo(
152 | packageName = packageInfo.packageName,
153 | appName = packageInfo.applicationInfo.loadLabel(pm).toString(),
154 | icon = packageInfo.applicationInfo.loadIcon(pm)
155 | )
156 | appList.add(appInfo)
157 | }
158 |
159 | appList.sortBy { it.appName.lowercase() }
160 | adapter.updateList(appList)
161 | }
162 |
163 | private fun checkShizukuStatus() {
164 | try {
165 | // 首先检查 Binder 是否已经准备好
166 | if (!Shizuku.pingBinder()) {
167 | binding.shizukuStatus.text = "等待 Shizuku 服务..."
168 | binding.shizukuStatus.setTextColor(Color.YELLOW)
169 |
170 | // 尝试请求 Binder
171 | if (Shizuku.shouldShowRequestPermissionRationale()) {
172 | binding.shizukuStatus.text = "请授权 Shizuku 权限"
173 | requestShizukuPermission()
174 | } else {
175 | // 如果 Shizuku 服务未运行,尝试启动它
176 | try {
177 | val intent = Intent("rikka.shizuku.intent.action.REQUEST_BINDER")
178 | .setPackage("moe.shizuku.privileged.api")
179 | startActivity(intent)
180 | } catch (e: Exception) {
181 | binding.shizukuStatus.text = "Shizuku 未安装或未运行"
182 | binding.shizukuStatus.setTextColor(Color.RED)
183 | Log.e("MainActivity", "无法启动 Shizuku", e)
184 | }
185 | }
186 | return
187 | }
188 |
189 | // Binder 已准备好,检查权限
190 | when {
191 | Shizuku.isPreV11() -> {
192 | binding.shizukuStatus.text = "Shizuku 版本过低"
193 | binding.shizukuStatus.setTextColor(Color.RED)
194 | }
195 | Shizuku.checkSelfPermission() == PackageManager.PERMISSION_GRANTED -> {
196 | binding.shizukuStatus.text = "Shizuku 已授权"
197 | binding.shizukuStatus.setTextColor(Color.GREEN)
198 | }
199 | else -> {
200 | binding.shizukuStatus.text = "Shizuku 未授权"
201 | binding.shizukuStatus.setTextColor(Color.YELLOW)
202 | requestShizukuPermission()
203 | }
204 | }
205 | } catch (e: Exception) {
206 | binding.shizukuStatus.text = "Shizuku 状态检查失败: ${e.message}"
207 | binding.shizukuStatus.setTextColor(Color.RED)
208 | Log.e("MainActivity", "Shizuku 检查失败", e)
209 | }
210 | }
211 |
212 | private fun requestShizukuPermission() {
213 | try {
214 | Shizuku.requestPermission(SHIZUKU_CODE)
215 | } catch (e: Exception) {
216 | Toast.makeText(this, "请求 Shizuku 权限失败: ${e.message}", Toast.LENGTH_SHORT).show()
217 | }
218 | }
219 |
220 | private fun checkShizukuPermission() {
221 | try {
222 | if (Shizuku.isPreV11()) {
223 | binding.shizukuStatus.text = "Shizuku 版本过低"
224 | binding.shizukuStatus.setTextColor(Color.RED)
225 | return
226 | }
227 |
228 | when {
229 | Shizuku.checkSelfPermission() == PackageManager.PERMISSION_GRANTED -> {
230 | binding.shizukuStatus.text = "Shizuku 已授权"
231 | binding.shizukuStatus.setTextColor(Color.GREEN)
232 | }
233 | Shizuku.shouldShowRequestPermissionRationale() -> {
234 | binding.shizukuStatus.text = "Shizuku 权限被永久拒绝"
235 | binding.shizukuStatus.setTextColor(Color.RED)
236 | }
237 | else -> {
238 | binding.shizukuStatus.text = "请求 Shizuku 权限..."
239 | binding.shizukuStatus.setTextColor(Color.YELLOW)
240 | Shizuku.requestPermission(SHIZUKU_CODE)
241 | }
242 | }
243 | } catch (e: Exception) {
244 | binding.shizukuStatus.text = "Shizuku 未运行"
245 | binding.shizukuStatus.setTextColor(Color.RED)
246 | Log.e("MainActivity", "Shizuku 检查失败", e)
247 | }
248 | }
249 |
250 | override fun onRequestPermissionsResult(
251 | requestCode: Int,
252 | permissions: Array,
253 | grantResults: IntArray
254 | ) {
255 | when (requestCode) {
256 | SHIZUKU_CODE -> {
257 | if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
258 | binding.shizukuStatus.text = "Shizuku 已授权"
259 | binding.shizukuStatus.setTextColor(Color.GREEN)
260 | }
261 | }
262 | else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
263 | }
264 | }
265 |
266 | override fun onDestroy() {
267 | super.onDestroy()
268 | unregisterReceiver(binderReceiver)
269 | // 移除所有监听器
270 | Shizuku.removeBinderReceivedListener(binderReceivedListener)
271 | Shizuku.removeBinderDeadListener(binderDeadListener)
272 | Shizuku.removeRequestPermissionResultListener(permissionResultListener)
273 | // 解绑服务
274 | if (::serviceArgs.isInitialized) {
275 | try {
276 | Shizuku.unbindUserService(serviceArgs, serviceConnection, true)
277 | } catch (e: Exception) {
278 | Log.e("MainActivity", "解绑服务失败", e)
279 | }
280 | }
281 | }
282 |
283 | override fun onResume() {
284 | super.onResume()
285 | // 页面恢复时延迟检查状态,给 Binder 一些准备时间
286 | binding.root.postDelayed({
287 | checkShizukuStatus()
288 | }, 500)
289 | }
290 |
291 | override fun onCreateOptionsMenu(menu: Menu): Boolean {
292 | menuInflater.inflate(R.menu.main_menu, menu)
293 | return true
294 | }
295 |
296 | override fun onOptionsItemSelected(item: MenuItem): Boolean {
297 | return when (item.itemId) {
298 | R.id.action_refresh -> {
299 | checkShizukuStatus()
300 | true
301 | }
302 | R.id.action_disable_vibrate -> {
303 | setDouyinPermission("VIBRATE", false)
304 | true
305 | }
306 | R.id.action_enable_vibrate -> {
307 | setDouyinPermission("VIBRATE", true)
308 | true
309 | }
310 | R.id.action_disable_mic -> {
311 | setDouyinPermission("RECORD_AUDIO", false)
312 | true
313 | }
314 | R.id.action_enable_mic -> {
315 | setDouyinPermission("RECORD_AUDIO", true)
316 | true
317 | }
318 | else -> super.onOptionsItemSelected(item)
319 | }
320 | }
321 |
322 | private fun setDouyinPermission(permission: String, enabled: Boolean) {
323 | if (Shizuku.checkSelfPermission() != PackageManager.PERMISSION_GRANTED) {
324 | Toast.makeText(this, "请先授予 Shizuku 权限", Toast.LENGTH_SHORT).show()
325 | return
326 | }
327 |
328 | // 绑定服务
329 | serviceArgs = Shizuku.UserServiceArgs(ComponentName(packageName, AppOpsService::class.java.name))
330 | .daemon(false)
331 | .processNameSuffix("appops")
332 | .version(1)
333 |
334 | try {
335 | Shizuku.bindUserService(serviceArgs, serviceConnection)
336 |
337 | // 延迟执行,确保服务已连接
338 | Handler(Looper.getMainLooper()).postDelayed({
339 | try {
340 | // 查找抖音包名
341 | val douyin = appList.find { it.appName.contains("抖音") }
342 | if (douyin != null) {
343 | appOpsService?.setAppOps(douyin.packageName, permission, enabled)
344 | val status = if (enabled) "启用" else "禁用"
345 | val permissionName = when(permission) {
346 | "VIBRATE" -> "震动"
347 | "RECORD_AUDIO" -> "麦克风"
348 | else -> permission
349 | }
350 | Toast.makeText(this, "${status}抖音的${permissionName}权限成功", Toast.LENGTH_SHORT).show()
351 | } else {
352 | Toast.makeText(this, "未找到抖音应用", Toast.LENGTH_SHORT).show()
353 | }
354 | } catch (e: Exception) {
355 | Toast.makeText(this, "执行失败: ${e.message}", Toast.LENGTH_SHORT).show()
356 | Log.e("MainActivity", "执行失败", e)
357 | }
358 | }, 1000)
359 |
360 | } catch (e: Exception) {
361 | Toast.makeText(this, "服务启动失败: ${e.message}", Toast.LENGTH_SHORT).show()
362 | Log.e("MainActivity", "服务启动失败", e)
363 | }
364 | }
365 |
366 | private fun onAppClick(appInfo: ApplicationInfo) {
367 | val intent = Intent(this, AppDetailActivity::class.java).apply {
368 | putExtra(AppDetailActivity.EXTRA_PACKAGE_NAME, appInfo.packageName)
369 | }
370 | startActivity(intent)
371 | }
372 | }
373 |
374 |
375 |
376 |
--------------------------------------------------------------------------------
/.idea/other.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 |
--------------------------------------------------------------------------------