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());
}
Listops = mAppOps.getOpsForPackage(pkgInfo.applicationInfo.uid, pkgName, null);
if(ops == null ||ops.size() == 0)
return;
for (PackageOps packageOps : ops) {
Listentries = 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
ArrayListintentList = new ArrayList();
ArrayListcnList = new ArrayList();
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);
Listlist = new ArrayList();
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);
}
}
}
}