Revise homepage highlight mechanism

- Create TopLevelHighlightMixin to handle highlight actions and simplify
  TopLevelSettings
- Fix the error highlight and the flicker after screen rotation
- Postpone creating the fragment until it's needed to accelerate the
  initialization and to fix the search highlight function breakage after
  toggling light/dark mode
- Register activity embedding rules only once for injection and
  wallpaper
- Do not highlight Tips & support since it's full screen
- Refactor ActivityEmbeddingRulesController

Bug: 207316936
Test: manual, robotest build pass
Change-Id: If322ec180b03ee123987c70779a25c6a570d9faf
This commit is contained in:
Jason Chiu
2021-11-30 16:13:37 +08:00
parent 506c6b804f
commit a305c23f5e
9 changed files with 346 additions and 166 deletions

View File

@@ -0,0 +1,189 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.homepage;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;
import androidx.fragment.app.FragmentActivity;
import androidx.preference.PreferenceScreen;
import androidx.recyclerview.widget.RecyclerView;
import com.android.settings.SettingsActivity;
import com.android.settings.widget.HighlightableTopLevelPreferenceAdapter;
/** A highlight mixin for the top level settings fragment. */
public class TopLevelHighlightMixin implements Parcelable, DialogInterface.OnShowListener,
DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
private static final String TAG = "TopLevelHighlightMixin";
private String mCurrentKey;
// Stores the previous key for the profile select dialog cancel event
private String mPreviousKey;
// Stores the key hidden for the search page presence
private String mHiddenKey;
private DialogInterface mDialog;
private HighlightableTopLevelPreferenceAdapter mTopLevelAdapter;
public TopLevelHighlightMixin() {
}
public TopLevelHighlightMixin(Parcel source) {
mCurrentKey = source.readString();
mPreviousKey = source.readString();
mHiddenKey = source.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mCurrentKey);
dest.writeString(mPreviousKey);
dest.writeString(mHiddenKey);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<TopLevelHighlightMixin> CREATOR = new Creator<>() {
@Override
public TopLevelHighlightMixin createFromParcel(Parcel source) {
return new TopLevelHighlightMixin(source);
}
@Override
public TopLevelHighlightMixin[] newArray(int size) {
return new TopLevelHighlightMixin[size];
}
};
@Override
public void onShow(DialogInterface dialog) {
mDialog = dialog;
}
@Override
public void onDismiss(DialogInterface dialog) {
mDialog = null;
}
@Override
public void onCancel(DialogInterface dialog) {
if (mTopLevelAdapter != null) {
mCurrentKey = mPreviousKey;
mPreviousKey = null;
mTopLevelAdapter.highlightPreference(mCurrentKey, /* scrollNeeded= */ false);
}
}
RecyclerView.Adapter onCreateAdapter(TopLevelSettings topLevelSettings,
PreferenceScreen preferenceScreen) {
if (TextUtils.isEmpty(mCurrentKey)) {
mCurrentKey = getHighlightPrefKeyFromArguments(topLevelSettings.getArguments());
}
Log.d(TAG, "onCreateAdapter, pref key: " + mCurrentKey);
mTopLevelAdapter = new HighlightableTopLevelPreferenceAdapter(
(SettingsHomepageActivity) topLevelSettings.getActivity(), preferenceScreen,
topLevelSettings.getListView(), mCurrentKey);
return mTopLevelAdapter;
}
void reloadHighlightMenuKey(Bundle arguments) {
if (mTopLevelAdapter == null) {
return;
}
ensureDialogDismissed();
mCurrentKey = getHighlightPrefKeyFromArguments(arguments);
Log.d(TAG, "reloadHighlightMenuKey, pref key: " + mCurrentKey);
mTopLevelAdapter.highlightPreference(mCurrentKey, /* scrollNeeded= */ true);
}
void setHighlightPreferenceKey(String prefKey) {
if (mTopLevelAdapter != null) {
ensureDialogDismissed();
mPreviousKey = mCurrentKey;
mCurrentKey = prefKey;
mTopLevelAdapter.highlightPreference(prefKey, /* scrollNeeded= */ false);
}
}
void highlightPreferenceIfNeeded(FragmentActivity activity) {
if (mTopLevelAdapter != null) {
mTopLevelAdapter.requestHighlight();
}
}
void setMenuHighlightShowed(boolean show) {
if (mTopLevelAdapter == null) {
return;
}
ensureDialogDismissed();
if (show) {
mCurrentKey = mHiddenKey;
mHiddenKey = null;
} else {
if (mHiddenKey == null) {
mHiddenKey = mCurrentKey;
}
mCurrentKey = null;
}
mTopLevelAdapter.highlightPreference(mCurrentKey, /* scrollNeeded= */ show);
}
void setHighlightMenuKey(String menuKey, boolean scrollNeeded) {
if (mTopLevelAdapter == null) {
return;
}
ensureDialogDismissed();
final String prefKey = HighlightableMenu.lookupPreferenceKey(menuKey);
if (TextUtils.isEmpty(prefKey)) {
Log.e(TAG, "Invalid highlight menu key: " + menuKey);
} else {
Log.d(TAG, "Menu key: " + menuKey);
mCurrentKey = prefKey;
mTopLevelAdapter.highlightPreference(prefKey, scrollNeeded);
}
}
private static String getHighlightPrefKeyFromArguments(Bundle arguments) {
final String menuKey = arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
final String prefKey = HighlightableMenu.lookupPreferenceKey(menuKey);
if (TextUtils.isEmpty(prefKey)) {
Log.e(TAG, "Invalid highlight menu key: " + menuKey);
} else {
Log.d(TAG, "Menu key: " + menuKey);
}
return prefKey;
}
private void ensureDialogDismissed() {
if (mDialog != null) {
onCancel(mDialog);
mDialog.dismiss();
}
}
}