Search in sources :

Example 11 with ComponentName

use of android.content.ComponentName in project ADWLauncher2 by boombuler.

the class CustomShirtcutActivity method loadFromShortcutInfo.

private void loadFromShortcutInfo(IconItemInfo info) {
    if (info == null)
        return;
    edLabel.setText(info.getTitle(mIconCache));
    if (info instanceof ShortcutInfo)
        mIntent = ((ShortcutInfo) info).intent;
    else {
        btPickActivity.setVisibility(View.GONE);
        tvHead.setText(getString(R.string.shirtcut_header_folder));
    }
    btPickIcon.setImageBitmap(info.getIcon(mIconCache));
    btPickIcon.setEnabled(true);
    btOk.setEnabled(true);
    if (mIntent != null) {
        ComponentName component = mIntent.getComponent();
        if (component != null) {
            if (component.getClassName().equals(RunActionActivity.class.getName()) && mIntent.getAction().equals(RunActionActivity.ACTION_LAUNCHERACTION)) {
            } else {
                ActivityInfo activityInfo = null;
                try {
                    activityInfo = mPackageManager.getActivityInfo(component, 0);
                } catch (NameNotFoundException e) {
                }
                String title = null;
                if (activityInfo != null) {
                    title = activityInfo.loadLabel(mPackageManager).toString();
                    if (title == null) {
                        title = activityInfo.name;
                    }
                    btPickActivity.setText(title);
                }
            }
        } else
            btPickActivity.setText(info.getTitle(mIconCache));
    }
}
Also used : ActivityInfo(android.content.pm.ActivityInfo) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) ComponentName(android.content.ComponentName)

Example 12 with ComponentName

use of android.content.ComponentName in project ADWLauncher2 by boombuler.

