Skip to content

Commit

Permalink
Change Material Preference library
Browse files Browse the repository at this point in the history
RingtonePreference was crashing the notification preferences screen on some devices, due to lack of external storage permissions.
Turns out some devices need this to show the title of ringtones.

Fixed by changing to a different material preference library that gives us the opportunity to work around this.
  • Loading branch information
gbl08ma committed Nov 3, 2017
1 parent 92c2804 commit 034d154
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ dependencies {
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.8.1'
compile 'org.msgpack:jackson-dataformat-msgpack:0.8.13'
compile 'com.evernote:android-job:1.1.9'
compile 'com.github.rikkaw:MaterialPreference:v1.4.4'
compile 'info.debatty:java-string-similarity:0.23'
compile 'org.sufficientlysecure:html-textview:3.4'
compile 'com.github.apl-devs:appintro:v4.2.2'
Expand All @@ -59,6 +58,12 @@ dependencies {
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.google.firebase:firebase-messaging:10.2.1'

compile 'net.xpece.android:support-preference:1.3.1'
ext.supportLibsVersion = "25.3.1"
compile "com.android.support:appcompat-v7:$supportLibsVersion"
compile "com.android.support:preference-v7:$supportLibsVersion"

testCompile 'junit:junit:4.12'
debugCompile 'com.facebook.stetho:stetho:1.5.0'
debugCompile 'com.uphyca:stetho_realm:2.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ public void onClick(View view) {
data.add(ajob);

Map<String, String> mpref = new HashMap<>(3);
mpref.put("name", "MaterialPreference");
mpref.put("license", RikkaW - Apache License 2.0");
mpref.put("url", "https://github.com/RikkaW/MaterialPreference");
mpref.put("name", "Material Preference");
mpref.put("license", consp1racy - Apache License 2.0");
mpref.put("url", "https://github.com/consp1racy/android-support-preference");
data.add(mpref);

Map<String, String> jss = new HashMap<>(3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.preference.XpPreferenceFragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import rikka.materialpreference.PreferenceFragment;

public class GeneralPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
public class GeneralPreferenceFragment extends XpPreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
private OnFragmentInteractionListener mListener;

public GeneralPreferenceFragment() {
Expand Down Expand Up @@ -44,19 +45,23 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
return view;
}

@Override
public void onCreatePreferences(Bundle bundle, String s) {
getPreferenceManager().setDefaultPackages(new String[]{"im.tny.segvault.disturbances."});

public void onCreatePreferences2(final Bundle savedInstanceState, final String rootKey) {
getPreferenceManager().setSharedPreferencesName("settings");
getPreferenceManager().setSharedPreferencesMode(Context.MODE_PRIVATE);

setPreferencesFromResource(R.xml.settings, null);
}

@Override
public DividerDecoration onCreateItemDecoration() {
return new CategoryDivideDividerDecoration();
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
// this solves the "preference screen background is a darker gray" problem:
// https://github.com/consp1racy/android-support-preference/issues/22
super.onViewCreated(view, savedInstanceState);

final RecyclerView listView = getListView();

// We don't want this. The children are still focusable.
listView.setFocusable(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,30 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.XpPreferenceFragment;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.xpece.android.support.preference.ListPreference;
import net.xpece.android.support.preference.MultiSelectListPreference;
import net.xpece.android.support.preference.RingtonePreference;
import net.xpece.android.support.preference.SeekBarPreference;
import net.xpece.android.support.preference.SharedPreferencesCompat;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -24,14 +39,8 @@

import im.tny.segvault.subway.Line;
import im.tny.segvault.subway.Network;
import rikka.materialpreference.CheckBoxPreference;
import rikka.materialpreference.MultiSelectListPreference;
import rikka.materialpreference.Preference;
import rikka.materialpreference.PreferenceFragment;
import rikka.materialpreference.PreferenceManager;
import rikka.materialpreference.PreferenceScreen;

public class NotifPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

public class NotifPreferenceFragment extends XpPreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
private OnFragmentInteractionListener mListener;

public NotifPreferenceFragment() {
Expand Down Expand Up @@ -66,15 +75,28 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
}

@Override
public void onCreatePreferences(Bundle bundle, String s) {
getPreferenceManager().setDefaultPackages(new String[]{"im.tny.segvault.disturbances."});

public void onCreatePreferences2(final Bundle savedInstanceState, final String rootKey) {
getPreferenceManager().setSharedPreferencesName("notifsettings");
getPreferenceManager().setSharedPreferencesMode(Context.MODE_PRIVATE);

setPreferencesFromResource(R.xml.notif_settings, null);

updatePreferences();
bindPreferenceSummaryToValue(findPreference("pref_notifs_ringtone"));
bindPreferenceSummaryToValue(findPreference("pref_notifs_regularization_ringtone"));
bindPreferenceSummaryToValue(findPreference("pref_notifs_announcement_ringtone"));
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
// this solves the "preference screen background is a darker gray" problem:
// https://github.com/consp1racy/android-support-preference/issues/22
super.onViewCreated(view, savedInstanceState);

final RecyclerView listView = getListView();

// We don't want this. The children are still focusable.
listView.setFocusable(false);
}

private void updatePreferences() {
Expand Down Expand Up @@ -196,12 +218,6 @@ private List<CharSequence> getSelectedEntries(List<String> values, MultiSelectLi
return labels;
}


@Override
public DividerDecoration onCreateItemDecoration() {
return new CategoryDivideDividerDecoration();
}

@Override
public void onPause() {
super.onPause();
Expand Down Expand Up @@ -264,4 +280,86 @@ public void onReceive(Context context, Intent intent) {
}
}
};

private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();

if (preference instanceof SeekBarPreference) {
SeekBarPreference pref = (SeekBarPreference) preference;
int progress = (int) value;
pref.setInfo(progress + "%");
} else if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);

// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else if (preference instanceof MultiSelectListPreference) {
String summary = stringValue.trim().substring(1, stringValue.length() - 1); // strip []
preference.setSummary(summary);
} else if (preference instanceof RingtonePreference) {
// For ringtone preferences, look up the correct display value using RingtoneManager.
if (TextUtils.isEmpty(stringValue)) {
// Empty values correspond to 'silent' (no ringtone).
preference.setSummary(R.string.frag_notif_summary_silent);
} else {
final Uri selectedUri = Uri.parse(stringValue);
try {
final Ringtone ringtone = RingtoneManager.getRingtone(
preference.getContext(), selectedUri);
if (ringtone == null) {
// Clear the summary if there was a lookup error, i.e. does not exist.
preference.setSummary(null);
} else {
// Set the summary to reflect the new ringtone display name.
final String name = ringtone.getTitle(preference.getContext());
preference.setSummary(name);
}
} catch (SecurityException ex) {
// The user has selected a ringtone from external storage
// and then revoked READ_EXTERNAL_STORAGE permission.
// We have no way of guessing the ringtone title.
// We'd have to store the title of selected ringtone in prefs as well.
preference.setSummary("???");
}
}

} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
};

private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);

// Trigger the listener immediately with the preference's
// current value.
final String key = preference.getKey();
if (preference instanceof MultiSelectListPreference) {
Set<String> summary = SharedPreferencesCompat.getStringSet(
PreferenceManager.getDefaultSharedPreferences(preference.getContext()),
key, new HashSet<String>());
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, summary);
} else if (preference instanceof SeekBarPreference) {
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, ((SeekBarPreference) preference).getValue());
} else {
String value = PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(key, "");
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, value);
}
}

}
1 change: 1 addition & 0 deletions app/src/main/res/values-pt/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,5 @@
<string name="frag_settings_other_title">Outros</string>
<string name="frag_settings_developer_mode">Modo de programador</string>
<string name="frag_settings_developer_mode_summary">Ligar funcionalidades de depuração</string>
<string name="frag_notif_summary_silent">Silencioso</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<string name="frag_notif_regularization_ringtone">Regularization ringtone</string>
<string name="frag_notif_ringtone_none">None</string>
<string name="frag_notif_vibrate">Vibrate</string>
<string name="frag_notif_summary_silent">Silent</string>
<string name="frag_notif_regularization_vibrate">Vibrate on regularization</string>
<string name="frag_route_title">Plan route</string>
<string name="frag_route_origin_station">Starting station</string>
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>

