Since home screen widget animation is very expensive, we might want to know if the widget is “visible” or not, to determine whether need to do the animation. In such case, we need home screen detection.

Following is how I do in the Eva Clock HD:

public class DetectHomeScreen {
	private static List<String> homePackageNamesCache;

	public static List<String> homePackageName(Context context){
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_HOME);
		List<ResolveInfo> info = context.getPackageManager().queryIntentActivities(intent, 0);
		List<String> homes = new ArrayList<String>(info.size());
		for(int i=0; i<info.size(); i++){
			homes.add(info.get(i).activityInfo.packageName);
		}
		homes.add(DetectHomeScreen.class.getPackage().getName());
		return homes;
	}

	public static boolean isHomeRunning(Context context){
		if (homePackageNamesCache==null)
			homePackageNamesCache = homePackageName(context);
		return isHomeRunning(context, homePackageNamesCache);
	}

	public static boolean isHomeRunning(Context context, List<String> homePackages){
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		String topPackage = am.getRunningTasks(1).get(0).topActivity.getPackageName();
		return homePackages.contains(topPackage);
	}
}

The above is checked periodically, or query it right before you want to update/start your home animation. Since querying all packages with HOME intent is very expensive, I cached that part, such that it may not work when user installed a NEW launcher and use it (well, a rare case).

About these ads