mtk android 9.0 给内置APK默认全部权限

内置客户APK后,首次开机有多次权限授权提示,非常麻烦,此方法会默认给予APK 申请的权限
packages\apps\Provision\src\com\android\provision\DefaultActivity.java

/*

Copyright © 2008 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.android.provision;

import android.app.Activity;
import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.provider.Settings;

//导入包 start
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageDeleteObserver;
import android.content.pm.IPackageInstallObserver2;
import android.content.pm.IPackageManager;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.PermissionInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.UserInfo;
import com.mediatek.cta.CtaManager;
import com.mediatek.cta.CtaManagerFactory;
import android.app.AppOpsManager;
import java.util.List;
import android.app.AppOpsManager.PackageOps;
import android.app.AppOpsManager.OpEntry;
import android.os.Process;

import android.content.Intent;
import java.util.ArrayList;
import android.content.IntentFilter;
//导入包 end
/**
Application that sets the provisioned bit, like SetupWizard does.
*/
public class DefaultActivity extends Activity {

@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
 // Add a persistent setting to allow other apps to know the device has been provisioned.
 Settings.Global.putInt(getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 1);
 Settings.Secure.putInt(getContentResolver(), Settings.Secure.USER_SETUP_COMPLETE, 1);

 // remove this activity from the package manager.
 PackageManager pm = getPackageManager();
 ComponentName name = new ComponentName(this, DefaultActivity.class);
 pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
         PackageManager.DONT_KILL_APP);

 // terminate the activity.
// setHomeDefault("com.wyt.drpan","com.android.launcher3.Launcher");
 setAppPermission("com.wyt.drpan");  // 写入需要权限的APK 包名

 finish();
}

