Giter Club home page Giter Club logo

delegated-scopes-manager's Introduction

Delegated-Scopes-Manager

这是什么?

Android 系统中,设备管理员可以在免 root 状态下提供相当广泛的权限。但是系统限制一台设备上仅能设置一个 App 为设备管理员。为了让一台设备上的诸多 App 能共享权限,此项目应运而生。

它能提供什么?

首先它基于系统提供的 DevicePolicyManager.DELEGATION 机制,能提供官方提供的所有权限,包括并不限于:

DELEGATION_APP_RESTRICTIONS

DELEGATION_BLOCK_UNINSTALL

DELEGATION_CERT_INSTALL

DELEGATION_ENABLE_SYSTEM_APP

......

然后它也提供各类自定义权限,目前有:

dsm-delegation-install-uninstall-app 安装卸载 App (API >= 23)

dsm-delegation-set-app-ops 设置 AppOps 状态(API >= 28)

更多权限和功能有待添加。

如何接入使用?

首先请您确认您已经知道设备管理员是什么,以及如何使用等信息。

我是设备管理员,想分享权限给其他 App

  1. 添加下列依赖:
implementation 'com.github.heruoxin.Delegated-Scopes-Manager:manager:master-SNAPSHOT'
  1. 在您的 DeviceAdminReceiver#onEnabled 方法中添加 CenterApp.getInstance(context).refreshState(); 以便刷新状态。

  2. 【推荐】在运行时检测,当 App 当前拥有设备管理员权限时,在适当位置添加一个入口,指向自身的权限授权管理页面,方便用户管理:

startActivity(new Intent(CenterApp.ACTION_APP_LIST).setPackage(context.getPackageName()));

我是普通 App,想使用设备管理员权限

  1. 添加依赖:
implementation 'com.github.heruoxin.Delegated-Scopes-Manager:client:master-SNAPSHOT'
  1. 在 manifests 文件中参考官方托管权限的方式进行注册:
        <receiver
            android:name="yourAdminReceiver"
            android:permission="android.permission.BIND_DEVICE_ADMIN">
            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
                <!-- 加入以下内容-->
                <action android:name="android.app.develop.action.DEVICE_DELEGATION" />
            </intent-filter>

            <meta-data
                android:name="android.app.develop.delegation"
                android:resource="@xml/app_delegation" />
        </receiver>

建立 app_delegation.xml 文件,注册你需要的权限:

<?xml version="1.0" encoding="utf-8"?>
<device-delegation>
    <uses-policies>
        <!-- 以官方 DevicePolicyManager.DELEGATION_APP_RESTRICTIONS 为例,需要此权限就直接添加其字符串常量 -->
        <delegation-app-restrictions />
        <!-- 上述的自定义权限也可以如此添加,SDK 自定义权限均以 dsm- 开头 -->
        <dsm-delegation-set-app-ops />
    </uses-policies>
</device-delegation>
  1. 在代码中,判断一下当前设备上有没有可用的已设置管理员的 App
DSMClient.getOwnerSDKVersion(context) >= DSMClient.SDK_VERSION // 判断当前是否有可用的 SDK

String packageName = DSMClient.getOwnerPackageName(context); // 可以获取到当前的设备管理员名称,用于展示给用户
  1. 检查并申请相关的托管设备管理员权限。
List<String> scopes = DSMClient.getDelegatedScopes(context); // 检查当前已得到哪些托管授权

// 申请权限
DSMClient.requestScopes(activity, DevicePolicyManager.DELEGATION_ENABLE_SYSTEM_APP, "dsm-delegation-install-uninstall-app", ...);
     
// 用户同意授予全部你申请的权限后, `Activity#onActivityResult` 会回调 `RESULT_OK`,否则回调 `RESULT_CANCEL`。
  1. 对于系统权限,直接调用 DevicePolicyManager 中的相关方法即可,对于自定义权限,请参考 DSMClient#installAppDSMClient#setAppOpsMode 等方法调用。

目前有哪些 App 支持?

目前作为管理员端支持的有冰箱 IceBox小黑屋。作为客户端支持的有权限狗App Ops

License

WTFPL

License 之外的承诺

如需要使用此项目,请尽量满足下列条件:

  1. 在其他开源项目中,遵守其 Lincense;
  2. 不以「不开源」为理由,对任何闭源,或开源转为闭源项目发起辱骂或对其作者施行人身攻击;

以上承诺不具备法律效力,但希望您能够遵守。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.