Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/UserInterface/DeviceSettingsActivity.java
Show All 15 Lines | |||||
16 | * | 16 | * | ||
17 | * You should have received a copy of the GNU General Public License | 17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | package org.kde.kdeconnect.UserInterface; | 21 | package org.kde.kdeconnect.UserInterface; | ||
22 | 22 | | |||
23 | import android.os.Bundle; | 23 | import android.os.Bundle; | ||
24 | import android.preference.PreferenceScreen; | | |||
25 | import android.view.MenuItem; | 24 | import android.view.MenuItem; | ||
26 | 25 | | |||
27 | import org.kde.kdeconnect.BackgroundService; | 26 | import org.kde.kdeconnect.BackgroundService; | ||
28 | import org.kde.kdeconnect.Device; | 27 | import org.kde.kdeconnect.Device; | ||
28 | import org.kde.kdeconnect.Plugins.Plugin; | ||||
29 | import org.kde.kdeconnect_tp.R; | ||||
29 | 30 | | |||
30 | import java.util.List; | 31 | import androidx.appcompat.app.AppCompatActivity; | ||
32 | import androidx.fragment.app.Fragment; | ||||
33 | import androidx.fragment.app.FragmentManager; | ||||
31 | 34 | | |||
32 | public class DeviceSettingsActivity extends AppCompatPreferenceActivity { | 35 | public class DeviceSettingsActivity | ||
36 | extends AppCompatActivity | ||||
37 | implements PluginPreference.PluginPreferenceCallback { | ||||
33 | 38 | | |||
39 | public static final String EXTRA_DEVICE_ID = "deviceId"; | ||||
40 | public static final String EXTRA_PLUGIN_KEY = "pluginKey"; | ||||
41 | | ||||
42 | //TODO: Save/restore state | ||||
34 | static private String deviceId; //Static because if we get here by using the back button in the action bar, the extra deviceId will not be set. | 43 | static private String deviceId; //Static because if we get here by using the back button in the action bar, the extra deviceId will not be set. | ||
35 | 44 | | |||
36 | @Override | 45 | @Override | ||
37 | public void onCreate(Bundle savedInstanceState) { | 46 | public void onCreate(Bundle savedInstanceState) { | ||
47 | ThemeUtil.setUserPreferredTheme(this); | ||||
38 | super.onCreate(savedInstanceState); | 48 | super.onCreate(savedInstanceState); | ||
39 | 49 | | |||
40 | final PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(this); | 50 | setContentView(R.layout.activity_device_settings); | ||
41 | setPreferenceScreen(preferenceScreen); | | |||
42 | 51 | | |||
43 | if (getIntent().hasExtra("deviceId")) { | 52 | if (getSupportActionBar() != null) { | ||
44 | deviceId = getIntent().getStringExtra("deviceId"); | 53 | getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true); | ||
45 | } | 54 | } | ||
46 | 55 | | |||
47 | BackgroundService.RunCommand(getApplicationContext(), service -> { | 56 | String pluginKey = null; | ||
48 | final Device device = service.getDevice(deviceId); | 57 | | ||
49 | if (device == null) { | 58 | if (getIntent().hasExtra(EXTRA_DEVICE_ID)) { | ||
50 | DeviceSettingsActivity.this.runOnUiThread(DeviceSettingsActivity.this::finish); | 59 | deviceId = getIntent().getStringExtra(EXTRA_DEVICE_ID); | ||
51 | return; | 60 | | ||
61 | if (getIntent().hasExtra(EXTRA_PLUGIN_KEY)) { | ||||
62 | pluginKey = getIntent().getStringExtra(EXTRA_PLUGIN_KEY); | ||||
63 | } | ||||
64 | } else if (deviceId == null) { | ||||
65 | throw new RuntimeException("You must start DeviceSettingActivity using an intent that has a " + EXTRA_DEVICE_ID + " extra"); | ||||
66 | } | ||||
67 | | ||||
68 | Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragmentPlaceHolder); | ||||
69 | if (fragment == null) { | ||||
70 | if (pluginKey == null) { | ||||
71 | fragment = DeviceSettingsFragment.newInstance(deviceId); | ||||
72 | } else { | ||||
73 | Device device = BackgroundService.getInstance().getDevice(deviceId); | ||||
74 | Plugin plugin = device.getPlugin(pluginKey, true); | ||||
75 | fragment = plugin.getSettingsFragment(); | ||||
52 | } | 76 | } | ||
53 | List<String> plugins = device.getSupportedPlugins(); | 77 | | ||
54 | for (final String pluginKey : plugins) { | 78 | getSupportFragmentManager() | ||
55 | PluginPreference pref = new PluginPreference(DeviceSettingsActivity.this, pluginKey, device); | 79 | .beginTransaction() | ||
56 | preferenceScreen.addPreference(pref); | 80 | .add(R.id.fragmentPlaceHolder, fragment) | ||
81 | .commit(); | ||||
57 | } | 82 | } | ||
58 | }); | | |||
59 | } | 83 | } | ||
60 | 84 | | |||
61 | @Override | 85 | @Override | ||
62 | public boolean onOptionsItemSelected(MenuItem item) { | 86 | public boolean onOptionsItemSelected(MenuItem item) { | ||
63 | //ActionBar's back button | | |||
64 | if (item.getItemId() == android.R.id.home) { | 87 | if (item.getItemId() == android.R.id.home) { | ||
65 | finish(); | 88 | FragmentManager fm = getSupportFragmentManager(); | ||
89 | | ||||
90 | if (fm.getBackStackEntryCount() > 0) { | ||||
91 | fm.popBackStack(); | ||||
66 | return true; | 92 | return true; | ||
67 | } else { | | |||
68 | return super.onOptionsItemSelected(item); | | |||
69 | } | 93 | } | ||
70 | } | 94 | } | ||
71 | 95 | | |||
96 | return super.onOptionsItemSelected(item); | ||||
97 | } | ||||
98 | | ||||
72 | @Override | 99 | @Override | ||
73 | protected void onStart() { | 100 | protected void onStart() { | ||
74 | super.onStart(); | 101 | super.onStart(); | ||
75 | BackgroundService.addGuiInUseCounter(this); | 102 | BackgroundService.addGuiInUseCounter(this); | ||
76 | } | 103 | } | ||
77 | 104 | | |||
78 | @Override | 105 | @Override | ||
79 | protected void onStop() { | 106 | protected void onStop() { | ||
80 | super.onStop(); | 107 | super.onStop(); | ||
81 | BackgroundService.removeGuiInUseCounter(this); | 108 | BackgroundService.removeGuiInUseCounter(this); | ||
82 | } | 109 | } | ||
83 | 110 | | |||
111 | @Override | ||||
112 | public void onStartPluginSettingsFragment(Plugin plugin) { | ||||
113 | setTitle(getString(R.string.plugin_settings_with_name, plugin.getDisplayName())); | ||||
114 | | ||||
115 | PluginSettingsFragment fragment = plugin.getSettingsFragment(); | ||||
116 | | ||||
117 | //TODO: Remove when NotificationFilterActivity has been turned into a PluginSettingsFragment | ||||
118 | if (fragment == null) { | ||||
119 | return; | ||||
120 | } | ||||
121 | | ||||
122 | getSupportFragmentManager() | ||||
123 | .beginTransaction() | ||||
124 | .replace(R.id.fragmentPlaceHolder, fragment) | ||||
125 | .addToBackStack(null) | ||||
126 | .commit(); | ||||
127 | | ||||
128 | } | ||||
129 | | ||||
130 | @Override | ||||
131 | public void onFinish() { | ||||
132 | finish(); | ||||
133 | } | ||||
84 | } | 134 | } |