launcher修改--獲取widget信息(圖片,文字等)(源碼追蹤)

在launcher中,長按屏幕,圖掛了,我們在代碼裏面找一下:
首先,找到onLongClick(View v)方法,然後在這個方法中,有如下代碼:
  1. if (mWorkspace.allowLongPress()) {  
  2.             if (cellInfo.cell == null) {  
  3.                 if (cellInfo.valid) {  
  4.                     // User long pressed on empty space  
  5.                     mWorkspace.setAllowLongPress(false);  
  6.                     showAddDialog(cellInfo);  
  7.                 }  
  8.             } else {  
  9.                 if (!(cellInfo.cell instanceof Folder)) {  
  10.                     // User long pressed on an item  
  11.                     mWorkspace.startDrag(cellInfo);  
  12.                 }  
  13.             }  
  14.         }  
複製代碼
然後,繼續追蹤showAddDialog()方法:
  1. <pre name="code" class="java">private void showAddDialog(CellLayout.CellInfo cellInfo) {  
  2.         mAddItemCellInfo = cellInfo;  
  3.         mWaitingForResult = true;  
  4.         showDialog(DIALOG_CREATE_SHORTCUT);  
  5.     }  
複製代碼
繼續找showDialog,沒什麼發現,showDialog()方法是該類父類Activity()方法,攜帶參數DIALOG_CREATE_SHORTCUT,但是它最後是調用的Launcher.java的onCreateDialog(int id)這個方法:
  1. <pre name="code" class="html"><span style="font-family:monospace;"> @Override  
  2.     protected Dialog onCreateDialog(int id) {  
  3.         switch (id) {  
  4.             case DIALOG_CREATE_SHORTCUT:  
  5.                 return new CreateShortcut().createDialog();  
  6. </span>  
複製代碼
在這裏,使用了CreateShortcut.createDialog()方法,在源代碼的2708行:
  1. <pre name="code" class="html"><pre name="code" class="java">private class CreateShortcut implements DialogInterface.OnClickListener,  
  2. <pre name="code" class="java"><span style="font-family:monospace;">            DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,  
  3.             DialogInterface.OnShowListener {  
  4.   
  5.         private AddAdapter mAdapter;  
  6.   
  7.         Dialog createDialog() {  
  8.             mWaitingForResult = true;  
  9.   
  10.             mAdapter = new AddAdapter(Launcher.this);  
  11.   
  12.             final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);  
  13.             builder.setTitle(getString(R.string.menu_item_add_item));  
  14.             builder.setAdapter(mAdapter, this);  
  15.   
  16.             builder.setInverseBackgroundForced(true);  
  17.   
  18.             AlertDialog dialog = builder.create();  
  19.             dialog.setOnCancelListener(this);  
  20.             dialog.setOnDismissListener(this);  
  21.             dialog.setOnShowListener(this);  
  22.   
  23.             return dialog;  
  24.         }  
  25.   
  26.         public void onCancel(DialogInterface dialog) {  
  27.             mWaitingForResult = false;  
  28.             cleanup();  
  29.         }  
  30.   
  31.         public void onDismiss(DialogInterface dialog) {  
  32.             mWorkspace.unlock();  
  33.         }  
  34.   
  35.         private void cleanup() {  
  36.             mWorkspace.unlock();  
  37.             dismissDialog(DIALOG_CREATE_SHORTCUT);  
  38.         }  
  39.   
  40.         /**
  41.          * Handle the action clicked in the "Add to home" dialog.
  42.          */  
  43.         public void onClick(DialogInterface dialog, int which) {  
  44.             Resources res = getResources();  
  45.             cleanup();  
  46.   
  47.             switch (which) {  
  48.                 case AddAdapter.ITEM_SHORTCUT: {  
  49.                     // Insert extra item to handle picking application  
  50.                     pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);  
  51.                     break;  
  52.                 }  
  53.   
  54.                 case AddAdapter.ITEM_APPWIDGET: {  
  55.                     int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();  
  56.   
  57.                     Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);  
  58.                     pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);  
  59.                     // add the search widget  
  60.                     ArrayList<AppWidgetProviderInfo> customInfo =  
  61.                             new ArrayList<AppWidgetProviderInfo>();  
  62.                     AppWidgetProviderInfo info = new AppWidgetProviderInfo();  
  63.                     info.provider = new ComponentName(getPackageName(), "XXX.YYY");  
  64.                     info.label = getString(R.string.group_search);  
  65.                     info.icon = R.drawable.ic_search_widget;  
  66.                     customInfo.add(info);  
  67.                     pickIntent.putParcelableArrayListExtra(  
  68.                             AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);  
  69.                     ArrayList<Bundle> customExtras = new ArrayList<Bundle>();  
  70.                     Bundle b = new Bundle();  
  71.                     b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);  
  72.                     customExtras.add(b);  
  73.                     pickIntent.putParcelableArrayListExtra(  
  74.                             AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);  
  75.                     // start the pick activity  
  76.                     startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);  
  77.                     break;  
  78.                 }  
  79.   
  80.                 case AddAdapter.ITEM_LIVE_FOLDER: {  
  81.                     // Insert extra item to handle inserting folder  
  82.                     Bundle bundle = new Bundle();  
  83.   
  84.                     ArrayList<String> shortcutNames = new ArrayList<String>();  
  85.                     shortcutNames.add(res.getString(R.string.group_folder));  
  86.                     bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);  
  87.   
  88.                     ArrayList<ShortcutIconResource> shortcutIcons =  
  89.                             new ArrayList<ShortcutIconResource>();  
  90.                     shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,  
  91.                             R.drawable.ic_launcher_folder));  
  92.                     bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);  
  93.   
  94.                     Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);  
  95.                     pickIntent.putExtra(Intent.EXTRA_INTENT,  
  96.                             new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));  
  97.                     pickIntent.putExtra(Intent.EXTRA_TITLE,  
  98.                             getText(R.string.title_select_live_folder));  
  99.                     pickIntent.putExtras(bundle);  
  100.   
  101.                     startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);  
  102.                     break;  
  103.                 }  
  104.   
  105.                 case AddAdapter.ITEM_WALLPAPER: {  
  106.                     startWallpaper();  
  107.                     break;  
  108.                 }  
  109.   
  110.                 case AddAdapter.ITEM_ANYCUT: {  
  111.                     Intent anycutIntent=new Intent();  
  112.                     anycutIntent.setClass(Launcher.this, CustomShirtcutActivity.class);  
  113.                     startActivityForResult(anycutIntent, REQUEST_PICK_ANYCUT);  
  114.                     break;  
  115.                 }  
  116.             }  
  117.         }  
  118.   
  119.         public void onShow(DialogInterface dialog) {  
  120.             mWorkspace.lock();  
  121.         }  
  122.     }  
  123. </span>  
