• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Html;
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.internal.view.RotationPolicy;
32 import com.android.settings.R;
33 import com.android.settings.SettingsActivity;
34 import com.android.settings.dashboard.DashboardFragment;
35 import com.android.settings.search.BaseSearchIndexProvider;
36 import com.android.settings.widget.SettingsMainSwitchBar;
37 import com.android.settingslib.search.Indexable;
38 import com.android.settingslib.search.SearchIndexable;
39 import com.android.settingslib.widget.FooterPreference;
40 
41 /**
42  * Preference fragment used for auto rotation
43  */
44 @SuppressWarnings("WeakerAccess")
45 @SearchIndexable
46 public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
47 
48     private static final String TAG = "SmartAutoRotatePreferenceFragment";
49 
50     private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
51     private AutoRotateSwitchBarController mSwitchBarController;
52     @VisibleForTesting static final String AUTO_ROTATE_SWITCH_PREFERENCE_ID = "auto_rotate_switch";
53 
54     @Override
getPreferenceScreenResId()55     protected int getPreferenceScreenResId() {
56         return R.xml.auto_rotate_settings;
57     }
58 
59     @Override
onAttach(Context context)60     public void onAttach(Context context) {
61         super.onAttach(context);
62         use(SmartAutoRotateController.class).init(getLifecycle());
63     }
64 
65     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)66     public View onCreateView(LayoutInflater inflater, ViewGroup container,
67             Bundle savedInstanceState) {
68         final View view = super.onCreateView(inflater, container, savedInstanceState);
69         final SettingsActivity activity = (SettingsActivity) getActivity();
70         createHeader(activity);
71         final Preference footerPreference = findPreference(FooterPreference.KEY_FOOTER);
72         if (footerPreference != null) {
73             footerPreference.setTitle(Html.fromHtml(getString(R.string.smart_rotate_text_headline),
74                     Html.FROM_HTML_MODE_COMPACT));
75             footerPreference.setVisible(isRotationResolverServiceAvailable(activity));
76         }
77         return view;
78     }
79 
80     @VisibleForTesting
createHeader(SettingsActivity activity)81     void createHeader(SettingsActivity activity) {
82         if (isRotationResolverServiceAvailable(activity)) {
83             final SettingsMainSwitchBar switchBar = activity.getSwitchBar();
84             switchBar.setTitle(
85                     getContext().getString(R.string.auto_rotate_settings_primary_switch_title));
86             switchBar.show();
87             mSwitchBarController = new AutoRotateSwitchBarController(activity, switchBar,
88                     getSettingsLifecycle());
89             findPreference(AUTO_ROTATE_SWITCH_PREFERENCE_ID).setVisible(false);
90         }
91     }
92 
93     @Override
onResume()94     public void onResume() {
95         super.onResume();
96         if (mRotationPolicyListener == null) {
97             mRotationPolicyListener = new RotationPolicy.RotationPolicyListener() {
98                 @Override
99                 public void onChange() {
100                     if (mSwitchBarController != null) {
101                         mSwitchBarController.onChange();
102                     }
103                 }
104             };
105         }
106         RotationPolicy.registerRotationPolicyListener(getPrefContext(),
107                 mRotationPolicyListener);
108     }
109 
110     @Override
onPause()111     public void onPause() {
112         super.onPause();
113         if (mRotationPolicyListener != null) {
114             RotationPolicy.unregisterRotationPolicyListener(getPrefContext(),
115                     mRotationPolicyListener);
116         }
117     }
118 
119     @Override
getMetricsCategory()120     public int getMetricsCategory() {
121         return SettingsEnums.DISPLAY_AUTO_ROTATE_SETTINGS;
122     }
123 
124     @Override
getLogTag()125     protected String getLogTag() {
126         return TAG;
127     }
128 
129     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
130             new BaseSearchIndexProvider(R.xml.auto_rotate_settings);
131 }
132