Java Code Examples for android.content.pm.ActivityInfo#loadIcon()

The following examples show how to use android.content.pm.ActivityInfo#loadIcon() .
Example 1
Project: DroidPlugin   File: PluginInstrumentation.java    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void fixTaskDescription(Activity activity, ActivityInfo targetInfo) {
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            PackageManager pm = mHostContext.getPackageManager();
            String lablel = String.valueOf(targetInfo.loadLabel(pm));
            Drawable icon = targetInfo.loadIcon(pm);
            Bitmap bitmap = null;
            if (icon instanceof BitmapDrawable) {
                bitmap = ((BitmapDrawable) icon).getBitmap();
            }
            if (bitmap != null) {
                activity.setTaskDescription(new android.app.ActivityManager.TaskDescription(lablel, bitmap));
            } else {
                activity.setTaskDescription(new android.app.ActivityManager.TaskDescription(lablel));
            }
        }
    } catch (Throwable e) {
        Log.w(TAG, "fixTaskDescription fail", e);
    }
}
 
Example 2
Project: LocationPrivacy   File: MainActivity.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create from probing for {@link Intent}s
 *
 * @param resolveInfo
 */
public TrustedAppEntry(ActivityInfo activityInfo) {
    this.packageName = activityInfo.packageName;
    this.simpleName = String.valueOf(activityInfo.loadLabel(pm));
    Drawable icon = activityInfo.loadIcon(pm);
    icon.setBounds(0, 0, iconSize, iconSize);
    this.icon = icon;
}
 
Example 3
Project: Android-Next   File: AdvancedShareActionProvider.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 根据ResolveInfo生成ShareTarget
 *
 * @param resolveInfo ResolveInfo
 * @return ShareTarget
 */
private ShareTarget toShareTarget(ResolveInfo resolveInfo) {
    if (resolveInfo == null || resolveInfo.activityInfo == null) {
        return null;
    }
    ActivityInfo info = resolveInfo.activityInfo;
    ShareTarget target = new ShareTarget(info.loadLabel(mPackageManager), info.loadIcon(mPackageManager), null);
    target.packageName = info.packageName;
    target.className = info.name;
    return target;
}
 
Example 4
Project: Android-Next   File: AdvancedShareActionProvider.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 根据ResolveInfo生成ShareTarget
 *
 * @param resolveInfo ResolveInfo
 * @return ShareTarget
 */
private ShareTarget toShareTarget(ResolveInfo resolveInfo) {
    if (resolveInfo == null || resolveInfo.activityInfo == null) {
        return null;
    }
    ActivityInfo info = resolveInfo.activityInfo;
    ShareTarget target = new ShareTarget(info.loadLabel(mPackageManager), info.loadIcon(mPackageManager), null);
    target.packageName = info.packageName;
    target.className = info.name;
    return target;
}
 
Example 5
Project: GravityBox   File: AppPickerPreference.java    License: Apache License 2.0 4 votes vote down vote up
private AppInfo getAppInfoFromValue(String value) {
    AppInfo appInfo = new AppInfo();
    if (value == null) return appInfo;

    try {
        Intent intent = Intent.parseUri(value, 0);

        int iconResId = intent.getStringExtra("iconResName") != null ?
                mResources.getIdentifier(intent.getStringExtra("iconResName"),
                        "drawable", mContext.getPackageName()) : 0;
        if (iconResId != 0) {
            appInfo.icon = mResources.getDrawable(iconResId);
        } else if (intent.hasExtra("icon")) {
            final String appIconPath = intent.getStringExtra("icon");
            if (appIconPath != null) {
                File f = new File(appIconPath);
                if (f.exists() && f.canRead()) {
                    FileInputStream fis = new FileInputStream(f);
                    appInfo.icon = new BitmapDrawable(mResources, BitmapFactory.decodeStream(fis));
                    fis.close();
                }
            }
        }

        int mode = intent.getIntExtra("mode", MODE_APP);
        if (mode == MODE_APP) {
            ComponentName cn = intent.getComponent();
            ActivityInfo ai = mPackageManager.getActivityInfo(cn, 0);
            appInfo.name = (ai.loadLabel(mPackageManager).toString());
            if (appInfo.icon == null) {
                appInfo.icon = ai.loadIcon(mPackageManager);
            }
        } else if (mode == MODE_SHORTCUT) {
            appInfo.name = intent.getStringExtra("prefLabel");
        }
        return appInfo;
    } catch (Exception e) {
        e.printStackTrace();
        return appInfo;
    }
}
 
Example 6
Project: Float-Bar   File: Util.java    License: Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 核心方法,加载最近启动的应用程序 注意:这里我们取出的最近任务为 MAX_RECENT_TASKS +
 * 1个,因为有可能最近任务中包好Launcher2。 这样可以保证我们展示出来的 最近任务 为 MAX_RECENT_TASKS 个
 * 通过以下步骤,可以获得近期任务列表,并将其存放在了appInfos这个list中,接下来就是展示这个list的工作了。
 */