the class CustomShirtcutActivity method onActivityResult.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        switch(requestCode) {
            case PICK_CUSTOM_PICTURE:
                mBitmap = (Bitmap) data.getParcelableExtra("data");
                if (mBitmap != null) {
                    if (mBitmap.getWidth() > mIconSize)
                        mBitmap = Bitmap.createScaledBitmap(mBitmap, mIconSize, mIconSize, true);
                    btPickIcon.setImageBitmap(mBitmap);
                }
                break;
            case PICK_CUSTOM_ICON:
                Uri photoUri = data.getData();
                try {
                    InputStream is = getContentResolver().openInputStream(photoUri);
                    BitmapFactory.Options opts = new BitmapFactory.Options();
                    opts.inJustDecodeBounds = true;
                    BitmapFactory.decodeStream(is, null, opts);
                    BitmapFactory.Options ops2 = new BitmapFactory.Options();
                    int width = mIconSize;
                    float w = opts.outWidth;
                    //int scale = Math.round(w / width);
                    int scale = (int) (w / width);
                    ops2.inSampleSize = scale;
                    is = getContentResolver().openInputStream(photoUri);
                    mBitmap = BitmapFactory.decodeStream(is, null, ops2);
                    if (mBitmap != null) {
                        if (mBitmap.getWidth() > mIconSize)
                            mBitmap = Bitmap.createScaledBitmap(mBitmap, mIconSize, mIconSize, true);
                        btPickIcon.setImageBitmap(mBitmap);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case PICK_FROM_ICON_PACK:
                mBitmap = (Bitmap) data.getParcelableExtra("icon");
                if (mBitmap != null) {
                    if (mBitmap.getWidth() > mIconSize)
                        mBitmap = Bitmap.createScaledBitmap(mBitmap, mIconSize, mIconSize, true);
                    btPickIcon.setImageBitmap(mBitmap);
                }
                break;
            case PICK_STANDARD_MENU:
                String applicationName = getResources().getString(R.string.group_applications);
                String activitiesName = getResources().getString(R.string.shirtcuts_activity);
                String launcheractionsName = getResources().getString(R.string.launcher_actions);
                String shortcutName = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
                if (applicationName != null && applicationName.equals(shortcutName)) {
                    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
                    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                    Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
                    pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
                    startActivityForResult(pickIntent, PICK_STANDARD_APPLICATION);
                } else if (activitiesName != null && activitiesName.equals(shortcutName)) {
                    Intent picker = new Intent();
                    picker.setClass(this, ActivityPickerActivity.class);
                    startActivityForResult(picker, PICK_STANDARD_SHORTCUT);
                } else if (launcheractionsName != null && launcheractionsName.equals(shortcutName)) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle(getString(R.string.launcher_actions));
                    final ListAdapter adapter = LauncherActions.getInstance().getSelectActionAdapter();
                    builder.setAdapter(adapter, new Dialog.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            LauncherActions.Action action = (LauncherActions.Action) adapter.getItem(which);
                            Intent result = new Intent();
                            result.putExtra(Intent.EXTRA_SHORTCUT_NAME, action.getName());
                            result.putExtra(Intent.EXTRA_SHORTCUT_INTENT, LauncherActions.getInstance().getIntentForAction(action));
                            ShortcutIconResource iconResource = new ShortcutIconResource();
                            iconResource.packageName = CustomShirtcutActivity.this.getPackageName();
                            iconResource.resourceName = getResources().getResourceName(action.getIconResourceId());
                            result.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
                            onActivityResult(PICK_STANDARD_SHORTCUT, RESULT_OK, result);
                        }
                    });
                    builder.create().show();
                } else {
                    startActivityForResult(data, PICK_STANDARD_SHORTCUT);
                }
                break;
            case PICK_STANDARD_APPLICATION:
                if (mBitmap != null) {
                    mBitmap.recycle();
                    mBitmap = null;
                }
                ComponentName component = data.getComponent();
                ActivityInfo activityInfo = null;
                try {
                    activityInfo = mPackageManager.getActivityInfo(component, 0);
                } catch (NameNotFoundException e) {
                }
                String title = null;
                if (activityInfo != null) {
                    title = activityInfo.loadLabel(mPackageManager).toString();
                    if (title == null) {
                        title = activityInfo.name;
                    }
                    mIntent = data;
                    btPickActivity.setText(title);
                    mBitmap = null;
                    btPickIcon.setImageDrawable(activityInfo.loadIcon(mPackageManager));
                    btPickIcon.setEnabled(true);
                    btOk.setEnabled(true);
                    edLabel.setText(title);
                }
                break;
            case PICK_STANDARD_SHORTCUT:
                if (mBitmap != null) {
                    mBitmap.recycle();
                    mBitmap = null;
                }
                Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
                String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
                Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
                Drawable icon = null;
                if (bitmap != null) {
                    icon = new FastBitmapDrawable(Bitmap.createScaledBitmap(bitmap, mIconSize, mIconSize, true));
                    mBitmap = bitmap;
                } else {
                    Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
                    if (extra != null && extra instanceof ShortcutIconResource) {
                        try {
                            ShortcutIconResource iconResource = (ShortcutIconResource) extra;
                            Resources resources = mPackageManager.getResourcesForApplication(iconResource.packageName);
                            final int id = resources.getIdentifier(iconResource.resourceName, null, null);
                            icon = resources.getDrawable(id);
                            mBitmap = Utilities.createIconBitmap(icon, this);
                        } catch (Exception e) {
                        }
                    }
                }
                if (icon == null) {
                    icon = getPackageManager().getDefaultActivityIcon();
                }
                mIntent = intent;
                btPickActivity.setText(name);
                btPickIcon.setImageDrawable(icon);
                btPickIcon.setEnabled(true);
                btOk.setEnabled(true);
                edLabel.setText(name);
                break;
            default:
                break;
        }
    }
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) ShortcutIconResource(android.content.Intent.ShortcutIconResource) Uri(android.net.Uri) Bitmap(android.graphics.Bitmap) Dialog(android.app.Dialog) AlertDialog(android.app.AlertDialog) ComponentName(android.content.ComponentName) BitmapFactory(android.graphics.BitmapFactory) ListAdapter(android.widget.ListAdapter) ActivityInfo(android.content.pm.ActivityInfo) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) InputStream(java.io.InputStream) Drawable(android.graphics.drawable.Drawable) Intent(android.content.Intent) Parcelable(android.os.Parcelable) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) LauncherActions(org.adw.launcher2.actions.LauncherActions) Resources(android.content.res.Resources)

