• 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.GRID_TYPE;
23 import static com.android.launcher3.LauncherPrefs.HOTSEAT_COUNT;
24 import static com.android.launcher3.LauncherPrefs.WORKSPACE_SIZE;
25 
26 import android.content.Context;
27 import android.text.TextUtils;
28 
29 import com.android.launcher3.InvariantDeviceProfile;
30 import com.android.launcher3.LauncherPrefs;
31 import com.android.launcher3.logging.StatsLogManager.LauncherEvent;
32 
33 import java.util.Locale;
34 import java.util.Objects;
35 
36 /**
37  * Utility class representing persisted grid properties.
38  */
39 public class DeviceGridState implements Comparable<DeviceGridState> {
40 
41     public static final String KEY_WORKSPACE_SIZE = "migration_src_workspace_size";
42     public static final String KEY_HOTSEAT_COUNT = "migration_src_hotseat_count";
43     public static final String KEY_DEVICE_TYPE = "migration_src_device_type";
44     public static final String KEY_DB_FILE = "migration_src_db_file";
45     public static final String KEY_GRID_TYPE = "migration_src_grid_type";
46 
47     private final String mGridSizeString;
48     private final int mNumHotseat;
49     private final @DeviceType int mDeviceType;
50     private final String mDbFile;
51     private final int mGridType;
52 
DeviceGridState(int columns, int row, int numHotseat, int deviceType, String dbFile, int gridType)53     public DeviceGridState(int columns, int row, int numHotseat, int deviceType, String dbFile,
54             int gridType) {
55         mGridSizeString = String.format(Locale.ENGLISH, "%d,%d", columns, row);
56         mNumHotseat = numHotseat;
57         mDeviceType = deviceType;
58         mDbFile = dbFile;
59         mGridType = gridType;
60     }
61 
DeviceGridState(InvariantDeviceProfile idp)62     public DeviceGridState(InvariantDeviceProfile idp) {
63         mGridSizeString = String.format(Locale.ENGLISH, "%d,%d", idp.numColumns, idp.numRows);
64         mNumHotseat = idp.numDatabaseHotseatIcons;
65         mDeviceType = idp.deviceType;
66         mDbFile = idp.dbFile;
67         mGridType = idp.gridType;
68     }
69 
DeviceGridState(Context context)70     public DeviceGridState(Context context) {
71         this(LauncherPrefs.get(context));
72     }
73 
DeviceGridState(LauncherPrefs lp)74     public DeviceGridState(LauncherPrefs lp) {
75         mGridSizeString = lp.get(WORKSPACE_SIZE);
76         mNumHotseat = lp.get(HOTSEAT_COUNT);
77         mDeviceType = lp.get(DEVICE_TYPE);
78         mDbFile = lp.get(DB_FILE);
79         mGridType = lp.get(GRID_TYPE);
80     }
81 
82     /**
83      * Returns the device type for the grid
84      */
getDeviceType()85     public @DeviceType int getDeviceType() {
86         return mDeviceType;
87     }
88 
89     /**
90      * Returns the databaseFile for the grid.
91      */
getDbFile()92     public String getDbFile() {
93         return mDbFile;
94     }
95 
96     /**
97      * Returns the number of hotseat icons.
98      */
getNumHotseat()99     public int getNumHotseat() {
100         return mNumHotseat;
101     }
102 
103     /**
104      * Returns the grid type.
105      */
getGridType()106     public int getGridType() {
107         return mGridType;
108     }
109 
110     /**
111      * Stores the device state to shared preferences
112      */
writeToPrefs(Context context)113     public void writeToPrefs(Context context) {
114         LauncherPrefs.get(context).put(
115                 WORKSPACE_SIZE.to(mGridSizeString),
116                 HOTSEAT_COUNT.to(mNumHotseat),
117                 DEVICE_TYPE.to(mDeviceType),
118                 DB_FILE.to(mDbFile),
119                 GRID_TYPE.to(mGridType));
120 
121     }
122 
123     /**
124      * Returns the logging event corresponding to the grid state
125      */
getWorkspaceSizeEvent()126     public LauncherEvent getWorkspaceSizeEvent() {
127         if (!TextUtils.isEmpty(mGridSizeString)) {
128             switch (mGridSizeString) {
129                 case "2,2":
130                     return LauncherEvent.LAUNCHER_GRID_SIZE_2_BY_2;
131                 case "3,3":
132                     return LauncherEvent.LAUNCHER_GRID_SIZE_3_BY_3;
133                 case "4,4":
134                     return LauncherEvent.LAUNCHER_GRID_SIZE_4_BY_4;
135                 case "4,5":
136                     return LauncherEvent.LAUNCHER_GRID_SIZE_4_BY_5;
137                 case "4,6":
138                     return LauncherEvent.LAUNCHER_GRID_SIZE_4_BY_6;
139                 case "5,5":
140                     return LauncherEvent.LAUNCHER_GRID_SIZE_5_BY_5;
141                 case "5,6":
142                     return LauncherEvent.LAUNCHER_GRID_SIZE_5_BY_6;
143                 case "6,5":
144                     return LauncherEvent.LAUNCHER_GRID_SIZE_6_BY_5;
145             }
146         }
147         return null;
148     }
149 
150     @Override
toString()151     public String toString() {
152         return "DeviceGridState{"
153                 + "mGridSizeString='" + mGridSizeString + '\''
154                 + ", mNumHotseat=" + mNumHotseat
155                 + ", mDeviceType=" + mDeviceType
156                 + ", mDbFile=" + mDbFile
157                 + '}';
158     }
159 
160     /**
161      * Returns true if the database from another DeviceGridState can be loaded into the current
162      * DeviceGridState without migration, or false otherwise.
163      */
isCompatible(DeviceGridState other)164     public boolean isCompatible(DeviceGridState other) {
165         if (this == other) {
166             return true;
167         }
168         if (other == null) {
169             return false;
170         }
171         return Objects.equals(mDbFile, other.mDbFile);
172     }
173 
getColumns()174     public Integer getColumns() {
175         if (TextUtils.isEmpty(mGridSizeString)) {
176             return -1;
177         }
178         return Integer.parseInt(String.valueOf(mGridSizeString.split(",")[0]));
179     }
180 
getRows()181     public Integer getRows() {
182         if (TextUtils.isEmpty(mGridSizeString)) {
183             return -1;
184         }
185         return Integer.parseInt(String.valueOf(mGridSizeString.split(",")[1]));
186     }
187 
188     @Override
compareTo(DeviceGridState other)189     public int compareTo(DeviceGridState other) {
190         Integer size = getColumns() * getRows();
191         Integer otherSize = other.getColumns() * other.getRows();
192 
193         return size.compareTo(otherSize);
194     }
195 
196 }
197