• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.launcher3.model;
18 
19 import static com.android.launcher3.InvariantDeviceProfile.DeviceType;
20 import static com.android.launcher3.LauncherPrefs.DB_FILE;
21 import static com.android.launcher3.LauncherPrefs.DEVICE_TYPE;
22 import static com.android.launcher3.LauncherPrefs.HOTSEAT_COUNT;
23 import static com.android.launcher3.LauncherPrefs.WORKSPACE_SIZE;
24 import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_GRID_SIZE_2;
25 import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_GRID_SIZE_3;
26 import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_GRID_SIZE_4;
27 import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_GRID_SIZE_5;
28 import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_GRID_SIZE_6;
29 
30 import android.content.Context;
31 import android.text.TextUtils;
32 
33 import com.android.launcher3.InvariantDeviceProfile;
34 import com.android.launcher3.LauncherPrefs;
35 import com.android.launcher3.logging.StatsLogManager.LauncherEvent;
36 import com.android.launcher3.util.MainThreadInitializedObject.SandboxContext;
37 
38 import java.util.Locale;
39 import java.util.Objects;
40 
41 /**
42  * Utility class representing persisted grid properties.
43  */
44 public class DeviceGridState implements Comparable<DeviceGridState> {
45 
46     public static final String KEY_WORKSPACE_SIZE = "migration_src_workspace_size";
47     public static final String KEY_HOTSEAT_COUNT = "migration_src_hotseat_count";
48     public static final String KEY_DEVICE_TYPE = "migration_src_device_type";
49     public static final String KEY_DB_FILE = "migration_src_db_file";
50 
51     private final String mGridSizeString;
52     private final int mNumHotseat;
53     private final @DeviceType int mDeviceType;
54     private final String mDbFile;
55 
DeviceGridState(InvariantDeviceProfile idp)56     public DeviceGridState(InvariantDeviceProfile idp) {
57         mGridSizeString = String.format(Locale.ENGLISH, "%d,%d", idp.numColumns, idp.numRows);
58         mNumHotseat = idp.numDatabaseHotseatIcons;
59         mDeviceType = idp.deviceType;
60         mDbFile = idp.dbFile;
61     }
62 
DeviceGridState(Context context)63     public DeviceGridState(Context context) {
64         LauncherPrefs lp = LauncherPrefs.get(context);
65         mGridSizeString = lp.get(WORKSPACE_SIZE);
66         mNumHotseat = lp.get(HOTSEAT_COUNT);
67         mDeviceType = lp.get(DEVICE_TYPE);
68         mDbFile = lp.get(DB_FILE);
69     }
70 
71     /**
72      * Returns the device type for the grid
73      */
getDeviceType()74     public @DeviceType int getDeviceType() {
75         return mDeviceType;
76     }
77 
78     /**
79      * Returns the databaseFile for the grid.
80      */
getDbFile()81     public String getDbFile() {
82         return mDbFile;
83     }
84 
85     /**
86      * Returns the number of hotseat icons.
87      */
getNumHotseat()88     public int getNumHotseat() {
89         return mNumHotseat;
90     }
91 
92     /**
93      * Stores the device state to shared preferences
94      */
writeToPrefs(Context context)95     public void writeToPrefs(Context context) {
96         if (context instanceof SandboxContext) {
97             return;
98         }
99         LauncherPrefs.get(context).put(
100                 WORKSPACE_SIZE.to(mGridSizeString),
101                 HOTSEAT_COUNT.to(mNumHotseat),
102                 DEVICE_TYPE.to(mDeviceType),
103                 DB_FILE.to(mDbFile));
104     }
105 
106     /**
107      * Returns the logging event corresponding to the grid state
108      */
getWorkspaceSizeEvent()109     public LauncherEvent getWorkspaceSizeEvent() {
110         if (!TextUtils.isEmpty(mGridSizeString)) {
111             switch (getColumns()) {
112                 case 6:
113                     return LAUNCHER_GRID_SIZE_6;
114                 case 5:
115                     return LAUNCHER_GRID_SIZE_5;
116                 case 4:
117                     return LAUNCHER_GRID_SIZE_4;
118                 case 3:
119                     return LAUNCHER_GRID_SIZE_3;
120                 case 2:
121                     return LAUNCHER_GRID_SIZE_2;
122             }
123         }
124         return null;
125     }
126 
127     @Override
toString()128     public String toString() {
129         return "DeviceGridState{"
130                 + "mGridSizeString='" + mGridSizeString + '\''
131                 + ", mNumHotseat=" + mNumHotseat
132                 + ", mDeviceType=" + mDeviceType
133                 + ", mDbFile=" + mDbFile
134                 + '}';
135     }
136 
137     /**
138      * Returns true if the database from another DeviceGridState can be loaded into the current
139      * DeviceGridState without migration, or false otherwise.
140      */
isCompatible(DeviceGridState other)141     public boolean isCompatible(DeviceGridState other) {
142         if (this == other) {
143             return true;
144         }
145         if (other == null) {
146             return false;
147         }
148         return Objects.equals(mDbFile, other.mDbFile);
149     }
150 
getColumns()151     public Integer getColumns() {
152         return Integer.parseInt(String.valueOf(mGridSizeString.charAt(0)));
153     }
154 
getRows()155     public Integer getRows() {
156         return Integer.parseInt(String.valueOf(mGridSizeString.charAt(2)));
157     }
158 
159     @Override
compareTo(DeviceGridState other)160     public int compareTo(DeviceGridState other) {
161         Integer size = getColumns() * getRows();
162         Integer otherSize = other.getColumns() * other.getRows();
163 
164         return size.compareTo(otherSize);
165     }
166 
167 }
168