1 /* 2 * Copyright (C) 2019 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 static com.android.settings.accessibility.ItemInfoArrayAdapter.ItemInfo; 20 21 import android.app.Dialog; 22 import android.content.Context; 23 import android.content.DialogInterface; 24 import android.view.View; 25 import android.widget.AbsListView; 26 import android.widget.AdapterView; 27 import android.widget.ListAdapter; 28 import android.widget.ListView; 29 import android.widget.ScrollView; 30 31 import androidx.annotation.NonNull; 32 import androidx.annotation.Nullable; 33 import androidx.appcompat.app.AlertDialog; 34 35 import java.lang.annotation.Retention; 36 import java.lang.annotation.RetentionPolicy; 37 import java.util.List; 38 import java.util.Optional; 39 40 /** 41 * Utility class for creating the edit dialog. 42 */ 43 public class AccessibilityDialogUtils { 44 private static final String TAG = AccessibilityDialogUtils.class.getSimpleName(); 45 46 /** Denotes the dialog enums for show dialog. */ 47 @Retention(RetentionPolicy.SOURCE) 48 public @interface DialogEnums { 49 /** 50 * OPEN: Settings > Accessibility > Downloaded toggle service > Toggle use service to 51 * enable service. 52 */ 53 int ENABLE_WARNING_FROM_TOGGLE = 1002; 54 55 /** 56 * OPEN: Settings > Accessibility > Downloaded toggle service > Shortcut options 57 * settings. 58 */ 59 int ENABLE_WARNING_FROM_SHORTCUT = 1003; 60 61 /** 62 * OPEN: Settings > Accessibility > Downloaded toggle service > Shortcut toggle 63 */ 64 int ENABLE_WARNING_FROM_SHORTCUT_TOGGLE = 1004; 65 66 /** 67 * OPEN: Settings > Accessibility > Downloaded toggle service > Toggle use service to 68 * disable service. 69 */ 70 int DISABLE_WARNING_FROM_TOGGLE = 1005; 71 72 /** 73 * OPEN: Settings > Accessibility > Magnification > Toggle user service in button 74 * navigation. 75 */ 76 int ACCESSIBILITY_BUTTON_TUTORIAL = 1006; 77 78 /** 79 * OPEN: Settings > Accessibility > Magnification > Toggle user service in gesture 80 * navigation. 81 */ 82 int GESTURE_NAVIGATION_TUTORIAL = 1007; 83 84 /** 85 * OPEN: Settings > Accessibility > Downloaded toggle service > Toggle user service > Show 86 * launch tutorial. 87 */ 88 int LAUNCH_ACCESSIBILITY_TUTORIAL = 1008; 89 90 /** 91 * OPEN: Settings > Accessibility > Display size and text > Click 'Reset settings' button. 92 */ 93 int DIALOG_RESET_SETTINGS = 1009; 94 95 /** 96 * OPEN: Settings > Accessibility > Magnification > Magnification type. 97 */ 98 int DIALOG_MAGNIFICATION_MODE = 1010; 99 100 /** 101 * Enable: Settings > Accessibility > Magnification > Magnification shortcut > Advanced > 102 * Triple tap. 103 * OPEN: Settings > Accessibility > Magnification > Magnification type > Magnify part of 104 * screen / Switch between full and partial screen > Save. 105 */ 106 int DIALOG_MAGNIFICATION_TRIPLE_TAP_WARNING = 1011; 107 108 /** 109 * OPEN: Settings > Accessibility > Magnification > Cursor following. 110 */ 111 int DIALOG_MAGNIFICATION_CURSOR_FOLLOWING_MODE = 1012; 112 } 113 114 /** 115 * Sets the scroll indicators for dialog view. The indicators appear while content view is 116 * out of vision for vertical scrolling. 117 * 118 * @param view The view contains customized dialog content. Usually it is {@link ScrollView} or 119 * {@link AbsListView} 120 */ setScrollIndicators(@onNull View view)121 private static void setScrollIndicators(@NonNull View view) { 122 view.setScrollIndicators( 123 View.SCROLL_INDICATOR_TOP | View.SCROLL_INDICATOR_BOTTOM, 124 View.SCROLL_INDICATOR_TOP | View.SCROLL_INDICATOR_BOTTOM); 125 } 126 127 /** 128 * Creates a dialog with the given view. 129 * 130 * @param context A valid context 131 * @param dialogTitle The title of the dialog 132 * @param customView The customized view 133 * @param positiveButtonText The text of the positive button 134 * @param positiveListener This listener will be invoked when the positive button in the dialog 135 * is clicked 136 * @param negativeButtonText The text of the negative button 137 * @param negativeListener This listener will be invoked when the negative button in the dialog 138 * is clicked 139 * @return the {@link Dialog} with the given view 140 */ 141 @NonNull createCustomDialog(@onNull Context context, @NonNull CharSequence dialogTitle, @Nullable View customView, @NonNull CharSequence positiveButtonText, @Nullable DialogInterface.OnClickListener positiveListener, @NonNull CharSequence negativeButtonText, @Nullable DialogInterface.OnClickListener negativeListener)142 public static Dialog createCustomDialog(@NonNull Context context, 143 @NonNull CharSequence dialogTitle, @Nullable View customView, 144 @NonNull CharSequence positiveButtonText, 145 @Nullable DialogInterface.OnClickListener positiveListener, 146 @NonNull CharSequence negativeButtonText, 147 @Nullable DialogInterface.OnClickListener negativeListener) { 148 DialogInterface.OnClickListener doNothingListener = 149 (DialogInterface dialogInterface, int which) -> {}; 150 final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context) 151 .setTitle(dialogTitle) 152 .setCancelable(true) 153 .setPositiveButton(positiveButtonText, 154 Optional.ofNullable(positiveListener).orElse(doNothingListener)) 155 .setNegativeButton(negativeButtonText, 156 Optional.ofNullable(negativeListener).orElse(doNothingListener)); 157 if (customView != null) { 158 dialogBuilder.setView(customView); 159 } 160 final AlertDialog alertDialog = dialogBuilder.create(); 161 if (customView instanceof ScrollView || customView instanceof AbsListView) { 162 setScrollIndicators(customView); 163 } 164 return alertDialog; 165 } 166 167 /** 168 * Creates a single choice {@link ListView} with given {@link ItemInfo} list. 169 * 170 * @param context A context. 171 * @param itemInfoList A {@link ItemInfo} list. 172 * @param itemListener The listener will be invoked when the item is clicked. 173 */ 174 @NonNull createSingleChoiceListView(@onNull Context context, @NonNull List<? extends ItemInfo> itemInfoList, @Nullable AdapterView.OnItemClickListener itemListener)175 public static ListView createSingleChoiceListView(@NonNull Context context, 176 @NonNull List<? extends ItemInfo> itemInfoList, 177 @Nullable AdapterView.OnItemClickListener itemListener) { 178 final ListView list = new ListView(context); 179 // Set an id to save its state. 180 list.setId(android.R.id.list); 181 list.setDivider(/* divider= */ null); 182 list.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 183 final ListAdapter adapter = new ItemInfoArrayAdapter<>(context, itemInfoList); 184 list.setAdapter(adapter); 185 list.setOnItemClickListener(itemListener); 186 return list; 187 } 188 } 189