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

The following examples show how to use android.content.pm.PackageManager#getActivityInfo() . 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: TaskStackBuilder.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Add the activity parent chain as specified by the
 * {@link android.R.attr#parentActivityName parentActivityName} attribute of the activity
 * (or activity-alias) element in the application's manifest to the task stack builder.
 *
 * @param sourceActivityName Must specify an Activity component. All parents of
 *                           this activity will be added
 * @return This TaskStackBuilder for method chaining
 */
public TaskStackBuilder addParentStack(ComponentName sourceActivityName) {
    final int insertAt = mIntents.size();
    PackageManager pm = mSourceContext.getPackageManager();
    try {
        ActivityInfo info = pm.getActivityInfo(sourceActivityName, 0);
        String parentActivity = info.parentActivityName;
        while (parentActivity != null) {
            final ComponentName target = new ComponentName(info.packageName, parentActivity);
            info = pm.getActivityInfo(target, 0);
            parentActivity = info.parentActivityName;
            final Intent parent = parentActivity == null && insertAt == 0
                    ? Intent.makeMainActivity(target)
                    : new Intent().setComponent(target);
            mIntents.add(insertAt, parent);
        }
    } catch (NameNotFoundException e) {
        Log.e(TAG, "Bad ComponentName while traversing activity parent metadata");
        throw new IllegalArgumentException(e);
    }
    return this;
}
 
Example 3
Source File: SuggestionsAdapter.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the activity or application icon for an activity.
 *
 * @param component Name of an activity.
 * @return A drawable, or {@code null} if neither the acitivy or the application
 *         have an icon set.
 */
private Drawable getActivityIcon(ComponentName component) {
    PackageManager pm = mContext.getPackageManager();
    final ActivityInfo activityInfo;
    try {
        activityInfo = pm.getActivityInfo(component, PackageManager.GET_META_DATA);
    } catch (NameNotFoundException ex) {
        Log.w(LOG_TAG, ex.toString());
        return null;
    }
    int iconId = activityInfo.getIconResource();
    if (iconId == 0) return null;
    String pkg = component.getPackageName();
    Drawable drawable = pm.getDrawable(pkg, iconId, activityInfo.applicationInfo);
    if (drawable == null) {
        Log.w(LOG_TAG, "Invalid icon resource " + iconId + " for "
                + component.flattenToShortString());
        return null;
    }
    return drawable;
}
 
Example 4
Source File: SuggestionsAdapter.java    From zen4android with MIT License 6 votes vote down vote up
/**
 * Gets the activity or application icon for an activity.
 *
 * @param component Name of an activity.
 * @return A drawable, or {@code null} if neither the acitivy or the application
 *         have an icon set.
 */
private Drawable getActivityIcon(ComponentName component) {
    PackageManager pm = mContext.getPackageManager();
    final ActivityInfo activityInfo;
    try {
        activityInfo = pm.getActivityInfo(component, PackageManager.GET_META_DATA);
    } catch (NameNotFoundException ex) {
        Log.w(LOG_TAG, ex.toString());
        return null;
    }
    int iconId = activityInfo.getIconResource();
    if (iconId == 0) return null;
    String pkg = component.getPackageName();
    Drawable drawable = pm.getDrawable(pkg, iconId, activityInfo.applicationInfo);
    if (drawable == null) {
        Log.w(LOG_TAG, "Invalid icon resource " + iconId + " for "
                + component.flattenToShortString());
        return null;
    }
    return drawable;
}
 
Example 5
Source File: Validate.java    From kognitivo with Apache License 2.0 6 votes vote down vote up
public static void hasFacebookActivity(Context context, boolean shouldThrow) {
    Validate.notNull(context, "context");
    PackageManager pm = context.getPackageManager();
    ActivityInfo activityInfo = null;
    if (pm != null) {
        ComponentName componentName =
                new ComponentName(context, FacebookActivity.class);
        try {
            activityInfo = pm.getActivityInfo(componentName, PackageManager.GET_ACTIVITIES);
        } catch (PackageManager.NameNotFoundException e) {
        }
    }
    if (activityInfo == null) {
        if (shouldThrow) {
            throw new IllegalStateException(FACEBOOK_ACTIVITY_NOT_FOUND_REASON);
        } else {
            Log.w(TAG, FACEBOOK_ACTIVITY_NOT_FOUND_REASON);
        }
    }
}
 
Example 6
Source File: SuggestionsAdapter.java    From zhangshangwuda with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the activity or application icon for an activity.
 *
 * @param component Name of an activity.
 * @return A drawable, or {@code null} if neither the acitivy or the application
 *         have an icon set.
 */
private Drawable getActivityIcon(ComponentName component) {
    PackageManager pm = mContext.getPackageManager();
    final ActivityInfo activityInfo;
    try {
        activityInfo = pm.getActivityInfo(component, PackageManager.GET_META_DATA);
    } catch (NameNotFoundException ex) {
        Log.w(LOG_TAG, ex.toString());
        return null;
    }
    int iconId = activityInfo.getIconResource();
    if (iconId == 0) return null;
    String pkg = component.getPackageName();
    Drawable drawable = pm.getDrawable(pkg, iconId, activityInfo.applicationInfo);
    if (drawable == null) {
        Log.w(LOG_TAG, "Invalid icon resource " + iconId + " for "
                + component.flattenToShortString());
        return null;
    }
    return drawable;
}
 
Example 7
Source File: SuggestionsAdapter.java    From CSipSimple with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Gets the activity or application icon for an activity.
 *
 * @param component Name of an activity.
 * @return A drawable, or {@code null} if neither the acitivy or the application
 *         have an icon set.
 */
private Drawable getActivityIcon(ComponentName component) {
    PackageManager pm = mContext.getPackageManager();
    final ActivityInfo activityInfo;
    try {
        activityInfo = pm.getActivityInfo(component, PackageManager.GET_META_DATA);
    } catch (NameNotFoundException ex) {
        Log.w(LOG_TAG, ex.toString());
        return null;
    }
    int iconId = activityInfo.getIconResource();
    if (iconId == 0) return null;
    String pkg = component.getPackageName();
    Drawable drawable = pm.getDrawable(pkg, iconId, activityInfo.applicationInfo);
    if (drawable == null) {
        Log.w(LOG_TAG, "Invalid icon resource " + iconId + " for "
                + component.flattenToShortString());
        return null;
    }
    return drawable;
}
 
Example 8
Source File: QQAuth.java    From letv with Apache License 2.0 6 votes vote down vote up
public static QQAuth createInstance(String str, Context context) {
    Global.setContext(context.getApplicationContext());
    f.c(f.d, "QQAuth -- createInstance() --start");
    try {
        PackageManager packageManager = context.getPackageManager();
        packageManager.getActivityInfo(new ComponentName(context.getPackageName(), "com.tencent.tauth.AuthActivity"), 0);
        packageManager.getActivityInfo(new ComponentName(context.getPackageName(), "com.tencent.connect.common.AssistActivity"), 0);
        QQAuth qQAuth = new QQAuth(str, context);
        f.c(f.d, "QQAuth -- createInstance()  --end");
        return qQAuth;
    } catch (Throwable e) {
        f.b(f.d, "createInstance() error --end", e);
        Toast.makeText(context.getApplicationContext(), "请参照文档在Androidmanifest.xml加上AuthActivity和AssitActivity的定义 ", 1).show();
        return null;
    }
}
 
Example 9
Source File: PermissionEnforcementTest.java    From coursera-android-labs with MIT License 6 votes vote down vote up
public void testRun() {
	
	// =============== Section One ==================
	solo.waitForActivity(
			course.labs.permissionslab.ActivityLoaderActivity.class, 2000);

	PackageManager pm = getActivity().getPackageManager();
	try {
		ActivityInfo activityInfo = pm.getActivityInfo(new ComponentName(
				"course.labs.dangerousapp",
				"course.labs.dangerousapp.DangerousActivity"), 0);
		assertTrue(
				"PermissionEnforcementTest:" +
				"Section One:" +
				"course.labs.permissions.DANGEROUS_ACTIVITY_PERM not enforced by DangerousActivity",
				null != activityInfo && null != activityInfo.permission
						&& activityInfo.permission
								.equals("course.labs.permissions.DANGEROUS_ACTIVITY_PERM"));
	} catch (NameNotFoundException e) {
		fail("PermissionEnforcementTest:" +
				"Section One:" +
				"DangerousActivity not found");
	}
}
 
Example 10
Source File: SensorsDataUtils.java    From sa-sdk-android with Apache License 2.0 5 votes vote down vote up
/**
 * 尝试读取页面 title
 *
 * @param properties JSONObject
 * @param activity Activity
 */
public static void getScreenNameAndTitleFromActivity(JSONObject properties, Activity activity) {
    if (activity == null || properties == null) {
        return;
    }

    try {
        properties.put("$screen_name", activity.getClass().getCanonicalName());

        String activityTitle = null;
        if (!TextUtils.isEmpty(activity.getTitle())) {
            activityTitle = activity.getTitle().toString();
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            String toolbarTitle = getToolbarTitle(activity);
            if (!TextUtils.isEmpty(toolbarTitle)) {
                activityTitle = toolbarTitle;
            }
        }

        if (TextUtils.isEmpty(activityTitle)) {
            PackageManager packageManager = activity.getPackageManager();
            if (packageManager != null) {
                ActivityInfo activityInfo = packageManager.getActivityInfo(activity.getComponentName(), 0);
                activityTitle = activityInfo.loadLabel(packageManager).toString();
            }
        }
        if (!TextUtils.isEmpty(activityTitle)) {
            properties.put("$title", activityTitle);
        }
    } catch (Exception e) {
        SALog.printStackTrace(e);
    }
}
 
Example 11
Source File: AllegroUtils.java    From ans-android-sdk with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取 Activity 的 title
 *
 * @param activity Activity
 * @return Activity 的 title
 */
private static String getActivityTitle(Activity activity) {
    if (activity != null) {
        try {
            String activityTitle = null;

            if (Build.VERSION.SDK_INT >= 11) {
                String toolbarTitle = getToolbarTitle(activity);
                if (!TextUtils.isEmpty(toolbarTitle)) {
                    activityTitle = toolbarTitle;
                }
            }

            if (!TextUtils.isEmpty(activityTitle)) {
                activityTitle = activity.getTitle().toString();
            }

            if (TextUtils.isEmpty(activityTitle)) {
                PackageManager packageManager = activity.getPackageManager();
                if (packageManager != null) {
                    ActivityInfo activityInfo = packageManager.getActivityInfo(activity.getComponentName(), 0);
                    if (!TextUtils.isEmpty(activityInfo.loadLabel(packageManager))) {
                        activityTitle = activityInfo.loadLabel(packageManager).toString();
                    }
                }
            }

            return activityTitle;
        } catch (Throwable ignore) {
            ExceptionUtil.exceptionThrow(ignore);
            return null;
        }
    }
    return null;
}
 
Example 12
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;
}
 
