Example usage for android.content.pm PackageManager getApplicationIcon

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

Introduction

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

Prototype

public abstract Drawable getApplicationIcon(String packageName) throws NameNotFoundException;

Source Link

Document

Retrieve the icon associated with an application.

Usage

From source file:com.harshad.linconnectclient.NotificationUtilities.java

public static boolean sendData(Context c, Notification n, String packageName) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);

    ConnectivityManager connManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    // Check Wifi state, whether notifications are enabled globally, and
    // whether notifications are enabled for specific application
    if (prefs.getBoolean("pref_toggle", true) && prefs.getBoolean(packageName, true) && mWifi.isConnected()) {
        String ip = prefs.getString("pref_ip", "0.0.0.0:9090");

        // Magically extract text from notification
        ArrayList<String> notificationData = NotificationUtilities.getNotificationText(n);

        // Use PackageManager to get application name and icon
        final PackageManager pm = c.getPackageManager();
        ApplicationInfo ai;/*w w w .j av a 2 s.c  om*/
        try {
            ai = pm.getApplicationInfo(packageName, 0);
        } catch (final NameNotFoundException e) {
            ai = null;
        }

        String notificationBody = "";
        String notificationHeader = "";
        // Create header and body of notification
        if (notificationData.size() > 0) {
            notificationHeader = notificationData.get(0);
            if (notificationData.size() > 1) {
                notificationBody = notificationData.get(1);
            }
        } else {
            return false;
        }

        for (int i = 2; i < notificationData.size(); i++) {
            notificationBody += "\n" + notificationData.get(i);
        }

        // Append application name to body
        if (pm.getApplicationLabel(ai) != null) {
            if (notificationBody.isEmpty()) {
                notificationBody = "via " + pm.getApplicationLabel(ai);
            } else {
                notificationBody += " (via " + pm.getApplicationLabel(ai) + ")";
            }
        }

        // Setup HTTP request
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        // If the notification contains an icon, use it
        if (n.largeIcon != null) {
            entity.addPart("notificon",
                    new InputStreamBody(ImageUtilities.bitmapToInputStream(n.largeIcon), "drawable.png"));
        }
        // Otherwise, use the application's icon
        else {
            entity.addPart("notificon",
                    new InputStreamBody(
                            ImageUtilities.bitmapToInputStream(
                                    ImageUtilities.drawableToBitmap(pm.getApplicationIcon(ai))),
                            "drawable.png"));
        }

        HttpPost post = new HttpPost("http://" + ip + "/notif");
        post.setEntity(entity);

        try {
            post.addHeader("notifheader", Base64.encodeToString(notificationHeader.getBytes("UTF-8"),
                    Base64.URL_SAFE | Base64.NO_WRAP));
            post.addHeader("notifdescription", Base64.encodeToString(notificationBody.getBytes("UTF-8"),
                    Base64.URL_SAFE | Base64.NO_WRAP));
        } catch (UnsupportedEncodingException e) {
            post.addHeader("notifheader",
                    Base64.encodeToString(notificationHeader.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP));
            post.addHeader("notifdescription",
                    Base64.encodeToString(notificationBody.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP));
        }

        // Send HTTP request
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        try {
            response = client.execute(post);
            String html = EntityUtils.toString(response.getEntity());
            if (html.contains("true")) {
                return true;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    return false;
}

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;/*from   w  w w  . j  ava 2s.c  o  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:info.guardianproject.pixelknot.PixelKnotActivity.java

@Override
public List<TrustedShareActivity> getTrustedShareActivities() {
    if (trusted_share_activities == null) {
        trusted_share_activities = new Vector<TrustedShareActivity>();

        Intent intent = new Intent(Intent.ACTION_SEND).setType("image/jpeg");

        PackageManager pm = getPackageManager();
        for (ResolveInfo ri : pm.queryIntentActivities(intent, 0)) {
            if (Arrays.asList(Image.TRUSTED_SHARE_ACTIVITIES)
                    .contains(Image.Activities.get(ri.activityInfo.packageName))) {
                try {
                    ApplicationInfo ai = pm.getApplicationInfo(ri.activityInfo.packageName, 0);
                    TrustedShareActivity tsa = new TrustedShareActivity(
                            Image.Activities.get(ri.activityInfo.packageName), ri.activityInfo.packageName);

                    tsa.setIcon(pm.getApplicationIcon(ai));
                    tsa.createView();/*from ww w.  jav  a  2  s .  com*/
                    tsa.setIntent();

                    trusted_share_activities.add(tsa);

                } catch (PackageManager.NameNotFoundException e) {
                    Log.e(Logger.UI, e.toString());
                    e.printStackTrace();
                    continue;
                }
            }
        }
    }

    return trusted_share_activities;
}

