fixed #488, really

This commit is contained in:
Mariotaku Lee 2016-04-05 21:34:01 +08:00
parent 62a1e2653f
commit a2ddb653d3
4 changed files with 89 additions and 187 deletions

View File

@ -8,7 +8,7 @@ buildscript {
} }
dependencies { dependencies {
classpath 'com.github.ben-manes:gradle-versions-plugin:0.12.0' classpath 'com.github.ben-manes:gradle-versions-plugin:0.12.0'
classpath 'com.android.tools.build:gradle:2.0.0-rc1' classpath 'com.android.tools.build:gradle:2.0.0-rc2'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath('fr.avianey.androidsvgdrawable:gradle-plugin:3.0.0') { classpath('fr.avianey.androidsvgdrawable:gradle-plugin:3.0.0') {
// should be excluded to avoid conflict // should be excluded to avoid conflict

View File

@ -19,10 +19,18 @@
package org.mariotaku.twidere.fragment; package org.mariotaku.twidere.fragment;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat; import android.support.v7.preference.PreferenceFragmentCompat;
import org.mariotaku.twidere.Constants; import org.mariotaku.twidere.Constants;
import org.mariotaku.twidere.preference.RingtonePreference;
import org.mariotaku.twidere.util.KeyboardShortcutsHandler; import org.mariotaku.twidere.util.KeyboardShortcutsHandler;
import org.mariotaku.twidere.util.UserColorNameManager; import org.mariotaku.twidere.util.UserColorNameManager;
import org.mariotaku.twidere.util.dagger.GeneralComponentHelper; import org.mariotaku.twidere.util.dagger.GeneralComponentHelper;
@ -31,6 +39,10 @@ import javax.inject.Inject;
public abstract class BasePreferenceFragment extends PreferenceFragmentCompat implements Constants { public abstract class BasePreferenceFragment extends PreferenceFragmentCompat implements Constants {
private static final int REQUEST_PICK_RINGTONE = 301;
private static final String EXTRA_RINGTONE_PREFERENCE_KEY = "internal:ringtone_preference_key";
private String mRingtonePreferenceKey;
@Inject @Inject
protected KeyboardShortcutsHandler mKeyboardShortcutHandler; protected KeyboardShortcutsHandler mKeyboardShortcutHandler;
@Inject @Inject
@ -42,5 +54,68 @@ public abstract class BasePreferenceFragment extends PreferenceFragmentCompat im
GeneralComponentHelper.build(context).inject(this); GeneralComponentHelper.build(context).inject(this);
} }
@Override
public void onActivityCreated(final Bundle savedInstanceState) {
if (savedInstanceState != null) {
mRingtonePreferenceKey = savedInstanceState.getString(EXTRA_RINGTONE_PREFERENCE_KEY);
}
super.onActivityCreated(savedInstanceState);
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString(EXTRA_RINGTONE_PREFERENCE_KEY, mRingtonePreferenceKey);
super.onSaveInstanceState(outState);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_PICK_RINGTONE: {
if (resultCode == Activity.RESULT_OK && data != null) {
Uri ringtone = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (mRingtonePreferenceKey != null) {
RingtonePreference ringtonePreference = (RingtonePreference)
findPreference(mRingtonePreferenceKey);
if (ringtonePreference != null) {
ringtonePreference.setValue(ringtone != null ? ringtone.toString() : null);
}
}
mRingtonePreferenceKey = null;
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onPreferenceTreeClick(Preference preference) {
if (preference instanceof RingtonePreference) {
RingtonePreference ringtonePreference = (RingtonePreference) preference;
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, ringtonePreference.getRingtoneType());
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, ringtonePreference.isShowDefault());
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, ringtonePreference.isShowSilent());
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, Settings.System.DEFAULT_NOTIFICATION_URI);
String existingValue = ringtonePreference.getValue(); // TODO
if (existingValue != null) {
if (existingValue.length() == 0) {
// Select "Silent"
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
} else {
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(existingValue));
}
} else {
// No ringtone has been selected, set to the default
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Settings.System.DEFAULT_NOTIFICATION_URI);
}
startActivityForResult(intent, REQUEST_PICK_RINGTONE);
mRingtonePreferenceKey = ringtonePreference.getKey();
return true;
}
return super.onPreferenceTreeClick(preference);
}
} }

