Merge "New Settings preference for One Handed mode"
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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.gestures;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.core.TogglePreferenceController;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||
|
||||
/**
|
||||
* The Controller to handle app taps to exit of one-handed mode
|
||||
*/
|
||||
public class OneHandedAppTapsExitPreferenceController extends TogglePreferenceController implements
|
||||
LifecycleObserver, OnStart, OnStop, OneHandedSettingsUtils.TogglesCallback {
|
||||
|
||||
private Preference mPreference;
|
||||
private final OneHandedSettingsUtils mUtils;
|
||||
|
||||
public OneHandedAppTapsExitPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
mUtils = new OneHandedSettingsUtils(context);
|
||||
|
||||
// By default, app taps to stop one-handed is enabled, this will get default value once.
|
||||
OneHandedSettingsUtils.setSettingsTapsAppToExit(mContext, isChecked());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
return OneHandedSettingsUtils.isOneHandedModeEnabled(mContext)
|
||||
? AVAILABLE : DISABLED_DEPENDENT_SETTING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mPreference = screen.findPreference(getPreferenceKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
|
||||
final int availabilityStatus = getAvailabilityStatus();
|
||||
preference.setEnabled(
|
||||
availabilityStatus == AVAILABLE || availabilityStatus == AVAILABLE_UNSEARCHABLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setChecked(boolean isChecked) {
|
||||
return OneHandedSettingsUtils.setSettingsTapsAppToExit(mContext, isChecked);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isChecked() {
|
||||
return OneHandedSettingsUtils.getSettingsTapsAppToExit(mContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
mUtils.registerToggleAwareObserver(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
mUtils.unregisterToggleAwareObserver();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChange(Uri uri) {
|
||||
updateState(mPreference);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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.gestures;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.core.TogglePreferenceController;
|
||||
|
||||
/**
|
||||
* The controller to handle one-handed mode enable or disable state.
|
||||
**/
|
||||
public class OneHandedEnablePreferenceController extends TogglePreferenceController {
|
||||
|
||||
public OneHandedEnablePreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
return BasePreferenceController.AVAILABLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setChecked(boolean isChecked) {
|
||||
OneHandedSettingsUtils.setSettingsOneHandedModeEnabled(mContext,
|
||||
isChecked);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isChecked() {
|
||||
return OneHandedSettingsUtils.isOneHandedModeEnabled(mContext);
|
||||
}
|
||||
}
|
||||
51
src/com/android/settings/gestures/OneHandedSettings.java
Normal file
51
src/com/android/settings/gestures/OneHandedSettings.java
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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.gestures;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settingslib.search.SearchIndexable;
|
||||
|
||||
/**
|
||||
* The Fragment for one-handed mode settings.
|
||||
*/
|
||||
@SearchIndexable
|
||||
public class OneHandedSettings extends DashboardFragment {
|
||||
|
||||
private static final String TAG = "OneHandedSettings";
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return SettingsEnums.SETTINGS_ONE_HANDED;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getLogTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getPreferenceScreenResId() {
|
||||
return R.xml.one_handed_settings;
|
||||
}
|
||||
|
||||
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
||||
new BaseSearchIndexProvider(R.xml.one_handed_settings);
|
||||
}
|
||||
173
src/com/android/settings/gestures/OneHandedSettingsUtils.java
Normal file
173
src/com/android/settings/gestures/OneHandedSettingsUtils.java
Normal file
@@ -0,0 +1,173 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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.gestures;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.database.ContentObserver;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.provider.Settings;
|
||||
|
||||
/**
|
||||
* The Util to query one-handed mode settings config
|
||||
*/
|
||||
public class OneHandedSettingsUtils {
|
||||
|
||||
public enum OneHandedTimeout {
|
||||
NEVER(0), SHORT(4), MEDIUM(8), LONG(12);
|
||||
|
||||
private final int mValue;
|
||||
|
||||
OneHandedTimeout(int value) {
|
||||
this.mValue = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return mValue;
|
||||
}
|
||||
}
|
||||
|
||||
private final Context mContext;
|
||||
private final SettingsObserver mSettingsObserver;
|
||||
|
||||
OneHandedSettingsUtils(Context context) {
|
||||
mContext = context;
|
||||
mSettingsObserver = new SettingsObserver(new Handler(Looper.getMainLooper()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get one-handed mode enable or disable flag from Settings provider.
|
||||
*
|
||||
* @param context App context
|
||||
* @return enable or disable one-handed mode flag.
|
||||
*/
|
||||
public static boolean isOneHandedModeEnabled(Context context) {
|
||||
return Settings.Secure.getInt(context.getContentResolver(),
|
||||
Settings.Secure.ONE_HANDED_MODE_ENABLED, 0) == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set one-handed mode enable or disable flag to Settings provider.
|
||||
*
|
||||
* @param context App context
|
||||
* @param enable enable or disable one-handed mode.
|
||||
*/
|
||||
public static void setSettingsOneHandedModeEnabled(Context context, boolean enable) {
|
||||
Settings.Secure.putInt(context.getContentResolver(),
|
||||
Settings.Secure.ONE_HANDED_MODE_ENABLED, enable ? 1 : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get enabling taps app to exit one-handed mode flag from Settings provider.
|
||||
*
|
||||
* @param context App context
|
||||
* @return enable or disable taps app to exit.
|
||||
*/
|
||||
public static boolean getSettingsTapsAppToExit(Context context) {
|
||||
return Settings.Secure.getInt(context.getContentResolver(),
|
||||
Settings.Secure.TAPS_APP_TO_EXIT, 1) == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set enabling taps app to exit one-handed mode flag to Settings provider.
|
||||
*
|
||||
* @param context App context
|
||||
* @param enable enable or disable when taping app to exit one-handed mode.
|
||||
*/
|
||||
public static boolean setSettingsTapsAppToExit(Context context, boolean enable) {
|
||||
return Settings.Secure.putInt(context.getContentResolver(),
|
||||
Settings.Secure.TAPS_APP_TO_EXIT, enable ? 1 : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get one-handed mode timeout value from Settings provider.
|
||||
*
|
||||
* @param context App context
|
||||
* @return timeout value in seconds.
|
||||
*/
|
||||
public static int getSettingsOneHandedModeTimeout(Context context) {
|
||||
return Settings.Secure.getInt(context.getContentResolver(),
|
||||
Settings.Secure.ONE_HANDED_MODE_TIMEOUT,
|
||||
OneHandedTimeout.MEDIUM.getValue() /* default MEDIUM(8) by UX */);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set one-handed mode timeout value to Settings provider.
|
||||
*
|
||||
* @param context App context
|
||||
* @param timeout timeout in seconds for exiting one-handed mode.
|
||||
*/
|
||||
public static void setSettingsOneHandedModeTimeout(Context context, int timeout) {
|
||||
Settings.Secure.putInt(context.getContentResolver(),
|
||||
Settings.Secure.ONE_HANDED_MODE_TIMEOUT, timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register callback for observing Settings.Secure.ONE_HANDED_MODE_ENABLED state.
|
||||
* @param callback for state changes
|
||||
*/
|
||||
public void registerToggleAwareObserver(TogglesCallback callback) {
|
||||
mSettingsObserver.observe();
|
||||
mSettingsObserver.setCallback(callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister callback for observing Settings.Secure.ONE_HANDED_MODE_ENABLED state.
|
||||
*/
|
||||
public void unregisterToggleAwareObserver() {
|
||||
final ContentResolver resolver = mContext.getContentResolver();
|
||||
resolver.unregisterContentObserver(mSettingsObserver);
|
||||
}
|
||||
|
||||
private final class SettingsObserver extends ContentObserver {
|
||||
private TogglesCallback mCallback;
|
||||
|
||||
private final Uri mOneHandedEnabledAware = Settings.Secure.getUriFor(
|
||||
Settings.Secure.ONE_HANDED_MODE_ENABLED);
|
||||
|
||||
SettingsObserver(Handler handler) {
|
||||
super(handler);
|
||||
}
|
||||
|
||||
private void setCallback(TogglesCallback callback) {
|
||||
mCallback = callback;
|
||||
}
|
||||
|
||||
public void observe() {
|
||||
final ContentResolver resolver = mContext.getContentResolver();
|
||||
resolver.registerContentObserver(mOneHandedEnabledAware, true, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChange(boolean selfChange, Uri uri) {
|
||||
if (mCallback != null) mCallback.onChange(uri);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An interface for when Settings.Secure key state changes.
|
||||
*/
|
||||
public interface TogglesCallback {
|
||||
/**
|
||||
* Callback method for Settings.Secure key state changes.
|
||||
* @param uri
|
||||
*/
|
||||
void onChange(Uri uri);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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.gestures;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
|
||||
import androidx.preference.ListPreference;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The Controller to handle one-handed mode timeout state.
|
||||
**/
|
||||
public class OneHandedTimeoutPreferenceController extends BasePreferenceController implements
|
||||
Preference.OnPreferenceChangeListener, LifecycleObserver, OnStart, OnStop,
|
||||
OneHandedSettingsUtils.TogglesCallback {
|
||||
|
||||
private final Map<String, String> mTimeoutMap;
|
||||
private Preference mTimeoutPreference;
|
||||
private final OneHandedSettingsUtils mUtils;
|
||||
|
||||
public OneHandedTimeoutPreferenceController(Context context, String preferenceKey) {
|
||||
super(context, preferenceKey);
|
||||
mTimeoutMap = new HashMap<>();
|
||||
initTimeoutMap();
|
||||
mUtils = new OneHandedSettingsUtils(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
return OneHandedSettingsUtils.isOneHandedModeEnabled(mContext)
|
||||
? AVAILABLE : DISABLED_DEPENDENT_SETTING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object object) {
|
||||
if (!(preference instanceof ListPreference)) {
|
||||
return false;
|
||||
}
|
||||
final int newValue = Integer.parseInt((String) object);
|
||||
OneHandedSettingsUtils.setSettingsOneHandedModeTimeout(mContext, newValue);
|
||||
updateState(preference);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
if (!(preference instanceof ListPreference)) {
|
||||
return;
|
||||
}
|
||||
final ListPreference listPreference = (ListPreference) preference;
|
||||
listPreference.setValue(getTimeoutValue());
|
||||
|
||||
final int availabilityStatus = getAvailabilityStatus();
|
||||
preference.setEnabled(
|
||||
availabilityStatus == AVAILABLE || availabilityStatus == AVAILABLE_UNSEARCHABLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getSummary() {
|
||||
return mTimeoutMap.get(getTimeoutValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mTimeoutPreference = screen.findPreference(mPreferenceKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
mUtils.registerToggleAwareObserver(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
mUtils.unregisterToggleAwareObserver();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChange(Uri uri) {
|
||||
updateState(mTimeoutPreference);
|
||||
}
|
||||
|
||||
private String getTimeoutValue() {
|
||||
return String.valueOf(OneHandedSettingsUtils.getSettingsOneHandedModeTimeout(mContext));
|
||||
}
|
||||
|
||||
private void initTimeoutMap() {
|
||||
if (mTimeoutMap.size() != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
final String[] timeoutValues = mContext.getResources().getStringArray(
|
||||
R.array.one_handed_timeout_values);
|
||||
final String[] timeoutTitles = mContext.getResources().getStringArray(
|
||||
R.array.one_handed_timeout_title);
|
||||
|
||||
if (timeoutValues.length != timeoutTitles.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < timeoutValues.length; i++) {
|
||||
mTimeoutMap.put(timeoutValues[i], timeoutTitles[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user