/**
* technoXist
*
* Copyright (c) 2014-2015 Suyash Bhatt
*
* 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 com.technoxist.fragment;
import android.app.Activity;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.text.TextUtils;
import com.technoxist.MainApplication;
import com.technoxist.R;
import com.technoxist.service.RefreshService;
import com.technoxist.utils.PrefUtils;
public class GeneralPrefsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.general_preferences);
setRingtoneSummary();
Preference preference = findPreference(PrefUtils.REFRESH_ENABLED);
preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Activity activity = getActivity();
if (activity != null) {
if (Boolean.TRUE.equals(newValue)) {
activity.startService(new Intent(activity, RefreshService.class));
} else {
PrefUtils.putLong(PrefUtils.LAST_SCHEDULED_REFRESH, 0);
activity.stopService(new Intent(activity, RefreshService.class));
}
}
return true;
}
});
}
@Override
public void onResume() {
// The ringtone summary text should be updated using
// OnSharedPreferenceChangeListener(), but I can't get it to work.
// Updating in onResume is a very simple hack that seems to work, but is inefficient.
setRingtoneSummary();
super.onResume();
}
private boolean setRingtoneSummary() {
Preference ringtone_preference = findPreference(PrefUtils.NOTIFICATIONS_RINGTONE);
Uri ringtoneUri = Uri.parse(PrefUtils.getString(PrefUtils.NOTIFICATIONS_RINGTONE, ""));
if (ringtoneUri == null || TextUtils.isEmpty(ringtoneUri.toString())) {
ringtone_preference.setSummary(R.string.settings_notifications_ringtone_none);
} else {
Ringtone ringtone = RingtoneManager.getRingtone(MainApplication.getContext(), ringtoneUri);
if (ringtone == null) {
ringtone_preference.setSummary(R.string.settings_notifications_ringtone_none);
} else {
ringtone_preference.setSummary(ringtone.getTitle(MainApplication.getContext()));
}
}
return true;
}
}