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

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;
          }
      }
  };
}


目录
相关文章
|
2月前
|
XML Java 数据库
安卓项目:app注册/登录界面设计
本文介绍了如何设计一个Android应用的注册/登录界面,包括布局文件的创建、登录和注册逻辑的实现,以及运行效果的展示。
197 0
安卓项目:app注册/登录界面设计
|
3月前
|
Java 数据库 Android开发
一个Android App最少有几个线程?实现多线程的方式有哪些?
本文介绍了Android多线程编程的重要性及其实现方法,涵盖了基本概念、常见线程类型(如主线程、工作线程)以及多种多线程实现方式(如`Thread`、`HandlerThread`、`Executors`、Kotlin协程等)。通过合理的多线程管理,可大幅提升应用性能和用户体验。
150 15
一个Android App最少有几个线程?实现多线程的方式有哪些?
|
3月前
|
存储 开发工具 Android开发
使用.NET MAUI开发第一个安卓APP
【9月更文挑战第24天】使用.NET MAUI开发首个安卓APP需完成以下步骤:首先,安装Visual Studio 2022并勾选“.NET Multi-platform App UI development”工作负载;接着,安装Android SDK。然后,创建新项目时选择“.NET Multi-platform App (MAUI)”模板,并仅针对Android平台进行配置。了解项目结构,包括`.csproj`配置文件、`Properties`配置文件夹、平台特定代码及共享代码等。
271 2
|
3月前
|
XML Android开发 数据格式
🌐Android国际化与本地化全攻略!让你的App走遍全球无障碍!🌍
在全球化背景下,实现Android应用的国际化与本地化至关重要。本文以一款旅游指南App为例,详细介绍如何通过资源文件拆分与命名、适配布局与方向、处理日期时间及货币格式、考虑文化习俗等步骤,完成多语言支持和本地化调整。通过邀请用户测试并收集反馈,确保应用能无缝融入不同市场,提升用户体验与满意度。
123 3
|
3月前
|
Java 数据库 Android开发
一个Android App最少有几个线程?实现多线程的方式有哪些?
本文介绍了Android应用开发中的多线程编程,涵盖基本概念、常见实现方式及最佳实践。主要内容包括主线程与工作线程的作用、多线程的多种实现方法(如 `Thread`、`HandlerThread`、`Executors` 和 Kotlin 协程),以及如何避免内存泄漏和合理使用线程池。通过有效的多线程管理,可以显著提升应用性能和用户体验。
103 10
|
2月前
|
安全 网络安全 Android开发
深度解析:利用Universal Links与Android App Links实现无缝网页至应用跳转的安全考量
【10月更文挑战第2天】在移动互联网时代,用户经常需要从网页无缝跳转到移动应用中。这种跳转不仅需要提供流畅的用户体验,还要确保安全性。本文将深入探讨如何利用Universal Links(仅限于iOS)和Android App Links技术实现这一目标,并分析其安全性。
374 0
|
3月前
|
XML 数据库 Android开发
10分钟手把手教你用Android手撸一个简易的个人记账App
该文章提供了使用Android Studio从零开始创建一个简单的个人记账应用的详细步骤,包括项目搭建、界面设计、数据库处理及各功能模块的实现方法。
|
4月前
|
API Android开发
Android P 性能优化:创建APP进程白名单,杀死白名单之外的进程
本文介绍了在Android P系统中通过创建应用进程白名单并杀死白名单之外的进程来优化性能的方法,包括设置权限、获取运行中的APP列表、配置白名单以及在应用启动时杀死非白名单进程的代码实现。
74 1
|
4月前
|
IDE Java 开发工具
探索安卓开发之旅:打造你的第一款App
【8月更文挑战第24天】在这篇文章中,我们将一起踏上激动人心的安卓开发之旅。不论你是编程新手还是希望扩展技能的老手,本文将为你提供一份详尽指南,帮助你理解安卓开发的基础知识并实现你的第一个应用程序。从搭建开发环境到编写“Hello World”,每一步都将用浅显易懂的语言进行解释。那么,让我们开始吧!
|
4月前
|
XML Android开发 UED
"掌握安卓开发新境界:深度解析AndroidManifest.xml中的Intent-filter配置,让你的App轻松响应scheme_url,开启无限交互可能!"
【8月更文挑战第2天】在安卓开发中,scheme_url 通过在`AndroidManifest.xml`中配置`Intent-filter`,使应用能响应特定URL启动或执行操作。基本配置下,应用可通过定义特定URL模式的`Intent-filter`响应相应链接。
121 12
下一篇
DataWorks