Implemented new preference setting screen for face based auto-rotation

Test: locally with phone and make -j64 RunSettingsRoboTests
ROBOTEST_FILTER="com.android.settings.display.SmartAutoRotatePreferenceControllerTest"

Bug: 172857585
Change-Id: I8aaeb9d726ff71f64fd241c01fe2a1268fff927e
This commit is contained in:
Abel Tesfaye
2020-11-19 20:37:20 +00:00
parent 5f20dd4ce2
commit a96775c58b
9 changed files with 612 additions and 1 deletions

View File

@@ -0,0 +1,102 @@
/*
* 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.display;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.widget.Switch;
import com.android.internal.view.RotationPolicy;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.widget.SettingsMainSwitchBar;
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 the location.
*/
public class AutoRotateSwitchBarController implements OnMainSwitchChangeListener,
LifecycleObserver, OnStart, OnStop {
private final SettingsMainSwitchBar mSwitchBar;
private final Context mContext;
private boolean mValidListener;
private final MetricsFeatureProvider mMetricsFeatureProvider;
public AutoRotateSwitchBarController(Context context, SettingsMainSwitchBar switchBar,
Lifecycle lifecycle) {
mSwitchBar = switchBar;
mContext = context;
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
if (lifecycle != null) {
lifecycle.addObserver(this);
}
}
@Override
public void onStart() {
if (!mValidListener) {
mSwitchBar.addOnSwitchChangeListener(this);
mValidListener = true;
}
onChange();
}
@Override
public void onStop() {
if (mValidListener) {
mSwitchBar.removeOnSwitchChangeListener(this);
mValidListener = false;
}
}
/**
* Listens to the state change of the rotation primary switch.
*/
@Override
public void onSwitchChanged(Switch switchView, boolean isChecked) {
setRotationLock(isChecked);
}
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) {
final boolean isLocked = !isChecked;
mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_ROTATION_LOCK, isLocked);
RotationPolicy.setRotationLock(mContext, isLocked);
return true;
}
}

View File

@@ -0,0 +1,65 @@
/*
* 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.display;
import static android.provider.Settings.Secure.CAMERA_AUTOROTATE;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.provider.Settings;
import androidx.preference.Preference;
import com.android.internal.view.RotationPolicy;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
/**
* SmartAutoRotateController controls whether auto rotation is enabled
*/
public class SmartAutoRotateController extends TogglePreferenceController implements
Preference.OnPreferenceChangeListener {
private final MetricsFeatureProvider mMetricsFeatureProvider;
public SmartAutoRotateController(Context context, String preferenceKey) {
super(context, preferenceKey);
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
}
@Override
public int getAvailabilityStatus() {
return !RotationPolicy.isRotationLocked(mContext)
? AVAILABLE : DISABLED_DEPENDENT_SETTING;
}
@Override
public boolean isChecked() {
return Settings.Secure.getInt(mContext.getContentResolver(),
CAMERA_AUTOROTATE, 0) == 1;
}
@Override
public boolean setChecked(boolean isChecked) {
mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_CAMERA_ROTATE_TOGGLE,
isChecked);
Settings.Secure.putInt(mContext.getContentResolver(),
CAMERA_AUTOROTATE,
isChecked ? 1 : 0);
return true;
}
}

View File

@@ -0,0 +1,70 @@
/*
* 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.display;
import static android.provider.Settings.Secure.CAMERA_AUTOROTATE;
import android.content.Context;
import android.os.UserHandle;
import android.provider.Settings;
import android.util.Log;
import androidx.preference.Preference;
import com.android.internal.view.RotationPolicy;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
/**
* SmartAutoRotatePreferenceController provides auto rotate summary in display settings
*/
public class SmartAutoRotatePreferenceController extends BasePreferenceController {
private static final String TAG = "SmartAutoRotatePreferenceController";
public SmartAutoRotatePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
@Override
public int getAvailabilityStatus() {
return RotationPolicy.isRotationLockToggleVisible(mContext)
? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
protected void update(Preference preference) {
refreshSummary(preference);
}
@Override
public CharSequence getSummary() {
int activeStringId = R.string.auto_rotate_option_off;
if (!RotationPolicy.isRotationLocked(mContext)) {
try {
final int cameraRotate = Settings.Secure.getIntForUser(
mContext.getContentResolver(),
CAMERA_AUTOROTATE,
UserHandle.USER_CURRENT);
activeStringId = cameraRotate == 1 ? R.string.auto_rotate_option_face_based
: R.string.auto_rotate_option_on;
} catch (Settings.SettingNotFoundException e) {
Log.w(TAG, "CAMERA_AUTOROTATE setting not found", e);
}
}
return mContext.getString(activeStringId);
}
}

View File

@@ -0,0 +1,147 @@
/*
* 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.display;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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.core.AbstractPreferenceController;
import com.android.settingslib.search.Indexable;
import com.android.settingslib.widget.FooterPreference;
import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList;
import java.util.List;
/**
* Preference fragment used to auto rotation
*/
@SuppressWarnings("WeakerAccess")
@SearchIndexable
public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
private static final String TAG = "SmartAutoRotatePreferenceFragment";
private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
private AutoRotateSwitchBarController mSwitchBarController;
private static final String FACE_SWITCH_PREFERENCE_ID = "face_based_rotate";
private static final String SMART_AUTO_ROTATE_CONTROLLER_KEY = "auto_rotate";
@Override
protected int getPreferenceScreenResId() {
return R.xml.auto_rotate_settings;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = super.onCreateView(inflater, container, savedInstanceState);
final SettingsActivity activity = (SettingsActivity) getActivity();
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());
return view;
}
@Override
public void onResume() {
super.onResume();
if (mRotationPolicyListener == null) {
mRotationPolicyListener = new RotationPolicy.RotationPolicyListener() {
@Override
public void onChange() {
mSwitchBarController.onChange();
final boolean isLocked = RotationPolicy.isRotationLocked(getContext());
final Preference preference = findPreference(FACE_SWITCH_PREFERENCE_ID);
if (preference != null) {
preference.setEnabled(!isLocked);
}
}
};
}
RotationPolicy.registerRotationPolicyListener(getPrefContext(),
mRotationPolicyListener);
findPreference(FooterPreference.KEY_FOOTER).setTitle(
Html.fromHtml(getString(R.string.smart_rotate_text_headline),
Html.FROM_HTML_MODE_COMPACT));
}
@Override
public void onPause() {
super.onPause();
if (mRotationPolicyListener != null) {
RotationPolicy.unregisterRotationPolicyListener(getPrefContext(),
mRotationPolicyListener);
}
}
@Override
public int getMetricsCategory() {
return SettingsEnums.DISPLAY_AUTO_ROTATE_SETTINGS;
}
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
return buildPreferenceControllers(context);
}
private static List<AbstractPreferenceController> buildPreferenceControllers(
Context context) {
final List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(
new SmartAutoRotatePreferenceController(context, SMART_AUTO_ROTATE_CONTROLLER_KEY));
return controllers;
}
@Override
protected String getLogTag() {
return TAG;
}
public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.auto_rotate_settings) {
@Override
public List<AbstractPreferenceController> createPreferenceControllers(
Context context) {
return buildPreferenceControllers(context);
}
@Override
protected boolean isPageSearchEnabled(Context context) {
return false;
}
};
}