From source file:com.linkbubble.Settings.java

public Drawable getConsumeBubbleIcon(Constant.BubbleAction action, boolean whiteShareIcon) {
    PackageManager packageManager = mContext.getPackageManager();
    try {/*from w ww. ja  v  a 2s  .c o  m*/
        String packageName = getConsumeBubblePackageName(action);
        String name = getConsumeBubbleActivityClassName(action);
        if (packageName != null && name != null) {
            if (name.equals(Constant.SHARE_PICKER_NAME)) {
                return mContext.getResources().getDrawable(
                        whiteShareIcon ? R.drawable.ic_share_white_24dp : R.drawable.ic_share_grey600_24dp);
            }
            ComponentName componentName = new ComponentName(packageName, name);
            return packageManager.getActivityIcon(componentName);
        } else if (packageName != null) {
            // Try rendering the icon if we only have a packageName.
            ApplicationInfo app = packageManager.getApplicationInfo(packageName, 0);
            Drawable icon = packageManager.getApplicationIcon(app);
            return icon;
        }
    } catch (OutOfMemoryError ex) {
    } catch (PackageManager.NameNotFoundException e) {
    }
    return mContext.getResources().getDrawable(R.drawable.ic_launcher);
}

From source file:com.android.calendar.EventInfoFragment.java

private void updateCustomAppButton() {
    buttonSetup: {// w  w  w  .  j av  a2  s.  co  m
        final Button launchButton = (Button) mView.findViewById(R.id.launch_custom_app_button);
        if (launchButton == null)
            break buttonSetup;

        final String customAppPackage = mEventCursor.getString(EVENT_INDEX_CUSTOM_APP_PACKAGE);
        final String customAppUri = mEventCursor.getString(EVENT_INDEX_CUSTOM_APP_URI);

        if (TextUtils.isEmpty(customAppPackage) || TextUtils.isEmpty(customAppUri))
            break buttonSetup;

        PackageManager pm = mContext.getPackageManager();
        if (pm == null)
            break buttonSetup;

        ApplicationInfo info;
        try {
            info = pm.getApplicationInfo(customAppPackage, 0);
            if (info == null)
                break buttonSetup;
        } catch (NameNotFoundException e) {
            break buttonSetup;
        }

        Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, mEventId);
        final Intent intent = new Intent(CalendarContract.ACTION_HANDLE_CUSTOM_EVENT, uri);
        intent.setPackage(customAppPackage);
        intent.putExtra(CalendarContract.EXTRA_CUSTOM_APP_URI, customAppUri);
        intent.putExtra(EXTRA_EVENT_BEGIN_TIME, mStartMillis);

        // See if we have a taker for our intent
        if (pm.resolveActivity(intent, 0) == null)
            break buttonSetup;

        Drawable icon = pm.getApplicationIcon(info);
        if (icon != null) {

            Drawable[] d = launchButton.getCompoundDrawables();
            icon.setBounds(0, 0, mCustomAppIconSize, mCustomAppIconSize);
            launchButton.setCompoundDrawables(icon, d[1], d[2], d[3]);
        }

        CharSequence label = pm.getApplicationLabel(info);
        if (label != null && label.length() != 0) {
            launchButton.setText(label);
        } else if (icon == null) {
            // No icon && no label. Hide button?
            break buttonSetup;
        }

        // Launch custom app
        launchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    startActivityForResult(intent, 0);
                } catch (ActivityNotFoundException e) {
                    // Shouldn't happen as we checked it already
                    setVisibilityCommon(mView, R.id.launch_custom_app_container, View.GONE);
                }
            }
        });

        setVisibilityCommon(mView, R.id.launch_custom_app_container, View.VISIBLE);
        return;

    }

    setVisibilityCommon(mView, R.id.launch_custom_app_container, View.GONE);
    return;
}