Example usage for android.content.pm PackageManager getActivityInfo

List of usage examples for android.content.pm PackageManager getActivityInfo

Introduction

In this page you can find the example usage for android.content.pm PackageManager getActivityInfo.

Prototype

public abstract ActivityInfo getActivityInfo(ComponentName component, @ComponentInfoFlags int flags)
        throws NameNotFoundException;

Source Link

Document

Retrieve all of the information we know about a particular activity class.

Usage

From source file:com.andernity.launcher2.InstallShortcutReceiver.java

public void onReceive(Context context, Intent data) {
    if (!ACTION_INSTALL_SHORTCUT.equals(data.getAction())) {
        return;/*from w w  w . j  av a2s .c o  m*/
    }

    Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
    if (intent == null) {
        return;
    }
    // This name is only used for comparisons and notifications, so fall back to activity name
    // if not supplied
    String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
    if (name == null) {
        try {
            PackageManager pm = context.getPackageManager();
            ActivityInfo info = pm.getActivityInfo(intent.getComponent(), 0);
            name = info.loadLabel(pm).toString();
        } catch (PackageManager.NameNotFoundException nnfe) {
            return;
        }
    }
    Bitmap icon = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
    Intent.ShortcutIconResource iconResource = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);

    // Queue the item up for adding if launcher has not loaded properly yet
    boolean launcherNotLoaded = LauncherModel.getCellCountX() <= 0 || LauncherModel.getCellCountY() <= 0;

    PendingInstallShortcutInfo info = new PendingInstallShortcutInfo(data, name, intent);
    info.icon = icon;
    info.iconResource = iconResource;
    if (mUseInstallQueue || launcherNotLoaded) {
        String spKey = LauncherApplication.getSharedPreferencesKey();
        SharedPreferences sp = context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
        addToInstallQueue(sp, info);
    } else {
        processInstallShortcut(context, info);
    }
}

From source file:com.ddj.launcher2.InstallShortcutReceiver.java

public void onReceive(Context context, Intent data) {
    if (!ACTION_INSTALL_SHORTCUT.equals(data.getAction())) {
        return;/*from   w  w w .  j  av  a 2 s .co  m*/
    }

    Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
    if (intent == null) {
        return;
    }
    // This name is only used for comparisons and notifications, so fall back to activity name
    // if not supplied
    String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
    if (name == null) {
        try {
            PackageManager pm = context.getPackageManager();
            ActivityInfo info = pm.getActivityInfo(intent.getComponent(), 0);
            name = info.loadLabel(pm).toString();
        } catch (PackageManager.NameNotFoundException nnfe) {
            return;
        }
    }
    Bitmap icon = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
    Intent.ShortcutIconResource iconResource = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);

    // Queue the item up for adding if launcher has not loaded properly yet
    boolean launcherNotLoaded = LauncherModel.getCellCountX() <= 0 || LauncherModel.getCellCountY() <= 0;

    PendingInstallShortcutInfo info = new PendingInstallShortcutInfo(data, name, intent);
    info.icon = icon;
    info.iconResource = iconResource;
    if (mUseInstallQueue || launcherNotLoaded) {
        String spKey = LauncherUtil.getSharedPreferencesKey();
        SharedPreferences sp = context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
        addToInstallQueue(sp, info);
    } else {
        processInstallShortcut(context, info);
    }
}

From source file:com.adwhirl.AdWhirlLayout.java

protected String getAdWhirlKey(Context context) {
    final String packageName = context.getPackageName();
    final String activityName = context.getClass().getName();
    final PackageManager pm = context.getPackageManager();
    Bundle bundle = null;/*from w  w  w .  ja v  a  2  s. co  m*/
    // Attempts to retrieve Activity-specific AdWhirl key first. If not
    // found, retrieve Application-wide AdWhirl key.
    try {
        ActivityInfo activityInfo = pm.getActivityInfo(new ComponentName(packageName, activityName),
                PackageManager.GET_META_DATA);
        bundle = activityInfo.metaData;
        if (bundle != null) {
            return bundle.getString(AdWhirlLayout.ADWHIRL_KEY);
        }
    } catch (NameNotFoundException exception) {
        // Activity cannot be found. Shouldn't be here.
        return null;
    }

    try {
        ApplicationInfo appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
        bundle = appInfo.metaData;
        if (bundle != null) {
            return bundle.getString(AdWhirlLayout.ADWHIRL_KEY);
        }
    } catch (NameNotFoundException exception) {
        // Application cannot be found. Shouldn't be here.
        return null;
    }
    return null;
}

