一键生成default_workspace.xml文件

昨天写了一个app demo,功能:一键生成桌面布局文件default_workspace.xml。已经在8.1上验证ok。把代码片段贴上来。

TreeSet mHotseatBean;//hotseat下的app
TreeSet mAppWidgetBean;//appwidget
TreeSet mIconBean;//app
TreeSet mFolderBean;//folder
TreeSet mFolderIconBean;//folder内的app
public class IconWorkspace{ //桌面的icon info
        public String packageName;//launcher:packageName
        public String className;//launcher:className
        public String title; //launcher:title
        public  int  screen;//launcher:screen
        public  int  container;//launcher:container
        public  int  cellX;//launcher:x
        public  int  cellY;//launcher:y
        public  int  rank = -1;
        public int spanX;//launcher:spanx
        public int spanY;//launcher:spany

        @Override
        public String toString() {
            return "["+"packageName:"+packageName+", className:"+className+",screen:"+screen+",cellX:"+cellX+",cellY:"+cellY+"]";
        }
    }
//属性
    String appwidgetBegin = "    ;
    String favoriteBegin = "    ;
    String favoritesEnd = "";
    String tagEnd = "        />\n\n";
    String attributeEnd = "\"\n";
    String folderBegin = ";
    String folderEnd = "\n";

    String packageName = "        launcher:packageName=\"";
    String className = "        launcher:className=\"";
    String screen = "        launcher:screen=\"";
    String x = "        launcher:x=\"";
    String y = "        launcher:y=\"";
    String spanX = "        launcher:spanX=\"";
    String spanY = "        launcher:spanY=\"";
    String container_tag = "        launcher:container=\"";
    String title_tag = "        launcher:title=\"";
