Search in sources :

Example 66 with PackageInfo

use of android.content.pm.PackageInfo in project android_frameworks_base by ResurrectionRemix.

the class AccountManagerService method filterSharedAccounts.

private Account[] filterSharedAccounts(UserAccounts userAccounts, Account[] unfiltered, int callingUid, String callingPackage) {
    if (getUserManager() == null || userAccounts == null || userAccounts.userId < 0 || callingUid == Process.myUid()) {
        return unfiltered;
    }
    UserInfo user = getUserManager().getUserInfo(userAccounts.userId);
    if (user != null && user.isRestricted()) {
        String[] packages = mPackageManager.getPackagesForUid(callingUid);
        // If any of the packages is a white listed package, return the full set,
        // otherwise return non-shared accounts only.
        // This might be a temporary way to specify a whitelist
        String whiteList = mContext.getResources().getString(com.android.internal.R.string.config_appsAuthorizedForSharedAccounts);
        for (String packageName : packages) {
            if (whiteList.contains(";" + packageName + ";")) {
                return unfiltered;
            }
        }
        ArrayList<Account> allowed = new ArrayList<Account>();
        Account[] sharedAccounts = getSharedAccountsAsUser(userAccounts.userId);
        if (sharedAccounts == null || sharedAccounts.length == 0)
            return unfiltered;
        String requiredAccountType = "";
        try {
            // opted in to see restricted accounts.
            if (callingPackage != null) {
                PackageInfo pi = mPackageManager.getPackageInfo(callingPackage, 0);
                if (pi != null && pi.restrictedAccountType != null) {
                    requiredAccountType = pi.restrictedAccountType;
                }
            } else {
                // Otherwise check if the callingUid has a package that has opted in
                for (String packageName : packages) {
                    PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0);
                    if (pi != null && pi.restrictedAccountType != null) {
                        requiredAccountType = pi.restrictedAccountType;
                        break;
                    }
                }
            }
        } catch (NameNotFoundException nnfe) {
        }
        for (Account account : unfiltered) {
            if (account.type.equals(requiredAccountType)) {
                allowed.add(account);
            } else {
                boolean found = false;
                for (Account shared : sharedAccounts) {
                    if (shared.equals(account)) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    allowed.add(account);
                }
            }
        }
        Account[] filtered = new Account[allowed.size()];
        allowed.toArray(filtered);
        return filtered;
    } else {
        return unfiltered;
    }
}
Also used : Account(android.accounts.Account) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) UserInfo(android.content.pm.UserInfo)

Example 67 with PackageInfo

use of android.content.pm.PackageInfo in project android_frameworks_base by ResurrectionRemix.

the class AccountManagerService method isPrivileged.

