Fixes #3465 Use AndroidX Pref (#3521)

* [WIP] Fixes #3465 Use AndroidX Pref

* Deleted Unused Files

* Added singleLineTitle

* Updated Gradle Properties

* Migrated to Androidx

* Inline Variable change

* optimise imports

* Fixed Crash on empty input
This commit is contained in:
Madhur Gupta 2020-03-17 14:10:26 +01:00 committed by GitHub
parent e55b5495a1
commit 15bccdf650
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 156 additions and 389 deletions

View file

@ -106,12 +106,17 @@ dependencies {
implementation 'com.daimajia.swipelayout:library:1.2.0@aar'
//Room
def room_version= '2.2.3'
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor
implementation "androidx.room:room-runtime:$ROOM_VERSION"
kapt "androidx.room:room-compiler:$ROOM_VERSION" // For Kotlin use kapt instead of annotationProcessor
implementation 'com.squareup.retrofit2:retrofit:2.7.1'
implementation "androidx.room:room-rxjava2:$room_version"
implementation "androidx.room:room-rxjava2:$ROOM_VERSION"
testImplementation "androidx.arch.core:core-testing:2.1.0"
// Pref
// Java language implementation
implementation "androidx.preference:preference:$PREFERENCE_VERSION"
// Kotlin
implementation "androidx.preference:preference-ktx:$PREFERENCE_VERSION"
}
android {

View file

@ -8,7 +8,7 @@ import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.preference.PreferenceManager;
import androidx.preference.PreferenceManager;
import android.provider.MediaStore;
import android.text.TextUtils;

View file

@ -1,7 +1,7 @@
package fr.free.nrw.commons.filepicker;
import android.content.Context;
import android.preference.PreferenceManager;
import androidx.preference.PreferenceManager;
public class FilePickerConfiguration implements Constants {

View file

@ -3,28 +3,17 @@ package fr.free.nrw.commons.settings;
import android.Manifest;
import android.net.Uri;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.MultiSelectListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.SwitchPreference;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.InputFilter;
import android.text.InputType;
import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
import androidx.preference.MultiSelectListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import com.google.android.material.snackbar.Snackbar;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.single.BasePermissionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import javax.inject.Inject;
import javax.inject.Named;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.di.ApplicationlessInjection;
@ -33,10 +22,15 @@ import fr.free.nrw.commons.logging.CommonsLogSender;
import fr.free.nrw.commons.upload.Language;
import fr.free.nrw.commons.utils.PermissionUtils;
import fr.free.nrw.commons.utils.ViewUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import javax.inject.Inject;
import javax.inject.Named;
import static fr.free.nrw.commons.utils.SystemThemeUtils.THEME_MODE_DEFAULT;
public class SettingsFragment extends PreferenceFragment {
public class SettingsFragment extends PreferenceFragmentCompat {
@Inject
@Named("default_preferences")
@ -49,29 +43,19 @@ public class SettingsFragment extends PreferenceFragment {
private ListPreference langListPreference;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
ApplicationlessInjection
.getInstance(getActivity().getApplicationContext())
.getCommonsApplicationComponent()
.inject(this);
.getInstance(getActivity().getApplicationContext())
.getCommonsApplicationComponent()
.inject(this);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
// Set the preferences from an XML resource
setPreferencesFromResource(R.xml.preferences, rootKey);
themeListPreference = (ListPreference) findPreference(Prefs.KEY_THEME_VALUE);
themeListPreference = findPreference(Prefs.KEY_THEME_VALUE);
prepareTheme();
//Check if the Author Name switch is enabled and appropriately handle the author name usage
SwitchPreference useAuthorName = (SwitchPreference) findPreference("useAuthorName");
EditTextPreference authorName = (EditTextPreference) findPreference("authorName");
authorName.setEnabled(defaultKvStore.getBoolean("useAuthorName", false));
useAuthorName.setOnPreferenceChangeListener((preference, newValue) -> {
authorName.setEnabled((Boolean)newValue);
return true;
});
MultiSelectListPreference multiSelectListPref = (MultiSelectListPreference) findPreference(Prefs.MANAGED_EXIF_TAGS);
MultiSelectListPreference multiSelectListPref = findPreference(Prefs.MANAGED_EXIF_TAGS);
if (multiSelectListPref != null) {
multiSelectListPref.setOnPreferenceChangeListener((preference, newValue) -> {
if (newValue instanceof HashSet && !((HashSet) newValue).contains(getString(R.string.exif_tag_location))) {
@ -81,42 +65,44 @@ public class SettingsFragment extends PreferenceFragment {
});
}
final EditTextPreference uploadLimit = (EditTextPreference) findPreference("uploads");
final EditTextPreference uploadLimit = findPreference("uploads");
int currentUploadLimit = defaultKvStore.getInt(Prefs.UPLOADS_SHOWING, 100);
uploadLimit.setText(Integer.toString(currentUploadLimit));
uploadLimit.setSummary(Integer.toString(currentUploadLimit));
uploadLimit.getEditText().addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
uploadLimit.setText(String.valueOf(currentUploadLimit));
uploadLimit.setOnPreferenceChangeListener((preference, newValue) -> {
if (newValue.toString().length() == 0) {
return false;
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.length() == 0) return;
int value = Integer.parseInt(s.toString());
if (value > 500) {
uploadLimit.getEditText().setError(getString(R.string.maximum_limit_alert));
value = 500;
} else if (value == 0) {
uploadLimit.getEditText().setError(getString(R.string.cannot_be_zero));
value = 100;
}
defaultKvStore.putInt(Prefs.UPLOADS_SHOWING, value);
defaultKvStore.putBoolean(Prefs.IS_CONTRIBUTION_COUNT_CHANGED, true);
uploadLimit.setText(Integer.toString(value));
uploadLimit.setSummary(Integer.toString(value));
int value = Integer.parseInt(newValue.toString());
if (value > 500) {
Snackbar error = Snackbar.make(getView(), R.string.maximum_limit_alert, Snackbar.LENGTH_LONG);
error.show();
return false;
} else if (value == 0) {
Snackbar error = Snackbar.make(getView(), R.string.cannot_be_zero, Snackbar.LENGTH_LONG);
error.show();
return false;
}
return true;
});
langListPreference = (ListPreference) findPreference("descriptionDefaultLanguagePref");
uploadLimit.setOnBindEditTextListener(editText -> {
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
editText.selectAll();
int maxLength = 3; // set maxLength to 3
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
int value = Integer.parseInt(editText.getText().toString());
defaultKvStore.putInt(Prefs.UPLOADS_SHOWING, value);
defaultKvStore.putBoolean(Prefs.IS_CONTRIBUTION_COUNT_CHANGED, true);
uploadLimit.setText(Integer.toString(value));
});
langListPreference = findPreference("descriptionDefaultLanguagePref");
prepareLanguages();
Preference betaTesterPreference = findPreference("becomeBetaTester");
betaTesterPreference.setOnPreferenceClickListener(preference -> {
@ -129,42 +115,26 @@ public class SettingsFragment extends PreferenceFragment {
return true;
});
// Disable some settings when not logged in.
if (defaultKvStore.getBoolean("login_skipped", false)){
SwitchPreference useExternalStorage = (SwitchPreference) findPreference("useExternalStorage");
SwitchPreference displayNearbyCardView = (SwitchPreference) findPreference("displayNearbyCardView");
SwitchPreference displayLocationPermissionForCardView = (SwitchPreference) findPreference("displayLocationPermissionForCardView");
SwitchPreference displayCampaignsCardView = (SwitchPreference) findPreference("displayCampaignsCardView");
useExternalStorage.setEnabled(false);
if (defaultKvStore.getBoolean("login_skipped", false)) {
findPreference("useExternalStorage").setEnabled(false);
findPreference("useAuthorName").setEnabled(false);
findPreference("displayNearbyCardView").setEnabled(false);
findPreference("displayLocationPermissionForCardView").setEnabled(false);
findPreference("displayCampaignsCardView").setEnabled(false);
uploadLimit.setEnabled(false);
useAuthorName.setEnabled(false);
displayNearbyCardView.setEnabled(false);
displayLocationPermissionForCardView.setEnabled(false);
displayCampaignsCardView.setEnabled(false);
}
}
/**
* Uses previously saved theme if there is any, if not then uses default.
* Sets the theme pref
*/
private void prepareTheme() {
themeListPreference.setSummary(getThemeSummary(getCurrentTheme()));
themeListPreference.setOnPreferenceChangeListener((preference, newValue) -> {
getActivity().recreate();
return true;
});
}
private CharSequence getThemeSummary(String value) {
int prefIndex = themeListPreference.findIndexOfValue(value);
return themeListPreference.getEntries()[prefIndex];
}
private String getCurrentTheme() {
return defaultKvStore.getString(Prefs.KEY_THEME_VALUE, THEME_MODE_DEFAULT);
}
/**
* Prepares language summary and language codes list and adds them to list preference as pairs.
* Uses previously saved language if there is any, if not uses phone local as initial language.
@ -195,19 +165,14 @@ public class SettingsFragment extends PreferenceFragment {
String languageCode = getCurrentLanguageCode();
if (languageCode.equals("")){
// If current language code is empty, means none selected by user yet so use phone local
langListPreference.setSummary(Locale.getDefault().getDisplayLanguage());
langListPreference.setValue(Locale.getDefault().getLanguage());
} else {
// If any language is selected by user previously, use it
int prefIndex = langListPreference.findIndexOfValue(languageCode);
langListPreference.setSummary(langListPreference.getEntries()[prefIndex]);
langListPreference.setValue(languageCode);
}
langListPreference.setOnPreferenceChangeListener((preference, newValue) -> {
String userSelectedValue = (String) newValue;
int prefIndex = langListPreference.findIndexOfValue(userSelectedValue);
langListPreference.setSummary(langListPreference.getEntries()[prefIndex]);
saveLanguageValue(userSelectedValue);
return true;
});

View file

@ -1,37 +0,0 @@
package fr.free.nrw.commons.ui.LongTitlePreferences;
import android.content.Context;
import android.preference.EditTextPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
/**
* Created by seannemann on 6/27/2018.
*/
public class LongTitleEditTextPreference extends EditTextPreference {
public LongTitleEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public LongTitleEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LongTitleEditTextPreference(Context context) {
super(context);
}
@Override
protected void onBindView(View view)
{
super.onBindView(view);
TextView title= view.findViewById(android.R.id.title);
if (title != null) {
title.setSingleLine(false);
}
}
}

View file

@ -1,32 +0,0 @@
package fr.free.nrw.commons.ui.LongTitlePreferences;
import android.content.Context;
import android.preference.ListPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
/**
* Created by seannemann on 6/27/2018.
*/
public class LongTitleListPreference extends ListPreference {
public LongTitleListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LongTitleListPreference(Context context) {
super(context);
}
@Override
protected void onBindView(View view)
{
super.onBindView(view);
TextView title= view.findViewById(android.R.id.title);
if (title != null) {
title.setSingleLine(false);
}
}
}

View file

@ -1,38 +0,0 @@
package fr.free.nrw.commons.ui.LongTitlePreferences;
import android.content.Context;
import android.preference.MultiSelectListPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
public class LongTitleMultiSelectListPreference extends MultiSelectListPreference {
/*
public LongTitleMultiSelectListPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public LongTitleMultiSelectListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
*/
public LongTitleMultiSelectListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LongTitleMultiSelectListPreference(Context context) {
super(context);
}
@Override
protected void onBindView(View view)
{
super.onBindView(view);
TextView title= view.findViewById(android.R.id.title);
if (title != null) {
title.setSingleLine(false);
}
}
}

View file

@ -1,36 +0,0 @@
package fr.free.nrw.commons.ui.LongTitlePreferences;
import android.content.Context;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
/**
* Created by seannemann on 6/27/2018.
*/
public class LongTitlePreference extends Preference {
public LongTitlePreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public LongTitlePreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LongTitlePreference(Context context) {
super(context);
}
@Override
protected void onBindView(View view)
{
super.onBindView(view);
TextView title= view.findViewById(android.R.id.title);
if (title != null) {
title.setSingleLine(false);
}
}
}

View file

@ -1,36 +0,0 @@
package fr.free.nrw.commons.ui.LongTitlePreferences;
import android.content.Context;
import android.preference.PreferenceCategory;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
/**
* Created by seannemann on 6/27/2018.
*/
public class LongTitlePreferenceCategory extends PreferenceCategory {
public LongTitlePreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public LongTitlePreferenceCategory(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LongTitlePreferenceCategory(Context context) {
super(context);
}
@Override
protected void onBindView(View view)
{
super.onBindView(view);
TextView title= view.findViewById(android.R.id.title);
if (title != null) {
title.setSingleLine(false);
}
}
}

View file

@ -1,36 +0,0 @@
package fr.free.nrw.commons.ui.LongTitlePreferences;
import android.content.Context;
import android.preference.SwitchPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
/**
* Created by seannemann on 6/27/2018.
*/
public class LongTitleSwitchPreference extends SwitchPreference {
public LongTitleSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public LongTitleSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LongTitleSwitchPreference(Context context) {
super(context);
}
@Override
protected void onBindView(View view)
{
super.onBindView(view);
TextView title= view.findViewById(android.R.id.title);
if (title != null) {
title.setSingleLine(false);
}
}
}

View file

@ -1,99 +1,109 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<fr.free.nrw.commons.ui.LongTitlePreferences.LongTitlePreferenceCategory
android:title="@string/preference_category_appearance">
<PreferenceCategory
android:title="@string/preference_category_appearance">
<fr.free.nrw.commons.ui.LongTitlePreferences.LongTitleListPreference
<ListPreference
android:key="appThemePref"
android:title= "@string/preference_theme"
app:singleLineTitle="false"
android:entries="@array/pref_theme_entries"
android:entryValues="@array/pref_theme_entries_values"
android:defaultValue="0"
android:summary="@string/theme_default_name" />
app:useSimpleSummaryProvider="true"
android:defaultValue="0" />
</fr.free.nrw.commons.ui.LongTitlePreferences.LongTitlePreferenceCategory>
</PreferenceCategory>
<fr.free.nrw.commons.ui.LongTitlePreferences.LongTitlePreferenceCategory
android:title="@string/preference_category_general">
<PreferenceCategory
android:title="@string/preference_category_general">
<fr.free.nrw.commons.ui.LongTitlePreferences.LongTitleSwitchPreference
android:key="useExternalStorage"
android:title="@string/use_external_storage"
android:defaultValue="true"
android:summary="@string/use_external_storage_summary" />
<SwitchPreference
android:defaultValue="true"
android:key="useExternalStorage"
app:singleLineTitle="false"
android:summary="@string/use_external_storage_summary"
android:title="@string/use_external_storage" />
<fr.free.nrw.commons.ui.LongTitlePreferences.LongTitleEditTextPreference
android:key="uploads"
android:defaultValue="100"
android:title= "@string/set_limit"
android:inputType="number"
android:maxLength="3" />
<EditTextPreference
android:defaultValue="100"
android:key="uploads"
app:useSimpleSummaryProvider="true"
app:singleLineTitle="false"
android:title="@string/set_limit" />
<fr.free.nrw.commons.ui.LongTitlePreferences.LongTitleListPreference
android:key="descriptionDefaultLanguagePref"
android:title= "@string/default_description_language"
android:summary="English" />
<ListPreference
android:key="descriptionDefaultLanguagePref"
app:useSimpleSummaryProvider="true"
app:singleLineTitle="false"
android:title="@string/default_description_language" />
<fr.free.nrw.commons.ui.LongTitlePreferences.LongTitleSwitchPreference
android:key="useAuthorName"
android:title="@string/preference_author_name_toggle"
android:summary="@string/preference_author_name_toggle_summary"/>
<SwitchPreference
android:key="useAuthorName"
app:singleLineTitle="false"
android:summary="@string/preference_author_name_toggle_summary"
android:title="@string/preference_author_name_toggle" />
<fr.free.nrw.commons.ui.LongTitlePreferences.LongTitleEditTextPreference
android:key="authorName"
android:title="@string/preference_author_name"
android:summary="@string/preference_author_name_summary"
/>
<EditTextPreference
android:key="authorName"
app:singleLineTitle="false"
app:dependency="useAuthorName"
android:summary="@string/preference_author_name_summary"
android:title="@string/preference_author_name" />
<fr.free.nrw.commons.ui.LongTitlePreferences.LongTitleSwitchPreference
android:key="displayNearbyCardView"
android:title="@string/display_nearby_notification"
android:defaultValue="true"
android:summary="@string/display_nearby_notification_summary" />
<SwitchPreference
android:defaultValue="true"
android:key="displayNearbyCardView"
app:singleLineTitle="false"
android:summary="@string/display_nearby_notification_summary"
android:title="@string/display_nearby_notification" />
<fr.free.nrw.commons.ui.LongTitlePreferences.LongTitleSwitchPreference
android:key="displayLocationPermissionForCardView"
android:title="@string/display_location_permission_title"
android:defaultValue="true"
android:summary="@string/display_location_permission_explanation" />
<fr.free.nrw.commons.ui.LongTitlePreferences.LongTitleSwitchPreference
android:key="displayCampaignsCardView"
android:title="@string/display_campaigns"
android:defaultValue="true"
android:summary="@string/display_campaigns_explanation" />
<SwitchPreference
android:defaultValue="true"
android:key="displayLocationPermissionForCardView"
app:singleLineTitle="false"
android:summary="@string/display_location_permission_explanation"
android:title="@string/display_location_permission_title" />
<SwitchPreference
android:defaultValue="true"
android:key="displayCampaignsCardView"
app:singleLineTitle="false"
android:summary="@string/display_campaigns_explanation"
android:title="@string/display_campaigns" />
</fr.free.nrw.commons.ui.LongTitlePreferences.LongTitlePreferenceCategory>
</PreferenceCategory>
<fr.free.nrw.commons.ui.LongTitlePreferences.LongTitlePreferenceCategory
android:title="@string/preference_category_privacy">
<PreferenceCategory
android:title="@string/preference_category_privacy">
<fr.free.nrw.commons.ui.LongTitlePreferences.LongTitleMultiSelectListPreference
android:entries="@array/pref_exifTag_entries"
android:entryValues="@array/pref_exifTag_values"
android:key="managed_exif_tags"
android:title="@string/manage_exif_tags"
android:summary="@string/manage_exif_tags_summary"/>
<MultiSelectListPreference
android:entries="@array/pref_exifTag_entries"
android:entryValues="@array/pref_exifTag_values"
android:key="managed_exif_tags"
app:singleLineTitle="false"
android:summary="@string/manage_exif_tags_summary"
android:title="@string/manage_exif_tags" />
</fr.free.nrw.commons.ui.LongTitlePreferences.LongTitlePreferenceCategory>
</PreferenceCategory>
<!-- The key 'allowGps' was used before and has since been removed based on the discussion at #1599.
Do not reuse this key unless you revive the same feature with the changes mentioned at #1599.-->
<fr.free.nrw.commons.ui.LongTitlePreferences.LongTitlePreferenceCategory
android:title="@string/preference_category_feedback">
<PreferenceCategory
android:title="@string/preference_category_feedback">
<fr.free.nrw.commons.ui.LongTitlePreferences.LongTitlePreference
android:key="becomeBetaTester"
android:title="@string/become_a_tester_title"
android:summary="@string/become_a_tester_description">
</fr.free.nrw.commons.ui.LongTitlePreferences.LongTitlePreference>
<Preference
android:key="becomeBetaTester"
android:summary="@string/become_a_tester_description"
app:singleLineTitle="false"
android:title="@string/become_a_tester_title"/>
<fr.free.nrw.commons.ui.LongTitlePreferences.LongTitlePreference
android:key="sendLogFile"
android:title="@string/send_log_file"
android:summary="@string/send_log_file_description"/>
<Preference
android:key="sendLogFile"
android:summary="@string/send_log_file_description"
app:singleLineTitle="false"
android:title="@string/send_log_file" />
</fr.free.nrw.commons.ui.LongTitlePreferences.LongTitlePreferenceCategory>
</PreferenceCategory>
</PreferenceScreen>

View file

@ -20,6 +20,8 @@ KOTLIN_VERSION=1.3.21
BUTTERKNIFE_VERSION=10.1.0
LEAK_CANARY_VERSION=1.6.2
DAGGER_VERSION=2.21
ROOM_VERSION=2.2.3
PREFERENCE_VERSION=1.1.0
systemProp.http.proxyPort=0
systemProp.http.proxyHost=