-
Notifications
You must be signed in to change notification settings - Fork 1
/
SettingsActivity.java
108 lines (87 loc) · 4.79 KB
/
SettingsActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.paulnogas.mazesforprogrammers;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.PreferenceFragment;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.paulnogas.seekbarpreference.SeekBarPreference;
import static android.view.View.generateViewId;
public class SettingsActivity extends AppCompatActivity {
public static final String MAZE_PREFS = "MazePreferences";
static SeekBarPreference widthSeekBarPreference;
static SeekBarPreference heightSeekBarPreference;
static CheckBoxPreference heatMapCheckPreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frame = new FrameLayout(this);
frame.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
if(Build.VERSION.SDK_INT > 16) frame.setId(generateViewId());
else //noinspection ResourceType
frame.setId(676442);
setContentView(frame);
getFragmentManager()
.beginTransaction()
.add(frame.getId(), new CreditsFragment())
.commit();
}
public static class CreditsFragment extends PreferenceFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
}
//Runtime preference demo:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SharedPreferences sharedPreferences = getActivity().getSharedPreferences(MAZE_PREFS, Context.MODE_PRIVATE);
widthSeekBarPreference = new SeekBarPreference(getActivity());
this.getPreferenceScreen().addPreference(widthSeekBarPreference);
widthSeekBarPreference.setTitle(getResources().getString(R.string.columns_pref_title));
widthSeekBarPreference.setSummary("The number of rows in your maze");
widthSeekBarPreference.setMaxValue(25);//17 for ascii
widthSeekBarPreference.setMinValue(2);
widthSeekBarPreference.setInterval(1);
widthSeekBarPreference.setMeasurementUnit("columns");
int currentWidth = sharedPreferences.getInt((String) widthSeekBarPreference.getTitle(), getResources().getInteger(R.integer.default_maze_columns));
widthSeekBarPreference.setCurrentValue(currentWidth);
heightSeekBarPreference = new SeekBarPreference(getActivity());
this.getPreferenceScreen().addPreference(heightSeekBarPreference);
heightSeekBarPreference.setTitle(getResources().getString(R.string.rows_pref_title));
heightSeekBarPreference.setSummary("The number of columns in your maze");
heightSeekBarPreference.setMaxValue(25); //10 for ascii
heightSeekBarPreference.setMinValue(2);
heightSeekBarPreference.setInterval(1);
heightSeekBarPreference.setMeasurementUnit("rows");
int currentHeight = sharedPreferences.getInt((String) heightSeekBarPreference.getTitle(), getResources().getInteger(R.integer.default_maze_rows));
heightSeekBarPreference.setCurrentValue(currentHeight);
heatMapCheckPreference = new CheckBoxPreference(getActivity());
this.getPreferenceScreen().addPreference(heatMapCheckPreference);
heatMapCheckPreference.setTitle(R.string.heat_map_pref_title);
heatMapCheckPreference.setSummary("colours the map showing distance from start to finish");
boolean currentHeatMapStatus = sharedPreferences.getBoolean((String) heatMapCheckPreference.getTitle(), false);
heatMapCheckPreference.setChecked(currentHeatMapStatus);
}
}
@Override
protected void onStop() {
super.onStop();
savePrefs();
}
private void savePrefs() {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(MAZE_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt((String) widthSeekBarPreference.getTitle(), widthSeekBarPreference.getCurrentValue());
editor.putInt((String) heightSeekBarPreference.getTitle(), heightSeekBarPreference.getCurrentValue());
editor.putBoolean((String) heatMapCheckPreference.getTitle(), heatMapCheckPreference.isChecked());
editor.apply();
}
}