android.content.pm.IPackageManager Java Examples

The following examples show how to use android.content.pm.IPackageManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BroadcastQueue.java    From AndroidComponentPlugin with Apache License 2.0 7 votes vote down vote up
/**
 * Return true if all given permissions are signature-only perms.
 */
final boolean isSignaturePerm(String[] perms) {
    if (perms == null) {
        return false;
    }
    IPackageManager pm = AppGlobals.getPackageManager();
    for (int i = perms.length-1; i >= 0; i--) {
        try {
            PermissionInfo pi = pm.getPermissionInfo(perms[i], "android", 0);
            if ((pi.protectionLevel & (PermissionInfo.PROTECTION_MASK_BASE
                    | PermissionInfo.PROTECTION_FLAG_PRIVILEGED))
                    != PermissionInfo.PROTECTION_SIGNATURE) {
                // If this a signature permission and NOT allowed for privileged apps, it
                // is okay...  otherwise, nope!
                return false;
            }
        } catch (RemoteException e) {
            return false;
        }
    }
    return true;
}
 
Example #2
Source File: SenderPackageFilter.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matches(IntentFirewall ifw, ComponentName resolvedComponent, Intent intent,
        int callerUid, int callerPid, String resolvedType, int receivingUid) {
    IPackageManager pm = AppGlobals.getPackageManager();

    int packageUid = -1;
    try {
        // USER_SYSTEM here is not important. Only app id is used and getPackageUid() will
        // return a uid whether the app is installed for a user or not.
        packageUid = pm.getPackageUid(mPackageName, PackageManager.MATCH_ANY_USER,
                UserHandle.USER_SYSTEM);
    } catch (RemoteException ex) {
        // handled below
    }

    if (packageUid == -1)  {
        return false;
    }

    return UserHandle.isSameApp(packageUid, callerUid);
}
 
Example #3
Source File: SenderFilter.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
static boolean isPrivilegedApp(int callerUid, int callerPid) {
    if (callerUid == Process.SYSTEM_UID || callerUid == 0 ||
            callerPid == Process.myPid() || callerPid == 0) {
        return true;
    }

    IPackageManager pm = AppGlobals.getPackageManager();
    try {
        return (pm.getPrivateFlagsForUid(callerUid) & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED)
                != 0;
    } catch (RemoteException ex) {
        Slog.e(IntentFirewall.TAG, "Remote exception while retrieving uid flags",
                ex);
    }

    return false;
}
 
Example #4
Source File: BroadcastQueue.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Return true if all given permissions are signature-only perms.
 */
final boolean isSignaturePerm(String[] perms) {
    if (perms == null) {
        return false;
    }
    IPackageManager pm = AppGlobals.getPackageManager();
    for (int i = perms.length-1; i >= 0; i--) {
        try {
            PermissionInfo pi = pm.getPermissionInfo(perms[i], "android", 0);
            if ((pi.protectionLevel & (PermissionInfo.PROTECTION_MASK_BASE
                    | PermissionInfo.PROTECTION_FLAG_PRIVILEGED))
                    != PermissionInfo.PROTECTION_SIGNATURE) {
                // If this a signature permission and NOT allowed for privileged apps, it
                // is okay...  otherwise, nope!
                return false;
            }
        } catch (RemoteException e) {
            return false;
        }
    }
    return true;
}
 
Example #5
Source File: JobSchedulerService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private void enforceValidJobRequest(int uid, JobInfo job) {
    final IPackageManager pm = AppGlobals.getPackageManager();
    final ComponentName service = job.getService();
    try {
        ServiceInfo si = pm.getServiceInfo(service,
                PackageManager.MATCH_DIRECT_BOOT_AWARE
                        | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
                UserHandle.getUserId(uid));
        if (si == null) {
            throw new IllegalArgumentException("No such service " + service);
        }
        if (si.applicationInfo.uid != uid) {
            throw new IllegalArgumentException("uid " + uid +
                    " cannot schedule job in " + service.getPackageName());
        }
        if (!JobService.PERMISSION_BIND.equals(si.permission)) {
            throw new IllegalArgumentException("Scheduled service " + service
                    + " does not require android.permission.BIND_JOB_SERVICE permission");
        }
    } catch (RemoteException e) {
        // Can't happen; the Package Manager is in this same process
    }
}
 
