PackageManagerService相关类结构
分析
从上图可以看出若想为PackageManagerService添加方法需要进行以下改动
1,在IPackageManager.aidl文件中增加方法的声明;
2,在PackageManager类中增加抽象方法说明;
3,在ApplicationPackageManager中增加方法,具体实现为调用远程PackageManagerService的方法;
4,在PackageManagerService中增加方法的具体实现;
注:由于MockPackageManager类也继承了PackageManager类,故也需要加入一些实现的代码;增加的方法要设置为hide,或者更改API-List加入新增加的方法
Demo
目标:在PackageManagerService加入方法public String getHello();
第一步:在IPackageManager.aidl增加getHello()方法声明
interface IPackageManager { PackageInfo getPackageInfo(String packageName, int flags); int getPackageUid(String packageName); int[] getPackageGids(String packageName); String[] currentToCanonicalPackageNames(in String[] names); String[] canonicalToCurrentPackageNames(in String[] names); PermissionInfo getPermissionInfo(String name, int flags); List<PermissionInfo> queryPermissionsByGroup(String group, int flags); PermissionGroupInfo getPermissionGroupInfo(String name, int flags); List<PermissionGroupInfo> getAllPermissionGroups(int flags); ApplicationInfo getApplicationInfo(String packageName, int flags); ActivityInfo getActivityInfo(in ComponentName className, int flags); ActivityInfo getReceiverInfo(in ComponentName className, int flags); . . . . . . String getHello();}
public abstract class PackageManager { /** * This exception is thrown when a given package, application, or component * name can not be found. */ public static class NameNotFoundException extends AndroidException { public NameNotFoundException() { } public NameNotFoundException(String name) { super(name); } } . . . . . . /** * @hide */ public abstract String getHello();}
第三步:在PackageManagerService中增加getHello()方法的具体实现
public class PackageManagerService extends IPackageManager.Stub { static final String TAG = "PackageManager"; static final boolean DEBUG_SETTINGS = false; private static final boolean DEBUG_PREFERRED = false; static final boolean DEBUG_UPGRADE = false; private static final boolean DEBUG_INSTALL = false; private static final boolean DEBUG_REMOVE = false; private static final boolean DEBUG_SHOW_INFO = false; private static final boolean DEBUG_PACKAGE_INFO = false; . . . . . . . public String getHello(){ return "Hello,PMS"; }}
final class ApplicationPackageManager extends PackageManager { private static final String TAG = "ApplicationPackageManager"; private final static boolean DEBUG = false; private final static boolean DEBUG_ICONS = false; @Override public PackageInfo getPackageInfo(String packageName, int flags) throws NameNotFoundException { try { PackageInfo pi = mPM.getPackageInfo(packageName, flags); if (pi != null) { return pi; } } catch (RemoteException e) { throw new RuntimeException("Package manager has died", e); } throw new NameNotFoundException(packageName); } @Override public String[] currentToCanonicalPackageNames(String[] names) { try { return mPM.currentToCanonicalPackageNames(names); } catch (RemoteException e) { throw new RuntimeException("Package manager has died", e); } } . . . . . . . /** * @hide */ @Override public String getHello() { // TODO Auto-generated method stub try { return mPM.isThirdParty(name); } catch (RemoteException e) { throw new RuntimeException("Package manager has died", e); } }}