-
-
[求助]BinderProxy方案实现PackageManagerService的包过滤
-
发表于:
2014-11-3 23:23
7804
-
[求助]BinderProxy方案实现PackageManagerService的包过滤
通过BinderProxy的技术方案,目标是当客户端执行getInstalledPackages时,在Binder的onTransact处实现过滤。
首先客户端代码是这样的:
public List<PackageInfo> getInstalledPackages(int flags) {
try {
final List<PackageInfo> packageInfos = new ArrayList<PackageInfo>();
PackageInfo lastItem = null;
ParceledListSlice<PackageInfo> slice;
do {
final String lastKey = lastItem != null ? lastItem.packageName : null;
slice = mPM.getInstalledPackages(flags, lastKey);
lastItem = slice.populateList(packageInfos, PackageInfo.CREATOR);
} while (!slice.isLastSlice());
return packageInfos;
} catch (RemoteException e) {
throw new RuntimeException("Package manager has died", e);
}
}
从此可以看出,getInstalledPackages这个方法是递归式的通过Binder通讯获取内容的。
我的处理代码是这样的:
protected boolean onTransact(int code, Parcel data, Parcel reply,
int flags) throws RemoteException {
boolean bk = false;
if (code == s_getInstalledPackages_code) {
//解析得到传入的包
data.setDataPosition(0);
int _arg0 = data.readInt();
java.lang.String _arg1;
_arg1 = data.readString();
int _arg2 = data.readInt();
java.lang.String _arg3;
_arg3 = data.readString();
bk = mBinder.transact(code, data, reply, flags);
//得到的返回包做处理
//构造一个空的包数据
ParceledListSlice<PackageInfo> packs = new ParceledListSlice<PackageInfo>();
PackageInfo pinfo = new PackageInfo();
pinfo.packageName = _arg3;
pinfo.versionCode = 0;
pinfo.versionName = "";
pinfo.sharedUserId = "";
pinfo.sharedUserLabel = 0;
pinfo.firstInstallTime = 0;
pinfo.lastUpdateTime = 0;
packs.append(pinfo);
packs.setLastSlice(true);
//问题就在这里
//??如何对reply进行清空重新填充呢?如果用
//1、reply.setDataCapacity(0);
//2、reply.setDataSize(0);
//将空包填充到reply
reply.writeNoException();
packs.writeToParcel(reply,Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
}
}
有没有做过这方面的朋友,给个解决方案。我目前用reply.setDataSize(0);处理过了。出现的问题就是,一直循环不停。packs.setLastSlice(true);这个方法都无法停止他的循环。估计是包清空处理的时候有问题。
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课