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(int columns, int row, int numHotseat, int deviceType, String dbFile)56 public DeviceGridState(int columns, int row, int numHotseat, int deviceType, String dbFile) { 57 mGridSizeString = String.format(Locale.ENGLISH, "%d,%d", columns, row); 58 mNumHotseat = numHotseat; 59 mDeviceType = deviceType; 60 mDbFile = dbFile; 61 } 62 DeviceGridState(InvariantDeviceProfile idp)63 public DeviceGridState(InvariantDeviceProfile idp) { 64 mGridSizeString = String.format(Locale.ENGLISH, "%d,%d", idp.numColumns, idp.numRows); 65 mNumHotseat = idp.numDatabaseHotseatIcons; 66 mDeviceType = idp.deviceType; 67 mDbFile = idp.dbFile; 68 } 69 DeviceGridState(Context context)70 public DeviceGridState(Context context) { 71 LauncherPrefs lp = LauncherPrefs.get(context); 72 mGridSizeString = lp.get(WORKSPACE_SIZE); 73 mNumHotseat = lp.get(HOTSEAT_COUNT); 74 mDeviceType = lp.get(DEVICE_TYPE); 75 mDbFile = lp.get(DB_FILE); 76 } 77 78 /** 79 * Returns the device type for the grid 80 */ getDeviceType()81 public @DeviceType int getDeviceType() { 82 return mDeviceType; 83 } 84 85 /** 86 * Returns the databaseFile for the grid. 87 */ getDbFile()88 public String getDbFile() { 89 return mDbFile; 90 } 91 92 /** 93 * Returns the number of hotseat icons. 94 */ getNumHotseat()95 public int getNumHotseat() { 96 return mNumHotseat; 97 } 98 99 /** 100 * Stores the device state to shared preferences 101 */ writeToPrefs(Context context)102 public void writeToPrefs(Context context) { 103 if (context instanceof SandboxContext) { 104 return; 105 } 106 LauncherPrefs.get(context).put( 107 WORKSPACE_SIZE.to(mGridSizeString), 108 HOTSEAT_COUNT.to(mNumHotseat), 109 DEVICE_TYPE.to(mDeviceType), 110 DB_FILE.to(mDbFile)); 111 } 112 113 /** 114 * Returns the logging event corresponding to the grid state 115 */ getWorkspaceSizeEvent()116 public LauncherEvent getWorkspaceSizeEvent() { 117 if (!TextUtils.isEmpty(mGridSizeString)) { 118 switch (getColumns()) { 119 case 6: 120 return LAUNCHER_GRID_SIZE_6; 121 case 5: 122 return LAUNCHER_GRID_SIZE_5; 123 case 4: 124 return LAUNCHER_GRID_SIZE_4; 125 case 3: 126 return LAUNCHER_GRID_SIZE_3; 127 case 2: 128 return LAUNCHER_GRID_SIZE_2; 129 } 130 } 131 return null; 132 } 133 134 @Override toString()135 public String toString() { 136 return "DeviceGridState{" 137 + "mGridSizeString='" + mGridSizeString + '\'' 138 + ", mNumHotseat=" + mNumHotseat 139 + ", mDeviceType=" + mDeviceType 140 + ", mDbFile=" + mDbFile 141 + '}'; 142 } 143 144 /** 145 * Returns true if the database from another DeviceGridState can be loaded into the current 146 * DeviceGridState without migration, or false otherwise. 147 */ isCompatible(DeviceGridState other)148 public boolean isCompatible(DeviceGridState other) { 149 if (this == other) { 150 return true; 151 } 152 if (other == null) { 153 return false; 154 } 155 return Objects.equals(mDbFile, other.mDbFile); 156 } 157 getColumns()158 public Integer getColumns() { 159 return Integer.parseInt(String.valueOf(mGridSizeString.split(",")[0])); 160 } 161 getRows()162 public Integer getRows() { 163 return Integer.parseInt(String.valueOf(mGridSizeString.split(",")[1])); 164 } 165 166 @Override compareTo(DeviceGridState other)167 public int compareTo(DeviceGridState other) { 168 Integer size = getColumns() * getRows(); 169 Integer otherSize = other.getColumns() * other.getRows(); 170 171 return size.compareTo(otherSize); 172 } 173 174 } 175