最近在项目过程中遇到这个一个问题:在ActivityManagerService里面的getContentProvider出现SecurityException异常,callingPackage和uid不一致
public final ContentProviderHolder getContentProvider(
IApplicationThread caller, String callingPackage, String name, int userId,
boolean stable) {
enforceNotIsolatedCaller("getContentProvider");
if (caller == null) {
String msg = "null IApplicationThread when getting content provider "
+ name;
Slog.w(TAG, msg);
throw new SecurityException(msg);
}
// The incoming user check is now handled in checkContentProviderPermissionLocked() to deal
// with cross-user grant.
final int callingUid = Binder.getCallingUid();
if (callingPackage != null && mAppOpsService.checkPackage(callingUid, callingPackage)
!= AppOpsManager.MODE_ALLOWED) {
throw new SecurityException("Given calling package " + callingPackage
+ " does not match caller's uid " + callingUid);
}
return getContentProviderImpl(caller, name, null, callingUid, callingPackage,
null, stable, userId);
}
发现是在传递的过程中,Context发生了变化,所以导致后面出现不一致的情况。所以想到用createPackageContext来创建一个Context来传递过来,方法定义如下:
@Override
public Context createPackageContext(String packageName, int flags)
throws NameNotFoundException {
return createPackageContextAsUser(packageName, flags, mUser);
}
@Override
public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
throws NameNotFoundException {
if (packageName.equals("system") || packageName.equals("android")) {
// The system resources are loaded in every application, so we can safely copy
// the context without reloading Resources.
return new ContextImpl(this, mMainThread, mPackageInfo, null, mActivityToken, user,
flags, null, null);
}
// 方法的核心是通过包名创建LoadedApk,然后加载了apk里面的资源
LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(),
flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier());
if (pi != null) {
ContextImpl c = new ContextImpl(this, mMainThread, pi, null, mActivityToken, user,
flags, null, null);
final int displayId = getDisplayId();
c.setResources(createResources(mActivityToken, pi, null, displayId, null,
getDisplayAdjustments(displayId).getCompatibilityInfo()));
if (c.mResources != null) {
return c;
}
}
// Should be a better exception.
throw new PackageManager.NameNotFoundException(
"Application package " + packageName + " not found");
}
通过查看方法的实现,创建context的核心是通过包名找到了LoadedApk对象,然后创建了一个ContextImpl对象,同时把相关的资源设置到这个context上,但是Context里面的一些值还是当前Context里面的,如
@UnsupportedAppUsage
private final String mBasePackageName;
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
private final String mOpPackageName;
在ContextImol里面,有些方法是通过LoadedApk mPackageInfo对象来实现的,使用这些方法的时候,是符合预期的;
@Override
public Context getApplicationContext() {
return (mPackageInfo != null) ?
mPackageInfo.getApplication() : mMainThread.getApplication();
}
@Override
public String getPackageName() {
if (mPackageInfo != null) {
return mPackageInfo.getPackageName();
}
// No mPackageInfo means this is a Context for the system itself,
// and this here is its name.
return "android";
}
有些方法不是通过LoadedApk对象来实现的,通过Context对象调用的返回值还是先前的Context,不符合预期
@Override
public Looper getMainLooper() {
return mMainThread.getLooper();
}
@Override
public Executor getMainExecutor() {
return mMainThread.getExecutor();
}
总结:通过Context A 的createPackageContext对象创建的Context B,只有通过LoadedApk
mPackageInfo对象实现的方法才是真正的调用Context B来实现,其它的还是通过Context A来实现的