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.accessibilitymenu.activity;
18 
19 import android.app.ActionBar;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.SharedPreferences;
23 import android.content.pm.PackageManager;
24 import android.net.Uri;
25 import android.os.Bundle;
26 import android.provider.Browser;
27 import android.provider.Settings;
28 import android.view.View;
29 import android.widget.TextView;
30 import android.window.OnBackInvokedCallback;
31 
32 import androidx.annotation.Nullable;
33 import androidx.fragment.app.FragmentActivity;
34 import androidx.preference.Preference;
35 import androidx.preference.PreferenceFragmentCompat;
36 import androidx.preference.PreferenceManager;
37 
38 import com.android.systemui.accessibility.accessibilitymenu.R;
39 
40 /**
41  * Settings activity for AccessibilityMenu.
42  */
43 public class A11yMenuSettingsActivity extends FragmentActivity {
44     private OnBackInvokedCallback mCallback = () -> {
45         finish();
46     };
47 
48     @Override
onCreate(Bundle savedInstanceState)49     protected void onCreate(Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51         getSupportFragmentManager()
52                 .beginTransaction()
53                 .replace(android.R.id.content, new A11yMenuPreferenceFragment())
54                 .commit();
55 
56         ActionBar actionBar = getActionBar();
57         actionBar.setDisplayShowCustomEnabled(true);
58         actionBar.setDisplayHomeAsUpEnabled(true);
59         actionBar.setCustomView(R.layout.preferences_action_bar);
60         ((TextView) findViewById(R.id.action_bar_title)).setText(
61                 getResources().getString(R.string.accessibility_menu_settings_name)
62         );
63     }
64 
65     @Override
onNavigateUp()66     public boolean onNavigateUp() {
67         mCallback.onBackInvoked();
68         return true;
69     }
70 
71     /**
72      * Settings/preferences fragment for AccessibilityMenu.
73      */
74     public static class A11yMenuPreferenceFragment extends PreferenceFragmentCompat {
75         @Override
onCreatePreferences(Bundle bundle, String s)76         public void onCreatePreferences(Bundle bundle, String s) {
77             setPreferencesFromResource(R.xml.accessibilitymenu_preferences, s);
78             initializeHelpAndFeedbackPreference();
79         }
80 
81         @Override
onViewCreated(View view, @Nullable Bundle savedInstanceState)82         public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
83             super.onViewCreated(view, savedInstanceState);
84             view.setLayoutDirection(
85                     view.getResources().getConfiguration().getLayoutDirection());
86         }
87 
88         /**
89          * Returns large buttons settings state.
90          *
91          * @param context The parent context
92          * @return {@code true} large button is enabled; {@code false} large button is disabled
93          */
isLargeButtonsEnabled(Context context)94         public static boolean isLargeButtonsEnabled(Context context) {
95             SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
96             String key = context.getResources().getString(R.string.pref_large_buttons);
97             return prefs.getBoolean(key, false);
98         }
99 
initializeHelpAndFeedbackPreference()100         private void initializeHelpAndFeedbackPreference() {
101             final Preference prefHelp = findPreference(getString(R.string.pref_help));
102             if (prefHelp != null) {
103                 // Do not allow access to web during setup.
104                 if (Settings.Secure.getInt(
105                         getContext().getContentResolver(),
106                         Settings.Secure.USER_SETUP_COMPLETE, 0) != 1) {
107                     return;
108                 }
109 
110                 // Configure preference to open the help page in the default web browser.
111                 // If the system has no browser, hide the preference.
112                 Uri uri = Uri.parse(getResources().getString(R.string.help_url));
113                 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
114                 intent.putExtra(Browser.EXTRA_APPLICATION_ID, getContext().getPackageName());
115                 if (getActivity().getPackageManager().queryIntentActivities(
116                         intent, PackageManager.ResolveInfoFlags.of(0)).isEmpty()) {
117                     prefHelp.setVisible(false);
118                     return;
119                 }
120                 prefHelp.setIntent(intent);
121             }
122         }
123     }
124 }
125