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