Launcher modification-Get widget information (images, text, etc.) (source code tracking)

Source: Internet
Author: User
Tags collator

In launcher, long press the screen and the figure goes down. Let's find it in the Code:

First, find the onlongclick (view v) method, and then in this method, there is the following code:

if (mWorkspace.allowLongPress()) {            if (cellInfo.cell == null) {                if (cellInfo.valid) {                    // User long pressed on empty space                    mWorkspace.setAllowLongPress(false);                    showAddDialog(cellInfo);                }            } else {                if (!(cellInfo.cell instanceof Folder)) {                    // User long pressed on an item                    mWorkspace.startDrag(cellInfo);                }            }        }

Then, continue tracing the showadddialog () method:

private void showAddDialog(CellLayout.CellInfo cellInfo) {        mAddItemCellInfo = cellInfo;        mWaitingForResult = true;        showDialog(DIALOG_CREATE_SHORTCUT);    }

Continue to find showdialog. Nothing is found. The showdialog () method is the class parent class activity () method that carries the dialog_create_shortcut parameter, but it is the called launcher. java oncreatedialog (int id) method:

@Override    protected Dialog onCreateDialog(int id) {        switch (id) {            case DIALOG_CREATE_SHORTCUT:                return new CreateShortcut().createDialog();
Here, the createshortcut. createdialog () method is used, in the 2708 line of the source code:
Private class createshortcut implements dialoginterface. onclicklistener,
            DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,            DialogInterface.OnShowListener {        private AddAdapter mAdapter;        Dialog createDialog() {            mWaitingForResult = true;            mAdapter = new AddAdapter(Launcher.this);            final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);            builder.setTitle(getString(R.string.menu_item_add_item));            builder.setAdapter(mAdapter, this);            builder.setInverseBackgroundForced(true);            AlertDialog dialog = builder.create();            dialog.setOnCancelListener(this);            dialog.setOnDismissListener(this);            dialog.setOnShowListener(this);            return dialog;        }        public void onCancel(DialogInterface dialog) {            mWaitingForResult = false;            cleanup();        }        public void onDismiss(DialogInterface dialog) {            mWorkspace.unlock();        }        private void cleanup() {            mWorkspace.unlock();            dismissDialog(DIALOG_CREATE_SHORTCUT);        }        /**         * Handle the action clicked in the "Add to home" dialog.         */        public void onClick(DialogInterface dialog, int which) {            Resources res = getResources();            cleanup();            switch (which) {                case AddAdapter.ITEM_SHORTCUT: {                    // Insert extra item to handle picking application                    pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);                    break;                }                case AddAdapter.ITEM_APPWIDGET: {                    int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();                    Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);                    pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);                    // add the search widget                    ArrayList<AppWidgetProviderInfo> customInfo =                            new ArrayList<AppWidgetProviderInfo>();                    AppWidgetProviderInfo info = new AppWidgetProviderInfo();                    info.provider = new ComponentName(getPackageName(), "XXX.YYY");                    info.label = getString(R.string.group_search);                    info.icon = R.drawable.ic_search_widget;                    customInfo.add(info);                    pickIntent.putParcelableArrayListExtra(                            AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);                    ArrayList<Bundle> customExtras = new ArrayList<Bundle>();                    Bundle b = new Bundle();                    b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);                    customExtras.add(b);                    pickIntent.putParcelableArrayListExtra(                            AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);                    // start the pick activity                    startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);                    break;                }                case AddAdapter.ITEM_LIVE_FOLDER: {                    // Insert extra item to handle inserting folder                    Bundle bundle = new Bundle();                    ArrayList<String> shortcutNames = new ArrayList<String>();                    shortcutNames.add(res.getString(R.string.group_folder));                    bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);                    ArrayList<ShortcutIconResource> shortcutIcons =                            new ArrayList<ShortcutIconResource>();                    shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,                            R.drawable.ic_launcher_folder));                    bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);                    Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);                    pickIntent.putExtra(Intent.EXTRA_INTENT,                            new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));                    pickIntent.putExtra(Intent.EXTRA_TITLE,                            getText(R.string.title_select_live_folder));                    pickIntent.putExtras(bundle);                    startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);                    break;                }                case AddAdapter.ITEM_WALLPAPER: {                    startWallpaper();                    break;                }                case AddAdapter.ITEM_ANYCUT: {                Intent anycutIntent=new Intent();                anycutIntent.setClass(Launcher.this, CustomShirtcutActivity.class);                startActivityForResult(anycutIntent, REQUEST_PICK_ANYCUT);                    break;                }            }        }        public void onShow(DialogInterface dialog) {            mWorkspace.lock();        }    }

We can find the code in the onclick () method in the createshortcut class (here the code is 2.2 ):

case AddAdapter.ITEM_APPWIDGET: {                    int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();                    Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);                    pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);                    // add the search widget                    ArrayList<AppWidgetProviderInfo> customInfo =                            new ArrayList<AppWidgetProviderInfo>();                    AppWidgetProviderInfo info = new AppWidgetProviderInfo();                    info.provider = new ComponentName(getPackageName(), "XXX.YYY");                    info.label = getString(R.string.group_search);                    info.icon = R.drawable.ic_search_widget;                    customInfo.add(info);                    pickIntent.putParcelableArrayListExtra(                            AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);                    ArrayList<Bundle> customExtras = new ArrayList<Bundle>();                    Bundle b = new Bundle();                    b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);                    customExtras.add(b);                    pickIntent.putParcelableArrayListExtra(                            AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);                    // start the pick activity                    startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);                    break;                }
Here is the 2.3 code:
case AddAdapter.ITEM_APPWIDGET: {                    int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();                    Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);                    pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);                    // start the pick activity                    startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);                    break;                }

How is this implemented? I have seen the startactivityforresult () method. It actually runs package/apps/settings/src/COM/Android/settings/appwidgetpickactivity. java:

void putAppWidgetItems(List<AppWidgetProviderInfo> appWidgets,            List<Bundle> customExtras, List<PickAdapter.Item> items) {        if (appWidgets == null) return;        final int size = appWidgets.size();        for (int i = 0; i < size; i++) {            AppWidgetProviderInfo info = appWidgets.get(i);                    Drawable icon = null;            CharSequence label = info.label;            if (info.icon != 0) {                icon = mPackageManager.getDrawable(info.provider.getPackageName(), info.icon, null);                if (icon == null) {                    Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)                            + " for provider: " + info.provider);                }            }                      PickAdapter.Item item = new PickAdapter.Item(this, label, icon);                       item.packageName = info.provider.getPackageName();            item.className = info.provider.getClassName();                       if (customExtras != null) {                item.extras = customExtras.get(i);            }                      items.add(item);        }
And method:
@Override    protected List<PickAdapter.Item> getItems() {        List<PickAdapter.Item> items = new ArrayList<PickAdapter.Item>();                putInstalledAppWidgets(items);        putCustomAppWidgets(items);                // Sort all items together by label        Collections.sort(items, new Comparator<PickAdapter.Item>() {                Collator mCollator = Collator.getInstance();                public int compare(PickAdapter.Item lhs, PickAdapter.Item rhs) {                    return mCollator.compare(lhs.label, rhs.label);                }            });   return items;    }
Below is my own widget acquisition icon:
List<AppWidgetProviderInfo> mAppinfoList = mAppWidgetManager.getInstalledProviders();            List<Drawable> mIconList=new ArrayList();            for(AppWidgetProviderInfo appWidgetInfo:mAppinfoList)            {                Drawable icon = null;                PackageManager packageManager = getPackageManager();                icon = packageManager.getDrawable(appWidgetInfo.provider.getPackageName(), appWidgetInfo.icon, null);                    mIconList.add(icon);            } 

In this code, you can obtain the widget list. If you do not know why, use the following code:

List<AppWidgetProviderInfo> mAppinfoList = mAppWidgetManager.getInstalledProviders();for(AppWidgetProviderInfo appWidgetInfo:mAppinfoList)        {            mThumbs.add(appWidgetInfo.icon);        }

However, you cannot obtain the widget icon. Instead, you can view other widgets.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.