android13(T) 三方APP默认授权

这篇具有很好参考价值的文章主要介绍了android13(T) 三方APP默认授权。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

按照以往经验修改给三方apk权限全开,发现13上坑不少。PermissionController 仅仅只是一个普通app,就和在AS中直接编写一样,完全用不了一些hide api,尝试修改了几次 android.bp 文件没有任何效果,引入核心api编译依旧报错。那只好曲线救国了,最终通过aidl方式搞定。

修改清单

 frameworks/base/packages/PackageInstaller/Android.bp                                                    
 frameworks/base/packages/PackageInstaller/AndroidManifest.xml                                           
 frameworks/base/packages/PackageInstaller/src/com/android/packageinstaller/IHEAppOpsManagerService.aidl 
 frameworks/base/packages/PackageInstaller/src/com/android/packageinstaller/HEAppOpsManagerService.java  
 frameworks/base/services/core/java/com/android/server/pm/permission/PermissionManagerServiceImpl.java   
 packages/modules/Permission/PermissionController/Android.bp                                             
 packages/modules/Permission/PermissionController/AndroidManifest.xml                                   
 packages/modules/Permission/PermissionController/src/com/android/packageinstaller/IHEAppOpsManagerService.aidl      
 packages/modules/Permission/PermissionController/src/com/android/permissioncontroller/PackageChangedService.java  
 packages/modules/Permission/PermissionController/src/com/android/permissioncontroller/PermissionGrantHelper.java 
 packages/modules/Permission/PermissionController/src/com/android/permissioncontroller/TemporaryFileManager.java    

frameworks/base/services/core/java/com/android/server/pm/permission/PermissionManagerServiceImpl.java

@@ -789,8 +789,9 @@ public class PermissionManagerServiceImpl implements PermissionManagerServiceInt
                "updatePermissionFlags");

        if ((flagMask & FLAG_PERMISSION_POLICY_FIXED) != 0 && !overridePolicy) {
-            throw new SecurityException("updatePermissionFlags requires "
-                    + Manifest.permission.ADJUST_RUNTIME_PERMISSIONS_POLICY);
+	    //cczheng annnotation for PermissionController app AppPermissionGroup.java:1280
+            /*throw new SecurityException("updatePermissionFlags requires "
+                    + Manifest.permission.ADJUST_RUNTIME_PERMISSIONS_POLICY);*/
        }

packages/modules/Permission/PermissionController/Android.bp

