1 /*
2  * Copyright (C) 2021 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 package com.android.settings.display;
17 
18 import static com.android.settings.display.SmartAutoRotateController.isRotationResolverServiceAvailable;
19 
20 import android.app.settings.SettingsEnums;
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.text.TextUtils;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.preference.Preference;
30 
31 import com.android.settings.R;
32 import com.android.settings.SettingsActivity;
33 import com.android.settings.dashboard.DashboardFragment;
34 import com.android.settings.search.BaseSearchIndexProvider;
35 import com.android.settingslib.HelpUtils;
36 import com.android.settingslib.core.AbstractPreferenceController;
37 import com.android.settingslib.search.Indexable;
38 import com.android.settingslib.search.SearchIndexable;
39 import com.android.settingslib.search.SearchIndexableRaw;
40 import com.android.settingslib.widget.FooterPreference;
41 
42 import java.util.List;
43 
44 /**
45  * Preference fragment used for auto rotation
46  */
47 @SuppressWarnings("WeakerAccess")
48 @SearchIndexable
49 public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
50 
51     private static final String TAG = "SmartAutoRotatePreferenceFragment";
52 
53     @VisibleForTesting
54     static final String AUTO_ROTATE_MAIN_SWITCH_PREFERENCE_KEY = "auto_rotate_main_switch";
55     @VisibleForTesting
56     static final String AUTO_ROTATE_SWITCH_PREFERENCE_KEY = "auto_rotate_switch";
57     private static final String KEY_FOOTER_PREFERENCE = "auto_rotate_footer_preference";
58 
59     @Override
getPreferenceScreenResId()60     protected int getPreferenceScreenResId() {
61         return R.xml.auto_rotate_settings;
62     }
63 
64     @Override
onAttach(Context context)65     public void onAttach(Context context) {
66         super.onAttach(context);
67         DeviceStateAutoRotationHelper.initControllers(
68                 getLifecycle(),
69                 useAll(DeviceStateAutoRotateSettingController.class)
70         );
71     }
72 
73     @Override
createPreferenceControllers(Context context)74     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
75         return DeviceStateAutoRotationHelper.createPreferenceControllers(context);
76     }
77 
78     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)79     public View onCreateView(LayoutInflater inflater, ViewGroup container,
80             Bundle savedInstanceState) {
81         final View view = super.onCreateView(inflater, container, savedInstanceState);
82         final SettingsActivity activity = (SettingsActivity) getActivity();
83         createHeader(activity);
84         final Preference footerPreference = findPreference(KEY_FOOTER_PREFERENCE);
85         if (footerPreference != null) {
86             footerPreference.setVisible(isRotationResolverServiceAvailable(activity));
87             setupFooter();
88         }
89         return view;
90     }
91 
92     @VisibleForTesting
createHeader(SettingsActivity activity)93     void createHeader(SettingsActivity activity) {
94         boolean deviceStateRotationEnabled =
95                 DeviceStateAutoRotationHelper.isDeviceStateRotationEnabled(activity);
96         if (isRotationResolverServiceAvailable(activity) && !deviceStateRotationEnabled) {
97             findPreference(AUTO_ROTATE_SWITCH_PREFERENCE_KEY).setVisible(false);
98         } else {
99             findPreference(AUTO_ROTATE_MAIN_SWITCH_PREFERENCE_KEY).setVisible(false);
100         }
101     }
102 
103     @Override
getMetricsCategory()104     public int getMetricsCategory() {
105         return SettingsEnums.DISPLAY_AUTO_ROTATE_SETTINGS;
106     }
107 
108     @Override
getLogTag()109     protected String getLogTag() {
110         return TAG;
111     }
112 
113     @Override
getHelpResource()114     public int getHelpResource() {
115         return R.string.help_url_auto_rotate_settings;
116     }
117 
118     // Updates the footer for this page.
119     @VisibleForTesting
setupFooter()120     void setupFooter() {
121         final String mHelpUri = getString(getHelpResource());
122         if (!TextUtils.isEmpty(mHelpUri)) {
123             addHelpLink();
124         }
125     }
126 
127     // Changes the text to include a learn more link if the link is defined.
128     @VisibleForTesting
addHelpLink()129     void addHelpLink() {
130         final FooterPreference pref = findPreference(KEY_FOOTER_PREFERENCE);
131         if (pref != null) {
132             pref.setLearnMoreAction(v -> {
133                 startActivityForResult(HelpUtils.getHelpIntent(getContext(),
134                         getString(getHelpResource()),
135                         /*backupContext=*/ ""), /*requestCode=*/ 0);
136             });
137             pref.setLearnMoreText(getString(R.string.auto_rotate_link_a11y));
138         }
139     }
140 
141     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
142             new BaseSearchIndexProvider(R.xml.auto_rotate_settings) {
143 
144                 @Override
145                 public List<SearchIndexableRaw> getRawDataToIndex(
146                         Context context, boolean enabled) {
147                     return DeviceStateAutoRotationHelper.getRawDataToIndex(context, enabled);
148                 }
149             };
150 }
151