private boolean isPrivileged(int callingUid) {
    final int callingUserId = UserHandle.getUserId(callingUid);
    final PackageManager userPackageManager;
    try {
        userPackageManager = mContext.createPackageContextAsUser("android", 0, new UserHandle(callingUserId)).getPackageManager();
    } catch (NameNotFoundException e) {
        return false;
    }
    String[] packages = userPackageManager.getPackagesForUid(callingUid);
    for (String name : packages) {
        try {
            PackageInfo packageInfo = userPackageManager.getPackageInfo(name, 0);
            if (packageInfo != null && (packageInfo.applicationInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
                return true;
            }
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
    return false;
}
Also used : PackageManager(android.content.pm.PackageManager) IPackageManager(android.content.pm.IPackageManager) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo) UserHandle(android.os.UserHandle)

Example 68 with PackageInfo

use of android.content.pm.PackageInfo in project android_frameworks_base by ResurrectionRemix.

the class SyncManager method getIsSyncable.

private int getIsSyncable(Account account, int userId, String providerName) {
    int isSyncable = mSyncStorageEngine.getIsSyncable(account, userId, providerName);
    UserInfo userInfo = UserManager.get(mContext).getUserInfo(userId);
    // If it's not a restricted user, return isSyncable.
    if (userInfo == null || !userInfo.isRestricted())
        return isSyncable;
    // Else check if the sync adapter has opted-in or not.
    RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo = mSyncAdapters.getServiceInfo(SyncAdapterType.newKey(providerName, account.type), userId);
    if (syncAdapterInfo == null)
        return AuthorityInfo.NOT_SYNCABLE;
    PackageInfo pInfo = null;
    try {
        pInfo = AppGlobals.getPackageManager().getPackageInfo(syncAdapterInfo.componentName.getPackageName(), 0, userId);
        if (pInfo == null)
            return AuthorityInfo.NOT_SYNCABLE;
    } catch (RemoteException re) {
        // Shouldn't happen.
        return AuthorityInfo.NOT_SYNCABLE;
    }
    if (pInfo.restrictedAccountType != null && pInfo.restrictedAccountType.equals(account.type)) {
        return isSyncable;
    } else {
        return AuthorityInfo.NOT_SYNCABLE;
    }
}
Also used : PackageInfo(android.content.pm.PackageInfo) UserInfo(android.content.pm.UserInfo) SyncAdapterType(android.content.SyncAdapterType) RemoteException(android.os.RemoteException) EndPoint(com.android.server.content.SyncStorageEngine.EndPoint) RegisteredServicesCache(android.content.pm.RegisteredServicesCache)

Example 69 with PackageInfo

use of android.content.pm.PackageInfo in project android_frameworks_base by ResurrectionRemix.

the class DevicePolicyManagerTest method testApplicationRestrictionsManagingApp.

public void testApplicationRestrictionsManagingApp() throws Exception {
    setAsProfileOwner(admin1);
    final String nonExistAppRestrictionsManagerPackage = "com.google.app.restrictions.manager2";
    final String appRestrictionsManagerPackage = "com.google.app.restrictions.manager";
    final int appRestrictionsManagerAppId = 20987;
    final int appRestrictionsManagerUid = UserHandle.getUid(DpmMockContext.CALLER_USER_HANDLE, appRestrictionsManagerAppId);
    doReturn(appRestrictionsManagerUid).when(mContext.packageManager).getPackageUidAsUser(eq(appRestrictionsManagerPackage), eq(DpmMockContext.CALLER_USER_HANDLE));
    mContext.binder.callingUid = appRestrictionsManagerUid;
    final PackageInfo pi = new PackageInfo();
    pi.applicationInfo = new ApplicationInfo();
    pi.applicationInfo.flags = ApplicationInfo.FLAG_HAS_CODE;
    doReturn(pi).when(mContext.ipackageManager).getPackageInfo(eq(appRestrictionsManagerPackage), anyInt(), eq(DpmMockContext.CALLER_USER_HANDLE));
    // appRestrictionsManager package shouldn't be able to manage restrictions as the PO hasn't
    // delegated that permission yet.
    assertFalse(dpm.isCallerApplicationRestrictionsManagingPackage());
    Bundle rest = new Bundle();
    rest.putString("KEY_STRING", "Foo1");
    try {
        dpm.setApplicationRestrictions(null, "pkg1", rest);
        fail("Didn't throw expected SecurityException");
    } catch (SecurityException expected) {
        MoreAsserts.assertContainsRegex("caller cannot manage application restrictions", expected.getMessage());
    }
    try {
        dpm.getApplicationRestrictions(null, "pkg1");
        fail("Didn't throw expected SecurityException");
    } catch (SecurityException expected) {
        MoreAsserts.assertContainsRegex("caller cannot manage application restrictions", expected.getMessage());
    }
    // Check via the profile owner that no restrictions were set.
    mContext.binder.callingUid = DpmMockContext.CALLER_UID;
    assertEquals(0, dpm.getApplicationRestrictions(admin1, "pkg1").size());
    // Check the API does not allow setting a non-existent package
    try {
        dpm.setApplicationRestrictionsManagingPackage(admin1, nonExistAppRestrictionsManagerPackage);
        fail("Non-existent app set as app restriction manager.");
    } catch (PackageManager.NameNotFoundException expected) {
        MoreAsserts.assertContainsRegex(nonExistAppRestrictionsManagerPackage, expected.getMessage());
    }
    // Let appRestrictionsManagerPackage manage app restrictions
    dpm.setApplicationRestrictionsManagingPackage(admin1, appRestrictionsManagerPackage);
    assertEquals(appRestrictionsManagerPackage, dpm.getApplicationRestrictionsManagingPackage(admin1));
    // Now that package should be able to set and retrieve app restrictions.
    mContext.binder.callingUid = appRestrictionsManagerUid;
    assertTrue(dpm.isCallerApplicationRestrictionsManagingPackage());
    dpm.setApplicationRestrictions(null, "pkg1", rest);
    Bundle returned = dpm.getApplicationRestrictions(null, "pkg1");
    assertEquals(1, returned.size(), 1);
    assertEquals("Foo1", returned.get("KEY_STRING"));
    // The same app running on a separate user shouldn't be able to manage app restrictions.
    mContext.binder.callingUid = UserHandle.getUid(UserHandle.USER_SYSTEM, appRestrictionsManagerAppId);
    assertFalse(dpm.isCallerApplicationRestrictionsManagingPackage());
    try {
        dpm.setApplicationRestrictions(null, "pkg1", rest);
        fail("Didn't throw expected SecurityException");
    } catch (SecurityException expected) {
        MoreAsserts.assertContainsRegex("caller cannot manage application restrictions", expected.getMessage());
    }
    // The DPM is still able to manage app restrictions, even if it allowed another app to do it
    // too.
    mContext.binder.callingUid = DpmMockContext.CALLER_UID;
    assertEquals(returned, dpm.getApplicationRestrictions(admin1, "pkg1"));
    dpm.setApplicationRestrictions(admin1, "pkg1", null);
    assertEquals(0, dpm.getApplicationRestrictions(admin1, "pkg1").size());
    // Removing the ability for the package to manage app restrictions.
    dpm.setApplicationRestrictionsManagingPackage(admin1, null);
    assertNull(dpm.getApplicationRestrictionsManagingPackage(admin1));
    mContext.binder.callingUid = appRestrictionsManagerUid;
    assertFalse(dpm.isCallerApplicationRestrictionsManagingPackage());
    try {
        dpm.setApplicationRestrictions(null, "pkg1", null);
        fail("Didn't throw expected SecurityException");
    } catch (SecurityException expected) {
        MoreAsserts.assertContainsRegex("caller cannot manage application restrictions", expected.getMessage());
    }
}
Also used : PackageManager(android.content.pm.PackageManager) PackageInfo(android.content.pm.PackageInfo) Bundle(android.os.Bundle) ApplicationInfo(android.content.pm.ApplicationInfo) Matchers.anyString(org.mockito.Matchers.anyString)

Example 70 with PackageInfo

use of android.content.pm.PackageInfo in project android_frameworks_base by ResurrectionRemix.

the class Pm method runSetAppLink.

// pm set-app-link [--user USER_ID] PACKAGE {always|ask|always-ask|never|undefined}
private int runSetAppLink() {
    int userId = UserHandle.USER_SYSTEM;
    String opt;
    while ((opt = nextOption()) != null) {
        if (opt.equals("--user")) {
            userId = Integer.parseInt(nextOptionData());
            if (userId < 0) {
                System.err.println("Error: user must be >= 0");
                return 1;
            }
        } else {
            System.err.println("Error: unknown option: " + opt);
            return showUsage();
        }
    }
    // Package name to act on; required
    final String pkg = nextArg();
    if (pkg == null) {
        System.err.println("Error: no package specified.");
        return showUsage();
    }
    // State to apply; {always|ask|never|undefined}, required
    final String modeString = nextArg();
    if (modeString == null) {
        System.err.println("Error: no app link state specified.");
        return showUsage();
    }
    final int newMode;
    switch(modeString.toLowerCase()) {
        case "undefined":
            newMode = INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
            break;
        case "always":
            newMode = INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS;
            break;
        case "ask":
            newMode = INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ASK;
            break;
        case "always-ask":
            newMode = INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS_ASK;
            break;
        case "never":
            newMode = INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER;
            break;
        default:
            System.err.println("Error: unknown app link state '" + modeString + "'");
            return 1;
    }
    try {
        final PackageInfo info = mPm.getPackageInfo(pkg, 0, userId);
        if (info == null) {
            System.err.println("Error: package " + pkg + " not found.");
            return 1;
        }
        if ((info.applicationInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_HAS_DOMAIN_URLS) == 0) {
            System.err.println("Error: package " + pkg + " does not handle web links.");
            return 1;
        }
        if (!mPm.updateIntentVerificationStatus(pkg, newMode, userId)) {
            System.err.println("Error: unable to update app link status for " + pkg);
            return 1;
        }
    } catch (Exception e) {
        System.err.println(e.toString());
        System.err.println(PM_NOT_RUNNING_ERR);
        return 1;
    }
    return 0;
}
Also used : PackageInfo(android.content.pm.PackageInfo) RemoteException(android.os.RemoteException) IOException(java.io.IOException) PackageParserException(android.content.pm.PackageParser.PackageParserException)

Aggregations

PackageInfo (android.content.pm.PackageInfo)1567 PackageManager (android.content.pm.PackageManager)703 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)327 ApplicationInfo (android.content.pm.ApplicationInfo)217 Test (org.junit.Test)193 RemoteException (android.os.RemoteException)179 ArrayList (java.util.ArrayList)166 Intent (android.content.Intent)126 IOException (java.io.IOException)121 Context (android.content.Context)81 IPackageManager (android.content.pm.IPackageManager)65 ResolveInfo (android.content.pm.ResolveInfo)65 File (java.io.File)62 SuppressLint (android.annotation.SuppressLint)58 ComponentName (android.content.ComponentName)57 ActivityInfo (android.content.pm.ActivityInfo)57 View (android.view.View)57 TextView (android.widget.TextView)54 Signature (android.content.pm.Signature)51 OverlayInfo (android.content.om.OverlayInfo)42