View File

@ -34,6 +34,7 @@ import org.mariotaku.twidere.util.Utils;
public class SettingsDetailsFragment extends BasePreferenceFragment implements Constants, public class SettingsDetailsFragment extends BasePreferenceFragment implements Constants,
OnSharedPreferenceChangeListener { OnSharedPreferenceChangeListener {
@Override @Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
final PreferenceManager preferenceManager = getPreferenceManager(); final PreferenceManager preferenceManager = getPreferenceManager();
@ -62,25 +63,6 @@ public class SettingsDetailsFragment extends BasePreferenceFragment implements C
addPreferencesFromResource(resId); addPreferencesFromResource(resId);
} }
// final Context context = preferenceScreen.getContext();
// if (args.containsKey(EXTRA_SETTINGS_INTENT_ACTION)) {
// final Intent hiddenEntryIntent = new Intent(args.getString(EXTRA_SETTINGS_INTENT_ACTION));
// final PackageManager pm = context.getPackageManager();
// for (ResolveInfo info : pm.queryIntentActivities(hiddenEntryIntent, PackageManager.MATCH_DEFAULT_ONLY)) {
// final Preference preference = new Preference(context);
// final Intent intent = new Intent(hiddenEntryIntent);
// intent.setPackage(info.resolvePackageName);
// intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
// preference.setIntent(intent);
// preference.setTitle(info.loadLabel(pm));
// preferenceScreen.addPreference(preference);
// }
// }
}
@Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
} }
@Override @Override
@ -113,4 +95,5 @@ public class SettingsDetailsFragment extends BasePreferenceFragment implements C
} }
} }
} }

View File