@@ -82,7 +82,8 @@ android_app {
    rename_resources_package: false,
    required: ["privapp_allowlist_com.android.permissioncontroller.xml"],

-    srcs: [":permissioncontroller-sources"],
+    srcs: [":permissioncontroller-sources",
+           "src/**/I*.aidl",],

    libs: [
        "android.car-stubs",

添加系统签名,为了在广播中直接启动服务

packages/modules/Permission/PermissionController/AndroidManifest.xml

@@ -6,7 +6,7 @@
   coreApp="true"
   android:versionCode="330000000"
   android:versionName="33 system image"
->
+  android:sharedUserId="android.uid.system">
 
     <original-package android:name="com.android.permissioncontroller" />

    <!--cczheng add S -->
     <receiver android:name="com.android.permissioncontroller.TemporaryFileManager"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <service android:name="com.android.permissioncontroller.PackageChangedService" 
      android:exported="false"/>
    <!--cczheng add E -->

packages/modules/Permission/PermissionController/src/com/android/packageinstaller/IHEAppOpsManagerService.aidl

package com.android.packageinstaller;
import android.content.pm.PackageInfo;

interface IHEAppOpsManagerService{

    boolean checkInstallPackagesPermission(String packageName, in PackageInfo mPackageInfo);
    
}

监听apk安装替换、卸载、重装广播直接赋权

packages/modules/Permission/PermissionController/src/com/android/permissioncontroller/PackageChangedService.java


package com.android.permissioncontroller;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.net.Uri;

import android.os.SystemProperties;
import android.content.pm.PackageManager;
import java.util.ArrayList;
import java.util.List;
import java.io.File;
import android.content.pm.PackageInfo;
import android.content.ServiceConnection;
import android.content.ComponentName;
import com.android.packageinstaller.IHEAppOpsManagerService;

public class PackageChangedService extends Service {

    private final String TAG = "permission";

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate OK");
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand OK");

        packageChangedBroadcastReceiver = new PackageChangedBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
        intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
        intentFilter.addDataScheme("package");
        registerReceiver(packageChangedBroadcastReceiver, intentFilter);


        Intent aidlIntent = new Intent("com.hisense.permissioncontroller.IHEAppOpsManagerService");
        aidlIntent.setPackage("com.android.packageinstaller");
        bindService(aidlIntent, appOpsManagerConn, Context.BIND_AUTO_CREATE);

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        try{
            unregisterReceiver(packageChangedBroadcastReceiver);
            unbindService(appOpsManagerConn);
        }catch(Exception e){
            e.printStackTrace();
        }
        super.onDestroy();
    }

    private IHEAppOpsManagerService mService = null;
    ServiceConnection appOpsManagerConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "onServiceConnected appOpsManagerConn");
            mService = IHEAppOpsManagerService.Stub.asInterface(service);
        }
    };

    private PackageChangedBroadcastReceiver packageChangedBroadcastReceiver;

    private class PackageChangedBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            try{
                String action = intent.getAction();
                String packageName = intent.getData().getSchemeSpecificPart();
                Log.e(TAG, "PackageChangedBroadcastReceiver action==" + action);
                Log.i(TAG, "PackageChangedBroadcastReceiver packageName==" + packageName);

                if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
                    PermissionGrantHelper.slientGrantRuntimePermission(mService, context, packageName);
                } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {

                } else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
                    Intent ccIntent = new Intent();
                    ccIntent.setAction("android.intent.action.MY_PACKAGE_REPLACED");
                    ccIntent.setData(Uri.parse("package:" + packageName));
                    ccIntent.addFlags(0x01000000);
                    context.sendBroadcast(ccIntent);
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

引用不到相关api已经注释,采用aidl调用

packages/modules/Permission/PermissionController/src/com/android/permissioncontroller/PermissionGrantHelper.java


package com.android.permissioncontroller;

import android.content.Context;
import android.util.Log;
import android.content.pm.PackageInfo;
// import android.content.pm.IPackageManager;
import com.android.packageinstaller.IHEAppOpsManagerService;
import android.content.pm.PackageManager;
import com.android.permissioncontroller.permission.model.AppPermissionGroup;
import com.android.permissioncontroller.permission.model.AppPermissions;
import com.android.permissioncontroller.permission.model.Permission;
import com.android.permissioncontroller.permission.utils.ArrayUtils;
import com.android.permissioncontroller.permission.utils.Utils;
import java.util.List;

// import android.app.AppGlobals;
import android.app.AppOpsManager;
import android.Manifest;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;

import static android.Manifest.permission.WRITE_SETTINGS;
import static android.Manifest.permission.SYSTEM_ALERT_WINDOW;

public class PermissionGrantHelper{

    // private static  IPackageManager mIpm;
    private static  AppOpsManager mAppOpsManager;
    private static final String TAG = "PermissionGrantHelper";

