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<PackageInfo> list = (ArrayList<PackageInfo>) packageManager.getInstalledPackages(PackageManager.GET_ACTIVITIES);
// for(int z = 0;z<list.size();z++) {Log.v(TAG, list.get(z).packageName);}
dwCxt = mContext.createPackageContext("com.letou.dw", Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
dwRes = packageManager.getResourcesForApplication("com.letou.dw");
rid = dwRes.getIdentifier("default_workspace", "xml", "com.letou.dw");
} catch (Exception e) {
e.printStackTrace();
}
*/
int i = 0;
boolean hasOutDw = false;
try {
File f = new File(external_workspace);
//System.out.println(" dw " + f.exists());
hasOutDw = f.exists();
} catch(Exception e) {
Log.w(TAG, "Got exception parsing favorites.", e);
}
if(!hasOutDw) return i;
try {
//System.out.println(" dw "+rid +" " + dwRes);
// if(rid != -1 && dwRes != null) parser = dwRes.getXml(rid);
XmlPullParser parserOut = Xml.newPullParser();
parserOut.setInput(new FileInputStream("/system/default_workspace.xml"), "UTF-8");
int event = parserOut.getEventType();// 产生第一个事件
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_DOCUMENT:// 判断当前事件是否是文档开始事件
break;
case XmlPullParser.START_TAG:// 判断当前事件是否是标签元素开始事件
String name = parserOut.getName();
int attributeCount = parserOut.getAttributeCount();
long container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
int type;
String screen = null;
String x = null;
String y = null;
String packName = null;
String clzName = null;
String spanX = null;
String spanY = null;
String title = null;
String icon = null;
String uri = null;
boolean added = false;
for (int z = 0; z < attributeCount; z++) {
//System.out.println(" attribute "+ parserOut.getAttributeName(z) + " "+ parserOut.getAttributeValue(z));
String key = parserOut.getAttributeName(z);
String value = parserOut.getAttributeValue(z);
if("container".equals(key)) {
container = Long.valueOf(value);
} else if ("screen".equals(key)) {
screen = value;
} else if ("x".equals(key)) {
x = value;
} else if ("y".equals(key)) {
y = value;
} else if ("packageName".equals(key)) {
packName = value;
} else if ("className".equals(key)) {
clzName = value;
} else if ("spanX".equals(key)) {
spanX = value;
} else if ("spanY".equals(key)) {
spanY = value;
} else if ("title".equals(key)) {
title = value;
} else if ("icon".equals(key)) {
icon = value;
} else if ("uri".equals(key)) {
uri = value;
}
}
// If we are adding to the hotseat, the screen is used as the position in the
// hotseat. This screen can't be at position 0 because AllApps is in the
// zeroth position.
if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
Hotseat.isAllAppsButtonRank(Integer.valueOf(screen))) {
throw new RuntimeException("Invalid screen position for hotseat item");
}
values.clear();
values.put(LauncherSettings.Favorites.CONTAINER, container);
values.put(LauncherSettings.Favorites.SCREEN, screen);
values.put(LauncherSettings.Favorites.CELLX, x);
values.put(LauncherSettings.Favorites.CELLY, y);
if (TAG_FAVORITE.equals(name)) {
long id = addAppShortcut(db, values, packName, clzName, packageManager, intent);
added = id >= 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<Long> folderItems = new ArrayList<Long>();
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;
}
}
网友评论