-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathSettingsActivity.java
82 lines (64 loc) · 2.61 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
package com.stuffaboutcode.bluedot;
import android.os.Bundle;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.Preference;
import androidx.preference.ListPreference;
import androidx.preference.SwitchPreferenceCompat;
import android.content.SharedPreferences;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public static class SettingsFragment
extends PreferenceFragmentCompat
implements SharedPreferences.OnSharedPreferenceChangeListener{
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
setupPreferences();
}
@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {
setupPreferences();
}
private void setupPreferences() {
Preference default_port = findPreference("default_port");
SwitchPreferenceCompat default_port_switch = (SwitchPreferenceCompat)default_port;
Preference port = findPreference("port");
ListPreference port_list = (ListPreference) port;
if (default_port_switch != null) {
if (port_list != null) {
// disable the port if the default_port is enabled
if (default_port_switch.isChecked()) port.setVisible(false);
else port.setVisible(true);
// set the port summary
port.setSummary(port_list.getEntry());
}
}
}
}
}