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.database.ContentObserver; 22 import android.provider.Settings; 23 24 import androidx.annotation.IntDef; 25 26 import com.android.settings.R; 27 28 import com.google.common.primitives.Ints; 29 30 import java.lang.annotation.Retention; 31 import java.lang.annotation.RetentionPolicy; 32 33 /** Class to provide magnification capabilities. */ 34 public final class MagnificationCapabilities { 35 36 private static final String KEY_CAPABILITY = 37 Settings.Secure.ACCESSIBILITY_MAGNIFICATION_CAPABILITY; 38 39 /** 40 * Annotation for supported magnification mode. 41 * 42 * @see Settings.Secure#ACCESSIBILITY_MAGNIFICATION_CAPABILITY 43 */ 44 @Retention(RetentionPolicy.SOURCE) 45 @IntDef({ 46 MagnificationMode.NONE, 47 MagnificationMode.FULLSCREEN, 48 MagnificationMode.WINDOW, 49 MagnificationMode.ALL, 50 }) 51 52 public @interface MagnificationMode { 53 int NONE = Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_NONE; 54 int FULLSCREEN = Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_FULLSCREEN; 55 int WINDOW = Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_WINDOW; 56 int ALL = Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_ALL; 57 } 58 59 /** 60 * Gets the summary for the given {@code capabilities}. 61 * 62 * @param context A {@link Context}. 63 * @param capabilities Magnification capabilities {@link MagnificationMode} 64 * @return The summary text represents the given capabilities 65 */ getSummary(Context context, @MagnificationMode int capabilities)66 public static String getSummary(Context context, @MagnificationMode int capabilities) { 67 final String[] summaries = context.getResources().getStringArray( 68 R.array.magnification_mode_summaries); 69 final int[] values = context.getResources().getIntArray( 70 R.array.magnification_mode_values); 71 72 final int idx = Ints.indexOf(values, capabilities); 73 return summaries[idx == /* no index exist */ -1 ? 0 : idx]; 74 } 75 76 /** 77 * Sets the magnification capabilities {@link MagnificationMode} to settings key. This 78 * overwrites any existing capabilities. 79 * 80 * @param context A {@link Context}. 81 * @param capabilities Magnification capabilities {@link MagnificationMode} 82 */ setCapabilities(Context context, @MagnificationMode int capabilities)83 public static void setCapabilities(Context context, @MagnificationMode int capabilities) { 84 final ContentResolver contentResolver = context.getContentResolver(); 85 86 Settings.Secure.putIntForUser(contentResolver, KEY_CAPABILITY, capabilities, 87 contentResolver.getUserId()); 88 } 89 90 /** 91 * Returns the magnification capabilities {@link MagnificationMode} from setting's key. May be 92 * default value {@link MagnificationMode#FULLSCREEN} if not set. 93 * 94 * @param context A {@link Context}. 95 * @return The magnification capabilities {@link MagnificationMode} 96 */ 97 @MagnificationMode getCapabilities(Context context)98 public static int getCapabilities(Context context) { 99 final ContentResolver contentResolver = context.getContentResolver(); 100 101 return Settings.Secure.getIntForUser(contentResolver, KEY_CAPABILITY, 102 MagnificationMode.FULLSCREEN, contentResolver.getUserId()); 103 } 104 105 /** 106 * Register an observer class that gets callbacks when magnification capabilities changes. 107 * 108 * @param context A {@link Context}. 109 * @param contentObserver The object that receives callbacks when changes occur. 110 */ registerObserver(Context context, ContentObserver contentObserver)111 public static void registerObserver(Context context, ContentObserver contentObserver) { 112 context.getContentResolver().registerContentObserver( 113 Settings.Secure.getUriFor(KEY_CAPABILITY), 114 /* notifyForDescendants= */ false, 115 contentObserver); 116 } 117 118 /** 119 * Unregisters a magnification capabilities change observer. 120 * 121 * @param context A {@link Context}. 122 * @param contentObserver The previously registered observer that is no longer needed. 123 */ unregisterObserver(Context context, ContentObserver contentObserver)124 public static void unregisterObserver(Context context, ContentObserver contentObserver) { 125 context.getContentResolver().unregisterContentObserver(contentObserver); 126 } 127 MagnificationCapabilities()128 private MagnificationCapabilities() {} 129 } 130