Java Code Examples for android.content.pm.PackageManager#getPackagesForUid()

The following examples show how to use android.content.pm.PackageManager#getPackagesForUid() . 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: SyncOperation.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
static String reasonToString(PackageManager pm, int reason) {
    if (reason >= 0) {
        if (pm != null) {
            final String[] packages = pm.getPackagesForUid(reason);
            if (packages != null && packages.length == 1) {
                return packages[0];
            }
            final String name = pm.getNameForUid(reason);
            if (name != null) {
                return name;
            }
            return String.valueOf(reason);
        } else {
            return String.valueOf(reason);
        }
    } else {
        final int index = -reason - 1;
        if (index >= REASON_NAMES.length) {
            return String.valueOf(reason);
        } else {
            return REASON_NAMES[index];
        }
    }
}
 
Example 2
Source File: Util.java    From NetGuard with GNU General Public License v3.0 6 votes vote down vote up
public static List<String> getApplicationNames(int uid, Context context) {
    List<String> listResult = new ArrayList<>();
    if (uid == 0)
        listResult.add(context.getString(R.string.title_root));
    else if (uid == 1013)
        listResult.add(context.getString(R.string.title_mediaserver));
    else if (uid == 9999)
        listResult.add(context.getString(R.string.title_nobody));
    else {
        PackageManager pm = context.getPackageManager();
        String[] pkgs = pm.getPackagesForUid(uid);
        if (pkgs == null)
            listResult.add(Integer.toString(uid));
        else
            for (String pkg : pkgs)
                try {
                    ApplicationInfo info = pm.getApplicationInfo(pkg, 0);
                    listResult.add(pm.getApplicationLabel(info).toString());
                } catch (PackageManager.NameNotFoundException ignored) {
                }
        Collections.sort(listResult);
    }
    return listResult;
}
 
Example 3
Source File: WallpaperManagerService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isSetWallpaperAllowed(String callingPackage) {
    final PackageManager pm = mContext.getPackageManager();
    String[] uidPackages = pm.getPackagesForUid(Binder.getCallingUid());
    boolean uidMatchPackage = Arrays.asList(uidPackages).contains(callingPackage);
    if (!uidMatchPackage) {
        return false;   // callingPackage was faked.
    }

    final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
    if (dpm.isDeviceOwnerApp(callingPackage) || dpm.isProfileOwnerApp(callingPackage)) {
        return true;
    }
    final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
    return !um.hasUserRestriction(UserManager.DISALLOW_SET_WALLPAPER);
}
 
Example 4
Source File: Util.java    From tracker-control-android with GNU General Public License v3.0 6 votes vote down vote up
public static List<String> getApplicationNames(int uid, Context context) {
    List<String> listResult = new ArrayList<>();
    if (uid == 0)
        listResult.add(context.getString(R.string.title_root));
    else if (uid == 1013)
        listResult.add(context.getString(R.string.title_mediaserver));
    else if (uid == 9999)
        listResult.add(context.getString(R.string.title_nobody));
    else {
        PackageManager pm = context.getPackageManager();
        String[] pkgs = pm.getPackagesForUid(uid);
        if (pkgs == null)
            listResult.add(Integer.toString(uid));
        else
            for (String pkg : pkgs)
                try {
                    ApplicationInfo info = pm.getApplicationInfo(pkg, 0);
                    listResult.add(pm.getApplicationLabel(info).toString());
                } catch (PackageManager.NameNotFoundException ignored) {
                }
        Collections.sort(listResult);
    }
    return listResult;
}
 
Example 5
Source File: Util.java    From kcanotify with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isSystem(int uid, Context context) {
    PackageManager pm = context.getPackageManager();
    String[] pkgs = pm.getPackagesForUid(uid);
    if (pkgs != null)
        for (String pkg : pkgs)
            if (isSystem(pkg, context))
                return true;
    return false;
}
 
Example 6
Source File: Apps.java    From deagle with Apache License 2.0 5 votes vote down vote up
public static boolean isPrivileged(final PackageManager pm, final int uid) {
    if (uid < 0) return false;
    if (uid < Process.FIRST_APPLICATION_UID) return true;
    final String[] pkgs = pm.getPackagesForUid(uid);
    if (pkgs == null) return false;
    for (final String pkg : pkgs) try {
        if (isPrivileged(pm.getApplicationInfo(pkg, 0))) return true;
    } catch (final NameNotFoundException ignored) {}
    return false;
}
 