Example 13 with ComponentName

use of android.content.ComponentName in project ADWLauncher2 by boombuler.

the class Workspace method updateShortcuts.

void updateShortcuts(List<ShortcutInfo> apps) {
    final int count = getChildCount();
    for (int i = 0; i < count; i++) {
        final CellLayout layout = (CellLayout) getChildAt(i);
        int childCount = layout.getChildCount();
        for (int j = 0; j < childCount; j++) {
            final View view = layout.getChildAt(j);
            Object tag = view.getTag();
            if (tag instanceof ShortcutInfo) {
                ShortcutInfo info = (ShortcutInfo) tag;
                // We need to check for ACTION_MAIN otherwise getComponent() might
                // return null for some shortcuts (for instance, for shortcuts to
                // web pages.)
                final Intent intent = info.intent;
                final ComponentName name = intent.getComponent();
                if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
                    final int appCount = apps.size();
                    for (int k = 0; k < appCount; k++) {
                        IconItemInfo app = apps.get(k);
                        ComponentName cname = null;
                        if (app instanceof ShortcutInfo)
                            cname = ((ShortcutInfo) app).intent.getComponent();
                        if (name.equals(cname)) {
                            ((TextView) view).setText(info.getTitle(mIconCache));
                            ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(info.getIcon(mIconCache)), null, null);
                        }
                    }
                }
            }
        }
    }
}
Also used : Intent(android.content.Intent) ComponentName(android.content.ComponentName) TextView(android.widget.TextView) View(android.view.View) TextView(android.widget.TextView)

Example 14 with ComponentName

use of android.content.ComponentName in project ADWLauncher2 by boombuler.

the class Workspace method removeItems.

