1 /* 2 * Copyright (C) 2023 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.systemui.accessibility; 18 19 import static com.android.systemui.accessibility.WindowMagnificationSettings.MagnificationSize; 20 21 import android.content.Context; 22 import android.content.SharedPreferences; 23 import android.util.Size; 24 25 /** 26 * Class to handle SharedPreference for window magnification size. 27 */ 28 final class WindowMagnificationFrameSizePrefs { 29 30 private static final String WINDOW_MAGNIFICATION_PREFERENCES = 31 "window_magnification_preferences"; 32 Context mContext; 33 SharedPreferences mWindowMagnificationSizePreferences; 34 WindowMagnificationFrameSizePrefs(Context context)35 WindowMagnificationFrameSizePrefs(Context context) { 36 mContext = context; 37 mWindowMagnificationSizePreferences = mContext 38 .getSharedPreferences(WINDOW_MAGNIFICATION_PREFERENCES, Context.MODE_PRIVATE); 39 } 40 41 /** 42 * Uses smallest screen width DP as the key for preference. 43 */ getKey()44 private String getKey() { 45 return String.valueOf( 46 mContext.getResources().getConfiguration().smallestScreenWidthDp); 47 } 48 49 /** 50 * Saves the window frame size for current screen density. 51 */ saveIndexAndSizeForCurrentDensity(int index, Size size)52 public void saveIndexAndSizeForCurrentDensity(int index, Size size) { 53 mWindowMagnificationSizePreferences 54 .edit() 55 .putString(getKey(), WindowMagnificationFrameSpec.serialize(index, size)) 56 .apply(); 57 } 58 59 /** 60 * Check if there is a preference saved for current screen density. 61 * 62 * @return true if there is a preference saved for current screen density, false if it is unset. 63 */ isPreferenceSavedForCurrentDensity()64 public boolean isPreferenceSavedForCurrentDensity() { 65 return mWindowMagnificationSizePreferences.contains(getKey()); 66 } 67 68 /** 69 * Gets the index preference for current screen density. Returns DEFAULT if no preference 70 * is found. 71 */ getIndexForCurrentDensity()72 public @MagnificationSize int getIndexForCurrentDensity() { 73 final String spec = mWindowMagnificationSizePreferences.getString(getKey(), null); 74 if (spec == null) { 75 return MagnificationSize.DEFAULT; 76 } 77 try { 78 return WindowMagnificationFrameSpec.deserialize(spec).getIndex(); 79 } catch (NumberFormatException e) { 80 return MagnificationSize.DEFAULT; 81 } 82 } 83 84 /** 85 * Gets the size preference for current screen density. 86 */ getSizeForCurrentDensity()87 public Size getSizeForCurrentDensity() { 88 return WindowMagnificationFrameSpec.deserialize( 89 mWindowMagnificationSizePreferences.getString(getKey(), null)) 90 .getSize(); 91 } 92 93 } 94