Use SettingsLib's MainSwitchBar to replace SwitchBar in Settings.

To log Settings metrics, the MainSwitch extends MainSwitchBar and
replace the SwitchBar in SettingsActivity.

Bug: 175181773
Test: Run robotest and apply the widget in Settings and see the ui

Change-Id: I3add3702e9058ad9192b5172c7cf0e2ccfb55a70
This commit is contained in:
Stanley Wang
2020-12-25 16:27:32 +08:00
parent c61c2fb1d7
commit b87ddba6cd
51 changed files with 783 additions and 271 deletions

View File

@@ -59,8 +59,8 @@ import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.ToggleSwitch;
import com.android.settings.widget.SettingsMainSwitchBar;
import com.android.settingslib.widget.OnMainSwitchChangeListener;
import java.util.ArrayList;
import java.util.LinkedHashMap;
@@ -71,7 +71,7 @@ import java.util.Map;
* Fragment with print service settings.
*/
public class PrintServiceSettingsFragment extends SettingsPreferenceFragment
implements SwitchBar.OnSwitchChangeListener,
implements OnMainSwitchChangeListener,
LoaderManager.LoaderCallbacks<List<PrintServiceInfo>> {
private static final String LOG_TAG = "PrintServiceSettings";
@@ -96,8 +96,7 @@ public class PrintServiceSettingsFragment extends SettingsPreferenceFragment
}
};
private SwitchBar mSwitchBar;
private ToggleSwitch mToggleSwitch;
private SettingsMainSwitchBar mSwitchBar;
private String mPreferenceKey;
@@ -166,14 +165,14 @@ public class PrintServiceSettingsFragment extends SettingsPreferenceFragment
}
private void onPreferenceToggled(String preferenceKey, boolean enabled) {
((PrintManager)getContext().getSystemService(Context.PRINT_SERVICE))
((PrintManager) getContext().getSystemService(Context.PRINT_SERVICE))
.setPrintServiceEnabled(mComponentName, enabled);
}
private void updateEmptyView() {
ViewGroup contentRoot = (ViewGroup) getListView().getParent();
View emptyView = getEmptyView();
if (!mToggleSwitch.isChecked()) {
if (!mSwitchBar.isChecked()) {
if (emptyView != null) {
contentRoot.removeView(emptyView);
emptyView = null;
@@ -235,11 +234,12 @@ public class PrintServiceSettingsFragment extends SettingsPreferenceFragment
final SettingsActivity activity = (SettingsActivity) getActivity();
mSwitchBar = activity.getSwitchBar();
mSwitchBar.setTitle(
getContext().getString(R.string.default_print_service_main_switch_title));
mSwitchBar.addOnSwitchChangeListener(this);
mSwitchBar.show();
mToggleSwitch = mSwitchBar.getSwitch();
mToggleSwitch.setOnBeforeCheckedChangeListener((toggleSwitch, checked) -> {
mSwitchBar.setOnBeforeCheckedChangeListener((toggleSwitch, checked) -> {
onPreferenceToggled(mPreferenceKey, checked);
return false;
});
@@ -388,6 +388,7 @@ public class PrintServiceSettingsFragment extends SettingsPreferenceFragment
R.string.print_search_box_shown_utterance));
}
}
@Override
public void onViewDetachedFromWindow(View view) {
Activity activity = getActivity();
@@ -565,7 +566,7 @@ public class PrintServiceSettingsFragment extends SettingsPreferenceFragment
TypedValue value = new TypedValue();
getActivity().getTheme().resolveAttribute(android.R.attr.disabledAlpha, value,
true);
icon.setAlpha((int)(value.getFloat() * 255));
icon.setAlpha((int) (value.getFloat() * 255));
}
iconView.setImageDrawable(icon);
} else {