• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.graphics.Insets;
25 import android.net.Uri;
26 import android.os.Bundle;
27 import android.provider.Browser;
28 import android.provider.Settings;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.view.WindowInsets;
32 import android.widget.TextView;
33 import android.window.OnBackInvokedCallback;
34 
35 import androidx.annotation.NonNull;
36 import androidx.annotation.Nullable;
37 import androidx.fragment.app.FragmentActivity;
38 import androidx.preference.Preference;
39 import androidx.preference.PreferenceFragmentCompat;
40 import androidx.preference.PreferenceManager;
41 
42 import com.android.systemui.accessibility.accessibilitymenu.R;
43 
44 /**
45  * Settings activity for AccessibilityMenu.
46  */
47 public class A11yMenuSettingsActivity extends FragmentActivity {
48     private OnBackInvokedCallback mCallback = () -> {
49         finish();
50     };
51 
52     @Override
onCreate(Bundle savedInstanceState)53     protected void onCreate(Bundle savedInstanceState) {
54         super.onCreate(savedInstanceState);
55         getSupportFragmentManager()
56                 .beginTransaction()
57                 .replace(android.R.id.content, new A11yMenuPreferenceFragment())
58                 .commit();
59 
60         ActionBar actionBar = getActionBar();
61         actionBar.setDisplayShowCustomEnabled(true);
62         actionBar.setDisplayHomeAsUpEnabled(true);
63         actionBar.setCustomView(R.layout.preferences_action_bar);
64         ((TextView) findViewById(R.id.action_bar_title)).setText(
65                 getResources().getString(R.string.accessibility_menu_settings_name)
66         );
67         setHeightWrapContent(findViewById(com.android.internal.R.id.action_bar));
68         setHeightWrapContent(findViewById(com.android.internal.R.id.action_bar_container));
69     }
70 
setHeightWrapContent(View view)71     private void setHeightWrapContent(View view) {
72         if (view != null) {
73             ViewGroup.LayoutParams params = view.getLayoutParams();
74             params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
75             view.setLayoutParams(params);
76         }
77     }
78 
79     @Override
onNavigateUp()80     public boolean onNavigateUp() {
81         mCallback.onBackInvoked();
82         return true;
83     }
84 
85     /**
86      * Settings/preferences fragment for AccessibilityMenu.
87      */
88     public static class A11yMenuPreferenceFragment extends PreferenceFragmentCompat {
89         @Override
onCreatePreferences(Bundle bundle, String s)90         public void onCreatePreferences(Bundle bundle, String s) {
91             setPreferencesFromResource(R.xml.accessibilitymenu_preferences, s);
92             initializeHelpAndFeedbackPreference();
93         }
94 
95         @Override
onViewCreated(View view, @Nullable Bundle savedInstanceState)96         public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
97             super.onViewCreated(view, savedInstanceState);
98             view.setLayoutDirection(
99                     view.getResources().getConfiguration().getLayoutDirection());
100             view.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
101                 @NonNull
102                 @Override
103                 public WindowInsets onApplyWindowInsets(@NonNull View v,
104                         @NonNull WindowInsets windowInsets) {
105                     Insets insets = windowInsets.getInsets(WindowInsets.Type.systemBars()
106                             | WindowInsets.Type.navigationBars()
107                             | WindowInsets.Type.displayCutout());
108                     v.setPadding(insets.left, insets.top, insets.right, insets.bottom);
109                     return WindowInsets.CONSUMED;
110                 }
111             });
112         }
113 
114         /**
115          * Returns large buttons settings state.
116          *
117          * @param context The parent context
118          * @return {@code true} large button is enabled; {@code false} large button is disabled
119          */
isLargeButtonsEnabled(Context context)120         public static boolean isLargeButtonsEnabled(Context context) {
121             SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
122             String key = context.getResources().getString(R.string.pref_large_buttons);
123             return prefs.getBoolean(key, false);
124         }
125 
initializeHelpAndFeedbackPreference()126         private void initializeHelpAndFeedbackPreference() {
127             final Preference prefHelp = findPreference(getString(R.string.pref_help));
128             if (prefHelp != null) {
129                 // Do not allow access to web during setup.
130                 if (Settings.Secure.getInt(
131                         getContext().getContentResolver(),
132                         Settings.Secure.USER_SETUP_COMPLETE, 0) != 1) {
133                     return;
134                 }
135 
136                 // Configure preference to open the help page in the default web browser.
137                 // If the system has no browser, hide the preference.
138                 Uri uri = Uri.parse(getResources().getString(R.string.help_url));
139                 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
140                 intent.putExtra(Browser.EXTRA_APPLICATION_ID, getContext().getPackageName());
141                 if (getActivity().getPackageManager().queryIntentActivities(
142                         intent, PackageManager.ResolveInfoFlags.of(0)).isEmpty()) {
143                     prefHelp.setVisible(false);
144                     return;
145                 }
146                 prefHelp.setIntent(intent);
147             }
148         }
149     }
150 }
151