private static final String external_workspace = "/system/default_workspace.xml";
//。。。。。
if(mOpenHelper.loadFavoritesFromExternal(mOpenHelper.getWritableDatabase()) == 0){
mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), R.xml.default_workspace);
//。。。。DatabaseHelper增加常量
private static final String TAG_OUT_SHORTCUT = "out-shortcut";
private static final String TAG_OUT_FOLDER = "out-folder";
//....
private int loadFavoritesFromExternal(SQLiteDatabase db) {
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ContentValues values = new ContentValues();
PackageManager packageManager = mContext.getPackageManager();
/*
int rid = -1;
Resources dwRes = null;
Context dwCxt = null;
try {
ArrayList list = (ArrayList) packageManager.getInstalledPackages(PackageManager.GET_ACTIVITIES);
// for(int z = 0;z= 0;
} 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, new ComponentName(packName, clzName), Integer.valueOf(spanX.trim()), Integer.valueOf(spanY.trim()));
} else if (TAG_OUT_SHORTCUT.equals(name)) {
long id = addUriShortcut(db, values, uri , title, icon);
added = id >= 0;
} else if (TAG_OUT_FOLDER.equals(name)) {
values.put(LauncherSettings.Favorites.TITLE, title);
long folderId = addFolder(db, values);
added = folderId >= 0;
ArrayList folderItems = new ArrayList();
int folderDepth = parserOut.getDepth();
while ((type = parserOut.next()) != XmlPullParser.END_TAG || parserOut.getDepth() > folderDepth) {
if (type != XmlPullParser.START_TAG) {
continue;
}
String packNameItem = null;
String clzNameItem = null;
String titleItem = null;
String iconItem = null;
String uriItem = null;
int itemCount = parserOut.getAttributeCount();
for (int z = 0; z < itemCount; z++) {
String key = parserOut.getAttributeName(z);
String value = parserOut.getAttributeValue(z);
//System.out.println(" ITEM " + key + " " + value);
if ("packageName".equals(key)) {
packNameItem = value;
} else if ("className".equals(key)) {
clzNameItem = value;
} else if ("title".equals(key)) {
titleItem = value;
} else if ("icon".equals(key)) {
iconItem = value;
} else if ("uri".equals(key)) {
uriItem = value;
}
}
final String folder_item_name = parserOut.getName();
values.clear();
values.put(LauncherSettings.Favorites.CONTAINER, folderId);
if (TAG_FAVORITE.equals(folder_item_name) && folderId >= 0) {
long id = addAppShortcut(db, values, packNameItem, clzNameItem, packageManager, intent);
if (id >= 0) {
folderItems.add(id);
}
} else if (TAG_OUT_SHORTCUT.equals(folder_item_name) && folderId >= 0) {
long id = addUriShortcut(db, values, uriItem, titleItem, iconItem);
if (id >= 0) {
folderItems.add(id);
}
} else {
throw new RuntimeException("Folders can " + "contain only shortcuts");
}
}
}
if (added) i++;
break;
case XmlPullParser.END_TAG:// 判断当前事件是否是标签元素结束事件
if (TAG_FAVORITE.equals(parserOut.getName())) {// 判断结束标签元素
}
break;
}
event = parserOut.next();// 进入下一个元素并触发相应事件
}// end while
} catch (XmlPullParserException e) {
Log.w(TAG, "Got exception parsing favorites.", e);
} catch (IOException e) {
Log.w(TAG, "Got exception parsing favorites.", e);
} catch (RuntimeException e) {
Log.w(TAG, "Got exception parsing favorites.", e);
}
return i;
}
private long addAppShortcut(SQLiteDatabase db, ContentValues values, String packName, String clzName,
PackageManager packageManager, Intent intent) {
long id = -1;
ActivityInfo info;
String packageName = packName;
String className = clzName;
try {
ComponentName cn;
try {
cn = new ComponentName(packageName, className);
info = packageManager.getActivityInfo(cn, 0);
} catch (PackageManager.NameNotFoundException nnfe) {
String[] packages = packageManager.currentToCanonicalPackageNames(
new String[] { packageName });
cn = new ComponentName(packages[0], className);
info = packageManager.getActivityInfo(cn, 0);
}
id = generateNewId();
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
values.put(Favorites.INTENT, intent.toUri(0));
values.put(Favorites.TITLE, info.loadLabel(packageManager).toString());
values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
values.put(Favorites.SPANX, 1);
values.put(Favorites.SPANY, 1);
values.put(Favorites._ID, generateNewId());
if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) {
return -1;
}
} catch (PackageManager.NameNotFoundException e) {
Log.w(TAG, "Unable to add favorite: " + packageName +
"/" + className, e);
}
return id;
}
private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn,
int spanX, int spanY) {
boolean allocatedAppWidgets = false;
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
try {
int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
values.put(Favorites.SPANX, spanX);
values.put(Favorites.SPANY, spanY);
values.put(Favorites.APPWIDGET_ID, appWidgetId);
values.put(Favorites._ID, generateNewId());
dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values);
allocatedAppWidgets = true;
appWidgetManager.bindAppWidgetId(appWidgetId, cn);
} catch (RuntimeException ex) {
Log.e(TAG, "Problem allocating appWidgetId", ex);
}
return allocatedAppWidgets;
}
private long addUriShortcut(SQLiteDatabase db, ContentValues values, String uri, String title, String icon) {
Resources r = mContext.getResources();
final int iconResId = R.drawable.ic_launcher_shortcut_browser_bookmark;
// final int titleResId =
// a.getResourceId(R.styleable.Favorite_title, 0);
Intent intent;
try {
intent = Intent.parseUri(uri, 0);
} catch (URISyntaxException e) {
Log.w(TAG, "Shortcut has malformed uri: " + uri);
return -1; // Oh well
}
if (iconResId == 0) { // || titleResId == 0) {
Log.w(TAG, "Shortcut is missing icon resource ID");
return -1;
}
long id = generateNewId();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
values.put(Favorites.INTENT, intent.toUri(0));
values.put(Favorites.TITLE, title);
values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
values.put(Favorites.SPANX, 1);
values.put(Favorites.SPANY, 1);
values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
values.put(Favorites.ICON_PACKAGE, mContext.getPackageName());
values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId));
values.put(Favorites._ID, id);
if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) {
return -1;
}
return id;
}
}