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.settings.accessibility.shortcuts;
18 
19 import android.content.Context;
20 import android.graphics.drawable.Drawable;
21 import android.text.Spannable;
22 import android.text.SpannableString;
23 import android.text.SpannableStringBuilder;
24 import android.text.style.ImageSpan;
25 
26 import androidx.annotation.NonNull;
27 import androidx.preference.Preference;
28 import androidx.preference.PreferenceScreen;
29 
30 import com.android.settings.R;
31 import com.android.settings.accessibility.AccessibilityUtil;
32 
33 /**
34  * A controller handles displaying the nav button shortcut option preference and
35  * configuring the shortcut.
36  */
37 public class NavButtonShortcutOptionController extends SoftwareShortcutOptionPreferenceController {
38 
NavButtonShortcutOptionController(Context context, String preferenceKey)39     public NavButtonShortcutOptionController(Context context, String preferenceKey) {
40         super(context, preferenceKey);
41     }
42 
43     @Override
displayPreference(PreferenceScreen screen)44     public void displayPreference(PreferenceScreen screen) {
45         super.displayPreference(screen);
46         final Preference preference = screen.findPreference(getPreferenceKey());
47         if (preference instanceof ShortcutOptionPreference shortcutOptionPreference) {
48             shortcutOptionPreference.setTitle(
49                     R.string.accessibility_shortcut_edit_dialog_title_software);
50             shortcutOptionPreference.setIntroImageResId(
51                     R.drawable.accessibility_shortcut_type_navbar);
52             shortcutOptionPreference.setSummaryProvider(
53                     new Preference.SummaryProvider<ShortcutOptionPreference>() {
54                         @Override
55                         public CharSequence provideSummary(
56                                 @NonNull ShortcutOptionPreference preference) {
57                             return getSummary(preference.getSummaryTextLineHeight());
58                         }
59                     });
60         }
61     }
62 
63     @Override
isShortcutAvailable()64     protected boolean isShortcutAvailable() {
65         return !AccessibilityUtil.isFloatingMenuEnabled(mContext)
66                 && !AccessibilityUtil.isGestureNavigateEnabled(mContext);
67     }
68 
getSummary(int lineHeight)69     private CharSequence getSummary(int lineHeight) {
70         final SpannableStringBuilder sb = new SpannableStringBuilder();
71         sb.append(getSummaryStringWithIcon(lineHeight));
72 
73         if (!isInSetupWizard()) {
74             sb.append("\n\n");
75             sb.append(getCustomizeAccessibilityButtonLink());
76         }
77 
78         return sb;
79     }
80 
getSummaryStringWithIcon(int lineHeight)81     private SpannableString getSummaryStringWithIcon(int lineHeight) {
82         final String summary = mContext
83                 .getString(R.string.accessibility_shortcut_edit_dialog_summary_software);
84         final SpannableString spannableMessage = SpannableString.valueOf(summary);
85 
86         // Icon
87         final int indexIconStart = summary.indexOf("%s");
88         final int indexIconEnd = indexIconStart + 2;
89         final Drawable icon = mContext.getDrawable(R.drawable.ic_accessibility_new);
90         final ImageSpan imageSpan = new ImageSpan(icon);
91         imageSpan.setContentDescription("");
92         icon.setBounds(
93                 /* left= */ 0, /* top= */ 0, /* right= */ lineHeight, /* bottom= */ lineHeight);
94         spannableMessage.setSpan(
95                 imageSpan, indexIconStart, indexIconEnd,
96                 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
97         return spannableMessage;
98     }
99 }
100