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

The following examples show how to use android.content.pm.PackageManager#getResourcesForActivity() . 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: LocaleHelper.java    From candybar with Apache License 2.0 6 votes vote down vote up
@Nullable
public static String getOtherAppLocaleName(@NonNull Context context, @NonNull Locale locale, @NonNull String fullComponentName) {
    try {
        int slashIndex = fullComponentName.indexOf("/");
        String activityName = fullComponentName.substring(slashIndex).replace("/", "");
        String packageName = fullComponentName.replace("/" + activityName, "");
        ComponentName componentName = new ComponentName(packageName, activityName);

        PackageManager packageManager = context.getPackageManager();
        ActivityInfo info = packageManager.getActivityInfo(componentName, PackageManager.GET_META_DATA);

        Resources res = packageManager.getResourcesForActivity(componentName);
        Configuration configuration = new Configuration();

        configuration.locale = locale;
        res.updateConfiguration(configuration, context.getResources().getDisplayMetrics());
        return info.loadLabel(packageManager).toString();
    } catch (Exception e) {
        LogUtil.e(Log.getStackTraceString(e));
    }
    return null;
}
 
Example 2
Source File: Launcher.java    From TurboLauncher with Apache License 2.0 6 votes vote down vote up
private Drawable getExternalPackageToolbarIcon(ComponentName activityName,
		String resourceName) {
	try {
		PackageManager packageManager = getPackageManager();
		// Look for the toolbar icon specified in the activity meta-data
		Bundle metaData = packageManager.getActivityInfo(activityName,
				PackageManager.GET_META_DATA).metaData;
		if (metaData != null) {
			int iconResId = metaData.getInt(resourceName);
			if (iconResId != 0) {
				Resources res = packageManager
						.getResourcesForActivity(activityName);
				return res.getDrawable(iconResId);
			}
		}
	} catch (NameNotFoundException e) {
		
	} catch (Resources.NotFoundException nfe) {
		
	}
	return null;
}
 
Example 3
Source File: GlowPadView.java    From CSipSimple with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Searches the given package for a resource to use to replace the Drawable on the
 * target with the given resource id
 * @param component of the .apk that contains the resource
 * @param name of the metadata in the .apk
 * @param existingResId the resource id of the target to search for
 * @return true if found in the given package and replaced at least one target Drawables
 */
public boolean replaceTargetDrawablesIfPresent(ComponentName component, String name,
            int existingResId) {
    if (existingResId == 0) return false;

    boolean replaced = false;
    if (component != null) {
        try {
            PackageManager packageManager = getContext().getPackageManager();
            // Look for the search icon specified in the activity meta-data
            Bundle metaData = packageManager.getActivityInfo(
                    component, PackageManager.GET_META_DATA).metaData;
            if (metaData != null) {
                int iconResId = metaData.getInt(name);
                if (iconResId != 0) {
                    Resources res = packageManager.getResourcesForActivity(component);
                    replaced = replaceTargetDrawables(res, existingResId, iconResId);
                }
            }
        } catch (NameNotFoundException e) {
            Log.w(THIS_FILE, "Failed to swap drawable; "
                    + component.flattenToShortString() + " not found", e);
        } catch (Resources.NotFoundException nfe) {
            Log.w(THIS_FILE, "Failed to swap drawable from "
                    + component.flattenToShortString(), nfe);
        }
    }
    if (!replaced) {
        // Restore the original drawable
        replaceTargetDrawables(getContext().getResources(), existingResId, existingResId);
    }
    return replaced;
}
 
Example 4
Source File: ColorUtils.java    From Status with Apache License 2.0 5 votes vote down vote up
@Nullable
public static Integer getPrimaryColor(Context context, ComponentName componentName) {
    PackageManager packageManager = context.getPackageManager();

    ActivityInfo activityInfo = null;
    PackageInfo packageInfo = null;
    Resources resources = null, activityResources = null;
    try {
        packageInfo = packageManager.getPackageInfo(componentName.getPackageName(), PackageManager.GET_META_DATA);
        resources = packageManager.getResourcesForApplication(packageInfo.applicationInfo);
        activityInfo = packageManager.getActivityInfo(componentName, 0);
        activityResources = packageManager.getResourcesForActivity(componentName);
    } catch (PackageManager.NameNotFoundException ignored) {
    }

    if (packageInfo != null && resources != null) {
        if (activityInfo != null && activityResources != null) {
            List<Integer> activityStatusBarColors = getStatusBarColors(activityInfo.packageName, resources, activityInfo.theme);
            if (activityStatusBarColors.size() > 0) {
                return activityStatusBarColors.get(0);
            }
        }

        List<Integer> statusBarColors = getStatusBarColors(packageInfo.packageName, resources, packageInfo.applicationInfo.theme);
        if (statusBarColors.size() > 0) {
            return statusBarColors.get(0);
        }

        if (packageInfo.activities != null) {
            for (ActivityInfo otherActivityInfo : packageInfo.activities) {
                List<Integer> otherStatusBarColors = getStatusBarColors(packageInfo.packageName, resources, otherActivityInfo.theme);
                if (otherStatusBarColors.size() > 0) {
                    return otherStatusBarColors.get(0);
                }
            }
        }
    }

    return null;
}
 
Example 5
Source File: ColorUtils.java    From Status with Apache License 2.0 5 votes vote down vote up
private static List<Integer> getPrimaryColors(Context context, ComponentName componentName) {
    List<Integer> colors = new ArrayList<>();

    PackageManager packageManager = context.getPackageManager();

    ActivityInfo activityInfo = null;
    PackageInfo packageInfo = null;
    Resources resources = null, activityResources = null;
    try {
        packageInfo = packageManager.getPackageInfo(componentName.getPackageName(), PackageManager.GET_META_DATA);
        resources = packageManager.getResourcesForApplication(packageInfo.applicationInfo);
        activityInfo = packageManager.getActivityInfo(componentName, 0);
        activityResources = packageManager.getResourcesForActivity(componentName);
    } catch (PackageManager.NameNotFoundException ignored) {
    }

    if (packageInfo != null && resources != null) {
        if (activityInfo != null && activityResources != null) {
            colors.addAll(getStatusBarColors(activityInfo.packageName, resources, activityInfo.theme));
        }

        colors.addAll(getStatusBarColors(packageInfo.packageName, resources, packageInfo.applicationInfo.theme));

        if (packageInfo.activities != null) {
            for (ActivityInfo otherActivityInfo : packageInfo.activities) {
                colors.addAll(getStatusBarColors(packageInfo.packageName, resources, otherActivityInfo.theme));
            }
        }
    }

    return colors;
}