<!-- Used to theme preference list and items. -->
<item name="preferenceTheme">@style/PreferenceThemeOverlay.Material</item>
<!-- Default icon tint for preferences. -->
<item name="asp_preferenceIconTint">?colorAccent</item>
<item name="asp_preferenceDialogIconTint">?asp_preferenceIconTint</item>

<item name="viewMapButtonStyle">@style/AppTheme.ViewMapButton</item>
<item name="android:navigationBarColor" tools:targetApi="21">@color/colorPrimary</item>
</style>
Expand Down
25 changes: 12 additions & 13 deletions app/src/main/res/xml/notif_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
android:defaultValue="content://settings/system/notification_sound"
android:key="pref_notifs_ringtone"
android:ringtoneType="notification"
android:summary="%s"
android:title="@string/frag_notif_ringtone"
app:summaryNone="@string/frag_notif_ringtone_none">
android:showDefault="true"
android:showSilent="true"
android:title="@string/frag_notif_ringtone">

</RingtonePreference>

Expand All @@ -26,18 +26,17 @@
<CheckBoxPreference
android:defaultValue="true"
android:key="pref_notifs_service_resumed"
android:title="@string/frag_notif_show_regularization"
android:summary="@string/frag_notif_show_regularization_summary"/>
android:summary="@string/frag_notif_show_regularization_summary"
android:title="@string/frag_notif_show_regularization" />

<RingtonePreference
android:defaultValue="content://settings/system/notification_sound"
android:key="pref_notifs_regularization_ringtone"
android:dependency="pref_notifs_service_resumed"
android:key="pref_notifs_regularization_ringtone"
android:ringtoneType="notification"
android:summary="%s"
android:title="@string/frag_notif_regularization_ringtone"
app:summaryNone="@string/frag_notif_ringtone_none">
</RingtonePreference>
android:showDefault="true"
android:showSilent="true"
android:title="@string/frag_notif_regularization_ringtone"></RingtonePreference>

<CheckBoxPreference
android:defaultValue="false"
Expand All @@ -57,9 +56,9 @@
android:defaultValue="content://settings/system/notification_sound"
android:key="pref_notifs_announcement_ringtone"
android:ringtoneType="notification"
android:summary="%s"
android:title="@string/frag_notif_ringtone"
app:summaryNone="@string/frag_notif_ringtone_none">
android:showDefault="true"
android:showSilent="true"
android:title="@string/frag_notif_ringtone">

</RingtonePreference>

Expand Down

0 comments on commit 034d154

Please sign in to comment.