Example 7
Source File: NevoDecoratorService.java    From sdk with Apache License 2.0 5 votes vote down vote up
@Override public int onConnected(final INevoController controller, final Bundle options) {
	RemoteImplementation.initializeIfNotYet(NevoDecoratorService.this);

	final PackageManager pm = getPackageManager();
	final int caller_uid = Binder.getCallingUid(), my_uid = Process.myUid();
	if (caller_uid != my_uid && pm.checkSignatures(caller_uid, my_uid) != SIGNATURE_MATCH) {
		final String[] caller_pkgs = pm.getPackagesForUid(caller_uid);
		if (caller_pkgs == null || caller_pkgs.length == 0) throw new SecurityException();
		try { @SuppressLint("PackageManagerGetSignatures")
		final PackageInfo caller_info = pm.getPackageInfo(caller_pkgs[0], GET_SIGNATURES);
			if (caller_info == null) throw new SecurityException();
			for (final Signature signature : caller_info.signatures)
				if (signature.hashCode() != SIGNATURE_HASH) throw new SecurityException("Caller signature mismatch");
		} catch (final PackageManager.NameNotFoundException e) { throw new SecurityException(); }	// Should not happen
	}
	mCallerUid = caller_uid;

	mController = controller;
	if (options != null) mSupportedApiVersion = options.getInt(KEY_SUPPORTED_API_VERSION);
	try {
		Log.v(TAG, "onConnected");
		NevoDecoratorService.this.onConnected();
	} catch (final Throwable t) {
		Log.e(TAG, "Error running onConnected()", t);
		throw asParcelableException(t);
	}
	return mFlags;
}
 
Example 8
Source File: DuwearServiceBinder.java    From AndroidWear-OpenWear with MIT License 5 votes vote down vote up
private boolean checkCallerUid(String paramString, int paramInt) {
    PackageManager localPackageManager = service.getPackageManager();
    String[] arrayOfString1 = localPackageManager.getPackagesForUid(paramInt);
    for (String str : arrayOfString1) {
        if (str.equals(paramString)) {
            return true;
        }
    }
    return false;
}
 
Example 9
Source File: AppUtils.java    From NetKnight with Apache License 2.0 5 votes vote down vote up
public static String getPackageNameByUid(Context context,int uid){


        PackageManager pm = context.getPackageManager();
        String[] packageName = pm.getPackagesForUid(uid);
        if(packageName!=null&&packageName.length==1){
            return packageName[0];
        }

        return "Null";
    }
 
Example 10
Source File: GmsUtils.java    From prevent with Do What The F*ck You Want To Public License 5 votes vote down vote up
private static boolean isGapps(PackageManager pm, int callingUid) {
    String[] packageNames = pm.getPackagesForUid(callingUid);
    if (packageNames == null) {
        return false;
    }
    for (String packageName : packageNames) {
        if (isGapps(packageName)) {
            return true;
        }
    }
    return false;
}
 
Example 11
Source File: PackageUtils.java    From NetCloud_android with GNU General Public License v2.0 5 votes vote down vote up
public static String getPackageName(int uid){
    PackageManager pm = ContextMgr.getApplicationContext().getPackageManager();
    String[] pkgs = pm.getPackagesForUid(uid);

    if(pkgs != null){
        for(String pkg:pkgs){
            Log.d(TAG,"pkg: " + pkg);
        }

        return pkgs[0];
    }

    return "system";
}
 