Example #6
Source File: LoadedApk.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Setup value for Thread.getContextClassLoader(). If the
 * package will not run in in a VM with other packages, we set
 * the Java context ClassLoader to the
 * PackageInfo.getClassLoader value. However, if this VM can
 * contain multiple packages, we intead set the Java context
 * ClassLoader to a proxy that will warn about the use of Java
 * context ClassLoaders and then fall through to use the
 * system ClassLoader.
 *
 * <p> Note that this is similar to but not the same as the
 * android.content.Context.getClassLoader(). While both
 * context class loaders are typically set to the
 * PathClassLoader used to load the package archive in the
 * single application per VM case, a single Android process
 * may contain several Contexts executing on one thread with
 * their own logical ClassLoaders while the Java context
 * ClassLoader is a thread local. This is why in the case when
 * we have multiple packages per VM we do not set the Java
 * context ClassLoader to an arbitrary but instead warn the
 * user to set their own if we detect that they are using a
 * Java library that expects it to be set.
 */
private void initializeJavaContextClassLoader() {
    IPackageManager pm = ActivityThread.getPackageManager();
    android.content.pm.PackageInfo pi;
    try {
        pi = pm.getPackageInfo(mPackageName, PackageManager.MATCH_DEBUG_TRIAGED_MISSING,
                UserHandle.myUserId());
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
    if (pi == null) {
        throw new IllegalStateException("Unable to get package info for "
                + mPackageName + "; is package not installed?");
    }
    /*
     * Two possible indications that this package could be
     * sharing its virtual machine with other packages:
     *
     * 1.) the sharedUserId attribute is set in the manifest,
     *     indicating a request to share a VM with other
     *     packages with the same sharedUserId.
     *
     * 2.) the application element of the manifest has an
     *     attribute specifying a non-default process name,
     *     indicating the desire to run in another packages VM.
     */
    boolean sharedUserIdSet = (pi.sharedUserId != null);
    boolean processNameNotDefault =
        (pi.applicationInfo != null &&
         !mPackageName.equals(pi.applicationInfo.processName));
    boolean sharable = (sharedUserIdSet || processNameNotDefault);
    ClassLoader contextClassLoader =
        (sharable)
        ? new WarningContextClassLoader()
        : mClassLoader;
    Thread.currentThread().setContextClassLoader(contextClassLoader);
}
 
Example #7
Source File: ClipboardService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private final void addActiveOwnerLocked(int uid, String pkg) {
    final IPackageManager pm = AppGlobals.getPackageManager();
    final int targetUserHandle = UserHandle.getCallingUserId();
    final long oldIdentity = Binder.clearCallingIdentity();
    try {
        PackageInfo pi = pm.getPackageInfo(pkg, 0, targetUserHandle);
        if (pi == null) {
            throw new IllegalArgumentException("Unknown package " + pkg);
        }
        if (!UserHandle.isSameApp(pi.applicationInfo.uid, uid)) {
            throw new SecurityException("Calling uid " + uid
                    + " does not own package " + pkg);
        }
    } catch (RemoteException e) {
        // Can't happen; the package manager is in the same process
    } finally {
        Binder.restoreCallingIdentity(oldIdentity);
    }
    PerUserClipboard clipboard = getClipboard();
    if (clipboard.primaryClip != null && !clipboard.activePermissionOwners.contains(pkg)) {
        final int N = clipboard.primaryClip.getItemCount();
        for (int i=0; i<N; i++) {
            grantItemLocked(clipboard.primaryClip.getItemAt(i), clipboard.primaryClipUid, pkg,
                    UserHandle.getUserId(uid));
        }
        clipboard.activePermissionOwners.add(pkg);
    }
}
 
Example #8
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
protected @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app,
        StorageManager storageManager, IPackageManager pm) {
    final VolumeInfo currentVol = getPackageCurrentVolume(app, storageManager);
    final List<VolumeInfo> vols = storageManager.getVolumes();
    final List<VolumeInfo> candidates = new ArrayList<>();
    for (VolumeInfo vol : vols) {
        if (Objects.equals(vol, currentVol)
                || isPackageCandidateVolume(mContext, app, vol, pm)) {
            candidates.add(vol);
        }
    }
    return candidates;
}
 