From source file:net.lp.actionbarpoirot.helpers.ActivityHelper.java

/**
 * Invoke "home" action/*from ww w .  ja v  a  2 s  .c om*/
 */
public void goHome() {
    // Automatically handle hierarchical Up navigation if the proper
    // metadata is available.
    Intent upIntent = DualNavUtils.getParentActivityIntent(mActivity);
    if (upIntent != null) {
        final PackageManager pm = mActivity.getPackageManager();
        ActivityInfo ai = null;
        try {
            ai = pm.getActivityInfo(mActivity.getComponentName(), 0);
        } catch (NameNotFoundException e) {
            // nothing
        }
        if (ai != null && ai.taskAffinity == null) {
            // Activities with a null affinity are special; they really
            // shouldn't
            // specify a parent activity intent in the first place. Just
            // finish
            // the current activity and call it a day.
            mActivity.finish();
        } else if (DualNavUtils.shouldUpRecreateTask(mActivity, upIntent)) {
            // This activity is NOT part of this app's task (was launched
            // from foreign app), so create a new task
            // when navigating up, with a synthesized back stack.
            DualTaskStackBuilder b = DualTaskStackBuilder.create(mActivity);
            // Add all of this activity's parents to the back stack
            b.addNextIntentWithParentStack(upIntent);
            // Navigate up to the closest parent
            b.startActivities();

            if (UiUtilities.hasJellyBeanOrUp()) {
                finishAffinity();
            }
        } else {
            // This activity is part of this app's task, so simply
            // navigate up to the logical parent activity.

            // If up intent is complicated, ask the activity, otherwise just use the static up intent.
            if (((ActivityHelperUser) mActivity).hasComplicatedUpIntent()) {
                upIntent = ((ActivityHelperUser) mActivity).getComplicatedUpIntent();
            }

            final boolean foundActivityInHistory = DualNavUtils.navigateUpTo(mActivity, upIntent);
            if (!foundActivityInHistory) {
                mActivity.startActivity(upIntent);
            }
        }
    } else {
        // Probably has no PARENT_ACTIVITY set for this form factor. So
        // activity might be used from multiple places. So just go back.
        if (UiUtilities.hasHoneycombOrUp()) {
            popBackStack();
        } else {
            mActivity.finish();// never really called
        }
    }
}

From source file:com.actionbarsherlock.internal.app.ActionBarHandlerWatson.java

@Override
protected void performAttach() {
    getActivity().requestWindowFeature(Window.FEATURE_NO_TITLE);
    setActivityContentView(R.layout.actionbarwatson_wrapper);

    mActionBar = (ActionBarWatson) getActivity().findViewById(R.id.actionbarwatson);
    mContentView = (FrameLayout) getActivity().findViewById(R.id.actionbarsherlock_content);

    final MenuItemImpl homeMenuItem = getHomeMenuItem();
    final ActionBarWatson.Item homeItem = mActionBar.getHomeItem();
    final WatsonItemViewWrapper homeWrapper = new WatsonItemViewWrapper(homeItem);
    homeWrapper.initialize(homeMenuItem, MenuBuilder.TYPE_WATSON);
    homeMenuItem.setItemView(MenuBuilder.TYPE_WATSON, homeWrapper);

    final PackageManager pm = getActivity().getPackageManager();
    final ApplicationInfo appInfo = getActivity().getApplicationInfo();
    ActivityInfo actInfo = null;/*w ww .  ja v a  2 s .  co m*/
    try {
        actInfo = pm.getActivityInfo(getActivity().getComponentName(), PackageManager.GET_ACTIVITIES);
    } catch (NameNotFoundException e) {
    }

    if ((actInfo != null) && (actInfo.labelRes != 0)) {
        //Load label string resource from the activity entry
        mActionBar.setTitle(actInfo.labelRes);
    } else if (mActionBar.getTitle() == null) {
        //No activity label string resource and none in theme
        mActionBar.setTitle(actInfo.loadLabel(pm));
    }

    if ((actInfo != null) && (actInfo.icon != 0)) {
        //Load the icon from the activity entry
        homeItem.setIcon(actInfo.icon);
    } else if (homeItem.getIcon() == null) {
        //No activity icon and none in theme
        homeItem.setIcon(pm.getApplicationIcon(appInfo));
    }

    //XXX LOGO LOADING DOES NOT WORK
    //XXX SEE: http://stackoverflow.com/questions/6105504/load-activity-and-or-application-logo-programmatically-from-manifest
    //XXX SEE: https://groups.google.com/forum/#!topic/android-developers/UFR4l0ZwJWc
    //if ((actInfo != null) && (actInfo.logo != 0)) {
    //   //Load the logo from the activity entry
    //   homeItem.setLogo(actInfo.logo);
    //} else if ((homeItem.getLogo() == null) && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD)) {
    //   //No activity logo and none in theme
    //   homeItem.setLogo(appInfo.logo);
    //}
}

