• 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 
17 package com.android.settings.homepage;
18 
19 import android.content.DialogInterface;
20 import android.os.Bundle;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.text.TextUtils;
24 import android.util.Log;
25 
26 import androidx.preference.PreferenceScreen;
27 import androidx.recyclerview.widget.RecyclerView;
28 
29 import com.android.settings.SettingsActivity;
30 import com.android.settings.widget.HighlightableTopLevelPreferenceAdapter;
31 
32 /** A highlight mixin for the top level settings fragment. */
33 public class TopLevelHighlightMixin implements Parcelable, DialogInterface.OnShowListener,
34         DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
35 
36     private static final String TAG = "TopLevelHighlightMixin";
37 
38     private String mCurrentKey;
39     // Stores the previous key for the profile select dialog cancel event
40     private String mPreviousKey;
41     // Stores the key hidden for the search page presence
42     private String mHiddenKey;
43     private DialogInterface mDialog;
44     private HighlightableTopLevelPreferenceAdapter mTopLevelAdapter;
45     private boolean mActivityEmbedded;
46 
TopLevelHighlightMixin(boolean activityEmbedded)47     public TopLevelHighlightMixin(boolean activityEmbedded) {
48         mActivityEmbedded = activityEmbedded;
49     }
50 
TopLevelHighlightMixin(Parcel source)51     public TopLevelHighlightMixin(Parcel source) {
52         mCurrentKey = source.readString();
53         mPreviousKey = source.readString();
54         mHiddenKey = source.readString();
55         mActivityEmbedded = source.readBoolean();
56     }
57 
58     @Override
writeToParcel(Parcel dest, int flags)59     public void writeToParcel(Parcel dest, int flags) {
60         dest.writeString(mCurrentKey);
61         dest.writeString(mPreviousKey);
62         dest.writeString(mHiddenKey);
63         dest.writeBoolean(mActivityEmbedded);
64     }
65 
66     @Override
describeContents()67     public int describeContents() {
68         return 0;
69     }
70 
71     public static final Creator<TopLevelHighlightMixin> CREATOR = new Creator<>() {
72         @Override
73         public TopLevelHighlightMixin createFromParcel(Parcel source) {
74             return new TopLevelHighlightMixin(source);
75         }
76 
77         @Override
78         public TopLevelHighlightMixin[] newArray(int size) {
79             return new TopLevelHighlightMixin[size];
80         }
81     };
82 
83     @Override
onShow(DialogInterface dialog)84     public void onShow(DialogInterface dialog) {
85         mDialog = dialog;
86     }
87 
88     @Override
onDismiss(DialogInterface dialog)89     public void onDismiss(DialogInterface dialog) {
90         mDialog = null;
91     }
92 
93     @Override
onCancel(DialogInterface dialog)94     public void onCancel(DialogInterface dialog) {
95         if (mTopLevelAdapter != null) {
96             mCurrentKey = mPreviousKey;
97             mPreviousKey = null;
98             mTopLevelAdapter.highlightPreference(mCurrentKey, /* scrollNeeded= */ false);
99         }
100     }
101 
setActivityEmbedded(boolean activityEmbedded)102     void setActivityEmbedded(boolean activityEmbedded) {
103         mActivityEmbedded = activityEmbedded;
104     }
105 
isActivityEmbedded()106     boolean isActivityEmbedded() {
107         return mActivityEmbedded;
108     }
109 
onCreateAdapter(TopLevelSettings topLevelSettings, PreferenceScreen preferenceScreen, boolean scrollNeeded)110     RecyclerView.Adapter onCreateAdapter(TopLevelSettings topLevelSettings,
111             PreferenceScreen preferenceScreen, boolean scrollNeeded) {
112         if (TextUtils.isEmpty(mCurrentKey)) {
113             mCurrentKey = getHighlightPrefKeyFromArguments(topLevelSettings.getArguments());
114         }
115 
116         Log.d(TAG, "onCreateAdapter, pref key: " + mCurrentKey);
117 
118         // Remove the animator to avoid a RecyclerView crash.
119         RecyclerView recyclerView = topLevelSettings.getListView();
120         recyclerView.setItemAnimator(null);
121 
122         mTopLevelAdapter = new HighlightableTopLevelPreferenceAdapter(
123                 (SettingsHomepageActivity) topLevelSettings.getActivity(), preferenceScreen,
124                 recyclerView, mCurrentKey, scrollNeeded);
125         return mTopLevelAdapter;
126     }
127 
reloadHighlightMenuKey(Bundle arguments)128     void reloadHighlightMenuKey(Bundle arguments) {
129         if (mTopLevelAdapter == null) {
130             return;
131         }
132         ensureDialogDismissed();
133 
134         mCurrentKey = getHighlightPrefKeyFromArguments(arguments);
135         Log.d(TAG, "reloadHighlightMenuKey, pref key: " + mCurrentKey);
136         mTopLevelAdapter.highlightPreference(mCurrentKey, /* scrollNeeded= */ true);
137     }
138 
setHighlightPreferenceKey(String prefKey)139     void setHighlightPreferenceKey(String prefKey) {
140         if (mTopLevelAdapter != null) {
141             ensureDialogDismissed();
142             mPreviousKey = mCurrentKey;
143             mCurrentKey = prefKey;
144             mTopLevelAdapter.highlightPreference(prefKey, /* scrollNeeded= */ false);
145         }
146     }
147 
getHighlightPreferenceKey()148     String getHighlightPreferenceKey() {
149         return mCurrentKey;
150     }
151 
highlightPreferenceIfNeeded()152     void highlightPreferenceIfNeeded() {
153         if (mTopLevelAdapter != null) {
154             mTopLevelAdapter.requestHighlight();
155         }
156     }
157 
setMenuHighlightShowed(boolean show)158     void setMenuHighlightShowed(boolean show) {
159         if (mTopLevelAdapter == null) {
160             return;
161         }
162         ensureDialogDismissed();
163 
164         if (show) {
165             mCurrentKey = mHiddenKey;
166             mHiddenKey = null;
167         } else {
168             if (mHiddenKey == null) {
169                 mHiddenKey = mCurrentKey;
170             }
171             mCurrentKey = null;
172         }
173         mTopLevelAdapter.highlightPreference(mCurrentKey, /* scrollNeeded= */ show);
174     }
175 
setHighlightMenuKey(String menuKey, boolean scrollNeeded)176     void setHighlightMenuKey(String menuKey, boolean scrollNeeded) {
177         if (mTopLevelAdapter == null) {
178             return;
179         }
180         ensureDialogDismissed();
181 
182         final String prefKey = HighlightableMenu.lookupPreferenceKey(menuKey);
183         if (TextUtils.isEmpty(prefKey)) {
184             Log.e(TAG, "Invalid highlight menu key: " + menuKey);
185         } else {
186             Log.d(TAG, "Menu key: " + menuKey);
187             mCurrentKey = prefKey;
188             mTopLevelAdapter.highlightPreference(prefKey, scrollNeeded);
189         }
190     }
191 
getHighlightPrefKeyFromArguments(Bundle arguments)192     private static String getHighlightPrefKeyFromArguments(Bundle arguments) {
193         final String menuKey = arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
194         final String prefKey = HighlightableMenu.lookupPreferenceKey(menuKey);
195         if (TextUtils.isEmpty(prefKey)) {
196             Log.e(TAG, "Invalid highlight menu key: " + menuKey);
197         } else {
198             Log.d(TAG, "Menu key: " + menuKey);
199         }
200         return prefKey;
201     }
202 
ensureDialogDismissed()203     private void ensureDialogDismissed() {
204         if (mDialog != null) {
205             onCancel(mDialog);
206             mDialog.dismiss();
207         }
208     }
209 }
210