//数据库读取,保存对应数据到TreeSet
   public void getDefaultWorkspace(){
        Log.d(TAG,"zrx getDefaultWorkspace");
        final String AUTHORITY = "com.android.launcher3.settings";
        final Uri CONTENT_URI = Uri.parse("content://" +  AUTHORITY + "/favorites?notify=true");

        final String SCREEN = "screen";
        final String CELLX = "cellX";
        final String CELLY = "cellY";
        final String INTENT = "intent";
        final String CONTAINER = "container";
        final String ITEMTYPE = "itemType";
        final String SPANX = "spanX";
        final String SPANY = "spanY";
        final String APPWIDGETID = "appWidgetId";
        final String RANK = "rank";
        final String TITLE = "title";

        int itemtype;
        String intent;
        String title;
        int screen_db;
        int cellX_db;
        int cellY_db;
        int spanX_db;
        int spanY_db;
        int container;
        int appwidgetid;
        String[] in;
        Cursor cursor = null;



        try
        {
            cursor = getContentResolver().query(CONTENT_URI, null, null, null, null);

            if (cursor != null && cursor.getCount() > 0)
            {

                mAppWidgetBean.clear();
                mFolderIconBean.clear();
                mHotseatBean.clear();
                mFolderBean.clear();
                mIconBean.clear();
                while (cursor.moveToNext())
                {

                    int rank = -1 ;
                    IconWorkspace iconBean = new IconWorkspace();

                    itemtype = cursor.getInt(cursor.getColumnIndex(ITEMTYPE));
                    title = cursor.getString(cursor.getColumnIndex(TITLE));
                    intent = cursor.getString(cursor.getColumnIndex(INTENT));
                    screen_db = cursor.getInt(cursor.getColumnIndex(SCREEN));
                    cellX_db = cursor.getInt(cursor.getColumnIndex(CELLX));
                    cellY_db = cursor.getInt(cursor.getColumnIndex(CELLY));
                    spanX_db = cursor.getInt(cursor.getColumnIndex(SPANX));
                    spanY_db = cursor.getInt(cursor.getColumnIndex(SPANY));
                    container = cursor.getInt(cursor.getColumnIndex(CONTAINER));
                    appwidgetid = cursor.getInt(cursor.getColumnIndex(APPWIDGETID));
                   // Log.d("MainActivity", "zrx--title:"+title+",itemtype:" + itemtype+",appwidgetid:"+appwidgetid+",rank:"+rank);

                    if(intent!=null){
                        Intent intent1 = Intent.parseUri(intent, 0);
                        ComponentName component = intent1.getComponent();
                        if(component!=null)
                        {
                            iconBean.className = component.getClassName();
                            iconBean.packageName = component.getPackageName();
                        }
                       /* in = intent.substring(intent.indexOf("component=")+10, intent.indexOf(";end")).split("/");
                        iconBean.className = in[1];
                        iconBean.packageName = in[0];
                        Log.d(TAG,"zrx className:"+in[1]+",packageName:"+in[0]);*/
                    }


                    iconBean.title = title;
                    iconBean.cellX = cellX_db;
                    iconBean.cellY = cellY_db;
                    iconBean.screen = screen_db;
                    iconBean.container = container;
                    iconBean.spanX = spanX_db;
                    iconBean.spanY = spanY_db;


                    Log.i(TAG,"zrx--appwidgetid:"+appwidgetid);
                  //  Log.d(TAG,"zrx title:"+title+", container:"+container+",")
                    if (container == -101)
                    {//hotseat
                        mHotseatBean.add(iconBean);
                        continue;

                    }

                    if(appwidgetid > 0){//app widget
                        mAppWidgetBean.add(iconBean);
                        continue;

                    }

                    if(container > 0){
                        rank = cursor.getInt(cursor.getColumnIndex(RANK));
                        Log.d("MainActivty","zrx--rank:"+rank);
                    }
                    iconBean.rank = rank;

                    if(rank < 0){
                        if(itemtype == 2){
                            iconBean.title = title;
                            mFolderBean.add(iconBean);
                            Log.d(TAG, "zrx mFolderBean=" +iconBean.toString());
                        }else {
                            mIconBean.add(iconBean);
                        }
                    }else {
                        mFolderIconBean.add(iconBean) ;
                        Log.d(TAG, "zrx mFolderIconBean=" +iconBean.toString());
                    }
                }

               // builder.append(favoritesEnd);
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        } finally
        {
            cursor.close();
            writeFile();
        }

    }
//生成default_workspace.xml文件保存到/storage/emulated/0/
   private void writeFile() {

        Log.i(TAG, Environment.getExternalStorageDirectory().getPath()
                + "/default_workspace.xml");

        File targetFile = new File(Environment.getExternalStorageDirectory().getPath()
                + "/default_workspace.xml");
        if (targetFile.exists()) {
            targetFile.delete();
        }

        builder = new StringBuilder();
        builder.append("\n");
        builder.append("\n");

        for(IconWorkspace iconBean:mHotseatBean){
            builder.append("    \n");

            builder.append(favoriteBegin);

            if (iconBean.packageName != null)
            {
                builder.append(packageName);
                builder.append(iconBean.packageName);
                builder.append(attributeEnd);

                builder.append(className);
                builder.append(iconBean.className);
                builder.append(attributeEnd);

            }
            builder.append(container_tag);
            builder.append(iconBean.container);
            builder.append(attributeEnd);

            builder.append(screen);
            builder.append(iconBean.screen);
            builder.append(attributeEnd);

            builder.append(x);
            builder.append(iconBean.cellX);
            builder.append(attributeEnd);

            builder.append(y);
            builder.append(iconBean.cellY);
            builder.append(attributeEnd);

            builder.append(tagEnd);
        }


        for(IconWorkspace iconBean:mAppWidgetBean){
            builder.append("    \n");

            builder.append(appwidgetBegin);

            if(iconBean.packageName != null){
                builder.append(packageName);
                builder.append(iconBean.packageName);
                builder.append(attributeEnd);

                builder.append(className);
                builder.append(iconBean.className);
                builder.append(attributeEnd);
            }

            builder.append(screen);
            builder.append(iconBean.screen);
            builder.append(attributeEnd);

            builder.append(x);
            builder.append(iconBean.cellX);
            builder.append(attributeEnd);

            builder.append(y);
            builder.append(iconBean.cellY);
            builder.append(attributeEnd);

            builder.append(spanX);
            builder.append(iconBean.spanX);
            builder.append(attributeEnd);

            builder.append(spanY);
            builder.append(iconBean.spanY);
            builder.append(attributeEnd);

            builder.append(tagEnd);
        }

        for(IconWorkspace iconBean: mIconBean ){
            builder.append("    \n");

            builder.append(favoriteBegin);

            if (iconBean.packageName != null)
            {
                builder.append(packageName);
                builder.append(iconBean.packageName);
                builder.append(attributeEnd);

                builder.append(className);
                builder.append(iconBean.className);
                builder.append(attributeEnd);

            }
            /*builder.append(container_tag);
            builder.append(iconBean.container);
            builder.append(attributeEnd);*/

            builder.append(screen);
            builder.append(iconBean.screen);
            builder.append(attributeEnd);

            builder.append(x);
            builder.append(iconBean.cellX);
            builder.append(attributeEnd);

            builder.append(y);
            builder.append(iconBean.cellY);
            builder.append(attributeEnd);

            builder.append(tagEnd);
        }

        for(IconWorkspace iconBean:mFolderBean){
            builder.append("    \n");

            builder.append(folderBegin);

            builder.append(title_tag);
            builder.append(iconBean.title);
            builder.append(attributeEnd);

            builder.append(screen);
            builder.append(iconBean.screen);
            builder.append(attributeEnd);

            builder.append(x);
            builder.append(iconBean.cellX);
            builder.append(attributeEnd);

            builder.append(y);
            builder.append(iconBean.cellY);
            builder.append(attributeEnd);

            builder.append(tagEnd);

            for(IconWorkspace bean:mFolderIconBean){
                builder.append("    \n");

                builder.append(favoriteBegin);

                if (bean.packageName != null)
                {
                    builder.append(packageName);
                    builder.append(bean.packageName);
                    builder.append(attributeEnd);

                    builder.append(className);
                    builder.append(bean.className);
                    builder.append(attributeEnd);

                }

                builder.append(screen);
                builder.append(bean.screen);
                builder.append(attributeEnd);

                builder.append(x);
                builder.append(bean.cellX);
                builder.append(attributeEnd);

                builder.append(y);
                builder.append(bean.cellY);
                builder.append(attributeEnd);

                builder.append(tagEnd);
            }

            builder.append(folderEnd);

        }

        builder.append(favoritesEnd);


        OutputStreamWriter osw;
        try{
            osw = new OutputStreamWriter(
                    new FileOutputStream(targetFile),"utf-8");

            osw.write(builder.toString());
            osw.flush();
            osw.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        builder=null;
    }
添加权限,在android 6.0及以上动态设置权限
"com.android.launcher3.permission.READ_SETTINGS"/>
    "com.android.launcher3.permission.WRITE_SETTINGS"/>
    "android.permission.WRITE_EXTERNAL_STORAGE"/>

public static final int REQUEST_PERMISSION_ALL = 0;
    public static String[] sAllPermissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private void checkPermission(){
        List noOkPermissions = new ArrayList<>();

        for (String permission : sAllPermissions) {
            if (ActivityCompat.checkSelfPermission(this,permission) == PackageManager.PERMISSION_DENIED) {
                noOkPermissions.add(permission);
            }
        }
        if (noOkPermissions.size() <= 0)
            return ;
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(noOkPermissions.toArray(new String[noOkPermissions.size()]), REQUEST_PERMISSION_ALL);
        }
    }

上面的代码是用StringBuilder拼接字符串生成一份xml文件,代码质量很low,质量高一点的代码可以用XmlSerializer格式化生成文档

android 版本8.0以下的平台可以戳我

你可能感兴趣的:(一键生成default_workspace.xml文件)