Skip to content

Commit

Permalink
Fix RingtoneManager cursor crash on some devices
Browse files Browse the repository at this point in the history
Fixes signalapp#7055
// FREEBIE
  • Loading branch information
moxie0 committed Oct 9, 2017
1 parent 445f3c2 commit 655be5a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ protected Void doInBackground(Void... params) {

MessageNotifier.updateNotification(context, masterSecret);
MarkReadReceiver.process(context, messageIds);

return null;
}
}.execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.thoughtcrime.securesms.util.DynamicNoActionBarTheme;
import org.thoughtcrime.securesms.util.DynamicTheme;
import org.thoughtcrime.securesms.util.IdentityUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.util.ViewUtil;
import org.thoughtcrime.securesms.util.concurrent.ListenableFuture;
Expand Down Expand Up @@ -335,7 +336,7 @@ private void setSummaries(Recipient recipient) {

if (toneUri == null) {
ringtonePreference.setSummary(R.string.preferences__default);
ringtonePreference.setCurrentRingtone(Settings.System.DEFAULT_NOTIFICATION_URI);
ringtonePreference.setCurrentRingtone(Uri.parse(TextSecurePreferences.getNotificationRingtone(getContext())));
} else if (toneUri.toString().isEmpty()) {
ringtonePreference.setSummary(R.string.preferences__silent);
ringtonePreference.setCurrentRingtone(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter;
import android.widget.HeaderViewListAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

Expand All @@ -47,6 +49,8 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.security.SecureRandom;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

import static android.app.Activity.RESULT_OK;
Expand All @@ -58,7 +62,7 @@ public class RingtonePreferenceDialogFragmentCompat extends PreferenceDialogFrag
private static final String CURSOR_NONE_ID = "-1";

private int selectedIndex = -1;
private Cursor cursor;
private String[] data;

private RingtoneManager ringtoneManager;
private Ringtone defaultRingtone;
Expand Down Expand Up @@ -87,7 +91,6 @@ public void onPause() {
stopPlaying();
}


private void stopPlaying() {
if (defaultRingtone != null && defaultRingtone.isPlaying()) {
defaultRingtone.stop();
Expand All @@ -104,9 +107,7 @@ protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {

RingtonePreference ringtonePreference = getRingtonePreference();

createCursor(ringtonePreference.getRingtone());

String colTitle = cursor.getColumnName(RingtoneManager.TITLE_COLUMN_INDEX);
createRingtoneList(ringtonePreference.getRingtone());

final Context context = getContext();

Expand All @@ -122,10 +123,10 @@ protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
}

builder
.setSingleChoiceItems(cursor, selectedIndex, colTitle, new DialogInterface.OnClickListener() {
.setSingleChoiceItems(data, selectedIndex, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (i < cursor.getCount()) {
if (i < data.length) {
selectedIndex = i;

int realIdx = i - (showDefault ? 1 : 0) - (showSilent ? 1 : 0);
Expand Down Expand Up @@ -208,23 +209,24 @@ public void onDialogClosed(boolean positiveResult) {
}

@NonNull
private Cursor createCursor(Uri ringtoneUri) {
private String[] createRingtoneList(Uri ringtoneUri) {
RingtonePreference ringtonePreference = getRingtonePreference();
ringtoneManager = new RingtoneManager(getContext());
List<String> results = new LinkedList<>();

ringtoneManager = new RingtoneManager(getContext());
ringtoneManager.setType(ringtonePreference.getRingtoneType());
ringtoneManager.setStopPreviousRingtone(true);

Cursor ringtoneCursor = ringtoneManager.getCursor();

String colId = ringtoneCursor.getColumnName(RingtoneManager.ID_COLUMN_INDEX);
String colId = ringtoneCursor.getColumnName(RingtoneManager.ID_COLUMN_INDEX);
String colTitle = ringtoneCursor.getColumnName(RingtoneManager.TITLE_COLUMN_INDEX);

MatrixCursor extras = new MatrixCursor(new String[]{colId, colTitle});

final int ringtoneType = ringtonePreference.getRingtoneType();
final boolean showDefault = ringtonePreference.getShowDefault();
final boolean showSilent = ringtonePreference.getShowSilent();
final int ringtoneType = ringtonePreference.getRingtoneType();
final boolean showDefault = ringtonePreference.getShowDefault();
final boolean showSilent = ringtonePreference.getShowSilent();

if (showDefault) {
switch (ringtoneType) {
Expand Down Expand Up @@ -262,7 +264,13 @@ private Cursor createCursor(Uri ringtoneUri) {
}

Cursor[] cursors = {extras, ringtoneCursor};
return this.cursor = new MergeCursor(cursors);
Cursor cursor = new MergeCursor(cursors);

while (cursor != null && cursor.moveToNext()) {
results.add(cursor.getString(1));
}

return data = results.toArray(new String[0]);
}

@Override
Expand All @@ -276,25 +284,27 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
final int ringtoneType = ringtonePreference.getRingtoneType();

// FIXME static field leak
@SuppressLint("StaticFieldLeak") final AsyncTask<Uri, Void, Cursor> installTask = new AsyncTask<Uri, Void, Cursor>() {
@SuppressLint("StaticFieldLeak") final AsyncTask<Uri, Void, String[]> installTask = new AsyncTask<Uri, Void, String[]>() {
@Override
protected Cursor doInBackground(Uri... params) {
protected String[] doInBackground(Uri... params) {
try {
Uri newUri = addCustomExternalRingtone(context, params[0], ringtoneType);

return createCursor(newUri);
return createRingtoneList(newUri);
} catch (IOException | IllegalArgumentException e) {
Log.e(TAG, "Unable to add new ringtone: ", e);
}
return null;
}

@Override
protected void onPostExecute(final Cursor newCursor) {
if (newCursor != null) {
protected void onPostExecute(final String[] newData) {
if (newData != null) {
final ListView listView = ((AlertDialog) getDialog()).getListView();
final CursorAdapter adapter = ((CursorAdapter) ((HeaderViewListAdapter) listView.getAdapter()).getWrappedAdapter());
adapter.changeCursor(newCursor);
ArrayAdapter<String> adapter = (ArrayAdapter<String>)((HeaderViewListAdapter)listView.getAdapter()).getWrappedAdapter();

adapter.clear();
adapter.addAll(newData);

listView.setItemChecked(selectedIndex, true);
listView.setSelection(selectedIndex);
Expand Down

0 comments on commit 655be5a

Please sign in to comment.