Example #9
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public PackageManager getPackageManager() {
    if (mPackageManager != null) {
        return mPackageManager;
    }

    IPackageManager pm = ActivityThread.getPackageManager();
    if (pm != null) {
        // Doesn't matter if we make more than one instance.
        return (mPackageManager = new ApplicationPackageManager(this, pm));
    }

    return null;
}
 
Example #10
Source File: UserController.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
boolean isFirstBootOrUpgrade() {
    IPackageManager pm = AppGlobals.getPackageManager();
    try {
        return pm.isFirstBoot() || pm.isUpgrade();
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
 
Example #11
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
protected @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app,
        StorageManager storageManager, IPackageManager pm) {
    final VolumeInfo currentVol = getPackageCurrentVolume(app, storageManager);
    final List<VolumeInfo> vols = storageManager.getVolumes();
    final List<VolumeInfo> candidates = new ArrayList<>();
    for (VolumeInfo vol : vols) {
        if (Objects.equals(vol, currentVol)
                || isPackageCandidateVolume(mContext, app, vol, pm)) {
            candidates.add(vol);
        }
    }
    return candidates;
}
 
Example #12
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public PackageManager getPackageManager() {
    if (mPackageManager != null) {
        return mPackageManager;
    }

    IPackageManager pm = ActivityThread.getPackageManager();
    if (pm != null) {
        // Doesn't matter if we make more than one instance.
        return (mPackageManager = new ApplicationPackageManager(this, pm));
    }

    return null;
}
 
Example #13
Source File: ApplicationPackageManager.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
protected @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app,
        StorageManager storageManager, IPackageManager pm) {
    final VolumeInfo currentVol = getPackageCurrentVolume(app, storageManager);
    final List<VolumeInfo> vols = storageManager.getVolumes();
    final List<VolumeInfo> candidates = new ArrayList<>();
    for (VolumeInfo vol : vols) {
        if (Objects.equals(vol, currentVol)
                || isPackageCandidateVolume(mContext, app, vol, pm)) {
            candidates.add(vol);
        }
    }
    return candidates;
}
 
Example #14
Source File: VpnService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void verifyApp(String packageName) throws PackageManager.NameNotFoundException {
    IPackageManager pm = IPackageManager.Stub.asInterface(
            ServiceManager.getService("package"));
    try {
        pm.getApplicationInfo(packageName, 0, UserHandle.getCallingUserId());
    } catch (RemoteException e) {
        throw new IllegalStateException(e);
    }
}
 
Example #15
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
protected @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app,
        StorageManager storageManager, IPackageManager pm) {
    final VolumeInfo currentVol = getPackageCurrentVolume(app, storageManager);
    final List<VolumeInfo> vols = storageManager.getVolumes();
    final List<VolumeInfo> candidates = new ArrayList<>();
    for (VolumeInfo vol : vols) {
        if (Objects.equals(vol, currentVol)
                || isPackageCandidateVolume(mContext, app, vol, pm)) {
            candidates.add(vol);
        }
    }
    return candidates;
}
 
Example #16
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public PackageManager getPackageManager() {
    if (mPackageManager != null) {
        return mPackageManager;
    }

    IPackageManager pm = ActivityThread.getPackageManager();
    if (pm != null) {
        // Doesn't matter if we make more than one instance.
        return (mPackageManager = new ApplicationPackageManager(this, pm));
    }

    return null;
}
 