複製代碼
我們可以在CreateShortcut類中onclick()方法裏面找到一下代碼(這裏是2.2的代碼):
  1. <pre name="code" class="html"><pre name="code" class="java"><pre name="code" class="html"><span style="font-family:monospace;">case AddAdapter.ITEM_APPWIDGET: {  
  2.                     int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();  
  3.   
  4.                     Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);  
  5.                     pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);  
  6.                     // add the search widget  
  7.                     ArrayList<AppWidgetProviderInfo> customInfo =  
  8.                             new ArrayList<AppWidgetProviderInfo>();  
  9.                     AppWidgetProviderInfo info = new AppWidgetProviderInfo();  
  10.                     info.provider = new ComponentName(getPackageName(), "XXX.YYY");  
  11.                     info.label = getString(R.string.group_search);  
  12.                     info.icon = R.drawable.ic_search_widget;  
  13.                     customInfo.add(info);  
  14.                     pickIntent.putParcelableArrayListExtra(  
  15.                             AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);  
  16.                     ArrayList<Bundle> customExtras = new ArrayList<Bundle>();  
  17.                     Bundle b = new Bundle();  
  18.                     b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);  
  19.                     customExtras.add(b);  
  20.                     pickIntent.putParcelableArrayListExtra(  
  21.                             AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);  
  22.                     // start the pick activity  
  23.                     startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);  
  24.                     break;  
  25.                 }</span>  
