android 单双层桌面切换

来源:互联网 发布:数据采集仪 编辑:程序博客网 时间:2024/05/21 08:40

单双层桌面切换由于必须持久化数据 所以必须多创建单层桌面所须要的数据库表;

  1. 一表为存储桌面图标,表结构跟原生 桌面表一样 直接copy一份就可以了
  2. 一表为存储桌面页,表结构跟原生一样

创建上面两张表时注意下 数据库版本升级带来的影响

由于表结构跟原生一样 所以加载逻辑不需要改动 直接按原生加载 所需要改动的是加载的是哪个表

建立一个判断是否为单双层桌面的方法,此方法单双层桌面的 标志

    /**add by lmjssjj begin*/    public static boolean isDisableAllApps(){        return false;    }    /**add by lmjssjj end*/

修改加载的数据表

 public static final class WorkspaceScreens implements ChangeLogColumns {        public static final String TABLE_NAME = "workspaceScreens";        /**add by lmjssjj begin*/        public static final String TABLE_DISABLEALLAPPS_NAME = "workspaceScreens_disable_allapps";        /**add by lmjssjj end*/        /**         * The content:// style URL for this table         */        static final Uri CONTENT_URI = Uri.parse("content://" +                ProviderConfig.AUTHORITY + "/" + /**modify by lmjssjj*/(LauncherAppState.isDisableAllApps()?TABLE_DISABLEALLAPPS_NAME:TABLE_NAME));        /**         * The rank of this screen -- ie. how it is ordered relative to the other screens.         * <P>Type: INTEGER</P>         */        public static final String SCREEN_RANK = "screenRank";    }
public static final class Favorites implements BaseLauncherColumns {        public static final String TABLE_NAME = "favorites";        /**add by lmjssjj begin*/        public static final String TABLE_DISABLEALLAPPS_NAME = "favorites_disable_allapps";        /**add by lmjssjj end*/        /**         * The content:// style URL for this table         */        public static final Uri CONTENT_URI = Uri.parse("content://" +                ProviderConfig.AUTHORITY + "/" +/**modify by lmjssjj*/ (LauncherAppState.isDisableAllApps()?TABLE_DISABLEALLAPPS_NAME:TABLE_NAME));        /**         * The content:// style URL for a given row, identified by its id.         *         * @param id The row id.         *         * @return The unique content URL for the specified row.         */        public static Uri getContentUri(long id) {            return Uri.parse("content://" + ProviderConfig.AUTHORITY +                    "/" + (LauncherAppState.isDisableAllApps()?TABLE_DISABLEALLAPPS_NAME:TABLE_NAME) + "/" + id);        }

添加数据库表
为了数据库版本升级 单独建立

//add by lmjssjj        private void addFavoritesDisableAllappsTable(SQLiteDatabase db){             UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);             long userSerialNumber =                     userManager.getSerialNumberForUser(UserHandleCompat.myUserHandle());            db.execSQL("CREATE TABLE favorites_disable_allapps (" + "_id INTEGER PRIMARY KEY," + "title TEXT,"                    + "intent TEXT," + "container INTEGER," + "screen INTEGER," + "cellX INTEGER,"                    + "cellY INTEGER," + "spanX INTEGER," + "spanY INTEGER," + "itemType INTEGER,"                    + "appWidgetId INTEGER NOT NULL DEFAULT -1," + "isShortcut INTEGER,"                    + "iconType INTEGER," + "iconPackage TEXT," + "iconResource TEXT,"                    + "icon BLOB," + "uri TEXT," + "displayMode INTEGER,"                    + "appWidgetProvider TEXT," + "modified INTEGER NOT NULL DEFAULT 0,"                    + "restored INTEGER NOT NULL DEFAULT 0," + "profileId INTEGER DEFAULT "                    + userSerialNumber + "," + "rank INTEGER NOT NULL DEFAULT 0,"                    + "options INTEGER NOT NULL DEFAULT 0" + ");");        }//end by lmjssjj        private void addWorkspacesTable(SQLiteDatabase db) {            db.execSQL("CREATE TABLE " + TABLE_WORKSPACE_SCREENS + " ("                    + LauncherSettings.WorkspaceScreens._ID + " INTEGER PRIMARY KEY,"                    + LauncherSettings.WorkspaceScreens.SCREEN_RANK + " INTEGER,"                    + LauncherSettings.ChangeLogColumns.MODIFIED + " INTEGER NOT NULL DEFAULT 0"                    + ");");        }
 private void setFlagDisableAllAppsEmptyDbCreated() {            String spKey = LauncherAppState.getSharedPreferencesKey();            SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);            sp.edit().putBoolean(EMPTY_DISABLEALLAPPS_DATABASE_CREATED, true).commit();        }public void clearFlagDisableAllAppsEmptyDbCreated() {        String spKey = LauncherAppState.getSharedPreferencesKey();        getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE).edit()        .remove(EMPTY_DISABLEALLAPPS_DATABASE_CREATED).commit();    }

