Imported SettingsActivity and related classes
This commit is contained in:
@@ -2,6 +2,7 @@ package com.philliphsu.clock2;
|
||||
|
||||
import android.media.AudioManager;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.CallSuper;
|
||||
import android.support.annotation.LayoutRes;
|
||||
import android.support.annotation.MenuRes;
|
||||
@@ -35,7 +36,7 @@ public abstract class BaseActivity extends AppCompatActivity {
|
||||
// When false, the system sets the default values only if this method has
|
||||
// never been called in the past (or the KEY_HAS_SET_DEFAULT_VALUES in the
|
||||
// default value shared preferences file is false).
|
||||
//TODO PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
|
||||
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
|
||||
// Direct volume changes to the alarm stream
|
||||
setVolumeControlStream(AudioManager.STREAM_ALARM);
|
||||
ButterKnife.bind(this);
|
||||
|
||||
@@ -18,6 +18,7 @@ import android.widget.TextView;
|
||||
|
||||
import com.philliphsu.clock2.alarms.AlarmsFragment;
|
||||
import com.philliphsu.clock2.editalarm.EditAlarmActivity;
|
||||
import com.philliphsu.clock2.settings.SettingsActivity;
|
||||
|
||||
public class MainActivity extends BaseActivity implements AlarmsFragment.OnAlarmInteractionListener {
|
||||
private static final String TAG = "MainActivity";
|
||||
@@ -85,6 +86,7 @@ public class MainActivity extends BaseActivity implements AlarmsFragment.OnAlarm
|
||||
|
||||
//noinspection SimplifiableIfStatement
|
||||
if (id == R.id.action_settings) {
|
||||
startActivity(new Intent(this, SettingsActivity.class));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.philliphsu.clock2.settings;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.philliphsu.clock2.BaseActivity;
|
||||
import com.philliphsu.clock2.R;
|
||||
|
||||
/**
|
||||
* Created by Phillip Hsu on 6/6/2016.
|
||||
*/
|
||||
public class SettingsActivity extends BaseActivity {
|
||||
/*
|
||||
* TODO: Define these keys as string resources instead and then delete these.
|
||||
* TODO: Move the existing string resources for the preferences below from strings.xml to prefs.xml
|
||||
*/
|
||||
// World Clock preference keys
|
||||
public static final String KEY_PREF_SHOW_TIME_OFFSETS_FROM = "pref_show_time_offsets_from";
|
||||
// Alarms preference keys
|
||||
public static final String KEY_PREF_TIME_PICKER_STYLE = "pref_time_picker_style";
|
||||
public static final String KEY_PREF_SNOOZE_DURATION = "pref_snooze_duration";
|
||||
public static final String KEY_PREF_FIRST_DAY_OF_WEEK = "pref_first_day_of_week";
|
||||
// Timers preference keys
|
||||
public static final String KEY_PREF_TIMER_RINGTONE = "pref_timer_ringtone";
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int layoutResId() {
|
||||
return R.layout.activity_settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int menuResId() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isDisplayShowTitleEnabled() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.philliphsu.clock2.settings;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.media.AudioManager;
|
||||
import android.media.Ringtone;
|
||||
import android.media.RingtoneManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.preference.ListPreference;
|
||||
import android.preference.Preference;
|
||||
import android.preference.PreferenceFragment;
|
||||
import android.preference.RingtonePreference;
|
||||
|
||||
import com.philliphsu.clock2.R;
|
||||
|
||||
public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
public SettingsFragment() {}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
addPreferencesFromResource(R.xml.preferences);
|
||||
// Set ringtone summary
|
||||
setSummary(getPreferenceScreen().getSharedPreferences(),
|
||||
SettingsActivity.KEY_PREF_TIMER_RINGTONE);
|
||||
findPreference(getString(R.string.key_alarm_volume))
|
||||
.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
AudioManager am = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
|
||||
am.adjustStreamVolume(
|
||||
AudioManager.STREAM_ALARM,
|
||||
AudioManager.ADJUST_SAME, // no adjustment
|
||||
AudioManager.FLAG_SHOW_UI); // show the volume toast
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
getPreferenceScreen().getSharedPreferences()
|
||||
.registerOnSharedPreferenceChangeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
getPreferenceScreen().getSharedPreferences()
|
||||
.unregisterOnSharedPreferenceChangeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||
setSummary(sharedPreferences, key);
|
||||
}
|
||||
|
||||
private void setSummary(SharedPreferences prefs, String key) {
|
||||
Preference pref = findPreference(key);
|
||||
// Setting a ListPreference's summary value to "%s" in XML automatically updates the
|
||||
// preference's summary to display the selected value.
|
||||
if (!(pref instanceof ListPreference)) {
|
||||
String summary = prefs.getString(key, "");
|
||||
if (pref instanceof RingtonePreference) {
|
||||
Uri ringtoneUri = Uri.parse(summary);
|
||||
Ringtone ringtone = RingtoneManager.getRingtone(getActivity(), ringtoneUri);
|
||||
summary = ringtone.getTitle(getActivity());
|
||||
}
|
||||
pref.setSummary(summary);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user