void removeItems(final ArrayList<ShortcutInfo> apps) {
    final int count = getChildCount();
    final PackageManager manager = getContext().getPackageManager();
    final AppWidgetManager widgets = AppWidgetManager.getInstance(getContext());
    final HashSet<String> packageNames = new HashSet<String>();
    final int appCount = apps.size();
    for (int i = 0; i < appCount; i++) {
        final ComponentName component = apps.get(i).intent.getComponent();
        if (component != null)
            packageNames.add(component.getPackageName());
    }
    for (int i = 0; i < count; i++) {
        final CellLayout layout = (CellLayout) getChildAt(i);
        // Avoid ANRs by treating each screen separately
        post(new Runnable() {

            public void run() {
                final ArrayList<View> childrenToRemove = new ArrayList<View>();
                childrenToRemove.clear();
                int childCount = layout.getChildCount();
                for (int j = 0; j < childCount; j++) {
                    final View view = layout.getChildAt(j);
                    Object tag = view.getTag();
                    if (tag instanceof ShortcutInfo) {
                        final ShortcutInfo info = (ShortcutInfo) tag;
                        final Intent intent = info.intent;
                        final ComponentName name = intent.getComponent();
                        if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
                            for (String packageName : packageNames) {
                                if (packageName.equals(name.getPackageName())) {
                                    mLauncher.getModel().deleteItemFromDatabase(mLauncher, info);
                                    childrenToRemove.add(view);
                                }
                            }
                        }
                    } else if (tag instanceof UserFolderInfo) {
                        final UserFolderInfo info = (UserFolderInfo) tag;
                        final ArrayList<ShortcutInfo> contents = info.contents;
                        final ArrayList<ShortcutInfo> toRemove = new ArrayList<ShortcutInfo>(1);
                        final int contentsCount = contents.size();
                        boolean removedFromFolder = false;
                        for (int k = 0; k < contentsCount; k++) {
                            final ShortcutInfo appInfo = contents.get(k);
                            final Intent intent = appInfo.intent;
                            final ComponentName name = intent.getComponent();
                            if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
                                for (String packageName : packageNames) {
                                    if (packageName.equals(name.getPackageName())) {
                                        toRemove.add(appInfo);
                                        mLauncher.getModel().deleteItemFromDatabase(mLauncher, appInfo);
                                        removedFromFolder = true;
                                    }
                                }
                            }
                        }
                        contents.removeAll(toRemove);
                        if (removedFromFolder) {
                            final Folder folder = getOpenFolder();
                            if (folder != null)
                                folder.notifyDataSetChanged();
                        }
                    } else if (tag instanceof LiveFolderInfo) {
                        final LiveFolderInfo info = (LiveFolderInfo) tag;
                        final Uri uri = info.uri;
                        final ProviderInfo providerInfo = manager.resolveContentProvider(uri.getAuthority(), 0);
                        if (providerInfo != null) {
                            for (String packageName : packageNames) {
                                if (packageName.equals(providerInfo.packageName)) {
                                    mLauncher.getModel().deleteItemFromDatabase(mLauncher, info);
                                    childrenToRemove.add(view);
                                }
                            }
                        }
                    } else if (tag instanceof LauncherAppWidgetInfo) {
                        final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) tag;
                        final AppWidgetProviderInfo provider = widgets.getAppWidgetInfo(info.appWidgetId);
                        if (provider != null) {
                            for (String packageName : packageNames) {
                                if (packageName.equals(provider.provider.getPackageName())) {
                                    mLauncher.getModel().deleteItemFromDatabase(mLauncher, info);
                                    childrenToRemove.add(view);
                                }
                            }
                        }
                    }
                }
                childCount = childrenToRemove.size();
                for (int j = 0; j < childCount; j++) {
                    View child = childrenToRemove.get(j);
                    layout.removeViewInLayout(child);
                    if (child instanceof DropTarget) {
                        mDragController.removeDropTarget((DropTarget) child);
                    }
                }
                if (childCount > 0) {
                    layout.requestLayout();
                    layout.invalidate();
                }
            }
        });
    }
}
Also used : ArrayList(java.util.ArrayList) Uri(android.net.Uri) PackageManager(android.content.pm.PackageManager) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) ProviderInfo(android.content.pm.ProviderInfo) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) ComponentName(android.content.ComponentName) HashSet(java.util.HashSet) AppWidgetManager(android.appwidget.AppWidgetManager) Intent(android.content.Intent) View(android.view.View) TextView(android.widget.TextView)

Example 15 with ComponentName

use of android.content.ComponentName in project ADWLauncher2 by boombuler.

the class Launcher method loadHotseats.