// 给apk 全部需要的权限 START
public void setAppPermission(String pkgName) {
PackageManager pm = getApplicationContext().getPackageManager();
AppOpsManager mAppOps = getApplicationContext().getSystemService(AppOpsManager.class);
try {
PackageInfo pkgInfo = pm.getPackageInfo(pkgName, PackageManager.GET_PERMISSIONS);
String sharedPkgList[] = pkgInfo.requestedPermissions;
for (int i = 0; i < sharedPkgList.length; i++) {
String permName = sharedPkgList[i];
PermissionInfo permissionInfo;
try {
permissionInfo = pm.getPermissionInfo(permName, 0);
} catch (PackageManager.NameNotFoundException e) {
continue;
}
final int opCode = AppOpsManager.permissionToOpCode(permName);
boolean hasAopsCode = opCode > AppOpsManager.OP_NONE && opCode < AppOpsManager._NUM_OP;
String aopStr = AppOpsManager.permissionToOp(permName);
boolean hasAopsOp = aopStr !=null;
boolean onlyAops = false;
final boolean granted = (pkgInfo.requestedPermissionsFlags[i] & PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0;
if ((permissionInfo.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE) != PermissionInfo.PROTECTION_DANGEROUS
|| (permissionInfo.flags & PermissionInfo.FLAG_INSTALLED) == 0 || (permissionInfo.flags & PermissionInfo.FLAG_REMOVED) != 0) {
if (permName.startsWith(“android.”) && (hasAopsCode || hasAopsOp)) {
onlyAops = true;
android.util.Log.d(“DHYCO”, “permissionName=” + permName +
“,opCode=” + opCode);
} else
continue;
}
boolean isAopsAllowed = mAppOps.checkOpNoThrow(opCode, pkgInfo.applicationInfo.uid, pkgInfo.packageName) == AppOpsManager.MODE_ALLOWED;
if ((hasAopsCode || onlyAops) && !isAopsAllowed)
mAppOps.setMode(opCode, pkgInfo.applicationInfo.uid, pkgInfo.packageName, AppOpsManager.MODE_ALLOWED);
if ((hasAopsOp || onlyAops) && !isAopsAllowed)
mAppOps.setUidMode(aopStr, pkgInfo.applicationInfo.uid, AppOpsManager.MODE_ALLOWED);
if (!granted && !onlyAops) {
pm.grantRuntimePermission(pkgName, permName, Process.myUserHandle());
         }
         // set flags
         pm.updatePermissionFlags(permName, pkgName, PackageManager.FLAG_PERMISSION_USER_FIXED | PackageManager.FLAG_PERMISSION_USER_SET
                     | PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED, 0, Process.myUserHandle());
     }
     List<AppOpsManager.PackageOps> ops = mAppOps.getOpsForPackage(pkgInfo.applicationInfo.uid, pkgName, null);
     if(ops == null ||ops.size() == 0)
         return;
     for (PackageOps packageOps : ops) {
         List<OpEntry> entries = packageOps.getOps();
         if(entries == null || entries.size() == 0)
             continue;
         for (OpEntry opEntry : entries) {
             mAppOps.setMode(opEntry.getOp(), packageOps.getUid(), pkgName, AppOpsManager.MODE_ALLOWED); 
         }
     }
 } catch (Exception e) {
     android.util.Log.e("DHYCO", "++setAppPermission exception ,the packageName is " + pkgName, e);
 }

// 给apk 全部需要的权限 end

public void setHomeDefault(String defaultlauncherpckname,String defaultlauncherclsname) {
final PackageManager mPm = getApplicationContext().getPackageManager();
// 判断指定的launcher是否存在
if (hasApkInstalled(defaultlauncherpckname)) {
     // 清除当前默认launcher
     ArrayList<IntentFilter> intentList = new ArrayList<IntentFilter>();
     ArrayList<ComponentName> cnList = new ArrayList<ComponentName>();
     mPm.getPreferredActivities(intentList, cnList, null);
     IntentFilter dhIF = null;
     for (int i = 0; i < cnList.size(); i++) {
         dhIF = intentList.get(i);
         if (dhIF.hasAction(Intent.ACTION_MAIN) && dhIF.hasCategory(Intent.CATEGORY_HOME)) {
             mPm.clearPackagePreferredActivities(cnList.get(i).getPackageName());
         }
     }

     // 获取所有launcher activity
     Intent intent = new Intent(Intent.ACTION_MAIN);
     intent.addCategory(Intent.CATEGORY_HOME);
     List<ResolveInfo> list = new ArrayList<ResolveInfo>();
     list = mPm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
     // get all components and the best match
     IntentFilter filter = new IntentFilter();
     filter.addAction(Intent.ACTION_MAIN);
     filter.addCategory(Intent.CATEGORY_HOME);
     filter.addCategory(Intent.CATEGORY_DEFAULT);
     final int N = list.size();

     // 设置默认launcher
     ComponentName launcher = new ComponentName(defaultlauncherpckname,         
     defaultlauncherclsname);

     ComponentName[] set = new ComponentName[N];
     int defaultMatch = 0;
     for (int i = 0; i < N; i++) {
         ResolveInfo r = list.get(i);
         set[i] = new ComponentName(r.activityInfo.packageName, r.activityInfo.name);
         if (launcher.getClassName().equals(r.activityInfo.name)) {
             defaultMatch = r.match;
         }
     }
     mPm.addPreferredActivity(filter, defaultMatch, set, launcher);
 }
}

private boolean hasApkInstalled(String pkgname) {
try {
getApplicationContext().getPackageManager().getPackageInfo(pkgname, 0);
} catch (Exception e) {
return false;
}
return true;
}

private void setDefaultBrowser(){
PackageManager pm = getApplicationContext().getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(“android.intent.category.BROWSABLE”);
List resolveInfoList = pm.queryIntentActivities(intent,0);
int size = resolveInfoList.size();
ComponentName[] arrayOfComponentName = new ComponentName[size];
for (int i = 0; i < size; i++) {
ActivityInfo activityInfo = resolveInfoList.get(i).activityInfo;
String packageName = activityInfo.packageName;
String className = activityInfo.name;

     ComponentName componentName = new ComponentName(packageName,className);
     arrayOfComponentName[i] = componentName;

     if (packageName.equals("com.hat.browser") && className.equals("com.hat.browser.ui.BrowserActivity")) {  
         pm.clearPackagePreferredActivities(packageName);
         pm.setDefaultBrowserPackageNameAsUser(packageName,0);
     }
   }

  }
}

AndroidManifest.xml
<manifest xmlns:android=“http://schemas.android.com/apk/res/android”

android:sharedUserId="android.uid.system"
    package="com.android.provision">

<original-package android:name="com.android.provision" />

<!-- For miscellaneous settings -->
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

<application>
    <activity android:name="DefaultActivity"
            android:excludeFromRecents="true">
        <intent-filter android:priority="1">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.SETUP_WIZARD" />
        </intent-filter>
    </activity>
</application>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值