複製代碼
這裏是2.3的代碼:
  1. case AddAdapter.ITEM_APPWIDGET: {  
  2.                     int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();  
  3.   
  4.                     Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);  
  5.                     pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);  
  6.                     // start the pick activity  
  7.                     startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);  
  8.                     break;  
  9.                 }  
複製代碼
在這裏又是如何實現的呢?我麼看到了startActivityForResult()方法,它實際上是運行了package/apps/Settings/src/com/android/settings/AppWidgetPickActivity.java
我們來看下代碼中是如何獲取widget信息的:
  1. <pre name="code" class="html"><pre name="code" class="java"><pre name="code" class="html"><pre name="code" class="html">void putAppWidgetItems(List<AppWidgetProviderInfo> appWidgets,  
  2.             List<Bundle> customExtras, List<PickAdapter.Item> items) {  
  3.         if (appWidgets == null) return;  
  4.         final int size = appWidgets.size();  
  5.         for (int i = 0; i < size; i++) {  
  6.             AppWidgetProviderInfo info = appWidgets.get(i);  
  7.                     Drawable icon = null;  
  8.             CharSequence label = info.label;  
  9.             if (info.icon != 0) {  
  10.                 icon = mPackageManager.getDrawable(info.provider.getPackageName(), info.icon, null);  
  11.                 if (icon == null) {  
  12.                     Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)  
  13.                             + " for provider: " + info.provider);  
  14.                 }  
  15.             }            
  16.             PickAdapter.Item item = new PickAdapter.Item(this, label, icon);            
  17.             item.packageName = info.provider.getPackageName();  
  18.             item.className = info.provider.getClassName();            
  19.             if (customExtras != null) {  
  20.                 item.extras = customExtras.get(i);  
  21.             }            
  22.             items.add(item);  
  23.         }  
複製代碼
和方法:
  1. <pre name="code" class="html"><pre name="code" class="java"><pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html">@Override  
  2.     protected List<PickAdapter.Item> getItems() {  
  3.         List<PickAdapter.Item> items = new ArrayList<PickAdapter.Item>();  
  4.          
  5.         putInstalledAppWidgets(items);  
  6.         putCustomAppWidgets(items);  
  7.          
  8.         // Sort all items together by label  
  9.         Collections.sort(items, new Comparator<PickAdapter.Item>() {  
  10.                 Collator mCollator = Collator.getInstance();  
  11.                 public int compare(PickAdapter.Item lhs, PickAdapter.Item rhs) {  
  12.                     return mCollator.compare(lhs.label, rhs.label);  
  13.                 }  
  14.             });  
  15.    return items;  
  16.     }  
複製代碼
下面是我自己寫的獲取widget的圖標:
  1. <pre name="code" class="html"><pre name="code" class="java"><pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html">List<AppWidgetProviderInfo> mAppinfoList = mAppWidgetManager.getInstalledProviders();  
  2.             List<Drawable> mIconList=new ArrayList();  
  3.             for(AppWidgetProviderInfo appWidgetInfo:mAppinfoList)  
  4.             {  
  5.                 Drawable icon = null;  
  6.                 PackageManager packageManager = getPackageManager();  
  7.                 icon = packageManager.getDrawable(appWidgetInfo.provider.getPackageName(), appWidgetInfo.icon, null);      
  8.                 mIconList.add(icon);  
  9.             }   
複製代碼
在這段代碼裏,能獲取widget的列表。
不知道是何種原因,使用以下代碼:
  1. <pre name="code" class="java">List<AppWidgetProviderInfo> mAppinfoList = mAppWidgetManager.getInstalledProviders();  
  2. for(AppWidgetProviderInfo appWidgetInfo:mAppinfoList)  
  3.         {  
  4.             mThumbs.add(appWidgetInfo.icon);  
  5.         }  
複製代碼



http://www.apkbus.com/android-18967-1-1.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章