// Load the Intent templates from arrays.xml to populate the hotseats. For
// each Intent, if it resolves to a single app, use that as the launch
// intent & use that app's label as the contentDescription. Otherwise,
// retain the ResolveActivity so the user can pick an app.
private void loadHotseats() {
    if (mHotseatConfig == null) {
        mHotseatConfig = getResources().getStringArray(R.array.hotseats);
        if (mHotseatConfig.length > 0) {
            mHotseats = new Intent[mHotseatConfig.length];
            mHotseatLabels = new CharSequence[mHotseatConfig.length];
            mHotseatIcons = new Drawable[mHotseatConfig.length];
        } else {
            mHotseats = null;
            mHotseatIcons = null;
            mHotseatLabels = null;
        }
        TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
        for (int i = 0; i < mHotseatConfig.length; i++) {
            // load icon for this slot; currently unrelated to the actual activity
            try {
                mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
            } catch (ArrayIndexOutOfBoundsException ex) {
                Log.w(TAG, "Missing hotseat_icons array item #" + i);
                mHotseatIcons[i] = null;
            }
        }
        hotseatIconDrawables.recycle();
    }
    PackageManager pm = getPackageManager();
    for (int i = 0; i < mHotseatConfig.length; i++) {
        Intent intent = null;
        if (mHotseatConfig[i].equals("*BROWSER*")) {
            // magic value meaning "launch user's default web browser"
            // replace it with a generic web request so we can see if there is indeed a default
            String defaultUri = getString(R.string.default_browser_url);
            intent = new Intent(Intent.ACTION_VIEW, ((defaultUri != null) ? Uri.parse(defaultUri) : getDefaultBrowserUri())).addCategory(Intent.CATEGORY_BROWSABLE);
        // note: if the user launches this without a default set, she
        // will always be taken to the default URL above; this is
        // unavoidable as we must specify a valid URL in order for the
        // chooser to appear, and once the user selects something, that
        // URL is unavoidably sent to the chosen app.
        } else {
            try {
                intent = Intent.parseUri(mHotseatConfig[i], 0);
            } catch (java.net.URISyntaxException ex) {
                Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
            // bogus; leave intent=null
            }
        }
        if (intent == null) {
            mHotseats[i] = null;
            mHotseatLabels[i] = getText(R.string.activity_not_found);
            continue;
        }
        if (LOGD) {
            Log.d(TAG, "loadHotseats: hotseat " + i + " initial intent=[" + intent.toUri(Intent.URI_INTENT_SCHEME) + "]");
        }
        ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
        List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        if (LOGD) {
            Log.d(TAG, "Best match for intent: " + bestMatch);
            Log.d(TAG, "All matches: ");
            for (ResolveInfo ri : allMatches) {
                Log.d(TAG, "  --> " + ri);
            }
        }
        // did this resolve to a single app, or the resolver?
        if (allMatches.size() == 0 || bestMatch == null) {
            // can't find any activity to handle this. let's leave the
            // intent as-is and let Launcher show a toast when it fails
            // to launch.
            mHotseats[i] = intent;
            // set accessibility text to "Not installed"
            mHotseatLabels[i] = getText(R.string.activity_not_found);
        } else {
            boolean found = false;
            for (ResolveInfo ri : allMatches) {
                if (bestMatch.activityInfo.name.equals(ri.activityInfo.name) && bestMatch.activityInfo.applicationInfo.packageName.equals(ri.activityInfo.applicationInfo.packageName)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                if (LOGD)
                    Log.d(TAG, "Multiple options, no default yet");
                // the bestMatch is probably the ResolveActivity, meaning the
                // user has not yet selected a default
                // so: we'll keep the original intent for now
                mHotseats[i] = intent;
                // set the accessibility text to "Select shortcut"
                mHotseatLabels[i] = getText(R.string.title_select_shortcut);
            } else {
                // we have an app!
                // now reconstruct the intent to launch it through the front
                // door
                ComponentName com = new ComponentName(bestMatch.activityInfo.applicationInfo.packageName, bestMatch.activityInfo.name);
                mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
                // load the app label for accessibility
                mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
            }
        }
        if (LOGD) {
            Log.d(TAG, "loadHotseats: hotseat " + i + " final intent=[" + ((mHotseats[i] == null) ? "null" : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME)) + "] label=[" + mHotseatLabels[i] + "]");
        }
    }
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) PackageManager(android.content.pm.PackageManager) TypedArray(android.content.res.TypedArray) Intent(android.content.Intent) LauncherIntent(mobi.intuitit.android.content.LauncherIntent) ComponentName(android.content.ComponentName)

Aggregations

ComponentName (android.content.ComponentName)2548 Intent (android.content.Intent)959 ResolveInfo (android.content.pm.ResolveInfo)375 RemoteException (android.os.RemoteException)317 PackageManager (android.content.pm.PackageManager)269 PendingIntent (android.app.PendingIntent)252 ActivityInfo (android.content.pm.ActivityInfo)243 ArrayList (java.util.ArrayList)242 ShortcutInfo (android.content.pm.ShortcutInfo)152 Point (android.graphics.Point)145 Bundle (android.os.Bundle)139 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)132 IBinder (android.os.IBinder)128 IOException (java.io.IOException)125 ServiceConnection (android.content.ServiceConnection)96 ServiceInfo (android.content.pm.ServiceInfo)96 UserHandle (android.os.UserHandle)86 ArraySet (android.util.ArraySet)73 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)69 Uri (android.net.Uri)68