Merge "Update auto-rotate strings/toggles for better accessibility" into tm-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
07950adad9
@@ -18,86 +18,78 @@ package com.android.settings.display;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.widget.Switch;
|
||||
|
||||
import com.android.internal.view.RotationPolicy;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settings.widget.SettingsMainSwitchBar;
|
||||
import com.android.settings.widget.SettingsMainSwitchPreferenceController;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||
import com.android.settingslib.widget.OnMainSwitchChangeListener;
|
||||
|
||||
/**
|
||||
* The switch controller for auto-rotate.
|
||||
* The main switch controller for auto-rotate.
|
||||
*/
|
||||
public class AutoRotateSwitchBarController implements OnMainSwitchChangeListener,
|
||||
public class AutoRotateSwitchBarController extends SettingsMainSwitchPreferenceController implements
|
||||
LifecycleObserver, OnStart, OnStop {
|
||||
|
||||
private final SettingsMainSwitchBar mSwitchBar;
|
||||
private final Context mContext;
|
||||
private boolean mValidListener;
|
||||
private final MetricsFeatureProvider mMetricsFeatureProvider;
|
||||
private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
|
||||
|
||||
public AutoRotateSwitchBarController(Context context, SettingsMainSwitchBar switchBar,
|
||||
Lifecycle lifecycle) {
|
||||
mSwitchBar = switchBar;
|
||||
mContext = context;
|
||||
public AutoRotateSwitchBarController(Context context, String key) {
|
||||
super(context, key);
|
||||
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
|
||||
if (lifecycle != null) {
|
||||
lifecycle.addObserver(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
return RotationPolicy.isRotationLockToggleVisible(mContext)
|
||||
&& !DeviceStateAutoRotationHelper.isDeviceStateRotationEnabled(mContext)
|
||||
? AVAILABLE : UNSUPPORTED_ON_DEVICE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
if (!mValidListener) {
|
||||
mSwitchBar.addOnSwitchChangeListener(this);
|
||||
mValidListener = true;
|
||||
if (mRotationPolicyListener == null) {
|
||||
mRotationPolicyListener = new RotationPolicy.RotationPolicyListener() {
|
||||
@Override
|
||||
public void onChange() {
|
||||
if (mSwitchPreference != null) {
|
||||
updateState(mSwitchPreference);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
onChange();
|
||||
RotationPolicy.registerRotationPolicyListener(mContext,
|
||||
mRotationPolicyListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
if (mValidListener) {
|
||||
mSwitchBar.removeOnSwitchChangeListener(this);
|
||||
mValidListener = false;
|
||||
if (mRotationPolicyListener != null) {
|
||||
RotationPolicy.unregisterRotationPolicyListener(mContext, mRotationPolicyListener);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Listens to the state change of the rotation primary switch.
|
||||
*/
|
||||
@Override
|
||||
public void onSwitchChanged(Switch switchView, boolean isChecked) {
|
||||
setRotationLock(isChecked);
|
||||
public boolean isChecked() {
|
||||
return !RotationPolicy.isRotationLocked(mContext);
|
||||
}
|
||||
|
||||
|
||||
protected void onChange() {
|
||||
final boolean isEnabled = !RotationPolicy.isRotationLocked(mContext);
|
||||
if (isEnabled != mSwitchBar.isChecked()) {
|
||||
// set listener to null so that that code below doesn't trigger onCheckedChanged()
|
||||
if (mValidListener) {
|
||||
mSwitchBar.removeOnSwitchChangeListener(this);
|
||||
}
|
||||
mSwitchBar.setChecked(isEnabled);
|
||||
if (mValidListener) {
|
||||
mSwitchBar.addOnSwitchChangeListener(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean setRotationLock(boolean isChecked) {
|
||||
@Override
|
||||
public boolean setChecked(boolean isChecked) {
|
||||
final boolean isLocked = !isChecked;
|
||||
mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_ROTATE_ROTATE_MASTER_TOGGLE,
|
||||
isChecked);
|
||||
isLocked);
|
||||
RotationPolicy.setRotationLock(mContext, isLocked);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSliceHighlightMenuRes() {
|
||||
return R.string.menu_key_display;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@@ -20,7 +20,7 @@ import static com.android.settings.display.SmartAutoRotateController.isRotationR
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.text.Html;
|
||||
import android.text.TextUtils;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
@@ -28,12 +28,11 @@ import android.view.ViewGroup;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.internal.view.RotationPolicy;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settings.widget.SettingsMainSwitchBar;
|
||||
import com.android.settingslib.HelpUtils;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.search.Indexable;
|
||||
import com.android.settingslib.search.SearchIndexable;
|
||||
@@ -51,9 +50,11 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
|
||||
|
||||
private static final String TAG = "SmartAutoRotatePreferenceFragment";
|
||||
|
||||
private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
|
||||
private AutoRotateSwitchBarController mSwitchBarController;
|
||||
@VisibleForTesting static final String AUTO_ROTATE_SWITCH_PREFERENCE_ID = "auto_rotate_switch";
|
||||
@VisibleForTesting
|
||||
static final String AUTO_ROTATE_MAIN_SWITCH_PREFERENCE_KEY = "auto_rotate_main_switch";
|
||||
@VisibleForTesting
|
||||
static final String AUTO_ROTATE_SWITCH_PREFERENCE_KEY = "auto_rotate_switch";
|
||||
private static final String KEY_FOOTER_PREFERENCE = "auto_rotate_footer_preference";
|
||||
|
||||
@Override
|
||||
protected int getPreferenceScreenResId() {
|
||||
@@ -81,11 +82,10 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
|
||||
final View view = super.onCreateView(inflater, container, savedInstanceState);
|
||||
final SettingsActivity activity = (SettingsActivity) getActivity();
|
||||
createHeader(activity);
|
||||
final Preference footerPreference = findPreference(FooterPreference.KEY_FOOTER);
|
||||
final Preference footerPreference = findPreference(KEY_FOOTER_PREFERENCE);
|
||||
if (footerPreference != null) {
|
||||
footerPreference.setTitle(Html.fromHtml(getString(R.string.smart_rotate_text_headline),
|
||||
Html.FROM_HTML_MODE_COMPACT));
|
||||
footerPreference.setVisible(isRotationResolverServiceAvailable(activity));
|
||||
setupFooter();
|
||||
}
|
||||
return view;
|
||||
}
|
||||
@@ -95,39 +95,9 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
|
||||
boolean deviceStateRotationEnabled =
|
||||
DeviceStateAutoRotationHelper.isDeviceStateRotationEnabled(activity);
|
||||
if (isRotationResolverServiceAvailable(activity) && !deviceStateRotationEnabled) {
|
||||
final SettingsMainSwitchBar switchBar = activity.getSwitchBar();
|
||||
switchBar.setTitle(
|
||||
getContext().getString(R.string.auto_rotate_settings_primary_switch_title));
|
||||
switchBar.show();
|
||||
mSwitchBarController = new AutoRotateSwitchBarController(activity, switchBar,
|
||||
getSettingsLifecycle());
|
||||
findPreference(AUTO_ROTATE_SWITCH_PREFERENCE_ID).setVisible(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
if (mRotationPolicyListener == null) {
|
||||
mRotationPolicyListener = new RotationPolicy.RotationPolicyListener() {
|
||||
@Override
|
||||
public void onChange() {
|
||||
if (mSwitchBarController != null) {
|
||||
mSwitchBarController.onChange();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
RotationPolicy.registerRotationPolicyListener(getPrefContext(),
|
||||
mRotationPolicyListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
if (mRotationPolicyListener != null) {
|
||||
RotationPolicy.unregisterRotationPolicyListener(getPrefContext(),
|
||||
mRotationPolicyListener);
|
||||
findPreference(AUTO_ROTATE_SWITCH_PREFERENCE_KEY).setVisible(false);
|
||||
} else {
|
||||
findPreference(AUTO_ROTATE_MAIN_SWITCH_PREFERENCE_KEY).setVisible(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,6 +111,34 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHelpResource() {
|
||||
return R.string.help_url_auto_rotate_settings;
|
||||
}
|
||||
|
||||
// Updates the footer for this page.
|
||||
@VisibleForTesting
|
||||
void setupFooter() {
|
||||
final String mHelpUri = getString(getHelpResource());
|
||||
if (!TextUtils.isEmpty(mHelpUri)) {
|
||||
addHelpLink();
|
||||
}
|
||||
}
|
||||
|
||||
// Changes the text to include a learn more link if the link is defined.
|
||||
@VisibleForTesting
|
||||
void addHelpLink() {
|
||||
final FooterPreference pref = findPreference(KEY_FOOTER_PREFERENCE);
|
||||
if (pref != null) {
|
||||
pref.setLearnMoreAction(v -> {
|
||||
startActivityForResult(HelpUtils.getHelpIntent(getContext(),
|
||||
getString(getHelpResource()),
|
||||
/*backupContext=*/ ""), /*requestCode=*/ 0);
|
||||
});
|
||||
pref.setLearnMoreContentDescription(getString(R.string.auto_rotate_link_a11y));
|
||||
}
|
||||
}
|
||||
|
||||
public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
||||
new BaseSearchIndexProvider(R.xml.auto_rotate_settings) {
|
||||
|
||||
|
Reference in New Issue
Block a user