1 /*
2  * Copyright (C) 2020 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.settings.accessibility;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.provider.Settings;
22 
23 import androidx.annotation.IntDef;
24 
25 import com.android.settings.R;
26 
27 import com.google.common.primitives.Ints;
28 
29 import java.lang.annotation.Retention;
30 import java.lang.annotation.RetentionPolicy;
31 
32 /** Class to provide magnification capabilities. */
33 public final class MagnificationCapabilities {
34 
35     private static final String KEY_CAPABILITY =
36             Settings.Secure.ACCESSIBILITY_MAGNIFICATION_CAPABILITY;
37 
38     /**
39      * Annotation for supported magnification mode.
40      *
41      * @see Settings.Secure#ACCESSIBILITY_MAGNIFICATION_CAPABILITY
42      */
43     @Retention(RetentionPolicy.SOURCE)
44     @IntDef({
45             MagnificationMode.NONE,
46             MagnificationMode.FULLSCREEN,
47             MagnificationMode.WINDOW,
48             MagnificationMode.ALL,
49     })
50 
51     public @interface MagnificationMode {
52         int NONE = Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_NONE;
53         int FULLSCREEN = Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_FULLSCREEN;
54         int WINDOW = Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_WINDOW;
55         int ALL = Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_ALL;
56     }
57 
58     /**
59      * Gets the summary for the given {@code capabilities}.
60      *
61      * @param context A {@link Context}.
62      * @param capabilities Magnification capabilities {@link MagnificationMode}
63      * @return The summary text represents the given capabilities
64      */
getSummary(Context context, @MagnificationMode int capabilities)65     public static String getSummary(Context context, @MagnificationMode int capabilities) {
66         final String[] summaries = context.getResources().getStringArray(
67                 R.array.magnification_mode_summaries);
68         final int[] values = context.getResources().getIntArray(
69                 R.array.magnification_mode_values);
70 
71         final int idx = Ints.indexOf(values, capabilities);
72         return summaries[idx == /* no index exist */ -1 ? 0 : idx];
73     }
74 
75     /**
76      * Sets the magnification capabilities {@link MagnificationMode} to settings key. This
77      * overwrites any existing capabilities.
78      *
79      * @param context      A {@link Context}.
80      * @param capabilities Magnification capabilities {@link MagnificationMode}
81      */
setCapabilities(Context context, @MagnificationMode int capabilities)82     public static void setCapabilities(Context context, @MagnificationMode int capabilities) {
83         final ContentResolver contentResolver = context.getContentResolver();
84 
85         Settings.Secure.putIntForUser(contentResolver, KEY_CAPABILITY, capabilities,
86                 contentResolver.getUserId());
87     }
88 
89     /**
90      * Returns the magnification capabilities {@link MagnificationMode} from setting's key. May be
91      * default value {@link MagnificationMode#FULLSCREEN} if not set.
92      *
93      * @param context A {@link Context}.
94      * @return The magnification capabilities {@link MagnificationMode}
95      */
96     @MagnificationMode
getCapabilities(Context context)97     public static int getCapabilities(Context context) {
98         final ContentResolver contentResolver = context.getContentResolver();
99 
100         return Settings.Secure.getIntForUser(contentResolver, KEY_CAPABILITY,
101                 MagnificationMode.FULLSCREEN, contentResolver.getUserId());
102     }
103 
MagnificationCapabilities()104     private MagnificationCapabilities() {}
105 }
106