Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisActivity.java
Show First 20 Lines • Show All 45 Lines • ▼ Show 20 Line(s) | |||||
46 | 46 | | |||
47 | import java.util.List; | 47 | import java.util.List; | ||
48 | 48 | | |||
49 | public class MprisActivity extends ActionBarActivity { | 49 | public class MprisActivity extends ActionBarActivity { | ||
50 | 50 | | |||
51 | private String deviceId; | 51 | private String deviceId; | ||
52 | private final Handler positionSeekUpdateHandler = new Handler(); | 52 | private final Handler positionSeekUpdateHandler = new Handler(); | ||
53 | private Runnable positionSeekUpdateRunnable = null; | 53 | private Runnable positionSeekUpdateRunnable = null; | ||
54 | private String targetPlayer = null; | 54 | private MprisPlugin.MprisPlayer targetPlayer = null; | ||
55 | 55 | | |||
56 | private static String milisToProgress(long milis) { | 56 | private static String milisToProgress(long milis) { | ||
57 | int length = (int)(milis / 1000); //From milis to seconds | 57 | int length = (int)(milis / 1000); //From milis to seconds | ||
58 | StringBuilder text = new StringBuilder(); | 58 | StringBuilder text = new StringBuilder(); | ||
59 | int minutes = length / 60; | 59 | int minutes = length / 60; | ||
60 | if (minutes > 60) { | 60 | if (minutes > 60) { | ||
61 | int hours = minutes / 60; | 61 | int hours = minutes / 60; | ||
62 | minutes = minutes % 60; | 62 | minutes = minutes % 60; | ||
63 | text.append(hours).append(':'); | 63 | text.append(hours).append(':'); | ||
64 | if (minutes < 10) text.append('0'); | 64 | if (minutes < 10) text.append('0'); | ||
65 | } | 65 | } | ||
66 | text.append(minutes).append(':'); | 66 | text.append(minutes).append(':'); | ||
67 | int seconds = (length % 60); | 67 | int seconds = (length % 60); | ||
68 | if(seconds < 10) text.append('0'); // needed to show length properly (eg 4:05 instead of 4:5) | 68 | if(seconds < 10) text.append('0'); // needed to show length properly (eg 4:05 instead of 4:5) | ||
69 | text.append(seconds); | 69 | text.append(seconds); | ||
70 | return text.toString(); | 70 | return text.toString(); | ||
71 | } | 71 | } | ||
72 | protected void connectToPlugin() { | 72 | protected void connectToPlugin(final String targetPlayerName) { | ||
73 | 73 | | |||
74 | BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { | 74 | BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { | ||
75 | @Override | 75 | @Override | ||
76 | public void onServiceStart(BackgroundService service) { | 76 | public void onServiceStart(BackgroundService service) { | ||
77 | 77 | | |||
78 | final Device device = service.getDevice(deviceId); | 78 | final Device device = service.getDevice(deviceId); | ||
79 | final MprisPlugin mpris = device.getPlugin(MprisPlugin.class); | 79 | final MprisPlugin mpris = device.getPlugin(MprisPlugin.class); | ||
80 | if (mpris == null) { | 80 | if (mpris == null) { | ||
81 | Log.e("MprisActivity", "device has no mpris plugin!"); | 81 | Log.e("MprisActivity", "device has no mpris plugin!"); | ||
82 | return; | 82 | return; | ||
83 | } | 83 | } | ||
84 | targetPlayer = mpris.getPlayerStatus(targetPlayerName); | ||||
84 | 85 | | |||
85 | mpris.setPlayerStatusUpdatedHandler("activity", new Handler() { | 86 | mpris.setPlayerStatusUpdatedHandler("activity", new Handler() { | ||
86 | @Override | 87 | @Override | ||
87 | public void handleMessage(Message msg) { | 88 | public void handleMessage(Message msg) { | ||
88 | runOnUiThread(new Runnable() { | 89 | runOnUiThread(new Runnable() { | ||
89 | @Override | 90 | @Override | ||
90 | public void run() { | 91 | public void run() { | ||
91 | String song = mpris.getCurrentSong(); | 92 | updatePlayerStatus(mpris); | ||
92 | | ||||
93 | TextView nowPlaying = (TextView) findViewById(R.id.now_playing_textview); | | |||
94 | if (!nowPlaying.getText().toString().equals(song)) { | | |||
95 | nowPlaying.setText(song); | | |||
96 | } | | |||
97 | | ||||
98 | //Hacks for Spotify because it reports incorrect info about what it supports | | |||
99 | boolean isSpotify = "spotify".equals(mpris.getPlayer().toLowerCase()); | | |||
100 | | ||||
101 | if (mpris.getLength() > -1 && mpris.getPosition() > -1 && !isSpotify) { | | |||
102 | ((TextView) findViewById(R.id.time_textview)).setText(milisToProgress(mpris.getLength())); | | |||
103 | SeekBar positionSeek = (SeekBar)findViewById(R.id.positionSeek); | | |||
104 | positionSeek.setMax((int)(mpris.getLength())); | | |||
105 | positionSeek.setProgress((int)(mpris.getPosition())); | | |||
106 | findViewById(R.id.progress_slider).setVisibility(View.VISIBLE); | | |||
107 | } else { | | |||
108 | findViewById(R.id.progress_slider).setVisibility(View.GONE); | | |||
109 | } | | |||
110 | | ||||
111 | int volume = mpris.getVolume(); | | |||
112 | ((SeekBar) findViewById(R.id.volume_seek)).setProgress(volume); | | |||
113 | | ||||
114 | boolean isPlaying = mpris.isPlaying(); | | |||
115 | if (isPlaying) { | | |||
116 | ((ImageButton) findViewById(R.id.play_button)).setImageResource(android.R.drawable.ic_media_pause); | | |||
117 | findViewById(R.id.play_button).setVisibility(mpris.isPauseAllowed() ? View.VISIBLE : View.GONE); | | |||
118 | } else { | | |||
119 | ((ImageButton) findViewById(R.id.play_button)).setImageResource(android.R.drawable.ic_media_play); | | |||
120 | findViewById(R.id.play_button).setVisibility(mpris.isPlayAllowed() ? View.VISIBLE : View.GONE); | | |||
121 | } | | |||
122 | | ||||
123 | if (isSpotify) { | | |||
124 | findViewById(R.id.volume_layout).setVisibility(View.INVISIBLE); | | |||
125 | findViewById(R.id.rew_button).setVisibility(View.GONE); | | |||
126 | findViewById(R.id.ff_button).setVisibility(View.GONE); | | |||
127 | } else { | | |||
128 | findViewById(R.id.volume_layout).setVisibility(View.VISIBLE); | | |||
129 | findViewById(R.id.rew_button).setVisibility(mpris.isSeekAllowed() ? View.VISIBLE : View.GONE); | | |||
130 | findViewById(R.id.ff_button).setVisibility(mpris.isSeekAllowed() ? View.VISIBLE : View.GONE); | | |||
131 | } | | |||
132 | findViewById(R.id.next_button).setVisibility(mpris.isGoNextAllowed() ? View.VISIBLE : View.GONE); | | |||
133 | findViewById(R.id.prev_button).setVisibility(mpris.isGoPreviousAllowed() ? View.VISIBLE : View.GONE); | | |||
134 | } | 93 | } | ||
135 | }); | 94 | }); | ||
136 | } | 95 | } | ||
137 | }); | 96 | }); | ||
138 | 97 | | |||
139 | mpris.setPlayerListUpdatedHandler("activity", new Handler() { | 98 | mpris.setPlayerListUpdatedHandler("activity", new Handler() { | ||
140 | @Override | 99 | @Override | ||
141 | public void handleMessage(Message msg) { | 100 | public void handleMessage(Message msg) { | ||
Show All 22 Lines | 110 | public void run() { | |||
164 | 123 | | |||
165 | spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | 124 | spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | ||
166 | @Override | 125 | @Override | ||
167 | public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long id) { | 126 | public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long id) { | ||
168 | 127 | | |||
169 | if (pos >= playerList.size()) return; | 128 | if (pos >= playerList.size()) return; | ||
170 | 129 | | |||
171 | String player = playerList.get(pos); | 130 | String player = playerList.get(pos); | ||
172 | if (player.equals(mpris.getPlayer())) { | 131 | if (targetPlayer != null && player.equals(targetPlayer.getPlayer())) { | ||
173 | return; //Player hasn't actually changed | 132 | return; //Player hasn't actually changed | ||
174 | } | 133 | } | ||
175 | mpris.setPlayer(player); | 134 | targetPlayer = mpris.getPlayerStatus(player); | ||
176 | 135 | updatePlayerStatus(mpris); | |||
177 | //Clear values from previous player | | |||
178 | ((TextView) findViewById(R.id.now_playing_textview)).setText(""); | | |||
179 | ((TextView) findViewById(R.id.time_textview)).setText(milisToProgress(0)); | | |||
180 | ((SeekBar)findViewById(R.id.positionSeek)).setMax(0); | | |||
181 | } | 136 | } | ||
182 | 137 | | |||
183 | @Override | 138 | @Override | ||
184 | public void onNothingSelected(AdapterView<?> arg0) { | 139 | public void onNothingSelected(AdapterView<?> arg0) { | ||
185 | mpris.setPlayer(null); | 140 | targetPlayer = null; | ||
186 | } | 141 | } | ||
187 | }); | 142 | }); | ||
188 | 143 | | |||
189 | if (targetPlayer != null) { | 144 | if (targetPlayer != null) { | ||
190 | int targetIndex = adapter.getPosition(targetPlayer); | 145 | int targetIndex = adapter.getPosition(targetPlayer.getPlayer()); | ||
191 | if (targetIndex >= 0) { | 146 | if (targetIndex >= 0) { | ||
192 | spinner.setSelection(targetIndex); | 147 | spinner.setSelection(targetIndex); | ||
193 | } | | |||
194 | targetPlayer = null; | | |||
195 | } else { | 148 | } else { | ||
196 | // restore last selected player | 149 | targetPlayer = null; | ||
197 | int position = adapter.getPosition(mpris.getPlayer()); | | |||
198 | if (position >= 0) { | | |||
199 | spinner.setSelection(position); | | |||
200 | } | 150 | } | ||
201 | } | 151 | } | ||
152 | updatePlayerStatus(mpris); | ||||
thomasp: `targetPlayer` is always `null` here. Please simplify beginning from line 143. | |||||
202 | } | 153 | } | ||
203 | }); | 154 | }); | ||
204 | } | 155 | } | ||
205 | }); | 156 | }); | ||
206 | 157 | | |||
207 | } | 158 | } | ||
208 | }); | 159 | }); | ||
209 | 160 | | |||
210 | } | 161 | } | ||
211 | 162 | | |||
212 | private final BaseLinkProvider.ConnectionReceiver connectionReceiver = new BaseLinkProvider.ConnectionReceiver() { | 163 | private final BaseLinkProvider.ConnectionReceiver connectionReceiver = new BaseLinkProvider.ConnectionReceiver() { | ||
213 | @Override | 164 | @Override | ||
214 | public void onConnectionReceived(NetworkPackage identityPackage, BaseLink link) { | 165 | public void onConnectionReceived(NetworkPackage identityPackage, BaseLink link) { | ||
215 | connectToPlugin(); | 166 | connectToPlugin(null); | ||
216 | } | 167 | } | ||
217 | 168 | | |||
218 | @Override | 169 | @Override | ||
219 | public void onConnectionLost(BaseLink link) { | 170 | public void onConnectionLost(BaseLink link) { | ||
220 | 171 | | |||
221 | } | 172 | } | ||
222 | }; | 173 | }; | ||
223 | 174 | | |||
224 | @Override | 175 | @Override | ||
225 | protected void onDestroy() { | 176 | protected void onDestroy() { | ||
226 | super.onDestroy(); | 177 | super.onDestroy(); | ||
227 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | 178 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | ||
228 | @Override | 179 | @Override | ||
229 | public void onServiceStart(BackgroundService service) { | 180 | public void onServiceStart(BackgroundService service) { | ||
230 | service.removeConnectionListener(connectionReceiver); | 181 | service.removeConnectionListener(connectionReceiver); | ||
231 | } | 182 | } | ||
232 | }); | 183 | }); | ||
233 | } | 184 | } | ||
234 | 185 | | |||
186 | private void updatePlayerStatus(MprisPlugin mpris) { | ||||
187 | MprisPlugin.MprisPlayer playerStatus = targetPlayer; | ||||
188 | if (playerStatus == null) { | ||||
189 | //No player with that name found, just display "empty" data | ||||
190 | playerStatus = mpris.getEmptyPlayer(); | ||||
191 | } | ||||
192 | String song = playerStatus.getCurrentSong(); | ||||
193 | | ||||
194 | TextView nowPlaying = (TextView) findViewById(R.id.now_playing_textview); | ||||
195 | if (!nowPlaying.getText().toString().equals(song)) { | ||||
196 | nowPlaying.setText(song); | ||||
197 | } | ||||
198 | | ||||
199 | if (playerStatus.isSeekAllowed()) { | ||||
200 | ((TextView) findViewById(R.id.time_textview)).setText(milisToProgress(playerStatus.getLength())); | ||||
thomasp: `MprisPlayer` could report correct info. | |||||
201 | SeekBar positionSeek = (SeekBar)findViewById(R.id.positionSeek); | ||||
202 | positionSeek.setMax((int)(playerStatus.getLength())); | ||||
203 | positionSeek.setProgress((int)(playerStatus.getPosition())); | ||||
204 | findViewById(R.id.progress_slider).setVisibility(View.VISIBLE); | ||||
205 | } else { | ||||
206 | findViewById(R.id.progress_slider).setVisibility(View.GONE); | ||||
207 | } | ||||
208 | | ||||
209 | int volume = playerStatus.getVolume(); | ||||
210 | ((SeekBar) findViewById(R.id.volume_seek)).setProgress(volume); | ||||
211 | | ||||
212 | boolean isPlaying = playerStatus.isPlaying(); | ||||
213 | if (isPlaying) { | ||||
214 | ((ImageButton) findViewById(R.id.play_button)).setImageResource(android.R.drawable.ic_media_pause); | ||||
215 | findViewById(R.id.play_button).setVisibility(playerStatus.isPauseAllowed() ? View.VISIBLE : View.GONE); | ||||
216 | } else { | ||||
217 | ((ImageButton) findViewById(R.id.play_button)).setImageResource(android.R.drawable.ic_media_play); | ||||
218 | findViewById(R.id.play_button).setVisibility(playerStatus.isPlayAllowed() ? View.VISIBLE : View.GONE); | ||||
219 | } | ||||
220 | | ||||
221 | findViewById(R.id.volume_layout).setVisibility(playerStatus.isSetVolumeAllowed() ? View.VISIBLE : View.INVISIBLE); | ||||
222 | findViewById(R.id.rew_button).setVisibility(playerStatus.isSeekAllowed() ? View.VISIBLE : View.GONE); | ||||
223 | findViewById(R.id.ff_button).setVisibility(playerStatus.isSeekAllowed() ? View.VISIBLE : View.GONE); | ||||
224 | findViewById(R.id.next_button).setVisibility(playerStatus.isGoNextAllowed() ? View.VISIBLE : View.GONE); | ||||
225 | findViewById(R.id.prev_button).setVisibility(playerStatus.isGoPreviousAllowed() ? View.VISIBLE : View.GONE); | ||||
226 | } | ||||
227 | | ||||
235 | /** | 228 | /** | ||
236 | * Change current volume with provided step. | 229 | * Change current volume with provided step. | ||
237 | * | 230 | * | ||
238 | * @param mpris multimedia controller | | |||
239 | * @param step step size volume change | 231 | * @param step step size volume change | ||
240 | */ | 232 | */ | ||
241 | private void updateVolume(MprisPlugin mpris, int step) { | 233 | private void updateVolume(int step) { | ||
242 | final int currentVolume = mpris.getVolume(); | 234 | if (targetPlayer == null) { | ||
235 | return; | ||||
236 | } | ||||
237 | final int currentVolume = targetPlayer.getVolume(); | ||||
238 | | ||||
243 | if(currentVolume < 100 || currentVolume > 0) { | 239 | if(currentVolume < 100 || currentVolume > 0) { | ||
244 | int newVolume = currentVolume + step; | 240 | int newVolume = currentVolume + step; | ||
245 | if(newVolume > 100) { | 241 | if(newVolume > 100) { | ||
246 | newVolume = 100; | 242 | newVolume = 100; | ||
247 | } else if (newVolume <0 ) { | 243 | } else if (newVolume <0 ) { | ||
248 | newVolume = 0; | 244 | newVolume = 0; | ||
249 | } | 245 | } | ||
250 | mpris.setVolume(newVolume); | 246 | targetPlayer.setVolume(newVolume); | ||
251 | } | 247 | } | ||
252 | } | 248 | } | ||
253 | 249 | | |||
254 | @Override | 250 | @Override | ||
255 | public boolean onKeyDown(int keyCode, KeyEvent event) { | 251 | public boolean onKeyDown(int keyCode, KeyEvent event) { | ||
256 | switch (keyCode) { | 252 | switch (keyCode) { | ||
257 | case KeyEvent.KEYCODE_VOLUME_UP: | 253 | case KeyEvent.KEYCODE_VOLUME_UP: | ||
258 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | 254 | updateVolume(5); | ||
259 | @Override | | |||
260 | public void onServiceStart(BackgroundService service) { | | |||
261 | Device device = service.getDevice(deviceId); | | |||
262 | MprisPlugin mpris = device.getPlugin(MprisPlugin.class); | | |||
263 | if (mpris == null) return; | | |||
264 | updateVolume(mpris, 5); | | |||
265 | } | | |||
266 | }); | | |||
267 | return true; | 255 | return true; | ||
268 | case KeyEvent.KEYCODE_VOLUME_DOWN: | 256 | case KeyEvent.KEYCODE_VOLUME_DOWN: | ||
269 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | 257 | updateVolume(-5); | ||
270 | @Override | | |||
271 | public void onServiceStart(BackgroundService service) { | | |||
272 | Device device = service.getDevice(deviceId); | | |||
273 | MprisPlugin mpris = device.getPlugin(MprisPlugin.class); | | |||
274 | if (mpris == null) return; | | |||
275 | updateVolume(mpris, -5); | | |||
276 | } | | |||
277 | }); | | |||
278 | return true; | 258 | return true; | ||
279 | default: | 259 | default: | ||
280 | return super.onKeyDown(keyCode, event); | 260 | return super.onKeyDown(keyCode, event); | ||
281 | } | 261 | } | ||
282 | } | 262 | } | ||
283 | 263 | | |||
284 | @Override | 264 | @Override | ||
285 | public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) { | 265 | public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) { | ||
286 | switch (keyCode) { | 266 | switch (keyCode) { | ||
287 | case KeyEvent.KEYCODE_VOLUME_UP: | 267 | case KeyEvent.KEYCODE_VOLUME_UP: | ||
288 | return true; | 268 | return true; | ||
289 | case KeyEvent.KEYCODE_VOLUME_DOWN: | 269 | case KeyEvent.KEYCODE_VOLUME_DOWN: | ||
290 | return true; | 270 | return true; | ||
291 | default: | 271 | default: | ||
292 | return super.onKeyUp(keyCode, event); | 272 | return super.onKeyUp(keyCode, event); | ||
293 | } | 273 | } | ||
294 | } | 274 | } | ||
295 | 275 | | |||
296 | @Override | 276 | @Override | ||
297 | protected void onCreate(Bundle savedInstanceState) { | 277 | protected void onCreate(Bundle savedInstanceState) { | ||
298 | super.onCreate(savedInstanceState); | 278 | super.onCreate(savedInstanceState); | ||
299 | setContentView(R.layout.mpris_control); | 279 | setContentView(R.layout.mpris_control); | ||
300 | 280 | | |||
301 | targetPlayer = getIntent().getStringExtra("player"); | 281 | final String targetPlayerName = getIntent().getStringExtra("player"); | ||
302 | getIntent().removeExtra("player"); | 282 | getIntent().removeExtra("player"); | ||
303 | deviceId = getIntent().getStringExtra("deviceId"); | 283 | deviceId = getIntent().getStringExtra("deviceId"); | ||
304 | 284 | | |||
305 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); | 285 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); | ||
306 | String interval_time_str = prefs.getString(getString(R.string.mpris_time_key), | 286 | String interval_time_str = prefs.getString(getString(R.string.mpris_time_key), | ||
307 | getString(R.string.mpris_time_default)); | 287 | getString(R.string.mpris_time_default)); | ||
308 | final int interval_time = Integer.parseInt(interval_time_str); | 288 | final int interval_time = Integer.parseInt(interval_time_str); | ||
309 | 289 | | |||
310 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | 290 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | ||
311 | @Override | 291 | @Override | ||
312 | public void onServiceStart(BackgroundService service) { | 292 | public void onServiceStart(BackgroundService service) { | ||
313 | service.addConnectionListener(connectionReceiver); | 293 | service.addConnectionListener(connectionReceiver); | ||
314 | } | 294 | } | ||
315 | }); | 295 | }); | ||
316 | connectToPlugin(); | 296 | connectToPlugin(targetPlayerName); | ||
317 | 297 | | |||
318 | findViewById(R.id.play_button).setOnClickListener(new View.OnClickListener() { | 298 | findViewById(R.id.play_button).setOnClickListener(new View.OnClickListener() { | ||
319 | @Override | 299 | @Override | ||
320 | public void onClick(View view) { | 300 | public void onClick(View view) { | ||
321 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | 301 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | ||
322 | @Override | 302 | @Override | ||
323 | public void onServiceStart(BackgroundService service) { | 303 | public void onServiceStart(BackgroundService service) { | ||
324 | Device device = service.getDevice(deviceId); | 304 | if (targetPlayer == null) return; | ||
325 | MprisPlugin mpris = device.getPlugin(MprisPlugin.class); | 305 | targetPlayer.playPause(); | ||
326 | if (mpris == null) return; | | |||
327 | mpris.sendAction("PlayPause"); | | |||
328 | } | 306 | } | ||
329 | }); | 307 | }); | ||
330 | } | 308 | } | ||
331 | }); | 309 | }); | ||
332 | 310 | | |||
333 | findViewById(R.id.prev_button).setOnClickListener(new View.OnClickListener() { | 311 | findViewById(R.id.prev_button).setOnClickListener(new View.OnClickListener() { | ||
334 | @Override | 312 | @Override | ||
335 | public void onClick(View view) { | 313 | public void onClick(View view) { | ||
336 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | 314 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | ||
337 | @Override | 315 | @Override | ||
338 | public void onServiceStart(BackgroundService service) { | 316 | public void onServiceStart(BackgroundService service) { | ||
339 | Device device = service.getDevice(deviceId); | 317 | if (targetPlayer == null) return; | ||
340 | MprisPlugin mpris = device.getPlugin(MprisPlugin.class); | 318 | targetPlayer.previous(); | ||
341 | if (mpris == null) return; | | |||
342 | mpris.sendAction("Previous"); | | |||
343 | } | 319 | } | ||
344 | }); | 320 | }); | ||
345 | } | 321 | } | ||
346 | }); | 322 | }); | ||
347 | 323 | | |||
348 | findViewById(R.id.rew_button).setOnClickListener(new View.OnClickListener() { | 324 | findViewById(R.id.rew_button).setOnClickListener(new View.OnClickListener() { | ||
349 | @Override | 325 | @Override | ||
350 | public void onClick(View view) { | 326 | public void onClick(View view) { | ||
351 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | 327 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | ||
352 | @Override | 328 | @Override | ||
353 | public void onServiceStart(BackgroundService service) { | 329 | public void onServiceStart(BackgroundService service) { | ||
354 | Device device = service.getDevice(deviceId); | 330 | if (targetPlayer == null) return; | ||
355 | MprisPlugin mpris = device.getPlugin(MprisPlugin.class); | 331 | targetPlayer.seek(interval_time * -1); | ||
356 | if (mpris == null) return; | | |||
357 | mpris.Seek(interval_time * -1); | | |||
358 | } | 332 | } | ||
359 | }); | 333 | }); | ||
360 | } | 334 | } | ||
361 | }); | 335 | }); | ||
362 | 336 | | |||
363 | findViewById(R.id.ff_button).setOnClickListener(new View.OnClickListener() { | 337 | findViewById(R.id.ff_button).setOnClickListener(new View.OnClickListener() { | ||
364 | @Override | 338 | @Override | ||
365 | public void onClick(View view) { | 339 | public void onClick(View view) { | ||
366 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | 340 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | ||
367 | @Override | 341 | @Override | ||
368 | public void onServiceStart(BackgroundService service) { | 342 | public void onServiceStart(BackgroundService service) { | ||
369 | Device device = service.getDevice(deviceId); | 343 | if (targetPlayer == null) return; | ||
370 | MprisPlugin mpris = device.getPlugin(MprisPlugin.class); | 344 | targetPlayer.seek(interval_time); | ||
371 | if (mpris == null) return; | | |||
372 | mpris.Seek(interval_time); | | |||
373 | } | 345 | } | ||
374 | }); | 346 | }); | ||
375 | } | 347 | } | ||
376 | }); | 348 | }); | ||
377 | 349 | | |||
378 | findViewById(R.id.next_button).setOnClickListener(new View.OnClickListener() { | 350 | findViewById(R.id.next_button).setOnClickListener(new View.OnClickListener() { | ||
379 | @Override | 351 | @Override | ||
380 | public void onClick(View view) { | 352 | public void onClick(View view) { | ||
381 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | 353 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | ||
382 | @Override | 354 | @Override | ||
383 | public void onServiceStart(BackgroundService service) { | 355 | public void onServiceStart(BackgroundService service) { | ||
384 | Device device = service.getDevice(deviceId); | 356 | if (targetPlayer == null) return; | ||
385 | MprisPlugin mpris = device.getPlugin(MprisPlugin.class); | 357 | targetPlayer.next(); | ||
386 | if (mpris == null) return; | | |||
387 | mpris.sendAction("Next"); | | |||
388 | } | 358 | } | ||
389 | }); | 359 | }); | ||
390 | } | 360 | } | ||
391 | }); | 361 | }); | ||
392 | 362 | | |||
393 | ((SeekBar)findViewById(R.id.volume_seek)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { | 363 | ((SeekBar)findViewById(R.id.volume_seek)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { | ||
394 | @Override | 364 | @Override | ||
395 | public void onProgressChanged(SeekBar seekBar, int i, boolean b) { | 365 | public void onProgressChanged(SeekBar seekBar, int i, boolean b) { | ||
396 | } | 366 | } | ||
397 | 367 | | |||
398 | @Override | 368 | @Override | ||
399 | public void onStartTrackingTouch(SeekBar seekBar) { | 369 | public void onStartTrackingTouch(SeekBar seekBar) { | ||
400 | } | 370 | } | ||
401 | 371 | | |||
402 | @Override | 372 | @Override | ||
403 | public void onStopTrackingTouch(final SeekBar seekBar) { | 373 | public void onStopTrackingTouch(final SeekBar seekBar) { | ||
404 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | 374 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | ||
405 | @Override | 375 | @Override | ||
406 | public void onServiceStart(BackgroundService service) { | 376 | public void onServiceStart(BackgroundService service) { | ||
407 | Device device = service.getDevice(deviceId); | 377 | if (targetPlayer == null) return; | ||
408 | MprisPlugin mpris = device.getPlugin(MprisPlugin.class); | 378 | targetPlayer.setVolume(seekBar.getProgress()); | ||
409 | if (mpris == null) return; | | |||
410 | mpris.setVolume(seekBar.getProgress()); | | |||
411 | } | 379 | } | ||
412 | }); | 380 | }); | ||
413 | } | 381 | } | ||
414 | 382 | | |||
415 | }); | 383 | }); | ||
416 | 384 | | |||
417 | positionSeekUpdateRunnable = new Runnable() { | 385 | positionSeekUpdateRunnable = new Runnable() { | ||
418 | @Override | 386 | @Override | ||
419 | public void run() { | 387 | public void run() { | ||
420 | final SeekBar positionSeek = (SeekBar)findViewById(R.id.positionSeek); | 388 | final SeekBar positionSeek = (SeekBar)findViewById(R.id.positionSeek); | ||
421 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | 389 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | ||
422 | @Override | 390 | @Override | ||
423 | public void onServiceStart(BackgroundService service) { | 391 | public void onServiceStart(BackgroundService service) { | ||
424 | Device device = service.getDevice(deviceId); | 392 | if (targetPlayer != null) { | ||
425 | if (device != null) { | 393 | positionSeek.setProgress((int) (targetPlayer.getPosition())); | ||
426 | MprisPlugin mpris = device.getPlugin(MprisPlugin.class); | | |||
427 | if (mpris != null) { | | |||
428 | positionSeek.setProgress((int) (mpris.getPosition())); | | |||
429 | } | | |||
430 | } | 394 | } | ||
431 | positionSeekUpdateHandler.removeCallbacks(positionSeekUpdateRunnable); | 395 | positionSeekUpdateHandler.removeCallbacks(positionSeekUpdateRunnable); | ||
432 | positionSeekUpdateHandler.postDelayed(positionSeekUpdateRunnable, 1000); | 396 | positionSeekUpdateHandler.postDelayed(positionSeekUpdateRunnable, 1000); | ||
433 | } | 397 | } | ||
434 | }); | 398 | }); | ||
435 | } | 399 | } | ||
436 | 400 | | |||
437 | }; | 401 | }; | ||
Show All 10 Lines | 411 | public void onStartTrackingTouch(SeekBar seekBar) { | |||
448 | positionSeekUpdateHandler.removeCallbacks(positionSeekUpdateRunnable); | 412 | positionSeekUpdateHandler.removeCallbacks(positionSeekUpdateRunnable); | ||
449 | } | 413 | } | ||
450 | 414 | | |||
451 | @Override | 415 | @Override | ||
452 | public void onStopTrackingTouch(final SeekBar seekBar) { | 416 | public void onStopTrackingTouch(final SeekBar seekBar) { | ||
453 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | 417 | BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() { | ||
454 | @Override | 418 | @Override | ||
455 | public void onServiceStart(BackgroundService service) { | 419 | public void onServiceStart(BackgroundService service) { | ||
456 | Device device = service.getDevice(deviceId); | 420 | if (targetPlayer != null) { | ||
457 | MprisPlugin mpris = device.getPlugin(MprisPlugin.class); | 421 | targetPlayer.setPosition(seekBar.getProgress()); | ||
458 | if (mpris != null) { | | |||
459 | mpris.setPosition(seekBar.getProgress()); | | |||
460 | } | 422 | } | ||
461 | positionSeekUpdateHandler.postDelayed(positionSeekUpdateRunnable, 200); | 423 | positionSeekUpdateHandler.postDelayed(positionSeekUpdateRunnable, 200); | ||
462 | } | 424 | } | ||
463 | }); | 425 | }); | ||
464 | } | 426 | } | ||
465 | 427 | | |||
466 | }); | 428 | }); | ||
467 | 429 | | |||
Show All 16 Lines |
targetPlayer is always null here. Please simplify beginning from line 143.