From ed7102c9850269533889268c4b2d653b9dd53417 Mon Sep 17 00:00:00 2001 From: ru26kif Date: Sun, 6 Sep 2020 15:23:27 +0200 Subject: [PATCH] add documentation --- build.gradle | 2 ++ colorpreference/build.gradle | 4 ++-- demo/build.gradle | 4 ++-- .../example/prefsplus/SettingsActivityX.java | 11 +++------- prefs-plusx/build.gradle | 4 ++-- .../prefsplusx/DefaultPreferenceFragment.java | 21 +++++++------------ .../prefsplusx/EditFloatPreference.java | 2 +- .../mapzen/prefsplusx/IntListPreference.java | 10 ++++++++- 8 files changed, 28 insertions(+), 30 deletions(-) diff --git a/build.gradle b/build.gradle index 9125792..2900326 100644 --- a/build.gradle +++ b/build.gradle @@ -24,6 +24,8 @@ allprojects { minSdkVersion = 21 compileSdkVersion = 29 targetSdkVersion = 29 + versionCode=1 + versionName="1.0" } } diff --git a/colorpreference/build.gradle b/colorpreference/build.gradle index bca7466..5e66237 100644 --- a/colorpreference/build.gradle +++ b/colorpreference/build.gradle @@ -9,8 +9,8 @@ android { defaultConfig { minSdkVersion rootProject.minSdkVersion targetSdkVersion rootProject.targetSdkVersion - versionCode 1 - versionName "1.0" + versionCode rootProject.versionCode + versionName rootProject.versionName } buildTypes { diff --git a/demo/build.gradle b/demo/build.gradle index aa6a06e..059ad26 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -8,8 +8,8 @@ android { applicationId "com.example.prefsplus" minSdkVersion rootProject.minSdkVersion targetSdkVersion rootProject.targetSdkVersion - versionCode 1 - versionName "1.0" + versionCode rootProject.versionCode + versionName rootProject.versionName testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' } diff --git a/demo/src/main/java/com/example/prefsplus/SettingsActivityX.java b/demo/src/main/java/com/example/prefsplus/SettingsActivityX.java index 7e98629..e08ed03 100644 --- a/demo/src/main/java/com/example/prefsplus/SettingsActivityX.java +++ b/demo/src/main/java/com/example/prefsplus/SettingsActivityX.java @@ -7,6 +7,8 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.preference.PreferenceFragmentCompat; +import com.mapzen.prefsplusx.DefaultPreferenceFragment; + /** * Settings Activity that uses the new preferences (PreferenceFragmentCompat) */ @@ -19,7 +21,7 @@ protected void onCreate(Bundle savedInstanceState) { actionBar.setDisplayHomeAsUpEnabled(true); } getSupportFragmentManager().beginTransaction().replace(android.R.id.content, - new SettingsFragment()).commit(); + new DefaultPreferenceFragment(R.xml.preferencesx)).commit(); } @Override @@ -32,11 +34,4 @@ public boolean onOptionsItemSelected(MenuItem item) { return super.onOptionsItemSelected(item); } - public static class SettingsFragment extends PreferenceFragmentCompat { - - @Override - public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - setPreferencesFromResource(R.xml.preferencesx, rootKey); - } - } } diff --git a/prefs-plusx/build.gradle b/prefs-plusx/build.gradle index b95fa6c..7e0d9d1 100644 --- a/prefs-plusx/build.gradle +++ b/prefs-plusx/build.gradle @@ -18,8 +18,8 @@ android { defaultConfig { minSdkVersion rootProject.minSdkVersion targetSdkVersion rootProject.targetSdkVersion - versionCode 1 - versionName "1.0" + versionCode rootProject.versionCode + versionName rootProject.versionName testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles "consumer-rules.pro" diff --git a/prefs-plusx/src/main/java/com/mapzen/prefsplusx/DefaultPreferenceFragment.java b/prefs-plusx/src/main/java/com/mapzen/prefsplusx/DefaultPreferenceFragment.java index 21526bd..5240736 100644 --- a/prefs-plusx/src/main/java/com/mapzen/prefsplusx/DefaultPreferenceFragment.java +++ b/prefs-plusx/src/main/java/com/mapzen/prefsplusx/DefaultPreferenceFragment.java @@ -1,29 +1,22 @@ package com.mapzen.prefsplusx; -import android.content.SharedPreferences; import android.os.Bundle; +import androidx.annotation.XmlRes; import androidx.preference.PreferenceFragmentCompat; -import androidx.preference.PreferenceManager; -import androidx.preference.PreferenceScreen; - -import static android.content.Context.MODE_PRIVATE; +// Takes the resource file in the constructor +// Does nothing special,you can use this one whenever you don't want to declare a new fragment just for an xml preference screen public class DefaultPreferenceFragment extends PreferenceFragmentCompat { - private final String sharedPreferencesName; - private final PreferenceScreen preferenceScreen; + private final @XmlRes int resourceFile; - DefaultPreferenceFragment(PreferenceScreen preferenceScreen, String sharedPreferencesName) { - this.preferenceScreen = preferenceScreen; - this.sharedPreferencesName=sharedPreferencesName; + public DefaultPreferenceFragment(@XmlRes int resourceFile) { + this.resourceFile=resourceFile; } @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - PreferenceManager prefMgr = getPreferenceManager(); - prefMgr.setSharedPreferencesName(sharedPreferencesName); - prefMgr.setSharedPreferencesMode(MODE_PRIVATE); - setPreferenceScreen(preferenceScreen); + setPreferencesFromResource(resourceFile, rootKey); } } diff --git a/prefs-plusx/src/main/java/com/mapzen/prefsplusx/EditFloatPreference.java b/prefs-plusx/src/main/java/com/mapzen/prefsplusx/EditFloatPreference.java index 0992708..20c285d 100644 --- a/prefs-plusx/src/main/java/com/mapzen/prefsplusx/EditFloatPreference.java +++ b/prefs-plusx/src/main/java/com/mapzen/prefsplusx/EditFloatPreference.java @@ -42,7 +42,7 @@ private void init(AttributeSet attrs, int defStyle){ attrs, R.styleable.EditIntPreference, defStyle, defStyle); minimumValue=a.getFloat(R.styleable.EditFloatPreference_minFloatValue,Float.MIN_VALUE); maximumValue=a.getFloat(R.styleable.EditFloatPreference_maxFloatValue,Float.MAX_VALUE); - Log.d(TAG,"Min & max"+minimumValue+" "+maximumValue); + //Log.d(TAG,"Min & max"+minimumValue+" "+maximumValue); a.recycle(); } super.setOnBindEditTextListener(new OnBindEditTextListener() { diff --git a/prefs-plusx/src/main/java/com/mapzen/prefsplusx/IntListPreference.java b/prefs-plusx/src/main/java/com/mapzen/prefsplusx/IntListPreference.java index bbf65dd..5824b14 100644 --- a/prefs-plusx/src/main/java/com/mapzen/prefsplusx/IntListPreference.java +++ b/prefs-plusx/src/main/java/com/mapzen/prefsplusx/IntListPreference.java @@ -5,6 +5,7 @@ import android.util.AttributeSet; import android.util.Log; +import androidx.core.content.res.TypedArrayUtils; import androidx.preference.ListPreference; import java.util.Arrays; @@ -29,10 +30,17 @@ public IntListPreference(Context context, AttributeSet attrs) { // If there are no entry values set populate it with a default array that holds // as many entries as the entry array and maps element 0 -> 0, element 1 -> 1 and more private void setDefaultEntryValuesIfNull(){ + /*if(getEntryValues()!=null){ + Log.d(TAG,"Has entry values "+ Arrays.toString(getEntryValues())); + }else{ + Log.d(TAG,"Has no entry values"); + }*/ if(getEntryValues()==null){ //Log.d(TAG,"Setting default entry values"); if(getEntries()==null){ - Log.w(TAG,"No entries - will crash"); + // You probably forgot to set the entry values + // example,in your xml,do : android:entries="@array/int_list_entries" + throw new RuntimeException("List preference has no entries"); } final int size=getEntries().length; CharSequence[] defaultEntryValues =new CharSequence[size];