一鍵生成default_workspace.xml檔案
阿新 • • 發佈:2019-02-01
昨天寫了一個app demo,功能:一鍵生成桌面佈局檔案default_workspace.xml。已經在8.1上驗證ok。把程式碼片段貼上來。
TreeSet<IconWorkspace> mHotseatBean;//hotseat下的app
TreeSet<IconWorkspace> mAppWidgetBean;//appwidget
TreeSet<IconWorkspace> mIconBean;//app
TreeSet<IconWorkspace> mFolderBean;//folder
TreeSet<IconWorkspace> 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 = " <appwidget\n";
String favoriteBegin = " <favorite\n";
String favoritesEnd = "</favorites>";
String tagEnd = " />\n\n";
String attributeEnd = "\"\n";
String folderBegin = "<folder\n";
String folderEnd = "</folder>\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("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
builder.append("<favorites xmlns:launcher=\"http://schemas.android.com/apk/res-auto/com.android.launcher3\">\n");
for(IconWorkspace iconBean:mHotseatBean){
builder.append(" <!-- ");
builder.append(iconBean.title);
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(" <!-- ");
builder.append(iconBean.title);
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(" <!-- ");
builder.append(iconBean.title);
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(" <!-- ");
builder.append(iconBean.title);
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(" <!-- ");
builder.append(bean.title);
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及以上動態設定許可權
<uses-permission android:name="com.android.launcher3.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher3.permission.WRITE_SETTINGS"/>
<uses-permission android:name="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<String> 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以下的平臺可以戳我