Google Search Widget

上一篇文章说了Googel Search Widget在不同的版本是在不同ap里面实现的,那今天就代码来分析一下:

 

1,在android 1.5上 Google Search 确切地说不是一个真正的Android 的widget, 因为它不是通过android.appwidget.action.APPWIDGET_UPDATE 和android.appwidget.provider来配置的,而只是Launcher 里面通过workspace主动加载它而已,他的主要相关的代码是Search.java.看看Launcher是如何加载它的:

LauncherProvider.java:

 

 

 

 

private int loadFavorites(SQLiteDatabase db) { Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent.CATEGORY_LAUNCHER); ContentValues values = new ContentValues(); PackageManager packageManager = mContext.getPackageManager(); int i = 0; try { XmlResourceParser parser = mContext.getResources().getXml(R.xml.default_workspace); AttributeSet attrs = Xml.asAttributeSet(parser); XmlUtils.beginDocument(parser, TAG_FAVORITES); ...... ...... if (TAG_FAVORITE.equals(name)) { added = addShortcut(db, values, a, packageManager, intent); } else if (TAG_SEARCH.equals(name)) { added = addSearchWidget(db, values); } else if (TAG_CLOCK.equals(name)) { added = addClockWidget(db, values); } .....  

 

if (TAG_SEARCH.equals(name)) {

                        added = addSearchWidget(db, values);

                    } 这一行

 

private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) { // Add a search box values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_WIDGET_SEARCH); values.put(Favorites.SPANX, 4); values.put(Favorites.SPANY, 1); db.insert(TABLE_FAVORITES, null, values); return true; }  

 

2,在android 2.2的上,Google Search Widget是一个真正的widget,它通过android.appwidget.action.APPWIDGET_UPDATE 和android.appwidget.provider来配置的,其主要代码是SearchWidgetProvider.java。那我们看看Launcher是如何加载它的:

LauncherProvider.java:

 

/** * Loads the default set of favorite packages from an xml file. * * @param db The database to write the values into */ private int loadFavorites(SQLiteDatabase db) { Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent.CATEGORY_LAUNCHER); ContentValues values = new ContentValues(); PackageManager packageManager = mContext.getPackageManager(); int i = 0; try { XmlResourceParser parser = mContext.getResources().getXml(R.xml.default_workspace); .... .... if (TAG_FAVORITE.equals(name)) { added = addAppShortcut(db, values, a, packageManager, intent); } else if (TAG_SEARCH.equals(name)) { added = addSearchWidget(db, values); } else if (TAG_CLOCK.equals(name)) { added = addClockWidget(db, values); } else if (TAG_APPWIDGET.equals(name)) { added = addAppWidget(db, values, a, packageManager); } else if (TAG_SHORTCUT.equals(name)) { added = addUriShortcut(db, values, a); } .... 

 

 

if (TAG_SEARCH.equals(name)) {

                        added = addSearchWidget(db, values);

                    }这一行

private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) { ComponentName cn = getSearchWidgetProvider(); return addAppWidget(db, values, cn, 4, 1); } 

private ComponentName getSearchWidgetProvider() { SearchManager searchManager = (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE); ComponentName searchComponent = searchManager.getGlobalSearchActivity(); if (searchComponent == null) return null; return getProviderInPackage(searchComponent.getPackageName()); } 

从代码就可以看得出android1.5和android 2.2的区别了。不过共同点就是在default_workspace.xml都是用search tag来定义Google search widget.

        launcher:screen="2"

        launcher:x="0"

        launcher:y="0" />

 

3,通过上面我们也知道Launcher 加载default 的ap或者widget是通过default_workspace.xml定义,然后通过LauncherProvider中的loadFavorites的函数来解析这个xml.

 

 

 

 

 

你可能感兴趣的:(android,apps)