Added autofill options on Developer Options screen.

Test: manual verification
Test: atest AutofillResetOptionsPreferenceControllerTest\
            AutofillLoggingLevelPreferenceControllerTest
Test: runtest --path packages/apps/Settings/tests/unit/src/com/android/settings/core/PreferenceControllerContractTest.java
Fixes: 65700540

Change-Id: I6b35fbf549529f4d97df164ce3fb6d641ee37650
This commit is contained in:
Felipe Leme
2018-07-24 17:24:40 -07:00
parent f24b749ec0
commit 9bf591b832
15 changed files with 839 additions and 0 deletions

View File

@@ -39,6 +39,8 @@ import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.Utils;
import com.android.settings.dashboard.RestrictedDashboardFragment;
import com.android.settings.development.autofill.AutofillLoggingLevelPreferenceController;
import com.android.settings.development.autofill.AutofillResetOptionsPreferenceController;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settings.widget.SwitchBar;
@@ -466,6 +468,8 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
controllers.add(new DefaultLaunchPreferenceController(context, "density"));
controllers.add(new DefaultLaunchPreferenceController(context, "background_check"));
controllers.add(new DefaultLaunchPreferenceController(context, "inactive_apps"));
controllers.add(new AutofillLoggingLevelPreferenceController(context));
controllers.add(new AutofillResetOptionsPreferenceController(context));
return controllers;
}

View File

@@ -0,0 +1,112 @@
/*
* Copyright (C) 2018 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.development.autofill;
import android.content.Context;
import android.content.res.Resources;
import android.provider.Settings;
import android.text.BidiFormatter;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.Slog;
import android.view.Display;
import android.view.View;
import android.view.autofill.AutofillManager;
import android.widget.EditText;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settingslib.CustomEditTextPreferenceCompat;
import java.text.NumberFormat;
/**
* Base class for Autofill integer properties that are backed by
* {@link android.provider.Settings.Global}.
*/
abstract class AbstractGlobalSettingsPreference extends CustomEditTextPreferenceCompat {
private static final String TAG = "AbstractGlobalSettingsPreference";
private final String mKey;
private final int mDefaultValue;
private final AutofillDeveloperSettingsObserver mObserver;
protected AbstractGlobalSettingsPreference(Context context, AttributeSet attrs,
String key, int defaultValue) {
super(context, attrs);
mKey = key;
mDefaultValue = defaultValue;
mObserver = new AutofillDeveloperSettingsObserver(context, () -> updateSummary());
}
@Override
public void onAttached() {
super.onAttached();
mObserver.register();
updateSummary();
}
@Override
public void onDetached() {
mObserver.unregister();
super.onDetached();
}
private String getCurrentValue() {
final int value = Settings.Global.getInt(getContext().getContentResolver(),
mKey, mDefaultValue);
return Integer.toString(value);
}
private void updateSummary() {
setSummary(getCurrentValue());
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
EditText editText = view.findViewById(android.R.id.edit);
if (editText != null) {
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
editText.setText(getCurrentValue());
Utils.setEditTextCursorPosition(editText);
}
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
final String stringValue = getText();
int newValue = mDefaultValue;
try {
newValue = Integer.parseInt(stringValue);
} catch (Exception e) {
Log.e(TAG, "Error converting '" + stringValue + "' to integer. Using "
+ mDefaultValue + " instead");
}
Settings.Global.putInt(getContext().getContentResolver(), mKey, newValue);
}
}
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) 2018 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.development.autofill;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.UserHandle;
import android.provider.Settings;
final class AutofillDeveloperSettingsObserver extends ContentObserver {
private final Runnable mChangeCallback;
private final ContentResolver mResolver;
public AutofillDeveloperSettingsObserver(Context context, Runnable changeCallback) {
super(new Handler());
mResolver = context.getContentResolver();
mChangeCallback = changeCallback;
}
public void register() {
mResolver.registerContentObserver(Settings.Global.getUriFor(
Settings.Global.AUTOFILL_LOGGING_LEVEL), false, this,
UserHandle.USER_ALL);
mResolver.registerContentObserver(Settings.Global.getUriFor(
Settings.Global.AUTOFILL_MAX_PARTITIONS_SIZE), false, this,
UserHandle.USER_ALL);
mResolver.registerContentObserver(Settings.Global.getUriFor(
Settings.Global.AUTOFILL_MAX_VISIBLE_DATASETS), false, this,
UserHandle.USER_ALL);
}
public void unregister() {
mResolver.unregisterContentObserver(this);
}
@Override
public void onChange(boolean selfChange, Uri uri, int userId) {
mChangeCallback.run(); // Run Forrest, Run!
}
}

View File