数据库表已建立完成 接下来需要修改数据库的写入 通过上面的判断方法 修改写的读的表

 private long initializeMaxItemId(SQLiteDatabase db) {            return getMaxId(db, LauncherAppState.isDisableAllApps()?TABLE_FAVORITES_DISABLE_ALLAPPS:TABLE_FAVORITES);        }
private long initializeMaxScreenId(SQLiteDatabase db) {            return getMaxId(db, LauncherAppState.isDisableAllApps()?TABLE_WORKSPACE_SCREENS_DISABLE_ALLAPPS:TABLE_WORKSPACE_SCREENS);        }
 @Override        public long insertAndCheck(SQLiteDatabase db, ContentValues values) {            return dbInsertAndCheck(this, db, LauncherAppState.isDisableAllApps()?TABLE_FAVORITES_DISABLE_ALLAPPS:TABLE_FAVORITES, null, values);        }

接下来 数据加载

boolean bl = prefs.getBoolean(LauncherAppState.isDisableAllApps()?LauncherProvider.EMPTY_DISABLEALLAPPS_DATABASE_CREATED:LauncherProvider.EMPTY_DATABASE_CREATED, false);
if(LauncherAppState.isDisableAllApps())                    LauncherAppState.getLauncherProvider().clearFlagDisableAllAppsEmptyDbCreated();

加载单层数据,双层数据照原生逻辑

        private void loadAllAppsFirst() {                final Callbacks oldCallbacks = mCallbacks.get();                if (oldCallbacks == null) {                    return;                }                final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();                mBgAllAppsList.clear();                for (UserHandleCompat user : profiles) {                    final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);                    if (apps == null || apps.isEmpty()) {                        return;                    }                    for (int i = 0; i < apps.size(); i++) {                        LauncherActivityInfoCompat app = apps.get(i);                        mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache));                    }                    mBgAllAppsList.added.clear();            }            verifyApplications();            updateIconCache();            loadAndBindWidgetsAndShortcuts(tryGetCallbacks(oldCallbacks), true /* refresh */);        }

切换时重新加载我采用的是在设置页面(设置页在自己的进程) kill 自己:

ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);            activityManager.killBackgroundProcesses(getPackageName());finish();

合并数据

private void verifyApplications() {        final Context context = mApp.getContext();        // Cross reference all the applications in our apps list with items in        // the workspace        ArrayList<ItemInfo> tmpInfos;        ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();        synchronized (sBgLock) {            for (AppInfo app : mBgAllAppsList.data) {                tmpInfos = getItemInfoForComponentName(app.componentName, app.user);                if (tmpInfos.isEmpty()) {                    // We are missing an application icon, so add this to the                    // workspace                    added.add(app);                    // This is a rare event, so lets log it                    Log.e(TAG, "Missing Application on load: " + app);                }            }        }        if (!added.isEmpty()) {            addAndBindAddedWorkspaceItems(context, added,true);        }    }

总结:
需要注意的
1、创建单层桌面所须数据库
2、数据库增删改查
3、当第一次加载单层桌面时,应用合并
4、当安装应用时
5、当卸载应用时(原生逻辑)
6、当应用更新时(原生逻辑)
7、判断应用是否是要卸载或者图标删除
8、快捷图标创建细节
9、单双切换

交流群:196040873

原创粉丝点击