    public static void slientGrantRuntimePermission(IHEAppOpsManagerService mService, Context context, String packageName){
        PackageInfo packageInfo;

        try {
            packageInfo =  context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("permission", "can't get PackageInfo for packageName="+ packageName);
            return;
        }
        
       AppPermissions  mAppPermissions = new AppPermissions(context, packageInfo, false, true,
                new Runnable() {
                    @Override
                    public void run() {
                        
                    }
                });

       // mIpm = AppGlobals.getPackageManager();
       mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
       try{
           if (mService.checkInstallPackagesPermission(packageName, packageInfo)) {
                 Log.e(TAG, packageName + " need grant INSTALL_PACKAGES permission");
                 mAppOpsManager.setMode(AppOpsManager.OPSTR_REQUEST_INSTALL_PACKAGES/*OP_REQUEST_INSTALL_PACKAGES*/,
                    packageInfo.applicationInfo.uid, packageName, AppOpsManager.MODE_ALLOWED);
                 Log.e(TAG, "grant INSTALL_PACKAGES permission done");
           }
        }catch(Exception e){
           e.printStackTrace();
        }

       if (checkAppOpsPermission(packageInfo, WRITE_SETTINGS)) {
           Log.e(TAG, packageName + " need grant WRITE_SETTINGS permission");
           //frameworks\proto_logging\stats\enums\app\enums.proto
           mAppOpsManager.setMode(AppOpsManager.OPSTR_WRITE_SETTINGS/*AppOpsManager.OP_WRITE_SETTINGS*/,
                packageInfo.applicationInfo.uid, packageName, AppOpsManager.MODE_ALLOWED);
           Log.e(TAG, "grant WriteSetting permission done");
       }

       if (checkAppOpsPermission(packageInfo, SYSTEM_ALERT_WINDOW)) {
           Log.e(TAG, packageName + " need grant SYSTEM_ALERT_WINDOW permission");
           mAppOpsManager.setMode(AppOpsManager.OPSTR_SYSTEM_ALERT_WINDOW/*AppOpsManager.OP_SYSTEM_ALERT_WINDOW*/,
                packageInfo.applicationInfo.uid, packageName, AppOpsManager.MODE_ALLOWED);
           Log.e(TAG, "grant SYSTEM_ALERT_WINDOW permission done");
       }

       Log.e("permission", " AppPermissionGroup size=="+mAppPermissions.getPermissionGroups().size());
       if (mAppPermissions.getPermissionGroups().isEmpty()) {
            Log.e("permission", "mAppPermissions size isEmpty");
            return;
        }
        for (AppPermissionGroup group : mAppPermissions.getPermissionGroups()) {
            String[] permissionsToGrant = null;
            final int permissionCount = group.getPermissions().size();
            for (int j = 0; j < permissionCount; j++) {
                final Permission permission = group.getPermissions().get(j);
                if (!permission.isGranted()) {
                    permissionsToGrant = ArrayUtils.appendString(
                            permissionsToGrant, permission.getName());
                     Log.e("permission", "permissionName=" + permission.getName());
                }
            }
            if (permissionsToGrant != null) {
                group.grantRuntimePermissions(true, false, permissionsToGrant);
                Log.i("permission", "grantRuntimePermissions permissionsToGrant");
                //group.revokeRuntimePermissions(false);
            }
            //group.resetReviewRequired();
        }

        mAppPermissions.persistChanges(true);
    }
    //[E]

    private static boolean checkAppOpsPermission(PackageInfo mPackageInfo, String permission){
        for (int i = 0; i < mPackageInfo.requestedPermissions.length; i++) {
            if (mPackageInfo.requestedPermissions[i].equals(permission)) {
                return true;
            }
        }
        return false;
    }
}

开机广播启动服务

packages/modules/Permission/PermissionController/src/com/android/permissioncontroller/TemporaryFileManager.java


package com.android.permissioncontroller;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import java.util.List;

import java.io.File;
import java.io.IOException;


public class TemporaryFileManager extends BroadcastReceiver {
    private static final String LOG_TAG = TemporaryFileManager.class.getSimpleName();

    @Override
    public void onReceive(final Context context, Intent intent) {
        Log.e("permission", "action==="+ intent.getAction());
            
        context.startService(new Intent(context, PackageChangedService.class));
  
    }
}

aidl服务端代码

frameworks/base/packages/PackageInstaller/Android.bp