Example #17
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
protected @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app,
        StorageManager storageManager, IPackageManager pm) {
    final VolumeInfo currentVol = getPackageCurrentVolume(app, storageManager);
    final List<VolumeInfo> vols = storageManager.getVolumes();
    final List<VolumeInfo> candidates = new ArrayList<>();
    for (VolumeInfo vol : vols) {
        if (Objects.equals(vol, currentVol)
                || isPackageCandidateVolume(mContext, app, vol, pm)) {
            candidates.add(vol);
        }
    }
    return candidates;
}
 
Example #18
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public PackageManager getPackageManager() {
    if (mPackageManager != null) {
        return mPackageManager;
    }

    IPackageManager pm = ActivityThread.getPackageManager();
    if (pm != null) {
        // Doesn't matter if we make more than one instance.
        return (mPackageManager = new ApplicationPackageManager(this, pm));
    }

    return null;
}
 
Example #19
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public PackageManager getPackageManager() {
    if (mPackageManager != null) {
        return mPackageManager;
    }

    IPackageManager pm = ActivityThread.getPackageManager();
    if (pm != null) {
        // Doesn't matter if we make more than one instance.
        return (mPackageManager = new ApplicationPackageManager(this, pm));
    }

    return null;
}
 
Example #20
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public PackageManager getPackageManager() {
    if (mPackageManager != null) {
        return mPackageManager;
    }

    IPackageManager pm = ActivityThread.getPackageManager();
    if (pm != null) {
        // Doesn't matter if we make more than one instance.
        return (mPackageManager = new ApplicationPackageManager(this, pm));
    }

    return null;
}
 
Example #21
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public PackageManager getPackageManager() {
    if (mPackageManager != null) {
        return mPackageManager;
    }

    IPackageManager pm = ActivityThread.getPackageManager();
    if (pm != null) {
        // Doesn't matter if we make more than one instance.
        return (mPackageManager = new ApplicationPackageManager(this, pm));
    }

    return null;
}
 
Example #22
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@Override
public PackageManager getPackageManager() {
    if (mPackageManager != null) {
        return mPackageManager;
    }

    IPackageManager pm = ActivityThread.getPackageManager();
    if (pm != null) {
        // Doesn't matter if we make more than one instance.
        return (mPackageManager = new ApplicationPackageManager(this, pm));
    }

    return null;
}
 
Example #23
Source File: ManagedServices.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public ManagedServices(Context context, Object mutex, UserProfiles userProfiles,
        IPackageManager pm) {
    mContext = context;
    mMutex = mutex;
    mUserProfiles = userProfiles;
    mPm = pm;
    mConfig = getConfig();
    mApprovalLevel = APPROVAL_BY_COMPONENT;
    mUm = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
}
 
Example #24
Source File: BluetoothManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Disables BluetoothOppLauncherActivity component, so the Bluetooth sharing option is not
 * offered to the user if Bluetooth or sharing is disallowed. Puts the component to its default
 * state if Bluetooth is not disallowed.
 *
 * @param userId user to disable bluetooth sharing for.
 * @param bluetoothSharingDisallowed whether bluetooth sharing is disallowed.
 */
private void updateOppLauncherComponentState(int userId, boolean bluetoothSharingDisallowed) {
    final ComponentName oppLauncherComponent = new ComponentName("com.android.bluetooth",
            "com.android.bluetooth.opp.BluetoothOppLauncherActivity");
    final int newState =
            bluetoothSharingDisallowed ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                    : PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
    try {
        final IPackageManager imp = AppGlobals.getPackageManager();
        imp.setComponentEnabledSetting(oppLauncherComponent, newState,
                PackageManager.DONT_KILL_APP, userId);
    } catch (Exception e) {
        // The component was not found, do nothing.
    }
}
 
