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.InvariantDeviceProfile.TYPE_PHONE; 21 import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_GRID_SIZE_2; 22 import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_GRID_SIZE_3; 23 import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_GRID_SIZE_4; 24 import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_GRID_SIZE_5; 25 26 import android.content.Context; 27 import android.content.SharedPreferences; 28 import android.text.TextUtils; 29 30 import com.android.launcher3.InvariantDeviceProfile; 31 import com.android.launcher3.Utilities; 32 import com.android.launcher3.logging.StatsLogManager.LauncherEvent; 33 34 import java.util.Locale; 35 import java.util.Objects; 36 37 /** 38 * Utility class representing persisted grid properties. 39 */ 40 public class DeviceGridState { 41 42 public static final String KEY_WORKSPACE_SIZE = "migration_src_workspace_size"; 43 public static final String KEY_HOTSEAT_COUNT = "migration_src_hotseat_count"; 44 public static final String KEY_DEVICE_TYPE = "migration_src_device_type"; 45 46 private final String mGridSizeString; 47 private final int mNumHotseat; 48 private final @DeviceType int mDeviceType; 49 DeviceGridState(InvariantDeviceProfile idp)50 public DeviceGridState(InvariantDeviceProfile idp) { 51 mGridSizeString = String.format(Locale.ENGLISH, "%d,%d", idp.numColumns, idp.numRows); 52 mNumHotseat = idp.numDatabaseHotseatIcons; 53 mDeviceType = idp.deviceType; 54 } 55 DeviceGridState(Context context)56 public DeviceGridState(Context context) { 57 SharedPreferences prefs = Utilities.getPrefs(context); 58 mGridSizeString = prefs.getString(KEY_WORKSPACE_SIZE, ""); 59 mNumHotseat = prefs.getInt(KEY_HOTSEAT_COUNT, -1); 60 mDeviceType = prefs.getInt(KEY_DEVICE_TYPE, TYPE_PHONE); 61 } 62 63 /** 64 * Returns the device type for the grid 65 */ getDeviceType()66 public @DeviceType int getDeviceType() { 67 return mDeviceType; 68 } 69 70 /** 71 * Stores the device state to shared preferences 72 */ writeToPrefs(Context context)73 public void writeToPrefs(Context context) { 74 Utilities.getPrefs(context).edit() 75 .putString(KEY_WORKSPACE_SIZE, mGridSizeString) 76 .putInt(KEY_HOTSEAT_COUNT, mNumHotseat) 77 .putInt(KEY_DEVICE_TYPE, mDeviceType) 78 .apply(); 79 } 80 81 /** 82 * Returns the logging event corresponding to the grid state 83 */ getWorkspaceSizeEvent()84 public LauncherEvent getWorkspaceSizeEvent() { 85 if (!TextUtils.isEmpty(mGridSizeString)) { 86 switch (mGridSizeString.charAt(0)) { 87 case '5': 88 return LAUNCHER_GRID_SIZE_5; 89 case '4': 90 return LAUNCHER_GRID_SIZE_4; 91 case '3': 92 return LAUNCHER_GRID_SIZE_3; 93 case '2': 94 return LAUNCHER_GRID_SIZE_2; 95 } 96 } 97 return null; 98 } 99 100 @Override toString()101 public String toString() { 102 return "DeviceGridState{" 103 + "mGridSizeString='" + mGridSizeString + '\'' 104 + ", mNumHotseat=" + mNumHotseat 105 + ", mDeviceType=" + mDeviceType 106 + '}'; 107 } 108 109 /** 110 * Returns true if the database from another DeviceGridState can be loaded into the current 111 * DeviceGridState without migration, or false otherwise. 112 */ isCompatible(DeviceGridState other)113 public boolean isCompatible(DeviceGridState other) { 114 if (this == other) return true; 115 if (other == null) return false; 116 return mNumHotseat == other.mNumHotseat 117 && Objects.equals(mGridSizeString, other.mGridSizeString); 118 } 119 } 120