launcher 图标删除分析
使用用ADW的Launcher的人应该有点印象里面的功能,就是拖拽应用或者Widget图标到删除图标上,停留1秒以上,
则会显示"Drop to uninstall"的提示,放开则打开卸载该程序的界面,现在就是要分析模仿这个功能。
主要用到有两方面的东西,卸载和停留提示。
卸载主要使用的是:
- Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,
- Uri.parse("package:" + mUninstallPkg));
- DeleteZone.this.getContext().startActivity(uninstallIntent);
- Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,
- Uri.parse("package:" + mUninstallPkg));
- DeleteZone.this.getContext().startActivity(uninstallIntent);
从这个来看,要卸载一个应用,主要是知道其PackageName即可。
当拖拽图标到删除栏上,停留超过一定时间的时候,就转到卸载的模式,这个可以用Handler的
postDelayed(Runnabler, long delayMillis)
来实现。
我们现在来分析一下拖拽所产生的响应事件,
当开始拖拽的时候,首先运行的是:
onDragStart->onDragOver->onDragExit->onDragEnd.
当把图标拖动进入删除区域的时候,会执行onDragEnter,移出则执行onDragExit.
而如果图标是拖入到删除区域再松开的话,则会是onDragEnter->onDragDrop->onDragExit->onDragEnd.
拖动结束一定会执行的两个事件是onDragExit->onDragEnd.其中onDragExit在onDragEnd之前发生。
因此在onDragEnter的时候,读取PackageName和postDelayed发送一个延迟消息给Runnable(设置卸载标志)
然后在onDragExit里面再
removeCallbacks(Runnabler)移除队列里面的消息,
并postDelayed一个消息,清空卸载标志,当然这里的延迟时间要比前面onDragEnter里面的时间要小得多,
这样实现的效果是:
1. 如果是拖拽进删除区域,再拖出来的时候,会清空卸载标志;
2. 如果是拖拽进删除区域足够长时间再放开(应该打开卸载界面,已经设置卸载标志),确保延迟的时间内onDragEnd已经执行,因为postDelayed是清空卸载标志的,所以如果要保留卸载标志,需要延迟时间要比onDragEnd执行时间大。
最后在onDragEnd的时候,判断卸载标志和程序PackageName如果符合条件则执行卸载动作。
呵呵,说得不是很清楚,下面上主要源码:
- // Yao.GUET add 2011-04-08
- private static final String TAG="DeleteZone";
- private static final int STAY_OVER_TIME = 1500;
- private static boolean mUninstall;
- private boolean mEnterDelZone;
- private String mUninstallPkg;
- private final Handler mHandler = new Handler();
- private final Runnable mShowUnstaller = new Runnable() {
- public void run() {
- // TODO Auto-generated method stub
- Log.e(TAG, "Runnable ShowUnstaller is called!");
- mUninstall = mEnterDelZone;
- if (mUninstall == true) {
- Toast.makeText(mContext, "Drop to unstall the app!", 500).show();
- }
- }
- };
- // Yao.GUET add 2011-04-08
- private static final String TAG="DeleteZone";
- private static final int STAY_OVER_TIME = 1500;
- private static boolean mUninstall;
- private boolean mEnterDelZone;
- private String mUninstallPkg;
- private final Handler mHandler = new Handler();
- private final Runnable mShowUnstaller = new Runnable() {
- public void run() {
- // TODO Auto-generated method stub
- Log.e(TAG, "Runnable ShowUnstaller is called!");
- mUninstall = mEnterDelZone;
- if (mUninstall == true) {
- Toast.makeText(mContext, "Drop to unstall the app!", 500).show();
- }
- }
- };
onDragEnter:
- // Yao.GUET
- Log.e(TAG, "DragEnter");
- if (item instanceof LauncherAppWidgetInfo ||
- item instanceof ApplicationInfo ||
- item instanceof ShortcutInfo) {
- mHandler.removeCallbacks(mShowUnstaller);
- Log.e(TAG, "postDelayed");
- mEnterDelZone = true;
- mHandler.postDelayed(mShowUnstaller, STAY_OVER_TIME);
- // get the package name
- if (item instanceof ApplicationInfo) {
- final ApplicationInfo ai = (ApplicationInfo)item;
- mUninstallPkg = ai.componentName.getPackageName();
- if (this.getClass().getPackage().getName().equals(mUninstallPkg))
- mUninstallPkg = null;
- } else if (item instanceof LauncherAppWidgetInfo) {
- final LauncherAppWidgetInfo appwidget = (LauncherAppWidgetInfo)item;
- final AppWidgetProviderInfo ap = AppWidgetManager.getInstance(mLauncher)
- .getAppWidgetInfo(appwidget.appWidgetId);
- if (ap != null)
- mUninstallPkg = ap.provider.getPackageName();
- } else if (item instanceof ShortcutInfo) {
- final ShortcutInfo scInfo = (ShortcutInfo)item;
- if (scInfo.iconResource != null) {
- mUninstallPkg = scInfo.iconResource.packageName;
- } else if (scInfo.intent != null) {
- mUninstallPkg = scInfo.intent.getComponent().getPackageName();
- }
- }
- // Yao.GUET
- Log.e(TAG, "DragEnter");
- if (item instanceof LauncherAppWidgetInfo ||
- item instanceof ApplicationInfo ||
- item instanceof ShortcutInfo) {
- mHandler.removeCallbacks(mShowUnstaller);
- Log.e(TAG, "postDelayed");
- mEnterDelZone = true;
- mHandler.postDelayed(mShowUnstaller, STAY_OVER_TIME);
- // get the package name
- if (item instanceof ApplicationInfo) {
- final ApplicationInfo ai = (ApplicationInfo)item;
- mUninstallPkg = ai.componentName.getPackageName();
- if (this.getClass().getPackage().getName().equals(mUninstallPkg))
- mUninstallPkg = null;
- } else if (item instanceof LauncherAppWidgetInfo) {
- final LauncherAppWidgetInfo appwidget = (LauncherAppWidgetInfo)item;
- final AppWidgetProviderInfo ap = AppWidgetManager.getInstance(mLauncher)
- .getAppWidgetInfo(appwidget.appWidgetId);
- if (ap != null)
- mUninstallPkg = ap.provider.getPackageName();
- } else if (item instanceof ShortcutInfo) {
- final ShortcutInfo scInfo = (ShortcutInfo)item;
- if (scInfo.iconResource != null) {
- mUninstallPkg = scInfo.iconResource.packageName;
- } else if (scInfo.intent != null) {
- mUninstallPkg = scInfo.intent.getComponent().getPackageName();
- }
- }
onDragExit:
- // Yao.GUET add to remove the call back function
- Log.e(TAG, "DragExit");
- mHandler.removeCallbacks(mShowUnstaller);
- dragView.setPaint(null);
- if (mUninstall == true) {
- mEnterDelZone = false;
- mHandler.postDelayed(mShowUnstaller, 100);
- }
- // Yao.GUET add to remove the call back function
- Log.e(TAG, "DragExit");
- mHandler.removeCallbacks(mShowUnstaller);
- dragView.setPaint(null);
- if (mUninstall == true) {
- mEnterDelZone = false;
- mHandler.postDelayed(mShowUnstaller, 100);
- }
onDragEnd:
- // Yao.GUET deal the uninstall flag when drag is end
- Log.e(TAG, "flag:" + mUninstall + ", pkg:" + mUninstallPkg);
- if (mUninstall && (mUninstallPkg != null)) {
- Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,
- Uri.parse("package:" + mUninstallPkg));
- DeleteZone.this.getContext().startActivity(uninstallIntent);
- }