Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/UserInterface/DeviceSettingsFragment.java
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Erik Duisters <e.duisters1@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
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/>. | ||||
19 | */ | ||||
20 | | ||||
21 | package org.kde.kdeconnect.UserInterface; | ||||
22 | | ||||
23 | import android.os.Bundle; | ||||
24 | import android.os.Parcelable; | ||||
25 | | ||||
26 | import org.kde.kdeconnect.BackgroundService; | ||||
27 | import org.kde.kdeconnect.Device; | ||||
28 | import org.kde.kdeconnect_tp.R; | ||||
29 | | ||||
30 | import java.util.List; | ||||
31 | | ||||
32 | import androidx.annotation.NonNull; | ||||
33 | import androidx.fragment.app.FragmentActivity; | ||||
34 | import androidx.preference.PreferenceFragmentCompat; | ||||
35 | import androidx.preference.PreferenceScreen; | ||||
36 | import androidx.recyclerview.widget.RecyclerView; | ||||
37 | | ||||
38 | public class DeviceSettingsFragment extends PreferenceFragmentCompat { | ||||
39 | private static final String ARG_DEVICE_ID = "deviceId"; | ||||
40 | private static final String KEY_RECYCLERVIEW_LAYOUTMANAGER_STATE = "RecyclerViewLayoutmanagerState"; | ||||
41 | | ||||
42 | private PluginPreference.PluginPreferenceCallback callback; | ||||
43 | private Parcelable recyclerViewLayoutManagerState; | ||||
44 | | ||||
45 | /* | ||||
46 | https://bricolsoftconsulting.com/state-preservation-in-backstack-fragments/ | ||||
47 | When adding a fragment to the backstack the fragments onDestroyView is called (which releases | ||||
48 | the RecyclerView) but the fragments onSaveInstanceState is not called. When the fragment is destroyed later | ||||
49 | on, its onSaveInstanceState() is called but I don't have access to the RecyclerView or it's LayoutManager any more | ||||
50 | */ | ||||
51 | private boolean stateSaved; | ||||
52 | | ||||
53 | public static DeviceSettingsFragment newInstance(@NonNull String deviceId) { | ||||
54 | DeviceSettingsFragment fragment = new DeviceSettingsFragment(); | ||||
55 | | ||||
56 | Bundle args = new Bundle(); | ||||
57 | args.putString(ARG_DEVICE_ID, deviceId); | ||||
58 | fragment.setArguments(args); | ||||
59 | | ||||
60 | return fragment; | ||||
61 | } | ||||
62 | | ||||
63 | @Override | ||||
64 | public void onCreate(Bundle savedInstanceState) { | ||||
65 | if (requireActivity() instanceof PluginPreference.PluginPreferenceCallback) { | ||||
66 | callback = (PluginPreference.PluginPreferenceCallback) getActivity(); | ||||
67 | } else { | ||||
68 | throw new RuntimeException(requireActivity().getClass().getSimpleName() | ||||
69 | + " must implement PluginPreference.PluginPreferenceCallback"); | ||||
70 | } | ||||
71 | | ||||
72 | super.onCreate(savedInstanceState); | ||||
73 | | ||||
74 | if (savedInstanceState != null && savedInstanceState.containsKey(KEY_RECYCLERVIEW_LAYOUTMANAGER_STATE)) { | ||||
75 | recyclerViewLayoutManagerState = savedInstanceState.getParcelable(KEY_RECYCLERVIEW_LAYOUTMANAGER_STATE); | ||||
76 | } | ||||
77 | } | ||||
78 | | ||||
79 | @Override | ||||
80 | public void onDestroy() { | ||||
81 | super.onDestroy(); | ||||
82 | | ||||
83 | callback = null; | ||||
84 | } | ||||
85 | | ||||
86 | @Override | ||||
87 | public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { | ||||
88 | PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(requireContext()); | ||||
89 | setPreferenceScreen(preferenceScreen); | ||||
90 | | ||||
91 | final String deviceId = getArguments().getString(ARG_DEVICE_ID); | ||||
92 | | ||||
93 | BackgroundService.RunCommand(requireContext(), service -> { | ||||
94 | final Device device = service.getDevice(deviceId); | ||||
95 | if (device == null) { | ||||
96 | final FragmentActivity activity = requireActivity(); | ||||
97 | activity.runOnUiThread(() -> activity.finish()); | ||||
98 | return; | ||||
99 | } | ||||
100 | List<String> plugins = device.getSupportedPlugins(); | ||||
101 | | ||||
102 | for (final String pluginKey : plugins) { | ||||
103 | PluginPreference pref = new PluginPreference(requireContext(), pluginKey, device, callback); | ||||
104 | preferenceScreen.addPreference(pref); | ||||
105 | } | ||||
106 | }); | ||||
107 | } | ||||
108 | | ||||
109 | @Override | ||||
110 | protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) { | ||||
111 | RecyclerView.Adapter adapter = super.onCreateAdapter(preferenceScreen); | ||||
112 | | ||||
113 | /* | ||||
114 | The layoutmanager's state (e.g. scroll position) can only be restored when the recyclerView's | ||||
115 | adapter has been re-populated with data. | ||||
116 | */ | ||||
117 | if (recyclerViewLayoutManagerState != null) { | ||||
118 | adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() { | ||||
119 | @Override | ||||
120 | public void onChanged() { | ||||
121 | RecyclerView.LayoutManager layoutManager = getListView().getLayoutManager(); | ||||
122 | | ||||
123 | if (layoutManager != null) { | ||||
124 | layoutManager.onRestoreInstanceState(recyclerViewLayoutManagerState); | ||||
125 | } | ||||
126 | | ||||
127 | recyclerViewLayoutManagerState = null; | ||||
128 | adapter.unregisterAdapterDataObserver(this); | ||||
129 | } | ||||
130 | }); | ||||
131 | } | ||||
132 | | ||||
133 | return adapter; | ||||
134 | } | ||||
135 | | ||||
136 | @Override | ||||
137 | public void onPause() { | ||||
138 | super.onPause(); | ||||
139 | | ||||
140 | stateSaved = false; | ||||
141 | } | ||||
142 | | ||||
143 | @Override | ||||
144 | public void onResume() { | ||||
145 | super.onResume(); | ||||
146 | | ||||
147 | requireActivity().setTitle(getString(R.string.device_menu_plugins)); | ||||
148 | } | ||||
149 | | ||||
150 | @Override | ||||
151 | public void onDestroyView() { | ||||
152 | if (!stateSaved && getListView() != null && getListView().getLayoutManager() != null) { | ||||
153 | recyclerViewLayoutManagerState = getListView().getLayoutManager().onSaveInstanceState(); | ||||
154 | } | ||||
155 | | ||||
156 | super.onDestroyView(); | ||||
157 | } | ||||
158 | | ||||
159 | @Override | ||||
160 | public void onSaveInstanceState(Bundle outState) { | ||||
161 | super.onSaveInstanceState(outState); | ||||
162 | | ||||
163 | Parcelable layoutManagerState = recyclerViewLayoutManagerState; | ||||
164 | | ||||
165 | if (getListView() != null && getListView().getLayoutManager() != null) { | ||||
166 | layoutManagerState = getListView().getLayoutManager().onSaveInstanceState(); | ||||
167 | } | ||||
168 | | ||||
169 | if (layoutManagerState != null) { | ||||
170 | outState.putParcelable(KEY_RECYCLERVIEW_LAYOUTMANAGER_STATE, layoutManagerState); | ||||
171 | } | ||||
172 | | ||||
173 | stateSaved = true; | ||||
174 | } | ||||
175 | } |