android中如何禁掉组件或package——PackageManager使用

 

前几天接到一个任务,要给N802(公司的平板电脑产品)增加对无模组的支持,要求代码同时兼容有无模组的版本。通过SystemProperties中加入的字串board.has_modem识别。

同事告知PackageManager中有相应api,这样的话并不难完成任务。但是最后还是碰到一些疑问的地方,现在记下来,算作笔记。

package manager service是一个java的服务(service),通过aidl定义接口(frameworks/base/core/java/android/content/pm/IPackageManager.aidl)

客户端通过PackageManager(frameworks/base/core/java/android/content/pm/PackageManager.java)调用如下接口:

int getComponentEnabledSetting(in ComponentName componentName);

int getApplicationEnabledSetting(in String packageName);

void setComponentEnabledSetting(in ComponentName componentName,

            in int newState, in int flags);

void setApplicationEnabledSetting(in String packageName, in int newState, int flags);

获取到服务端PackageManagerService(frameworks/base/services/java/com/android/server/PackageManagerService.java)的服务。

 

讲下我的思路,创建一个BroadcastReceiver,接收Intent.ACTION_BOOT_COMPLETED这个动作,调用SystemProperties.get("board.has_modem"),判断模组存在情况,决定是否执行禁止组件的操作。

这里有几点要注意:

1、比如,我禁掉“com.android.phone”这个包,这个包里面的所有组件的enable属性将写进userdata里(包括上面的BroadcastReceiver),也就是说第二次重启之后,将收不到Intent.ACTION_BOOT_COMPLETED这个动作。除非你清除用户数据,这样重新解包。

2、在AndroidManifest里面,有很多组件有别名,禁止掉别名,并不能禁止本来的这个组件。同样的,禁止掉本来的这个组件,别名同样有效!

3、不能在包里面禁掉另外一个包的组建,因为这是没有权限的。查看PackageManagerService.java你会发现,它会检查调用者的user id,如果跟包的user id不匹配则抛出异常。

 

 

如下代码取自PackageManagerService.java:

public void setComponentEnabledSetting(ComponentName componentName, int newState, int flags) { setEnabledSetting(componentName.getPackageName(), componentName.getClassName(), newState, flags); }

 

public void setApplicationEnabledSetting(String appPackageName, int newState, int flags) { setEnabledSetting(appPackageName, null, newState, flags); }

 

 

 

 

private void setEnabledSetting( final String packageName, String className, int newState, final int flags) { if (!(newState == COMPONENT_ENABLED_STATE_DEFAULT || newState == COMPONENT_ENABLED_STATE_ENABLED || newState == COMPONENT_ENABLED_STATE_DISABLED)) { throw new IllegalArgumentException("Invalid new component state: " + newState); } PackageSetting pkgSetting; final int uid = Binder.getCallingUid(); final int permission = mContext.checkCallingPermission( android.Manifest.permission.CHANGE_COMPONENT_ENABLED_STATE); final boolean allowedByPermission = (permission == PackageManager.PERMISSION_GRANTED); boolean sendNow = false; boolean isApp = (className == null); String componentName = isApp ? packageName : className; int packageUid = -1; ArrayList<String> components; synchronized (mPackages) { pkgSetting = mSettings.mPackages.get(packageName); if (pkgSetting == null) { if (className == null) { throw new IllegalArgumentException( "Unknown package: " + packageName); } throw new IllegalArgumentException( "Unknown component: " + packageName + "/" + className); } if (!allowedByPermission && (uid != pkgSetting.userId)) { throw new SecurityException( "Permission Denial: attempt to change component state from pid=" + Binder.getCallingPid() + ", uid=" + uid + ", package uid=" + pkgSetting.userId); } .... } 

 

你可能感兴趣的:(android,String,manager,service,null,Components)