从外部读取预置桌面布局

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;  
        }  
  
    }  
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,029评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,395评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,570评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,535评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,650评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,850评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,006评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,747评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,207评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,536评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,683评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,342评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,964评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,772评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,004评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,401评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,566评论 2 349

推荐阅读更多精彩内容

  • golang调用外部程序,如何读取外部程序的stdout/stderr输出: 例子1 读取标准输出 运行结果: c...
    CodingCode阅读 6,015评论 0 1
  • 说明 我们在运行一个系统时,不可避免的要加载一些常用的数据,如默认用户,各种数据枚举等。现在流行的方式为配置一个j...
    BeeHoney阅读 17,378评论 1 4
  • 不论是数据分析,数据可视化,还是数据挖掘,一切的一切全都是以数据作为最基础的元素。利用Python进行数据分析,同...
    蝴蝶兰玫瑰阅读 10,153评论 1 30
  • 866598020904260已激活866598020904260
    辉少网络阅读 188评论 0 0
  • 余自幼时即亲近自然,嗜桃,爱花,喜树。至今近二十年矣。每念及童年回忆,爬树摘果,嗅花嚼草,游戏于天地之间,恍如吸纳...
    行者陶然阅读 259评论 0 1