@ -16,42 +16,15 @@
package org.mariotaku.twidere.preference; package org.mariotaku.twidere.preference;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.media.RingtoneManager; import android.media.RingtoneManager;
import android.net.Uri; import android.support.v7.preference.Preference;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.support.annotation.NonNull;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.preference.DialogPreference;
import android.support.v7.preference.PreferenceDialogFragmentCompat;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View;
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import org.mariotaku.sqliteqb.library.Expression;
import org.mariotaku.twidere.R; import org.mariotaku.twidere.R;
import org.mariotaku.twidere.preference.iface.IDialogPreference;
import java.util.ArrayList; public class RingtonePreference extends Preference {
import java.util.Arrays;
import java.util.List;
import static android.text.TextUtils.isEmpty;
public class RingtonePreference extends DialogPreference implements IDialogPreference {
private final int mRingtoneType; private final int mRingtoneType;
private final boolean mShowDefault; private final boolean mShowDefault;
@ -79,146 +52,17 @@ public class RingtonePreference extends DialogPreference implements IDialogPrefe
return mShowSilent; return mShowSilent;
} }
@Override public String getValue() {
public void displayDialog(PreferenceFragmentCompat fragment) { return getPersistedString(null);
RingtonePreferenceDialogFragment df = RingtonePreferenceDialogFragment.newInstance(getKey());
df.setTargetFragment(fragment, 0);
df.show(fragment.getFragmentManager(), getKey());
} }
public static class RingtonePreferenceDialogFragment extends PreferenceDialogFragmentCompat public int getRequestCode() {
implements LoaderManager.LoaderCallbacks<Cursor> { return getKey().hashCode() & 0x0000FFFF;
private MediaPlayer mMediaPlayer; }
private SimpleCursorAdapter mAdapter;
private Uri mCurrentUri;
public static RingtonePreferenceDialogFragment newInstance(String key) { public void setValue(String value) {
final RingtonePreferenceDialogFragment df = new RingtonePreferenceDialogFragment(); persistString(value);
final Bundle args = new Bundle(); callChangeListener(value);
args.putString(ARG_KEY, key); notifyChanged();
df.setArguments(args);
return df;
}
@Override
public void onDialogClosed(boolean positive) {
if (positive && mCurrentUri != null) {
final RingtonePreference preference = (RingtonePreference) getPreference();
if (preference.isPersistent()) {
preference.persistString(mCurrentUri.toString());
}
}
if (mMediaPlayer != null) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
}
mMediaPlayer.release();
mMediaPlayer = null;
}
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Context context = getContext();
final TypedArray a = context.obtainStyledAttributes(null, android.support.v7.appcompat.R.styleable.AlertDialog,
android.support.v7.appcompat.R.attr.alertDialogStyle, 0);
@SuppressLint("PrivateResource")
final int layout = a.getResourceId(android.support.v7.appcompat.R.styleable.AlertDialog_singleChoiceItemLayout, 0);
a.recycle();
mAdapter = new SimpleCursorAdapter(context, layout, null, new String[]{Audio.Media.TITLE}, new int[]{android.R.id.text1}, 0);
final MaterialDialog.Builder builder = new MaterialDialog.Builder(context);
builder.title(getPreference().getDialogTitle());
builder.positiveText(android.R.string.ok);
builder.negativeText(android.R.string.cancel);
builder.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog materialDialog, @NonNull DialogAction dialogAction) {
RingtonePreferenceDialogFragment.this.onClick(materialDialog, DialogInterface.BUTTON_POSITIVE);
}
});
builder.onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog materialDialog, @NonNull DialogAction dialogAction) {
RingtonePreferenceDialogFragment.this.onClick(materialDialog, DialogInterface.BUTTON_NEGATIVE);
}
});
builder.cancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
RingtonePreferenceDialogFragment.this.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
}
});
int checkedItem = -1;
builder.adapter(mAdapter, new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
if (mMediaPlayer != null) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
}
mMediaPlayer.release();
}
final Cursor cursor = mAdapter.getCursor();
if (!cursor.moveToPosition(which)) return;
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setLooping(false);
final String ringtone = cursor.getString(cursor.getColumnIndex(Audio.Media.DATA));
final Uri defUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
final Uri uri = isEmpty(ringtone) ? defUri : Uri.parse(ringtone);
mCurrentUri = uri;
try {
mMediaPlayer.setDataSource(getContext(), uri);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (final Exception e) {
// Ignore
}
}
});
getLoaderManager().initLoader(0, null, this);
return builder.build();
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
final String[] cols = new String[]{Audio.Media._ID, Audio.Media.DATA, Audio.Media.TITLE};
RingtonePreference preference = (RingtonePreference) getPreference();
int ringtoneType = preference.getRingtoneType();
List<Expression> expressions = new ArrayList<>();
if ((ringtoneType & RingtoneManager.TYPE_NOTIFICATION) != 0) {
expressions.add(Expression.equalsArgs(Audio.Media.IS_NOTIFICATION));
}
if ((ringtoneType & RingtoneManager.TYPE_RINGTONE) != 0) {
expressions.add(Expression.equalsArgs(Audio.Media.IS_RINGTONE));
}
if ((ringtoneType & RingtoneManager.TYPE_ALARM) != 0) {
expressions.add(Expression.equalsArgs(Audio.Media.IS_ALARM));
}
final String selection;
final String[] selectionArgs;
if (expressions.isEmpty()) {
selection = null;
selectionArgs = null;
} else {
final int size = expressions.size();
selection = Expression.or(expressions.toArray(new Expression[size])).getSQL();
selectionArgs = new String[size];
Arrays.fill(selectionArgs, "1");
}
return new CursorLoader(getContext(), Audio.Media.INTERNAL_CONTENT_URI, cols, selection,
selectionArgs, Audio.Media.DEFAULT_SORT_ORDER);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.changeCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.changeCursor(null);
}
} }
} }