Example 12
Source File: XProvider.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
private static void enforcePermission(Context context) throws SecurityException {
    int cuid = Util.getAppId(Binder.getCallingUid());

    // Access package manager as system user
    long ident = Binder.clearCallingIdentity();
    try {
        // Allow system
        if (cuid == Process.SYSTEM_UID)
            return;

        // Allow same signature
        PackageManager pm = context.getPackageManager();
        int uid = pm.getApplicationInfo(BuildConfig.APPLICATION_ID, 0).uid;
        if (pm.checkSignatures(cuid, uid) == PackageManager.SIGNATURE_MATCH)
            return;

        // Allow specific signature
        String[] cpkg = pm.getPackagesForUid(cuid);
        if (cpkg.length > 0) {
            byte[] bytes = Util.getSha1Fingerprint(context, cpkg[0]);
            StringBuilder sb = new StringBuilder();
            for (byte b : bytes)
                sb.append(Integer.toString(b & 0xff, 16).toLowerCase());

            Resources resources = pm.getResourcesForApplication(BuildConfig.APPLICATION_ID);
            if (sb.toString().equals(resources.getString(R.string.pro_fingerprint)))
                return;
        }
        throw new SecurityException("Signature error cuid=" + cuid);
    } catch (Throwable ex) {
        throw new SecurityException(ex);
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}
 
Example 13
Source File: Util.java    From NetGuard with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isSystem(int uid, Context context) {
    PackageManager pm = context.getPackageManager();
    String[] pkgs = pm.getPackagesForUid(uid);
    if (pkgs != null)
        for (String pkg : pkgs)
            if (isSystem(pkg, context))
                return true;
    return false;
}
 
Example 14
Source File: Util.java    From tracker-control-android with GNU General Public License v3.0 5 votes vote down vote up
public static boolean hasInternet(int uid, Context context) {
    PackageManager pm = context.getPackageManager();
    String[] pkgs = pm.getPackagesForUid(uid);
    if (pkgs != null)
        for (String pkg : pkgs)
            if (hasInternet(pkg, context))
                return true;
    return false;
}
 
Example 15
Source File: Util.java    From NetGuard with GNU General Public License v3.0 5 votes vote down vote up
public static boolean hasInternet(int uid, Context context) {
    PackageManager pm = context.getPackageManager();
    String[] pkgs = pm.getPackagesForUid(uid);
    if (pkgs != null)
        for (String pkg : pkgs)
            if (hasInternet(pkg, context))
                return true;
    return false;
}
 
Example 16
Source File: StubProvider.java    From aptoide-client with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {

    int uid = Binder.getCallingUid();

    PackageManager pm = getContext().getPackageManager();
    String callerPackage = pm.getPackagesForUid(uid)[0];

    Log.d("AptoideDebug", "Someone is trying to update preferences");

    int result = pm.checkSignatures(callerPackage, getContext().getPackageName());

    if(result == PackageManager.SIGNATURE_MATCH) {
        switch (uriMatcher.match(uri)) {
            case CHANGE_PREFERENCE:

                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
                SharedPreferences.Editor edit = preferences.edit();
                int changed = 0;
                for (final Map.Entry<String, Object> entry : values.valueSet()) {
                    Object value = entry.getValue();
                    if (value instanceof String) {
                        edit.putString(entry.getKey(), (String) value);
                    } else if (value instanceof Integer) {
                        edit.putInt(entry.getKey(), (Integer) value);
                    } else if (value instanceof Long) {
                        edit.putLong(entry.getKey(), (Long) value);
                    } else if (value instanceof Boolean) {

                        if(entry.getKey().equals("debugmode")){
                            Aptoide.DEBUG_MODE = (Boolean) entry.getValue();
                        }

                        edit.putBoolean(entry.getKey(), (Boolean) value);
                    } else if (value instanceof Float) {
                        edit.putFloat(entry.getKey(), (Float) value);
                    }
                    changed++;
                    Handler handler = new Handler(Looper.getMainLooper());
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(Aptoide.getContext(), "Preference set: " + entry.getKey() + "=" + entry.getValue(), Toast.LENGTH_LONG).show();
                        }
                    });
                }



                Log.d("AptoideDebug", "Commited");

                edit.commit();
                return changed;
            default:
                return 0;
        }

    }
    return 0;
}
 
Example 17
Source File: ExternalAuthUtils.java    From delion with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the calling package names for the current transaction.
 * @param context The context to use for accessing the package manager.
 * @return The calling package names.
 */
private static String[] getCallingPackages(Context context) {
    int callingUid = Binder.getCallingUid();
    PackageManager pm = context.getApplicationContext().getPackageManager();
    return pm.getPackagesForUid(callingUid);
}
 
Example 18
Source File: ClientManager.java    From delion with Apache License 2.0 4 votes vote down vote up
private static String getPackageName(Context context, int uid) {
    PackageManager packageManager = context.getPackageManager();
    String[] packageList = packageManager.getPackagesForUid(uid);
    if (packageList.length != 1 || TextUtils.isEmpty(packageList[0])) return null;
    return packageList[0];
}
 
Example 19
Source File: ExternalAuthUtils.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the calling package names for the current transaction.
 * @param context The context to use for accessing the package manager.
 * @return The calling package names.
 */
private static String[] getCallingPackages(Context context) {
    int callingUid = Binder.getCallingUid();
    PackageManager pm = context.getApplicationContext().getPackageManager();
    return pm.getPackagesForUid(callingUid);
}
 
Example 20
Source File: ClientManager.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
private static String getPackageName(Context context, int uid) {
    PackageManager packageManager = context.getPackageManager();
    String[] packageList = packageManager.getPackagesForUid(uid);
    if (packageList.length != 1 || TextUtils.isEmpty(packageList[0])) return null;
    return packageList[0];
}