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 android.content.Context;
20 import android.content.SharedPreferences;
21 import android.util.Size;
22 
23 /**
24  * Class to handle SharedPreference for window magnification size.
25  */
26 final class WindowMagnificationFrameSizePrefs {
27 
28     private static final String WINDOW_MAGNIFICATION_PREFERENCES =
29             "window_magnification_preferences";
30     Context mContext;
31     SharedPreferences mWindowMagnificationSizePreferences;
32 
WindowMagnificationFrameSizePrefs(Context context)33     WindowMagnificationFrameSizePrefs(Context context) {
34         mContext = context;
35         mWindowMagnificationSizePreferences = mContext
36                 .getSharedPreferences(WINDOW_MAGNIFICATION_PREFERENCES, Context.MODE_PRIVATE);
37     }
38 
39     /**
40      * Uses smallest screen width DP as the key for preference.
41      */
getKey()42     private String getKey() {
43         return String.valueOf(
44                 mContext.getResources().getConfiguration().smallestScreenWidthDp);
45     }
46 
47     /**
48      * Saves the window frame size for current screen density.
49      */
saveSizeForCurrentDensity(Size size)50     public void saveSizeForCurrentDensity(Size size) {
51         mWindowMagnificationSizePreferences.edit()
52                 .putString(getKey(), size.toString()).apply();
53     }
54 
55     /**
56      * Check if there is a preference saved for current screen density.
57      *
58      * @return true if there is a preference saved for current screen density, false if it is unset.
59      */
isPreferenceSavedForCurrentDensity()60     public boolean isPreferenceSavedForCurrentDensity() {
61         return mWindowMagnificationSizePreferences.contains(getKey());
62     }
63 
64     /**
65      * Gets the size preference for current screen density.
66      */
getSizeForCurrentDensity()67     public Size getSizeForCurrentDensity() {
68         return Size.parseSize(mWindowMagnificationSizePreferences.getString(getKey(), null));
69     }
70 
71 }
72