2014-07-03 07:48:39 +02:00
|
|
|
/*
|
|
|
|
* Twidere - Twitter client for Android
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.mariotaku.twidere.fragment;
|
|
|
|
|
2016-04-05 15:34:01 +02:00
|
|
|
import android.app.Activity;
|
2016-03-13 17:25:13 +01:00
|
|
|
import android.content.Context;
|
2016-04-05 15:34:01 +02:00
|
|
|
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;
|
2016-03-13 16:30:49 +01:00
|
|
|
import android.support.v7.preference.PreferenceFragmentCompat;
|
2014-07-03 07:48:39 +02:00
|
|
|
|
|
|
|
import org.mariotaku.twidere.Constants;
|
2016-04-05 15:34:01 +02:00
|
|
|
import org.mariotaku.twidere.preference.RingtonePreference;
|
2015-10-09 09:58:36 +02:00
|
|
|
import org.mariotaku.twidere.util.KeyboardShortcutsHandler;
|
|
|
|
import org.mariotaku.twidere.util.UserColorNameManager;
|
2015-12-31 09:32:27 +01:00
|
|
|
import org.mariotaku.twidere.util.dagger.GeneralComponentHelper;
|
2015-10-09 09:58:36 +02:00
|
|
|
|
|
|
|
import javax.inject.Inject;
|
2014-07-03 07:48:39 +02:00
|
|
|
|
2016-03-13 16:30:49 +01:00
|
|
|
public abstract class BasePreferenceFragment extends PreferenceFragmentCompat implements Constants {
|
2014-07-03 07:48:39 +02:00
|
|
|
|
2016-04-05 15:34:01 +02:00
|
|
|
private static final int REQUEST_PICK_RINGTONE = 301;
|
|
|
|
private static final String EXTRA_RINGTONE_PREFERENCE_KEY = "internal:ringtone_preference_key";
|
|
|
|
private String mRingtonePreferenceKey;
|
|
|
|
|
2015-10-09 09:58:36 +02:00
|
|
|
@Inject
|
|
|
|
protected KeyboardShortcutsHandler mKeyboardShortcutHandler;
|
|
|
|
@Inject
|
|
|
|
protected UserColorNameManager mUserColorNameManager;
|
|
|
|
|
|
|
|
@Override
|
2016-03-13 17:25:13 +01:00
|
|
|
public void onAttach(Context context) {
|
|
|
|
super.onAttach(context);
|
|
|
|
GeneralComponentHelper.build(context).inject(this);
|
2015-10-09 09:58:36 +02:00
|
|
|
}
|
|
|
|
|
2016-04-05 15:34:01 +02:00
|
|
|
@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);
|
|
|
|
}
|
2014-07-28 12:42:48 +02:00
|
|
|
|
2014-07-03 07:48:39 +02:00
|
|
|
}
|