Example #25
Source File: JobSchedulerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
int executeCancelCommand(PrintWriter pw, String pkgName, int userId,
        boolean hasJobId, int jobId) {
    if (DEBUG) {
        Slog.v(TAG, "executeCancelCommand(): " + pkgName + "/" + userId + " " + jobId);
    }

    int pkgUid = -1;
    try {
        IPackageManager pm = AppGlobals.getPackageManager();
        pkgUid = pm.getPackageUid(pkgName, 0, userId);
    } catch (RemoteException e) { /* can't happen */ }

    if (pkgUid < 0) {
        pw.println("Package " + pkgName + " not found.");
        return JobSchedulerShellCommand.CMD_ERR_NO_PACKAGE;
    }

    if (!hasJobId) {
        pw.println("Canceling all jobs for " + pkgName + " in user " + userId);
        if (!cancelJobsForUid(pkgUid, "cancel shell command for package")) {
            pw.println("No matching jobs found.");
        }
    } else {
        pw.println("Canceling job " + pkgName + "/#" + jobId + " in user " + userId);
        if (!cancelJob(pkgUid, jobId, Process.SHELL_UID)) {
            pw.println("No matching job found.");
        }
    }

    return 0;
}
 
Example #26
Source File: ArtManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public ArtManagerService(Context context, IPackageManager pm, Installer installer,
        Object installLock) {
    mContext = context;
    mPackageManager = pm;
    mInstaller = installer;
    mInstallLock = installLock;
    mHandler = new Handler(BackgroundThread.getHandler().getLooper());

    LocalServices.addService(ArtManagerInternal.class, new ArtManagerInternalImpl());
}
 
Example #27
Source File: DexManager.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public DexManager(Context context, IPackageManager pms, PackageDexOptimizer pdo,
        Installer installer, Object installLock, Listener listener) {
  mContext = context;
  mPackageCodeLocationsCache = new HashMap<>();
  mPackageDexUsage = new PackageDexUsage();
  mPackageManager = pms;
  mPackageDexOptimizer = pdo;
  mInstaller = installer;
  mInstallLock = installLock;
  mListener = listener;
}
 
Example #28
Source File: ConditionProviders.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public ConditionProviders(Context context, UserProfiles userProfiles, IPackageManager pm) {
    super(context, new Object(), userProfiles, pm);
    mSystemConditionProviderNames = safeSet(PropConfig.getStringArray(mContext,
            "system.condition.providers",
            R.array.config_system_condition_providers));
    mApprovalLevel = APPROVAL_BY_PACKAGE;
}
 
Example #29
Source File: IntentFirewall.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static void logIntent(int intentType, Intent intent, int callerUid,
        String resolvedType) {
    // The component shouldn't be null, but let's double check just to be safe
    ComponentName cn = intent.getComponent();
    String shortComponent = null;
    if (cn != null) {
        shortComponent = cn.flattenToShortString();
    }

    String callerPackages = null;
    int callerPackageCount = 0;
    IPackageManager pm = AppGlobals.getPackageManager();
    if (pm != null) {
        try {
            String[] callerPackagesArray = pm.getPackagesForUid(callerUid);
            if (callerPackagesArray != null) {
                callerPackageCount = callerPackagesArray.length;
                callerPackages = joinPackages(callerPackagesArray);
            }
        } catch (RemoteException ex) {
            Slog.e(TAG, "Remote exception while retrieving packages", ex);
        }
    }

    EventLogTags.writeIfwIntentMatched(intentType, shortComponent, callerUid,
            callerPackageCount, callerPackages, intent.getAction(), resolvedType,
            intent.getDataString(), intent.getFlags());
}
 
Example #30
Source File: IntentFirewall.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
boolean signaturesMatch(int uid1, int uid2) {
    try {
        IPackageManager pm = AppGlobals.getPackageManager();
        return pm.checkUidSignatures(uid1, uid2) == PackageManager.SIGNATURE_MATCH;
    } catch (RemoteException ex) {
        Slog.e(TAG, "Remote exception while checking signatures", ex);
        return false;
    }
}