From source file:android.support.v7.widget.SuggestionsAdapter.java

/**
 * 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./*from w w  w  . j a  v  a 2 s .c  o m*/
 */
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;
}

From source file:com.apexlabs.alarm.AlarmService.java

private Intent getClockIntent() {
    PackageManager packageManager = getPackageManager();
    Intent alarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);

    // Verify clock implementation
    String clockImpls[][] = {//from w w w  .  j  a va  2  s .c o m
            { "HTC", "com.htc.android.worldclock", "com.htc.android.worldclock.WorldClockTabControl" },
            { "Standard", "com.android.deskclock", "com.android.deskclock.AlarmClock" },
            { "Froyo", "com.google.android.deskclock", "com.android.deskclock.DeskClock" },
            { "Motorola", "com.motorola.blur.alarmclock", "com.motorola.blur.alarmclock.AlarmClock" },
            { "Sony Ericsson", "com.sonyericsson.alarm", "com.sonyericsson.alarm.Alarm" }, { "Samsung",
                    "com.sec.android.app.clockpackage", "com.sec.android.app.clockpackage.ClockPackage" } };

    boolean foundClockImpl = false;

    for (int i = 0; i < clockImpls.length; i++) {
        String packageName = clockImpls[i][1];
        String className = clockImpls[i][2];
        try {
            ComponentName cn = new ComponentName(packageName, className);
            packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA);
            alarmClockIntent.setComponent(cn);
            foundClockImpl = true;
            break;
        } catch (NameNotFoundException e) {
            Log.d(TAG, "Alarm clock " + clockImpls[i][0] + " not found");
        }
    }

    if (foundClockImpl) {
        alarmClockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    }
    return alarmClockIntent;
}

From source file:com.android.incallui.widget.multiwaveview.GlowPadView.java

/**
 * Searches the given package for a resource to use to replace the Drawable on the
 * target with the given resource id//from   w  ww .  j  av a  2s .c o  m
 * @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(TAG, "Failed to swap drawable; " + component.flattenToShortString() + " not found", e);
        } catch (Resources.NotFoundException nfe) {
            Log.w(TAG, "Failed to swap drawable from " + component.flattenToShortString(), nfe);
        }
    }
    if (!replaced) {
        // Restore the original drawable
        replaceTargetDrawables(getContext().getResources(), existingResId, existingResId);
    }
    return replaced;
}

From source file:com.android.settings.HWSettings.java

/**
 * Switch to parent fragment and store the grand parent's info
 * @param className name of the activity wrapper for the parent fragment.
 *//*from w w  w.j ava  2  s  .  c o m*/
private void switchToParent(String className) {
    final ComponentName cn = new ComponentName(this, className);
    try {
        final PackageManager pm = getPackageManager();
        final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);

        if (parentInfo != null && parentInfo.metaData != null) {
            String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
            CharSequence fragmentTitle = parentInfo.loadLabel(pm);
            Header parentHeader = new Header();
            parentHeader.fragment = fragmentClass;
            parentHeader.title = fragmentTitle;
            mCurrentHeader = parentHeader;

            switchToHeaderLocal(parentHeader);
            highlightHeader(mTopLevelHeaderId);

            mParentHeader = new Header();
            mParentHeader.fragment = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
            mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
        }
    } catch (NameNotFoundException nnfe) {
        Log.w(LOG_TAG, "Could not find parent activity : " + className);
    }
}

From source file:com.android.launcher2.Launcher.java

private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
    try {/*from   www.j  a v  a  2 s  .c  om*/
        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) {
        // This can happen if the activity defines an invalid drawable
        Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() + " not found", e);
    } catch (Resources.NotFoundException nfe) {
        // This can happen if the activity defines an invalid drawable
        Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(), nfe);
    }
    return null;
}