Example 13
Source File: MetaDataUtils.java    From AndroidUtilCode with Apache License 2.0 5 votes vote down vote up
/**
 * Return the value of meta-data in activity.
 *
 * @param clz The activity class.
 * @param key The key of meta-data.
 * @return the value of meta-data in activity
 */
public static String getMetaDataInActivity(@NonNull final Class<? extends Activity> clz,
                                           @NonNull final String key) {
    String value = "";
    PackageManager pm = Utils.getApp().getPackageManager();
    ComponentName componentName = new ComponentName(Utils.getApp(), clz);
    try {
        ActivityInfo ai = pm.getActivityInfo(componentName, PackageManager.GET_META_DATA);
        value = String.valueOf(ai.metaData.get(key));
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return value;
}
 
Example 14
Source File: AppTools.java    From RxEasyHttp with Apache License 2.0 5 votes vote down vote up
private static ActivityInfo getActivityInfo(Activity context) {
    PackageManager packageManager = context.getPackageManager();
    try {
        return packageManager.getActivityInfo(context.getComponentName(),
                PackageManager.GET_META_DATA);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 15
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 16
Source File: MainActivity.java    From hk with GNU General Public License v3.0 5 votes vote down vote up
private void testPackageManager() {
		PackageManager p = getPackageManager();
		try {
			p.getActivityInfo(getComponentName(), 0);
			Log.i("TEST2", "GetActivityInfo is done");
			p.getPackageInfo(getPackageName(), 0);
			Log.i("TEST2", "getPackageInfo is done");
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
//		p.setComponentEnabledSetting(getComponentName(),
//				PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
//				PackageManager.DONT_KILL_APP);
//		Log.i("TEST2", "App icon should disappear");
	}
 
Example 17
Source File: SensorsDataPrivate.java    From AutoTrackAppViewScreen with Apache License 2.0 5 votes vote down vote up
/**
 * 获取 Activity 的 title
 *
 * @param activity Activity
 * @return String 当前页面 title
 */
@SuppressWarnings("all")
private static String getActivityTitle(Activity activity) {
    String activityTitle = null;

    if (activity == null) {
        return null;
    }

    try {
        activityTitle = activity.getTitle().toString();

        if (Build.VERSION.SDK_INT >= 11) {
            String toolbarTitle = getToolbarTitle(activity);
            if (!TextUtils.isEmpty(toolbarTitle)) {
                activityTitle = toolbarTitle;
            }
        }

        if (TextUtils.isEmpty(activityTitle)) {
            PackageManager packageManager = activity.getPackageManager();
            if (packageManager != null) {
                ActivityInfo activityInfo = packageManager.getActivityInfo(activity.getComponentName(), 0);
                if (activityInfo != null) {
                    activityTitle = activityInfo.loadLabel(packageManager).toString();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return activityTitle;
}
 
Example 18
Source File: Utils.java    From al-muazzin with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Intent getDefaultAlarmsIntent(Context context) {
    PackageManager pm = context.getPackageManager();
    for (String packageName : DESK_CLOCK_PACKAGES) {
        try {
            ComponentName cn = new ComponentName(packageName, "com.android.deskclock.AlarmClock");
            pm.getActivityInfo(cn, 0);
            return Intent.makeMainActivity(cn);
        } catch (PackageManager.NameNotFoundException ignored) {
        }
    }
    return getDefaultClockIntent(context);
}
 
Example 19
Source File: NavUtils.java    From V.FlyoutTest with MIT License 3 votes vote down vote up
/**
 * Return the fully qualified class name of a source activity's parent activity as specified by
 * a {@link #PARENT_ACTIVITY} &lt;meta-data&gt; element within the activity element in
 * the application's manifest. The source activity is provided by componentName.
 *
 * @param context Context for looking up the activity component for the source activity
 * @param componentName ComponentName for the source Activity
 * @return The fully qualified class name of sourceActivity's parent activity or null if
 *         it was not specified
 */
public static String getParentActivityName(Context context, ComponentName componentName)
        throws NameNotFoundException {
    PackageManager pm = context.getPackageManager();
    ActivityInfo info = pm.getActivityInfo(componentName, PackageManager.GET_META_DATA);
    String parentActivity = IMPL.getParentActivityName(context, info);
    return parentActivity;
}
 
Example 20
Source File: NavUtils.java    From CodenameOne with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Return the fully qualified class name of a source activity's parent activity as specified by
 * a {@link #PARENT_ACTIVITY} &lt;meta-data&gt; element within the activity element in
 * the application's manifest. The source activity is provided by componentName.
 *
 * @param context Context for looking up the activity component for the source activity
 * @param componentName ComponentName for the source Activity
 * @return The fully qualified class name of sourceActivity's parent activity or null if
 *         it was not specified
 */
public static String getParentActivityName(Context context, ComponentName componentName)
        throws NameNotFoundException {
    PackageManager pm = context.getPackageManager();
    ActivityInfo info = pm.getActivityInfo(componentName, PackageManager.GET_META_DATA);
    String parentActivity = IMPL.getParentActivityName(context, info);
    return parentActivity;
}