public static void reloadButtons(Service service, List<HashMap<String, Object>> appInfos, int appNumber) {
	int MAX_RECENT_TASKS = appNumber; // allow for some discards
	int repeatCount = appNumber;// 保证上面两个值相等,设定存放的程序个数

	/* 每次加载必须清空list中的内容 */
	appInfos.removeAll(appInfos);

	// 得到包管理器和activity管理器
	final Context context = service.getApplication();
	final PackageManager pm = context.getPackageManager();
	final ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

	// 从ActivityManager中取出用户最近launch过的 MAX_RECENT_TASKS + 1 个,以从早到晚的时间排序,
	// 注意这个 0x0002,它的值在launcher中是用ActivityManager.RECENT_IGNORE_UNAVAILABLE
	// 但是这是一个隐藏域,因此我把它的值直接拷贝到这里
	@SuppressWarnings("deprecation")
	final List<ActivityManager.RecentTaskInfo> recentTasks = am.getRecentTasks(MAX_RECENT_TASKS + 1, 0x0002);

	// 这个activity的信息是我们的launcher
	ActivityInfo homeInfo = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME).resolveActivityInfo(pm, 0);
	int numTasks = recentTasks.size();
	for (int i = 0; i < numTasks && (i < MAX_RECENT_TASKS); i++) {
		HashMap<String, Object> singleAppInfo = new HashMap<String, Object>();// 当个启动过的应用程序的信息
		final ActivityManager.RecentTaskInfo info = recentTasks.get(i);

		Intent intent = new Intent(info.baseIntent);
		if (info.origActivity != null) {
			intent.setComponent(info.origActivity);
		}
		/**
		 * 如果找到是launcher,直接continue,后面的appInfos.add操作就不会发生了
		 */
		if (homeInfo != null) {
			if (homeInfo.packageName.equals(intent.getComponent().getPackageName()) && homeInfo.name.equals(intent.getComponent().getClassName())) {
				MAX_RECENT_TASKS = MAX_RECENT_TASKS + 1;
				continue;
			}
		}
		// 设置intent的启动方式为 创建新task()【并不一定会创建】
		intent.setFlags((intent.getFlags() & ~Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) | Intent.FLAG_ACTIVITY_NEW_TASK);
		// 获取指定应用程序activity的信息(按我的理解是:某一个应用程序的最后一个在前台出现过的activity。)
		final ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);
		if (resolveInfo != null) {
			final ActivityInfo activityInfo = resolveInfo.activityInfo;
			final String title = activityInfo.loadLabel(pm).toString();
			Drawable icon = activityInfo.loadIcon(pm);

			if (title != null && title.length() > 0 && icon != null) {
				singleAppInfo.put("title", title);
				singleAppInfo.put("icon", icon);
				singleAppInfo.put("tag", intent);
				singleAppInfo.put("packageName", activityInfo.packageName);
				appInfos.add(singleAppInfo);
			}
		}
	}
	MAX_RECENT_TASKS = repeatCount;
}
 
Example 7
Project: Float-Bar   File: Util.java    License: Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 核心方法,加载最近启动的应用程序 注意:这里我们取出的最近任务为 MAX_RECENT_TASKS +
 * 1个,因为有可能最近任务中包好Launcher2。 这样可以保证我们展示出来的 最近任务 为 MAX_RECENT_TASKS 个
 * 通过以下步骤,可以获得近期任务列表,并将其存放在了appInfos这个list中,接下来就是展示这个list的工作了。
 */
public static void reloadButtons(Service service, List<HashMap<String, Object>> appInfos, int appNumber) {
	int MAX_RECENT_TASKS = appNumber; // allow for some discards
	int repeatCount = appNumber;// 保证上面两个值相等,设定存放的程序个数

	/* 每次加载必须清空list中的内容 */
	appInfos.removeAll(appInfos);

	// 得到包管理器和activity管理器
	final Context context = service.getApplication();
	final PackageManager pm = context.getPackageManager();
	final ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

	// 从ActivityManager中取出用户最近launch过的 MAX_RECENT_TASKS + 1 个,以从早到晚的时间排序,
	// 注意这个 0x0002,它的值在launcher中是用ActivityManager.RECENT_IGNORE_UNAVAILABLE
	// 但是这是一个隐藏域,因此我把它的值直接拷贝到这里
	@SuppressWarnings("deprecation")
	final List<ActivityManager.RecentTaskInfo> recentTasks = am.getRecentTasks(MAX_RECENT_TASKS + 1, 0x0002);

	// 这个activity的信息是我们的launcher
	ActivityInfo homeInfo = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME).resolveActivityInfo(pm, 0);
	int numTasks = recentTasks.size();
	for (int i = 0; i < numTasks && (i < MAX_RECENT_TASKS); i++) {
		HashMap<String, Object> singleAppInfo = new HashMap<String, Object>();// 当个启动过的应用程序的信息
		final ActivityManager.RecentTaskInfo info = recentTasks.get(i);

		Intent intent = new Intent(info.baseIntent);
		if (info.origActivity != null) {
			intent.setComponent(info.origActivity);
		}
		/**
		 * 如果找到是launcher,直接continue,后面的appInfos.add操作就不会发生了
		 */
		if (homeInfo != null) {
			if (homeInfo.packageName.equals(intent.getComponent().getPackageName()) && homeInfo.name.equals(intent.getComponent().getClassName())) {
				MAX_RECENT_TASKS = MAX_RECENT_TASKS + 1;
				continue;
			}
		}
		// 设置intent的启动方式为 创建新task()【并不一定会创建】
		intent.setFlags((intent.getFlags() & ~Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) | Intent.FLAG_ACTIVITY_NEW_TASK);
		// 获取指定应用程序activity的信息(按我的理解是:某一个应用程序的最后一个在前台出现过的activity。)
		final ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);
		if (resolveInfo != null) {
			final ActivityInfo activityInfo = resolveInfo.activityInfo;
			final String title = activityInfo.loadLabel(pm).toString();
			Drawable icon = activityInfo.loadIcon(pm);

			if (title != null && title.length() > 0 && icon != null) {
				singleAppInfo.put("title", title);
				singleAppInfo.put("icon", icon);
				singleAppInfo.put("tag", intent);
				singleAppInfo.put("packageName", activityInfo.packageName);
				appInfos.add(singleAppInfo);
			}
		}
	}
	MAX_RECENT_TASKS = repeatCount;
}