AppWidget的总结一

玩过android appWidget的人都知道若想把一个widget放到桌面上,是通过长按screen的空白处或者按menu键-->add选项,这两种方式都会出现一个Add to Home Screen的对话框,再点击Widgets,这样system里面所有的widget都显示出来供你选择,那我们从代码看看:

 

1,长按Home之后显示Add to Home Screen的对话框是在Launcher.java实现的

public boolean onLongClick(View v) { if (mDesktopLocked) { return false; } if (!(v instanceof CellLayout)) { v = (View) v.getParent(); } CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag(); // This happens when long clicking an item with the dpad/trackball if (cellInfo == null) { return true; } 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); } } } return true; } 

showAddDialog(cellInfo);

private void showAddDialog(CellLayout.CellInfo cellInfo) { mAddItemCellInfo = cellInfo; mWaitingForResult = true; showDialog(DIALOG_CREATE_SHORTCUT); } private class CreateShortcut implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener { 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); return dialog; } public void onCancel(DialogInterface dialog) { mWaitingForResult = false; cleanup(); } 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 Bundle bundle = new Bundle(); ArrayList shortcutNames = new ArrayList(); shortcutNames.add(res.getString(R.string.group_applications)); bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames); ArrayList shortcutIcons = new ArrayList(); shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this, R.drawable.ic_launcher_application)); bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons); Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT)); pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut)); pickIntent.putExtras(bundle); startActivityForResult(pickIntent, REQUEST_PICK_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 customInfo = new ArrayList(); 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 customExtras = new ArrayList(); 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 shortcutNames = new ArrayList(); shortcutNames.add(res.getString(R.string.group_folder)); bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames); ArrayList shortcutIcons = new ArrayList(); 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; } } } } 

  Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);发出了ACTION_APPWIDGET_PICK这样的Intent,那对应的Activity就会被呼叫起来,这个appWidgetPicker是在Setting中的实现的。

Setting的AndrdoidManifest.xml

 

 

看看AppWidgetPickActivity

/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.settings; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProviderInfo; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Parcelable; import android.util.Log; import java.text.Collator; import java.util.List; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; /** * Displays a list of {@link AppWidgetProviderInfo} widgets, along with any * injected special widgets specified through * {@link AppWidgetManager#EXTRA_CUSTOM_INFO} and * {@link AppWidgetManager#EXTRA_CUSTOM_EXTRAS}. *

* When an installed {@link AppWidgetProviderInfo} is selected, this activity * will bind it to the given {@link AppWidgetManager#EXTRA_APPWIDGET_ID}, * otherwise it will return the requested extras. */ public class AppWidgetPickActivity extends ActivityPicker { private static final String TAG = "AppWidgetPickActivity"; private static final boolean LOGD = false; private PackageManager mPackageManager; private AppWidgetManager mAppWidgetManager; /** * The allocated {@link AppWidgetManager#EXTRA_APPWIDGET_ID} that this * activity is binding. */ private int mAppWidgetId; @Override public void onCreate(Bundle icicle) { mPackageManager = getPackageManager(); mAppWidgetManager = AppWidgetManager.getInstance(this); super.onCreate(icicle); // Set default return data setResultData(RESULT_CANCELED, null); // Read the appWidgetId passed our direction, otherwise bail if not found final Intent intent = getIntent(); if (intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) { mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); } else { finish(); } } /** * Create list entries for any custom widgets requested through * {@link AppWidgetManager#EXTRA_CUSTOM_INFO}. */ void putCustomAppWidgets(List items) { final Bundle extras = getIntent().getExtras(); // get and validate the extras they gave us ArrayList customInfo = null; ArrayList customExtras = null; try_custom_items: { customInfo = extras.getParcelableArrayList(AppWidgetManager.EXTRA_CUSTOM_INFO); if (customInfo == null || customInfo.size() == 0) { Log.i(TAG, "EXTRA_CUSTOM_INFO not present."); break try_custom_items; } int customInfoSize = customInfo.size(); for (int i=0; i appWidgets, List customExtras, List items) { if (appWidgets == null) return; final int size = appWidgets.size(); for (int i = 0; i < size; i++) { AppWidgetProviderInfo info = appWidgets.get(i); CharSequence label = info.label; Drawable icon = null; 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); } } /** * Build and return list of items to be shown in dialog. This will mix both * installed {@link AppWidgetProviderInfo} and those provided through * {@link AppWidgetManager#EXTRA_CUSTOM_INFO}, sorting them alphabetically. */ @Override protected List getItems() { List items = new ArrayList(); putInstalledAppWidgets(items); putCustomAppWidgets(items); // Sort all items together by label Collections.sort(items, new Comparator() { Collator mCollator = Collator.getInstance(); public int compare(PickAdapter.Item lhs, PickAdapter.Item rhs) { return mCollator.compare(lhs.label, rhs.label); } }); return items; } /** * Create list entries for installed {@link AppWidgetProviderInfo} widgets. */ void putInstalledAppWidgets(List items) { List installed = mAppWidgetManager.getInstalledProviders(); putAppWidgetItems(installed, null, items); } /** * Convenience method for setting the result code and intent. This method * correctly injects the {@link AppWidgetManager#EXTRA_APPWIDGET_ID} that * most hosts expect returned. */ void setResultData(int code, Intent intent) { Intent result = intent != null ? intent : new Intent(); result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); setResult(code, result); } } 

你可能感兴趣的:(android,apps,null,dialog,android,search,permissions,list)