@@ -35,7 +35,10 @@ android_app {
     name: "PackageInstaller",
     defaults: ["platform_app_defaults"],
 
-    srcs: ["src/**/*.java"],
+    srcs: [
+           "src/**/*.java",
+           "src/**/I*.aidl",
+    ],
 
     certificate: "platform",
     privileged: true,

frameworks/base/packages/PackageInstaller/AndroidManifest.xml

	<service android:name="com.android.packageinstaller.HEAppOpsManagerService"   
	         android:enabled="true"
	         android:exported="true">
	        <intent-filter android:priority="1000">
	        <action android:name="com.hisense.permissioncontroller.IHEAppOpsManagerService"/>
	    </intent-filter>
	</service>

frameworks/base/packages/PackageInstaller/src/com/android/packageinstaller/IHEAppOpsManagerService.aidl

package com.android.packageinstaller;
import android.content.pm.PackageInfo;

interface IHEAppOpsManagerService{

    boolean checkInstallPackagesPermission(String packageName, in PackageInfo mPackageInfo);
    
}

frameworks/base/packages/PackageInstaller/src/com/android/packageinstaller/HEAppOpsManagerService.java文章来源地址https://www.toymoban.com/news/detail-435787.html

package com.android.packageinstaller;

import android.app.Service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.provider.Settings;
import android.util.Log;
import android.content.ContentResolver;
import android.text.TextUtils;
import android.content.pm.IPackageManager;
import android.content.pm.PackageManager;
import android.app.AppGlobals;
import android.content.pm.PackageInfo;
import android.app.AppOpsManager;
import android.content.Context;
import android.Manifest;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import com.android.packageinstaller.IHEAppOpsManagerService;


public class HEAppOpsManagerService extends Service {
	private static final String TAG = "HEAppOpsManagerService";
	public static final boolean DEBUG = true;
	
	@Override
	public IBinder onBind(Intent arg0) {
		IBinder iBinder = new HEAppOpsManagerServiceIml().asBinder();
		if (iBinder == null) {
			if (DEBUG)
				Log.d(TAG, "iBinder null");
		} else {
			if (DEBUG)
				Log.d(TAG, "iBinder ok");
		}
		return iBinder;
	}

	public class HEAppOpsManagerServiceIml extends IHEAppOpsManagerService.Stub {
		private  IPackageManager mIpm;
		private  AppOpsManager mAppOpsManager;

		public HEAppOpsManagerServiceIml() {
			mIpm = AppGlobals.getPackageManager();
			mAppOpsManager = (AppOpsManager)getSystemService(Context.APP_OPS_SERVICE);
		}

		@Override
		public boolean checkInstallPackagesPermission(String packageName, PackageInfo mPackageInfo)
				throws RemoteException {
			synchronized (HEAppOpsManagerServiceIml.class) {
				boolean flag = checkInstallPermission(packageName, mPackageInfo);
				Log.d(TAG,"packageName="+packageName+" need grant "+flag);
				return flag;
			}
		}

		private boolean checkInstallPermission(String packageName, PackageInfo mPackageInfo){
	        int uid = mPackageInfo.applicationInfo.uid;
	        //boolean permissionGranted = hasPermission(Manifest.permission.REQUEST_INSTALL_PACKAGES, uid);
	        boolean permissionRequested = hasRequestedAppOpPermission(Manifest.permission.REQUEST_INSTALL_PACKAGES, packageName);
	        int appOpMode = getAppOpMode(AppOpsManager.OP_REQUEST_INSTALL_PACKAGES, uid, packageName);

	        return appOpMode != AppOpsManager.MODE_DEFAULT || permissionRequested;
	    }

	    private int getAppOpMode(int appOpCode, int uid, String packageName) {
	        return mAppOpsManager.checkOpNoThrow(appOpCode, uid, packageName);
	    }

	    private boolean hasRequestedAppOpPermission(String permission, String packageName) {
	        try {
	            String[] packages = mIpm.getAppOpPermissionPackages(permission);
	            return com.android.internal.util.ArrayUtils.contains(packages, packageName);
	        } catch (Exception exc) {
	            Log.e(TAG, "PackageManager dead. Cannot get permission info");
	            return false;
	        }
	    }

	};

}

到了这里,关于android13(T) 三方APP默认授权的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android 13.0 在系统app安装第三方app弹出 解析安装包出现问题 的解决方案

    在13.0的系统定制化开发中,对于系统内置app中用代码调用系统安装接口安装app时抛出Permission Denial: that is not exported from UID 1000的异常,查询资料这个异常发现通常是由于Uri权限导致的问题,这就需要看PMS在安装的时候,需要什么权限,然后根据相关的日志分析 相关的安装流程

    2024年02月10日
    浏览(50)
  • Android 13.0 系统多个播放器app时,设置默认播放器

     在13.0的系统产品开发中,对于在系统中有多个播放器的时候,这时候如果调用代码打开播放器,会出现多个播放器列表让用户 选择启动哪个播放器,所以产品开发需求需要设置默认播放器,当打开播放器的时候,就直接打开播放器就可以了,所以就需要 了解查询播放器列

    2024年02月08日
    浏览(66)
  • Android12.0首次开机默认授予app运行时权限(去掉运行时授权弹窗)第二种方法

      在12.0的系统产品开发中,在6.0以后对于权限的申请,都需要动态申请,所以会在系统首次启动后,在app的首次运行时,会弹出授权窗口,会让用户手动授予app运行时权限,在由于系统产品开发需要要求默认授予app运行时权限,不需要用户默认授予运行时弹窗,所以需要在首

    2024年02月09日
    浏览(61)
  • Android13 网络 Adb 默认开启

    通过代码分析发现Android13 上对 网络adb 进行了限制! Android13原生代码要求:必现连接上某个wifi,才能进行adb ,并且切换wifi或者关闭wifi都是会停止adb。 如果要做成Android11 或者之前一样,设备连接WiFi/有线网络后电脑可以直接进行adb连接,就要对系统代码进行适配修改。 之前

    2024年02月12日
    浏览(42)
  • android 12.0去掉usb授权提示框 默认给予权限

    在12.0的系统rom产品开发中,在进行iot开发过程中,在插入usb设备时会弹出usb授权提示框,也带来一些不便,这个需要默认授予USB权限,插拔usb都不弹出usb弹窗所以这要从usb授权相关管理页默认给与usb权限 在进行开发的过程中,发现在插入usb后,通过adb shell命令adb shell dumpsys

    2024年02月15日
    浏览(59)
  • android相机、相册相关(android13 适配)13又又又又又又搞事,让第三方难以生存,踩坑

    前几天公司开发的新项目要上线,结果...... 由于项目中有用户更换头像功能,应用市场经过检测android13无法更换头像,也没有崩溃也没有反应,所以App就被无情退回,相当无奈,那就查问题,手头有没有android13测试机,怎么办,怎么办,怎么办...那就想办法,还好vivo有云测

    2024年02月06日
    浏览(68)
  • 高通Android 12/13 默认应用程序授予权限

    1、一提到权限很多Android开发者都会想到 比如拨打电话 读取手机通讯录 定位 这些都是需要申请权限,Google Android 6.0之后(sdk 23) 需要app动态申请权限 或者权限组 2、我这里打个比方 比如需要在fm应用 默认打开mic权限  3、我们需要知道这个默认应用程序的包名 (例如 xxx.

    2024年02月01日
    浏览(52)
  • android 13.0 添加系统字体并且设置为默认字体

    在13.0系统定制化开发中,在产品定制中,有产品需求对于系统字体风格不太满意,所以想要更换系统的默认字体,对于系统字体的修改也是常有的功能,而系统默认也支持增加字体,所以就来添加楷体字体为系统字体,并替换为系统默认字体, 接下来就来分析下替换默认字

    2024年02月22日
    浏览(62)
  • uni-app 经验分享,从入门到离职(实战篇)——模拟从后台获取图片路径数据后授权相册以及保存图片到本地(手机相册)

    这篇文章是本专栏 uni-app 的项目实战篇,主要内容的是模拟前端通过调用接口,然后获取到数据图片的路径数据,然后授权相册,最后把图片保存到本地(相册)。 本专栏主要是分享和介绍从零到一学习和使用的 uni-app 的笔记和个人经验。通过个人的学习经验和工作经验来给

    2024年02月08日
    浏览(55)
  • [SM6225][Android13]user版本默认允许root和remount

    芯片:  高通SM6225 版本:  Android 13 kernel:  msm-5.15        刚刚从Framework踏入性能的小殿堂,User版本默认是不会开启root权限的,而且一般调试需要设置一下CPU GPU DDR performance模式或者修改一些schedule util等调核调频节点去对比复测,userdebug版本的话本身整机性能就比user卡很多,

    2024年02月09日
    浏览(53)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包