Refactor autofill preference

This commit is contained in:
J-Jamet
2018-03-05 13:05:59 +01:00
parent d5b752e4c0
commit 4876623f86
12 changed files with 132 additions and 220 deletions

View File

@@ -133,10 +133,6 @@
<activity android:name="com.keepassdroid.settings.SettingsActivity" />
<activity android:name="com.keepassdroid.AutoFillAuthActivity"
android:configChanges="orientation|keyboardHidden" />
<activity
android:name="com.keepassdroid.settings.SettingsAutofillActivity"
android:exported="true">
</activity>
<service android:name="com.keepassdroid.services.TimeoutService" />
<service

View File

@@ -1,98 +0,0 @@
/*
* Copyright 2017 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.keepassdroid.settings;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.util.Log;
import android.view.autofill.AutofillManager;
import com.kunzisoft.keepass.R;
@RequiresApi(api = Build.VERSION_CODES.O)
public class AutofillPreferenceFragment extends PreferenceFragmentCompat implements Preference.OnPreferenceClickListener {
private static final int REQUEST_CODE_SET_DEFAULT = 1;
private AutofillManager mAutofillManager;
private SwitchPreference enablePreference;
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.autofill_preferences, rootKey);
mAutofillManager = getActivity().getSystemService(AutofillManager.class);
// add listeners for non-default actions
enablePreference = (SwitchPreference) findPreference(getString(R.string.settings_autofill_enable_key));
}
@Override
public void onResume() {
super.onResume();
enablePreference.setOnPreferenceClickListener(null);
if (mAutofillManager != null && mAutofillManager.hasEnabledAutofillServices())
enablePreference.setChecked(mAutofillManager.hasEnabledAutofillServices());
enablePreference.setOnPreferenceClickListener(this);
}
@Override
public boolean onPreferenceClick(Preference preference) {
if (preference.getKey().equals(getString(R.string.settings_autofill_enable_key))) {
setService(((SwitchPreference) preference).isChecked());
}
return false;
}
private void setService(boolean enableService) {
if (enableService) {
startEnableService();
} else {
disableService();
}
}
private void disableService() {
if (mAutofillManager != null && mAutofillManager.hasEnabledAutofillServices()) {
mAutofillManager.disableAutofillServices();
} else {
Log.d(getClass().getName(), "Sample service already disabled.");
}
}
private void startEnableService() {
if (mAutofillManager != null && !mAutofillManager.hasEnabledAutofillServices()) {
Intent intent = new Intent(Settings.ACTION_REQUEST_SET_AUTOFILL_SERVICE);
intent.setData(Uri.parse("package:com.example.android.autofill.service"));
Log.d(getClass().getName(), "enableService(): intent="+ intent);
startActivityForResult(intent, REQUEST_CODE_SET_DEFAULT);
} else {
Log.d(getClass().getName(), "Sample service already enabled.");
}
}
}

View File

@@ -1,13 +1,14 @@
package com.keepassdroid.settings;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import com.keepassdroid.database.Database;
import com.keepassdroid.app.App;
import com.keepassdroid.database.Database;
import com.kunzisoft.keepass.R;
public class MainPreferenceFragment extends PreferenceFragmentCompat implements Preference.OnPreferenceClickListener {
@@ -33,6 +34,13 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat implements
Preference preference = findPreference(getString(R.string.app_key));
preference.setOnPreferenceClickListener(this);
Preference preferenceAutofill = findPreference(getString(R.string.settings_form_filling_key));
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
preferenceAutofill.setEnabled(false);
} else {
preferenceAutofill.setOnPreferenceClickListener(this);
}
preference = findPreference(getString(R.string.db_key));
preference.setOnPreferenceClickListener(this);
@@ -63,6 +71,10 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat implements
mCallback.onNestedPreferenceSelected(NestedSettingsFragment.NESTED_SCREEN_APP_KEY);
}
if (preference.getKey().equals(getString(R.string.settings_form_filling_key))) {
mCallback.onNestedPreferenceSelected(NestedSettingsFragment.NESTED_SCREEN_FORM_FILLING_KEY);
}
if (preference.getKey().equals(getString(R.string.db_key))) {
mCallback.onNestedPreferenceSelected(NestedSettingsFragment.NESTED_SCREEN_DB_KEY);
}

View File

@@ -22,8 +22,10 @@ package com.keepassdroid.settings;
import android.content.Intent;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.support.v14.preference.SwitchPreference;
import android.support.v4.app.DialogFragment;
@@ -32,6 +34,7 @@ import android.support.v7.app.AlertDialog;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.util.Log;
import android.view.autofill.AutofillManager;
import android.widget.Toast;
import com.keepassdroid.database.Database;
@@ -46,10 +49,13 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat
implements Preference.OnPreferenceClickListener {
public static final int NESTED_SCREEN_APP_KEY = 1;
public static final int NESTED_SCREEN_DB_KEY = 2;
public static final int NESTED_SCREEN_FORM_FILLING_KEY = 2;
public static final int NESTED_SCREEN_DB_KEY = 3;
private static final String TAG_KEY = "NESTED_KEY";
private static final int REQUEST_CODE_AUTOFILL = 5201;
public static NestedSettingsFragment newInstance(int key) {
NestedSettingsFragment fragment = new NestedSettingsFragment();
// supply arguments to bundle.
@@ -59,6 +65,23 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat
return fragment;
}
@Override
public void onResume() {
super.onResume();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
SwitchPreference autoFillEnablePreference =
(SwitchPreference) findPreference(getString(R.string.settings_autofill_enable_key));
if (autoFillEnablePreference != null) {
AutofillManager autofillManager = getActivity().getSystemService(AutofillManager.class);
if (autofillManager != null && autofillManager.hasEnabledAutofillServices())
autoFillEnablePreference.setChecked(true);
else
autoFillEnablePreference.setChecked(false);
}
}
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
int key = getArguments().getInt(TAG_KEY);
@@ -117,13 +140,6 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat
});
}
Preference preferenceAutofill = findPreference(getString(R.string.settings_autofill_key));
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
preferenceAutofill.setEnabled(false);
} else {
preferenceAutofill.setOnPreferenceClickListener(this);
}
Preference deleteKeysFingerprints = findPreference(getString(R.string.fingerprint_delete_all_key));
if (!fingerprintSupported) {
deleteKeysFingerprints.setEnabled(false);
@@ -171,6 +187,51 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat
}
break;
case NESTED_SCREEN_FORM_FILLING_KEY:
setPreferencesFromResource(R.xml.form_filling_preferences, rootKey);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
SwitchPreference autoFillEnablePreference =
(SwitchPreference) findPreference(getString(R.string.settings_autofill_enable_key));
AutofillManager autofillManager = getActivity().getSystemService(AutofillManager.class);
if (autofillManager != null && autofillManager.hasEnabledAutofillServices())
autoFillEnablePreference.setChecked(autofillManager.hasEnabledAutofillServices());
autoFillEnablePreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public boolean onPreferenceClick(Preference preference) {
if (((SwitchPreference) preference).isChecked()) {
startEnableService();
} else {
disableService();
}
return false;
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void disableService() {
if (autofillManager != null && autofillManager.hasEnabledAutofillServices()) {
autofillManager.disableAutofillServices();
} else {
Log.d(getClass().getName(), "Sample service already disabled.");
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void startEnableService() {
if (autofillManager != null && !autofillManager.hasEnabledAutofillServices()) {
Intent intent = new Intent(Settings.ACTION_REQUEST_SET_AUTOFILL_SERVICE);
intent.setData(Uri.parse("package:com.example.android.autofill.service"));
Log.d(getClass().getName(), "enableService(): intent="+ intent);
startActivityForResult(intent, REQUEST_CODE_AUTOFILL);
} else {
Log.d(getClass().getName(), "Sample service already enabled.");
}
}
});
}
break;
case NESTED_SCREEN_DB_KEY:
setPreferencesFromResource(R.xml.db_preferences, rootKey);
@@ -205,8 +266,6 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat
Log.e(getClass().getName(), "Database isn't ready");
}
break;
default:
@@ -247,6 +306,8 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat
switch (key) {
case NESTED_SCREEN_APP_KEY:
return resources.getString(R.string.menu_app_settings);
case NESTED_SCREEN_FORM_FILLING_KEY:
return resources.getString(R.string.menu_form_filling_settings);
case NESTED_SCREEN_DB_KEY:
return resources.getString(R.string.menu_db_settings);
default:
@@ -256,10 +317,7 @@ public class NestedSettingsFragment extends PreferenceFragmentCompat
@Override
public boolean onPreferenceClick(Preference preference) {
if (preference.getKey().equals(getString(R.string.settings_autofill_key))) {
Intent intent = new Intent(getContext(), SettingsAutofillActivity.class);
getActivity().startActivity(intent);
}
// TODO encapsulate
return false;
}

View File

@@ -1,61 +0,0 @@
/*
* Copyright 2017 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.keepassdroid.settings;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import com.kunzisoft.keepass.R;
@RequiresApi(api = Build.VERSION_CODES.O)
public class SettingsAutofillActivity extends SettingsActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toolbar);
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle(R.string.menu_autofill_settings);
setSupportActionBar(toolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new AutofillPreferenceFragment())
.commit();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch ( item.getItemId() ) {
case android.R.id.home:
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}
}

View File

@@ -34,6 +34,7 @@
<string name="app_timeout_summary">Temps avant le verrouillage de la base de données lorsque l\'application est inactive.</string>
<string name="application">Application</string>
<string name="menu_app_settings">Préférences de l\'application</string>
<string name="menu_form_filling_settings">Préférences de remplissage de formulaire</string>
<string name="beta_dontask">Ne plus afficher</string>
<string name="beta_warning">Enregistrer des modifications dans les fichiers kdbx est EXPÉRIMENTAL. Faites des sauvegardes de votre base de données avant toute modification.</string>
<string name="brackets">Crochets</string>
@@ -222,6 +223,7 @@
<string name="password_size_summary">Définir la taille par défaut du mot de passe généré</string>
<string name="list_password_generator_options_title">Caractères de mot de passe</string>
<string name="list_password_generator_options_summary">Définir les caractères par défaut du générateur de mot de passe</string>
<string name="notifications">Notifications</string>
<string name="clipboard_notifications_title">Notifications du presse-papiers</string>
<string name="clipboard_notifications_summary">Activer les notifications du presse-papiers pour copier le nom d\'utilisateur et le mot de passe</string>
<string name="lock_database_screen_off_title">Verrouillage d\'écran</string>

View File

@@ -59,7 +59,7 @@
<string name="clipboard_notifications_key" translatable="false">clipboard_notifications_key</string>
<string name="lock_database_screen_off_key" translatable="false">lock_database_screen_off_key</string>
<string name="fingerprint_enable_key" translatable="true">fingerprint_enable_key</string>
<string name="settings_autofill_key" translatable="false">settings_autofill_key</string>
<string name="settings_form_filling_key" translatable="false">settings_form_filling_key</string>
<string name="settings_autofill_enable_key" translatable="false">settings_autofill_enable_key</string>
<string name="full_file_path_enable_key" translatable="true">full_file_path_enable_key</string>
<string name="fingerprint_delete_all_key" translatable="true">fingerprint_delete_all_key</string>

View File

@@ -133,8 +133,8 @@
<string name="menu_create">Create</string>
<string name="settings">Settings</string>
<string name="menu_app_settings">Application settings</string>
<string name="menu_form_filling_settings">Form filling settings</string>
<string name="menu_db_settings">Database settings</string>
<string name="menu_autofill_settings">Autofill settings</string>
<string name="menu_delete">Delete</string>
<string name="menu_donate">Donate</string>
<string name="menu_edit">Edit</string>
@@ -228,6 +228,7 @@
<string name="password_size_summary">Set the default size of the generated password</string>
<string name="list_password_generator_options_title">Password characters</string>
<string name="list_password_generator_options_summary">Set the default password generator characters</string>
<string name="notifications">Notifications</string>
<string name="clipboard_notifications_title">Clipboard Notifications</string>
<string name="clipboard_notifications_summary">Enable clipboard notifications to copy username and password</string>
<string name="lock">Lock</string>

View File

@@ -33,11 +33,6 @@
android:defaultValue="@bool/saf_default"
android:title="@string/use_saf_title"
android:key="@string/saf_key"/>
<android.support.v14.preference.SwitchPreference
android:key="@string/clipboard_notifications_key"
android:title="@string/clipboard_notifications_title"
android:defaultValue="@bool/clipboard_notifications_default"
android:summary="@string/clipboard_notifications_summary" />
</android.support.v7.preference.PreferenceCategory>
@@ -125,15 +120,6 @@
</android.support.v7.preference.PreferenceCategory>
<android.support.v7.preference.PreferenceCategory
android:title="@string/autofill">
<android.support.v7.preference.Preference
android:key="@string/settings_autofill_key"
android:title="@string/menu_autofill_settings"
android:icon="@drawable/ic_content_paste_prefs_24dp"
android:persistent="false" />
</android.support.v7.preference.PreferenceCategory>
<android.support.v7.preference.PreferenceCategory
android:title="@string/appearance">

View File

@@ -1,25 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2017 Jeremy Jamet / Kunzisoft.
This file is part of KeePass DX.
KeePass DX is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
KeePass DX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:title="@string/set_autofill_service"
android:defaultValue="@bool/settings_autofill_enable_default"
android:key="@string/settings_autofill_enable_key"/>
</PreferenceScreen>

View File

@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2017 Jeremy Jamet / Kunzisoft.
This file is part of KeePass DX.
KeePass DX is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
KeePass DX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.preference.PreferenceCategory
android:title="@string/autofill">
<SwitchPreference
android:title="@string/set_autofill_service"
android:defaultValue="@bool/settings_autofill_enable_default"
android:key="@string/settings_autofill_enable_key"/>
</android.support.v7.preference.PreferenceCategory>
<android.support.v7.preference.PreferenceCategory
android:title="@string/notifications">
<android.support.v14.preference.SwitchPreference
android:key="@string/clipboard_notifications_key"
android:title="@string/clipboard_notifications_title"
android:defaultValue="@bool/clipboard_notifications_default"
android:summary="@string/clipboard_notifications_summary" />
</android.support.v7.preference.PreferenceCategory>
</PreferenceScreen>

View File

@@ -27,6 +27,11 @@
android:title="@string/menu_app_settings"
android:icon="@drawable/ic_phone_android_prefs_24dp"
android:persistent="false" />
<android.support.v7.preference.Preference
android:key="@string/settings_form_filling_key"
android:title="@string/menu_form_filling_settings"
android:icon="@drawable/ic_content_paste_prefs_24dp"
android:persistent="false" />
</android.support.v7.preference.PreferenceCategory>
<android.support.v7.preference.PreferenceCategory
@@ -36,7 +41,6 @@
android:title="@string/menu_db_settings"
android:icon="@drawable/ic_data_usage_prefs_24dp"
android:persistent="false" />
<com.keepassdroid.settings.RoundsPreference
android:key="@string/roundsFix_key"
android:title="@string/rounds_fix_title"