1 /*
2  * Copyright (C) 2016 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.AccessibilityScreenSizeForSetupWizardActivity.VISION_FRAGMENT_NO;
20 
21 import android.content.ComponentName;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.util.Log;
25 import android.view.Menu;
26 import android.view.accessibility.AccessibilityEvent;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceFragmentCompat;
31 
32 import com.android.settings.R;
33 import com.android.settings.SettingsActivity;
34 import com.android.settings.SetupWizardUtils;
35 import com.android.settings.accessibility.AccessibilityScreenSizeForSetupWizardActivity.FragmentType;
36 import com.android.settings.core.SubSettingLauncher;
37 import com.android.settings.search.actionbar.SearchMenuController;
38 import com.android.settings.support.actionbar.HelpResourceProvider;
39 import com.android.settingslib.core.instrumentation.Instrumentable;
40 import com.android.settingslib.transition.SettingsTransitionHelper;
41 
42 import com.google.android.setupcompat.util.WizardManagerHelper;
43 import com.google.android.setupdesign.util.ThemeHelper;
44 
45 public class AccessibilitySettingsForSetupWizardActivity extends SettingsActivity {
46 
47     private static final String LOG_TAG = "A11ySettingsForSUW";
48     private static final String SAVE_KEY_TITLE = "activity_title";
49 
50     @VisibleForTesting
51     static final String CLASS_NAME_FONT_SIZE_SETTINGS_FOR_SUW =
52             "com.android.settings.FontSizeSettingsForSetupWizardActivity";
53 
54     @Override
onSaveInstanceState(Bundle savedState)55     protected void onSaveInstanceState(Bundle savedState) {
56         savedState.putCharSequence(SAVE_KEY_TITLE, getTitle());
57         super.onSaveInstanceState(savedState);
58     }
59 
60     @Override
onRestoreInstanceState(Bundle savedState)61     protected void onRestoreInstanceState(Bundle savedState) {
62         super.onRestoreInstanceState(savedState);
63         setTitle(savedState.getCharSequence(SAVE_KEY_TITLE));
64     }
65 
66     @Override
onCreateOptionsMenu(Menu menu)67     public boolean onCreateOptionsMenu(Menu menu) {
68         // Return true, so we get notified when items in the menu are clicked.
69         return true;
70     }
71 
72     @Override
onNavigateUp()73     public boolean onNavigateUp() {
74         onBackPressed();
75 
76         // Clear accessibility focus and let the screen reader announce the new title.
77         getWindow().getDecorView()
78                 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
79 
80         return true;
81     }
82 
83     @Override
onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref)84     public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
85         Bundle args = pref.getExtras();
86         if (args == null) {
87             args = new Bundle();
88         }
89         args.putInt(HelpResourceProvider.HELP_URI_RESOURCE_KEY, 0);
90         args.putBoolean(SearchMenuController.NEED_SEARCH_ICON_IN_ACTION_BAR, false);
91         new SubSettingLauncher(this)
92                 .setDestination(pref.getFragment())
93                 .setArguments(args)
94                 .setSourceMetricsCategory(caller instanceof Instrumentable
95                         ? ((Instrumentable) caller).getMetricsCategory()
96                         : Instrumentable.METRICS_CATEGORY_UNKNOWN)
97                 .setExtras(SetupWizardUtils.copyLifecycleExtra(getIntent().getExtras(),
98                         new Bundle()))
99                 .setTransitionType(SettingsTransitionHelper.TransitionType.TRANSITION_FADE)
100                 .launch();
101         return true;
102     }
103 
104     @Override
onCreate(Bundle savedState)105     protected void onCreate(Bundle savedState) {
106         super.onCreate(savedState);
107         applyTheme();
108         tryLaunchFontSizeSettings();
109         findViewById(R.id.content_parent).setFitsSystemWindows(false);
110     }
111 
applyTheme()112     private void applyTheme() {
113         if (ThemeHelper.trySetDynamicColor(this)) {
114             final int appliedTheme = ThemeHelper.isSetupWizardDayNightEnabled(this)
115                     ? R.style.SudDynamicColorThemeSettings_SetupWizard_DayNight
116                     : R.style.SudDynamicColorThemeSettings_SetupWizard;
117             setTheme(appliedTheme);
118         } else {
119             setTheme(SetupWizardUtils.getTheme(this, getIntent()));
120         }
121     }
122 
123     @VisibleForTesting
tryLaunchFontSizeSettings()124     void tryLaunchFontSizeSettings() {
125         if (WizardManagerHelper.isAnySetupWizard(getIntent())
126                 && new ComponentName(getPackageName(),
127                 CLASS_NAME_FONT_SIZE_SETTINGS_FOR_SUW).equals(
128                 getIntent().getComponent())) {
129             final Intent intent = new Intent(this,
130                     AccessibilityScreenSizeForSetupWizardActivity.class);
131             intent.putExtra(VISION_FRAGMENT_NO, FragmentType.FONT_SIZE);
132             startActivity(intent);
133             Log.d(LOG_TAG, "Launch font size settings");
134             finish();
135         }
136     }
137 }
138