launcher修改--获取widget信息(图片,文字等)(源码追踪)
来源:互联网 发布:python game engine 编辑:程序博客网 时间:2024/06/09 16:46
在launcher中,长按屏幕,图挂了,我们在代码里面找一下:
首先,找到onLongClick(View v)方法,然后在这个方法中,有如下代码:
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); } } }
然后,继续追踪showAddDialog()方法:
private void showAddDialog(CellLayout.CellInfo cellInfo) { mAddItemCellInfo = cellInfo; mWaitingForResult = true; showDialog(DIALOG_CREATE_SHORTCUT); }
继续找showDialog,没什么发现,showDialog()方法是该类父类Activity()方法,携带参数DIALOG_CREATE_SHORTCUT,但是它最后是调用的Launcher.java的onCreateDialog(int id)这个方法:
@Override protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG_CREATE_SHORTCUT: return new CreateShortcut().createDialog();
在这里,使用了CreateShortcut.createDialog()方法,在源代码的2708行:
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(); } }
我们可以在CreateShortcut类中onclick()方法里面找到一下代码(这里是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; }
这里是2.3的代码:
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; }
在这里又是如何实现的呢?我么看到了startActivityForResult()方法,它实际上是运行了package/apps/Settings/src/com/android/settings/AppWidgetPickActivity.java
我们来看下代码中是如何获取widget信息的:
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); }
和方法:
@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; }
下面是我自己写的获取widget的图标:
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); }
在这段代码里,能获取widget的列表。
不知道是何种原因,使用以下代码:
List<AppWidgetProviderInfo> mAppinfoList = mAppWidgetManager.getInstalledProviders();for(AppWidgetProviderInfo appWidgetInfo:mAppinfoList) { mThumbs.add(appWidgetInfo.icon); }
却不能获取widget的图标,而是别的图,望清楚原因者告知。
- launcher修改--获取widget信息(图片,文字等)(源码追踪)
- launcher修改--获取widget信息(图片,文字等)(源码追踪)
- launcher修改--页面标记实现原理(代码追踪)
- launcher修改---launcher的搜索框和ProtipWidget(widget修改)(有图有真相)
- PS编辑按钮图片(修改或添加文字等)
- launcher修改--左右滑动屏幕切换源码追踪
- Android Launcher修改--左右滑动屏幕切换源码追踪
- Android Launcher修改--左右滑动屏幕切换源码追踪
- launcher修改--获取应用列表launcher源码解析
- launcher修改--获取应用列表launcher源码解析
- java 获取图片信息(经纬度,像素等)
- Launcher开发之添加widget(一)
- 在Eclipse中调试运行launcher源码(一)——获取launcher源码
- Android ListView 列表分隔,条目中添加分类信息(文字,图片等)
- Android Launcher分析和修改12——Widget列表信息收集
- Android Launcher分析和修改12——Widget列表信息收集
- Android Launcher分析和修改12——Widget列表信息收集
- 在Eclipse中调试运行launcher源码(五)——运行自己修改的Launcher
- 提高Android Service 优先级的方法 .
- 有关去除dataflash以及换为64M nand的相关修改及bugfix
- 逆向序列号生成算法(一)
- 空连接IPC$攻击与防范
- 用CreateToolHelp32Snapshot 查看进程及其模块列表(delphi xe2)
- launcher修改--获取widget信息(图片,文字等)(源码追踪)
- Anchorfree Hotspot Shield 去广告方法
- 画法几何3---- 基本几何体的投影
- 一些Domino内置的有特殊含义或作用的内置表单
- 继续聊WPF——自定义CheckBox控件外观
- Java事件处理总结
- TopStyle Pro 的使用技巧
- JSP各种标签
- 果然还不适合看源码