@@ -0,0 +1,100 @@
/*
* Copyright (C) 2018 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.development.autofill;
import android.content.Context;
import android.content.res.Resources;
import android.provider.Settings;
import android.view.autofill.AutofillManager;
import com.android.settings.R;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.development.DeveloperOptionsPreferenceController;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
public final class AutofillLoggingLevelPreferenceController
extends DeveloperOptionsPreferenceController
implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
private static final String AUTOFILL_LOGGING_LEVEL_KEY = "autofill_logging_level";
private final String[] mListValues;
private final String[] mListSummaries;
private final AutofillDeveloperSettingsObserver mObserver;
public AutofillLoggingLevelPreferenceController(Context context) {
super(context);
Resources resources = context.getResources();
mListValues = resources.getStringArray(R.array.autofill_logging_level_values);
mListSummaries = resources.getStringArray(R.array.autofill_logging_level_entries);
mObserver = new AutofillDeveloperSettingsObserver(mContext, () -> updateOptions());
mObserver.register();
// TODO: there should be a hook on AbstractPreferenceController where we could unregister it
}
@Override
public String getPreferenceKey() {
return AUTOFILL_LOGGING_LEVEL_KEY;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
writeLevel(newValue);
updateOptions();
return true;
}
@Override
public void updateState(Preference preference) {
updateOptions();
}
@Override
protected void onDeveloperOptionsSwitchDisabled() {
super.onDeveloperOptionsSwitchDisabled();
writeLevel(null);
}
private void updateOptions() {
final int level = Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.AUTOFILL_LOGGING_LEVEL, AutofillManager.DEFAULT_LOGGING_LEVEL);
final int index;
if (level == AutofillManager.FLAG_ADD_CLIENT_DEBUG) {
index = 1;
} else if (level == AutofillManager.FLAG_ADD_CLIENT_VERBOSE) {
index = 2;
} else {
index = 0;
}
final ListPreference listPreference = (ListPreference) mPreference;
listPreference.setValue(mListValues[index]);
listPreference.setSummary(mListSummaries[index]);
}
private void writeLevel(Object newValue) {
int level = AutofillManager.NO_LOGGING;
if (newValue instanceof String) {
level = Integer.parseInt((String) newValue);
}
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.AUTOFILL_LOGGING_LEVEL, level);
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright (C) 2018 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.development.autofill;
import android.content.Context;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.autofill.AutofillManager;
import java.text.NumberFormat;
public final class AutofillMaxPartitionsPreference extends AbstractGlobalSettingsPreference {
public AutofillMaxPartitionsPreference(Context context, AttributeSet attrs) {
super(context, attrs, Settings.Global.AUTOFILL_MAX_PARTITIONS_SIZE,
AutofillManager.DEFAULT_MAX_PARTITIONS_SIZE);
}
}

View File

@@ -0,0 +1,85 @@
/*
* Copyright (C) 2018 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.development.autofill;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.UserHandle;
import android.provider.Settings;
import android.util.AttributeSet;
import android.util.Log;
import android.view.autofill.AutofillManager;
import androidx.preference.PreferenceCategory;
public final class AutofillPreferenceCategory extends PreferenceCategory {
private static final String TAG = "AutofillPreferenceCategory";
private final ContentResolver mContentResolver;
private final ContentObserver mSettingsObserver;
public AutofillPreferenceCategory(Context context, AttributeSet attrs) {
super(context, attrs);
mSettingsObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange, Uri uri, int userId) {
Log.w(TAG, "Autofill Service changed, but UI cannot be refreshed");
// TODO(b/111838239): we cannot update the UI because AFM.isEnabled() will return
// the previous value. Once that's fixed, we'll need to call one of the 2 callbacks
// below:
// notifyChanged();
// notifyDependencyChange(shouldDisableDependents());
}
};
mContentResolver = context.getContentResolver();
}
@Override
public void onAttached() {
super.onAttached();
mContentResolver.registerContentObserver(
Settings.Secure.getUriFor(Settings.Secure.AUTOFILL_SERVICE), false,
mSettingsObserver);
}
@Override
public void onDetached() {
mContentResolver.unregisterContentObserver(mSettingsObserver);
super.onDetached();
}
// PreferenceCategory.isEnabled() always return false, so we rather not change that logic
// decide whether the children should be shown using isAutofillEnabled() instead.
private boolean isAutofillEnabled() {
final AutofillManager afm = getContext().getSystemService(AutofillManager.class);
final boolean enabled = afm != null && afm.isEnabled();
Log.v(TAG, "isAutofillEnabled(): " + enabled);
return enabled;
}
@Override
public boolean shouldDisableDependents() {
final boolean shouldIt = !isAutofillEnabled();
Log.v(TAG, "shouldDisableDependents(): " + shouldIt);
return shouldIt;
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2018 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.development.autofill;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.autofill.AutofillManager;
import android.widget.Toast;
import com.android.settings.R;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.development.DeveloperOptionsPreferenceController;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
public final class AutofillResetOptionsPreferenceController
extends DeveloperOptionsPreferenceController
implements PreferenceControllerMixin {
private static final String AUTOFILL_RESET_OPTIONS_KEY = "autofill_reset_developer_options";
public AutofillResetOptionsPreferenceController(Context context) {
super(context);
}
@Override
public String getPreferenceKey() {
return AUTOFILL_RESET_OPTIONS_KEY;
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (!TextUtils.equals(AUTOFILL_RESET_OPTIONS_KEY, preference.getKey())) {
return false;
}
final ContentResolver contentResolver = mContext.getContentResolver();
Settings.Global.putInt(contentResolver, Settings.Global.AUTOFILL_LOGGING_LEVEL,
AutofillManager.DEFAULT_LOGGING_LEVEL);
Settings.Global.putInt(contentResolver, Settings.Global.AUTOFILL_MAX_PARTITIONS_SIZE,
AutofillManager.DEFAULT_MAX_PARTITIONS_SIZE);
Settings.Global.putInt(contentResolver, Settings.Global.AUTOFILL_MAX_VISIBLE_DATASETS, 0);
Toast.makeText(mContext, R.string.autofill_reset_developer_options_complete,
Toast.LENGTH_SHORT).show();
return true;
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2018 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.development.autofill;
import android.content.Context;
import android.provider.Settings;
import android.util.AttributeSet;
public final class AutofillVisibleDatasetsPreference extends AbstractGlobalSettingsPreference {
public AutofillVisibleDatasetsPreference(Context context, AttributeSet attrs) {
super(context, attrs, Settings.Global.AUTOFILL_MAX_VISIBLE_DATASETS, 0);
}
}