Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/UserInterface/PairingFragment.java
Show First 20 Lines • Show All 73 Lines • ▼ Show 20 Line(s) | 68 | public View onCreateView(LayoutInflater inflater, ViewGroup container, | |||
---|---|---|---|---|---|
74 | 74 | | |||
75 | 75 | | |||
76 | setHasOptionsMenu(true); | 76 | setHasOptionsMenu(true); | ||
77 | 77 | | |||
78 | rootView = inflater.inflate(R.layout.devices_list, container, false); | 78 | rootView = inflater.inflate(R.layout.devices_list, container, false); | ||
79 | View listRootView = rootView.findViewById(R.id.devices_list); | 79 | View listRootView = rootView.findViewById(R.id.devices_list); | ||
80 | mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh_list_layout); | 80 | mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh_list_layout); | ||
81 | mSwipeRefreshLayout.setOnRefreshListener( | 81 | mSwipeRefreshLayout.setOnRefreshListener( | ||
82 | new SwipeRefreshLayout.OnRefreshListener() { | 82 | this::updateComputerListAction | ||
83 | @Override | | |||
84 | public void onRefresh() { | | |||
85 | updateComputerListAction(); | | |||
86 | } | | |||
87 | } | | |||
88 | ); | 83 | ); | ||
89 | headerText = new TextView(inflater.getContext()); | 84 | headerText = new TextView(inflater.getContext()); | ||
90 | headerText.setText(getString(R.string.pairing_description)); | 85 | headerText.setText(getString(R.string.pairing_description)); | ||
91 | headerText.setPadding(0, (int) (16 * getResources().getDisplayMetrics().density), 0, (int) (12 * getResources().getDisplayMetrics().density)); | 86 | headerText.setPadding(0, (int) (16 * getResources().getDisplayMetrics().density), 0, (int) (12 * getResources().getDisplayMetrics().density)); | ||
92 | ((ListView) listRootView).addHeaderView(headerText); | 87 | ((ListView) listRootView).addHeaderView(headerText); | ||
93 | 88 | | |||
94 | return rootView; | 89 | return rootView; | ||
95 | } | 90 | } | ||
96 | 91 | | |||
97 | @Override | 92 | @Override | ||
98 | public void onAttach(Context context) { | 93 | public void onAttach(Context context) { | ||
99 | super.onAttach(context); | 94 | super.onAttach(context); | ||
100 | mActivity = ((MainActivity) getActivity()); | 95 | mActivity = ((MainActivity) getActivity()); | ||
101 | } | 96 | } | ||
102 | 97 | | |||
103 | private void updateComputerListAction() { | 98 | private void updateComputerListAction() { | ||
104 | updateComputerList(); | 99 | updateComputerList(); | ||
105 | BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() { | 100 | BackgroundService.RunCommand(mActivity, BackgroundService::onNetworkChange); | ||
106 | @Override | | |||
107 | public void onServiceStart(BackgroundService service) { | | |||
108 | service.onNetworkChange(); | | |||
109 | } | | |||
110 | }); | | |||
111 | mSwipeRefreshLayout.setRefreshing(true); | 101 | mSwipeRefreshLayout.setRefreshing(true); | ||
112 | new Thread(new Runnable() { | 102 | new Thread(() -> { | ||
113 | @Override | | |||
114 | public void run() { | | |||
115 | try { | 103 | try { | ||
116 | Thread.sleep(1500); | 104 | Thread.sleep(1500); | ||
117 | } catch (InterruptedException ignored) { | 105 | } catch (InterruptedException ignored) { | ||
118 | } | 106 | } | ||
119 | mActivity.runOnUiThread(new Runnable() { | 107 | mActivity.runOnUiThread(() -> mSwipeRefreshLayout.setRefreshing(false)); | ||
120 | @Override | | |||
121 | public void run() { | | |||
122 | mSwipeRefreshLayout.setRefreshing(false); | | |||
123 | } | | |||
124 | }); | | |||
125 | } | | |||
126 | }).start(); | 108 | }).start(); | ||
127 | } | 109 | } | ||
128 | 110 | | |||
129 | private void updateComputerList() { | 111 | private void updateComputerList() { | ||
130 | BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() { | 112 | BackgroundService.RunCommand(mActivity, service -> mActivity.runOnUiThread(() -> { | ||
131 | @Override | | |||
132 | public void onServiceStart(final BackgroundService service) { | | |||
133 | mActivity.runOnUiThread(new Runnable() { | | |||
134 | @Override | | |||
135 | public void run() { | | |||
136 | 113 | | |||
137 | if (!isAdded()) { | 114 | if (!isAdded()) { | ||
138 | //Fragment is not attached to an activity. We will crash if we try to do anything here. | 115 | //Fragment is not attached to an activity. We will crash if we try to do anything here. | ||
139 | return; | 116 | return; | ||
140 | } | 117 | } | ||
141 | 118 | | |||
142 | if (listRefreshCalledThisFrame) { | 119 | if (listRefreshCalledThisFrame) { | ||
143 | // This makes sure we don't try to call list.getFirstVisiblePosition() | 120 | // This makes sure we don't try to call list.getFirstVisiblePosition() | ||
▲ Show 20 Lines • Show All 63 Lines • ▼ Show 20 Line(s) | 132 | try { | |||
207 | //Restore scroll | 184 | //Restore scroll | ||
208 | list.setSelectionFromTop(index, top); | 185 | list.setSelectionFromTop(index, top); | ||
209 | } catch (IllegalStateException e) { | 186 | } catch (IllegalStateException e) { | ||
210 | e.printStackTrace(); | 187 | e.printStackTrace(); | ||
211 | //Ignore: The activity was closed while we were trying to update it | 188 | //Ignore: The activity was closed while we were trying to update it | ||
212 | } finally { | 189 | } finally { | ||
213 | listRefreshCalledThisFrame = false; | 190 | listRefreshCalledThisFrame = false; | ||
214 | } | 191 | } | ||
215 | } | | |||
216 | }); | | |||
217 | 192 | | |||
218 | } | 193 | })); | ||
219 | }); | | |||
220 | } | 194 | } | ||
221 | 195 | | |||
222 | | ||||
223 | @Override | 196 | @Override | ||
224 | public void onStart() { | 197 | public void onStart() { | ||
225 | super.onStart(); | 198 | super.onStart(); | ||
226 | BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() { | 199 | BackgroundService.RunCommand(mActivity, service -> service.addDeviceListChangedCallback("PairingFragment", this::updateComputerList)); | ||
227 | @Override | | |||
228 | public void onServiceStart(BackgroundService service) { | | |||
229 | service.addDeviceListChangedCallback("PairingFragment", new BackgroundService.DeviceListChangedCallback() { | | |||
230 | @Override | | |||
231 | public void onDeviceListChanged() { | | |||
232 | updateComputerList(); | | |||
233 | } | | |||
234 | }); | | |||
235 | } | | |||
236 | }); | | |||
237 | updateComputerList(); | 200 | updateComputerList(); | ||
238 | } | 201 | } | ||
239 | 202 | | |||
240 | @Override | 203 | @Override | ||
241 | public void onStop() { | 204 | public void onStop() { | ||
242 | super.onStop(); | 205 | super.onStop(); | ||
243 | mSwipeRefreshLayout.setEnabled(false); | 206 | mSwipeRefreshLayout.setEnabled(false); | ||
244 | BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() { | 207 | BackgroundService.RunCommand(mActivity, service -> service.removeDeviceListChangedCallback("PairingFragment")); | ||
245 | @Override | | |||
246 | public void onServiceStart(BackgroundService service) { | | |||
247 | service.removeDeviceListChangedCallback("PairingFragment"); | | |||
248 | } | | |||
249 | }); | | |||
250 | } | 208 | } | ||
251 | 209 | | |||
252 | @Override | 210 | @Override | ||
253 | public void pairingClicked(Device device) { | 211 | public void pairingClicked(Device device) { | ||
254 | mActivity.onDeviceSelected(device.getDeviceId(), !device.isPaired() || !device.isReachable()); | 212 | mActivity.onDeviceSelected(device.getDeviceId(), !device.isPaired() || !device.isReachable()); | ||
255 | } | 213 | } | ||
256 | 214 | | |||
257 | @Override | 215 | @Override | ||
Show All 38 Lines |