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 37 import java.util.Locale; 38 import java.util.Objects; 39 40 /** 41 * Utility class representing persisted grid properties. 42 */ 43 public class DeviceGridState implements Comparable<DeviceGridState> { 44 45 public static final String KEY_WORKSPACE_SIZE = "migration_src_workspace_size"; 46 public static final String KEY_HOTSEAT_COUNT = "migration_src_hotseat_count"; 47 public static final String KEY_DEVICE_TYPE = "migration_src_device_type"; 48 public static final String KEY_DB_FILE = "migration_src_db_file"; 49 50 private final String mGridSizeString; 51 private final int mNumHotseat; 52 private final @DeviceType int mDeviceType; 53 private final String mDbFile; 54 DeviceGridState(InvariantDeviceProfile idp)55 public DeviceGridState(InvariantDeviceProfile idp) { 56 mGridSizeString = String.format(Locale.ENGLISH, "%d,%d", idp.numColumns, idp.numRows); 57 mNumHotseat = idp.numDatabaseHotseatIcons; 58 mDeviceType = idp.deviceType; 59 mDbFile = idp.dbFile; 60 } 61 DeviceGridState(Context context)62 public DeviceGridState(Context context) { 63 LauncherPrefs lp = LauncherPrefs.get(context); 64 mGridSizeString = lp.get(WORKSPACE_SIZE); 65 mNumHotseat = lp.get(HOTSEAT_COUNT); 66 mDeviceType = lp.get(DEVICE_TYPE); 67 mDbFile = lp.get(DB_FILE); 68 } 69 70 /** 71 * Returns the device type for the grid 72 */ getDeviceType()73 public @DeviceType int getDeviceType() { 74 return mDeviceType; 75 } 76 77 /** 78 * Returns the databaseFile for the grid. 79 */ getDbFile()80 public String getDbFile() { 81 return mDbFile; 82 } 83 84 /** 85 * Returns the number of hotseat icons. 86 */ getNumHotseat()87 public int getNumHotseat() { 88 return mNumHotseat; 89 } 90 91 /** 92 * Stores the device state to shared preferences 93 */ writeToPrefs(Context context)94 public void writeToPrefs(Context context) { 95 LauncherPrefs.get(context).put( 96 WORKSPACE_SIZE.to(mGridSizeString), 97 HOTSEAT_COUNT.to(mNumHotseat), 98 DEVICE_TYPE.to(mDeviceType), 99 DB_FILE.to(mDbFile)); 100 } 101 102 /** 103 * Returns the logging event corresponding to the grid state 104 */ getWorkspaceSizeEvent()105 public LauncherEvent getWorkspaceSizeEvent() { 106 if (!TextUtils.isEmpty(mGridSizeString)) { 107 switch (getColumns()) { 108 case 6: 109 return LAUNCHER_GRID_SIZE_6; 110 case 5: 111 return LAUNCHER_GRID_SIZE_5; 112 case 4: 113 return LAUNCHER_GRID_SIZE_4; 114 case 3: 115 return LAUNCHER_GRID_SIZE_3; 116 case 2: 117 return LAUNCHER_GRID_SIZE_2; 118 } 119 } 120 return null; 121 } 122 123 @Override toString()124 public String toString() { 125 return "DeviceGridState{" 126 + "mGridSizeString='" + mGridSizeString + '\'' 127 + ", mNumHotseat=" + mNumHotseat 128 + ", mDeviceType=" + mDeviceType 129 + ", mDbFile=" + mDbFile 130 + '}'; 131 } 132 133 /** 134 * Returns true if the database from another DeviceGridState can be loaded into the current 135 * DeviceGridState without migration, or false otherwise. 136 */ isCompatible(DeviceGridState other)137 public boolean isCompatible(DeviceGridState other) { 138 if (this == other) { 139 return true; 140 } 141 if (other == null) { 142 return false; 143 } 144 return Objects.equals(mDbFile, other.mDbFile); 145 } 146 getColumns()147 public Integer getColumns() { 148 return Integer.parseInt(String.valueOf(mGridSizeString.charAt(0))); 149 } 150 getRows()151 public Integer getRows() { 152 return Integer.parseInt(String.valueOf(mGridSizeString.charAt(2))); 153 } 154 155 @Override compareTo(DeviceGridState other)156 public int compareTo(DeviceGridState other) { 157 Integer size = getColumns() * getRows(); 158 Integer otherSize = other.getColumns() * other.getRows(); 159 160 return size.compareTo(otherSize); 161 } 162 163 } 164