Skip to content

Commit 19f0abd

Browse files
author
Chris Bellew
committed
Handle shortcuts that were created before multiple server connections were supported; Allow choosing whether or not to resume when creating a shortcut; Don't give focus to now playing screen when stopping playback if it's not in the foreground.
1 parent 5c1d168 commit 19f0abd

File tree

11 files changed

+106
-50
lines changed

11 files changed

+106
-50
lines changed

Voice Control For Plex/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.atomjack.vcfp"
44
android:versionCode="15"
5-
android:versionName="1.8.1b1" >
5+
android:versionName="1.8.1b2" >
66

77
<uses-permission android:name="com.mohammadag.googlesearchapi.permission.ACCESS_GGOGLE_SEARCH_API" />
88
<uses-permission android:name="android.permission.INTERNET" />
@@ -18,6 +18,7 @@
1818
android:targetSdkVersion="19" />
1919

2020
<application
21+
android:name=".VoiceControlForPlexApplication"
2122
android:allowBackup="true"
2223
android:icon="@drawable/ic_launcher"
2324
android:label="@string/app_name"

Voice Control For Plex/src/main/java/com/atomjack/vcfp/LocalScan.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.content.SharedPreferences;
99
import android.view.View;
1010
import android.widget.AdapterView;
11+
import android.widget.CheckBox;
1112
import android.widget.ListView;
1213

1314
import com.atomjack.vcfp.adapters.PlexListAdapter;
@@ -92,7 +93,7 @@ public void onItemClick(AdapterView<?> parentAdapter, View view, int position, l
9293
Logger.d("Clicked position %d", position);
9394
PlexServer s = (PlexServer)parentAdapter.getItemAtPosition(position);
9495
serverSelectDialog.dismiss();
95-
scanHandler.onDeviceSelected(s);
96+
scanHandler.onDeviceSelected(s, false);
9697
}
9798
});
9899
}
@@ -121,6 +122,10 @@ public void searchForPlexClients() {
121122
}
122123

123124
public void showPlexClients(Map<String, PlexClient> clients) {
125+
showPlexClients(clients, false);
126+
}
127+
128+
public void showPlexClients(Map<String, PlexClient> clients, boolean showResume) {
124129
if(searchDialog != null)
125130
searchDialog.dismiss();
126131
if(serverSelectDialog == null) {
@@ -130,6 +135,11 @@ public void showPlexClients(Map<String, PlexClient> clients) {
130135
serverSelectDialog.setTitle(R.string.select_plex_client);
131136
serverSelectDialog.show();
132137

138+
if(showResume) {
139+
CheckBox resumeCheckbox = (CheckBox)serverSelectDialog.findViewById(R.id.serverListResume);
140+
resumeCheckbox.setVisibility(View.VISIBLE);
141+
}
142+
133143
final ListView serverListView = (ListView)serverSelectDialog.findViewById(R.id.serverListView);
134144
final PlexListAdapter adapter = new PlexListAdapter(context, PlexListAdapter.TYPE_CLIENT);
135145
adapter.setClients(clients);
@@ -141,7 +151,8 @@ public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
141151
long id) {
142152
PlexClient s = (PlexClient)parentAdapter.getItemAtPosition(position);
143153
serverSelectDialog.dismiss();
144-
scanHandler.onDeviceSelected(s);
154+
CheckBox resumeCheckbox = (CheckBox)serverSelectDialog.findViewById(R.id.serverListResume);
155+
scanHandler.onDeviceSelected(s, resumeCheckbox.isChecked());
145156
}
146157

147158
});

Voice Control For Plex/src/main/java/com/atomjack/vcfp/PlexSearchService.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,16 @@ public int onStartCommand(Intent intent, int flags, int startId) {
100100

101101
queries = new ArrayList<String>();
102102

103+
resumePlayback = false;
103104

104105
specifiedServer = gson.fromJson(intent.getStringExtra(VoiceControlForPlexApplication.Intent.EXTRA_SERVER), PlexServer.class);
105106
if(specifiedServer != null)
106107
Logger.d("specified server %s", specifiedServer);
107108
PlexClient thisClient = gson.fromJson(intent.getStringExtra(VoiceControlForPlexApplication.Intent.EXTRA_CLIENT), PlexClient.class);
108109
if(thisClient != null)
109110
client = thisClient;
111+
if(intent.getBooleanExtra(VoiceControlForPlexApplication.Intent.EXTRA_RESUME, false))
112+
resumePlayback = true;
110113

111114
if (intent.getExtras().getStringArrayList(RecognizerIntent.EXTRA_RESULTS) != null) {
112115
Logger.d("internal query");
@@ -246,8 +249,8 @@ private void setClient() {
246249
serversScanned = 0;
247250
clients = new ArrayList<PlexClient>();
248251
for(PlexServer server : plexmediaServers.values()) {
249-
Logger.d("ip: %s", server.address);
250-
Logger.d("port: %s", server.port);
252+
Logger.d("ip: %s", server.activeConnection.address);
253+
Logger.d("port: %s", server.activeConnection.port);
251254

252255
PlexHttpClient.get(server, "/clients", new PlexHttpMediaContainerHandler() {
253256
@Override
@@ -282,7 +285,7 @@ private myRunnable handleVoiceSearch() {
282285
private myRunnable handleVoiceSearch(boolean noChange) {
283286
Logger.d("GOT QUERY: %s", queryText);
284287

285-
resumePlayback = false;
288+
// resumePlayback = false;
286289

287290
Pattern p;
288291
Matcher matcher;
@@ -592,12 +595,14 @@ private void resumePlayback() {
592595

593596
private void stopPlayback() {
594597
adjustPlayback("stop", getResources().getString(R.string.playback_stopped));
595-
Intent stopIntent = new Intent(this, NowPlayingActivity.class);
596-
stopIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
597-
stopIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
598-
stopIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
599-
stopIntent.putExtra("finish", true);
600-
startActivity(stopIntent);
598+
if(VoiceControlForPlexApplication.isNowPlayingVisible()) {
599+
Intent stopIntent = new Intent(this, NowPlayingActivity.class);
600+
stopIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
601+
stopIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
602+
stopIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
603+
stopIntent.putExtra("finish", true);
604+
startActivity(stopIntent);
605+
}
601606
}
602607

603608
private void seekTo(int hours, int minutes, int seconds) {
@@ -789,10 +794,10 @@ private void playVideo(final PlexVideo video, String transientToken) {
789794
Logger.d("machine id: %s", video.server.machineIdentifier);
790795
qs.add("key", video.key);
791796
Logger.d("key: %s", video.key);
792-
qs.add("port", video.server.port);
793-
Logger.d("port: %s", video.server.port);
794-
qs.add("address", video.server.address);
795-
Logger.d("address: %s", video.server.address);
797+
qs.add("port", video.server.activeConnection.port);
798+
Logger.d("port: %s", video.server.activeConnection.port);
799+
qs.add("address", video.server.activeConnection.address);
800+
Logger.d("address: %s", video.server.activeConnection.address);
796801

797802
if(mPrefs.getBoolean("resume", false) || resumePlayback)
798803
qs.add("viewOffset", video.viewOffset);
@@ -1520,8 +1525,8 @@ private void playTrack(final PlexTrack track) {
15201525
private void playTrack(final PlexTrack track, final PlexDirectory album) {
15211526
QueryString qs = new QueryString("machineIdentifier", track.server.machineIdentifier);
15221527
qs.add("key", track.key);
1523-
qs.add("port", track.server.port);
1524-
qs.add("address", track.server.address);
1528+
qs.add("port", track.server.activeConnection.port);
1529+
qs.add("address", track.server.activeConnection.address);
15251530
if(album != null)
15261531
qs.add("containerKey", album.key);
15271532
if(mPrefs.getBoolean("resume", false) || resumePlayback)

Voice Control For Plex/src/main/java/com/atomjack/vcfp/ScanHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
import com.atomjack.vcfp.model.PlexDevice;
44

55
public interface ScanHandler {
6-
void onDeviceSelected(PlexDevice device);
6+
void onDeviceSelected(PlexDevice device, boolean resume);
77
}

Voice Control For Plex/src/main/java/com/atomjack/vcfp/VoiceControlForPlexApplication.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.simpleframework.xml.core.Persister;
1111

1212
import android.app.AlertDialog;
13+
import android.app.Application;
1314
import android.content.Context;
1415
import android.content.DialogInterface;
1516
import android.content.SharedPreferences;
@@ -23,15 +24,18 @@
2324
import com.loopj.android.http.AsyncHttpClient;
2425
import com.loopj.android.http.AsyncHttpResponseHandler;
2526

26-
public class VoiceControlForPlexApplication
27+
public class VoiceControlForPlexApplication extends Application
2728
{
2829
public final static String MINIMUM_PHT_VERSION = "1.0.7";
2930

31+
private static boolean nowPlayingVisible;
32+
3033
public final static class Intent {
3134
public final static String GDMRECEIVE = "com.atomjack.vcfp.intent.gdmreceive";
3235

3336
public final static String EXTRA_SERVER = "com.atomjack.vcfp.intent.extra_server";
3437
public final static String EXTRA_CLIENT = "com.atomjack.vcfp.intent.extra_client";
38+
public final static String EXTRA_RESUME = "com.atomjack.vcfp.intent.extra_resume";
3539

3640
public final static String SCAN_TYPE = "com.atomjack.vcfp.intent.scan_type";
3741
public final static String EXTRA_SERVERS = "com.atomjack.vcfp.intent.extra_servers";
@@ -148,4 +152,16 @@ public void onClick(DialogInterface dialog, int id) {
148152
});
149153
usageDialog.show();
150154
}
155+
156+
public static boolean isNowPlayingVisible() {
157+
return nowPlayingVisible;
158+
}
159+
160+
public static void nowPlayingResumed() {
161+
nowPlayingVisible = true;
162+
}
163+
164+
public static void nowPlayingPaused() {
165+
nowPlayingVisible = false;
166+
}
151167
}

Voice Control For Plex/src/main/java/com/atomjack/vcfp/activities/MainActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ protected void onCreate(Bundle savedInstanceState) {
154154

155155
localScan = new LocalScan(this, MainActivity.class, mPrefs, new ScanHandler() {
156156
@Override
157-
public void onDeviceSelected(PlexDevice device) {
157+
public void onDeviceSelected(PlexDevice device, boolean resume) {
158158
if(device instanceof PlexServer)
159159
setServer((PlexServer)device);
160160
else if(device instanceof PlexClient)
@@ -467,6 +467,7 @@ public void logout(MenuItem item) {
467467
// If the currently selected server is not local, reset it to scan all.
468468
if(!server.local) {
469469
server = new PlexServer(getString(R.string.scan_all));
470+
saveSettings();
470471
initMainWithServer();
471472
}
472473

Voice Control For Plex/src/main/java/com/atomjack/vcfp/activities/NowPlayingActivity.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ public void run() {
167167

168168
try {
169169

170+
if(serverSocket == null)
171+
return;
170172
socket = serverSocket.accept();
171173

172174
Map<String, String> headers = new HashMap<String, String>();
@@ -254,14 +256,28 @@ else if(playingTrack != null)
254256
unsubscribe(new Runnable() {
255257
@Override
256258
public void run() {
257-
finish();
259+
if(VoiceControlForPlexApplication.isNowPlayingVisible())
260+
finish();
258261
}
259262
});
260263
}
261264
}
262265
}
263266
}
264267

268+
@Override
269+
protected void onPause() {
270+
super.onPause();
271+
VoiceControlForPlexApplication.nowPlayingPaused();
272+
Logger.d("now playing paused");
273+
}
274+
275+
@Override
276+
protected void onResume() {
277+
super.onResume();
278+
VoiceControlForPlexApplication.nowPlayingResumed();
279+
}
280+
265281
private void subscribe() {
266282
QueryString qs = new QueryString("port", String.valueOf(subscriptionPort));
267283
qs.add("commandID", String.valueOf(commandId));

Voice Control For Plex/src/main/java/com/atomjack/vcfp/activities/ShortcutActivity.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,38 @@
77
import android.os.Bundle;
88
import android.speech.RecognizerIntent;
99

10+
import com.atomjack.vcfp.Logger;
1011
import com.atomjack.vcfp.PlexSearchService;
1112
import com.atomjack.vcfp.R;
1213
import com.atomjack.vcfp.VoiceControlForPlexApplication;
14+
import com.atomjack.vcfp.model.Connection;
15+
import com.atomjack.vcfp.model.PlexServer;
16+
import com.google.gson.Gson;
1317

1418
import java.math.BigInteger;
1519
import java.security.SecureRandom;
20+
import java.util.ArrayList;
1621

1722
public class ShortcutActivity extends Activity {
1823
@Override
1924
protected void onCreate(Bundle savedInstanceState) {
2025
super.onCreate(savedInstanceState);
2126
Intent serviceIntent = new Intent(getApplicationContext(), PlexSearchService.class);
2227

23-
serviceIntent.putExtra(VoiceControlForPlexApplication.Intent.EXTRA_SERVER, getIntent().getStringExtra(VoiceControlForPlexApplication.Intent.EXTRA_SERVER));
28+
// Shortcuts created before multiple connections were supported will not have any connections at all. So let's add one to the server
29+
// this shortcut was created for, composed of the server's address and port.
30+
Gson gson = new Gson();
31+
PlexServer server = gson.fromJson(getIntent().getStringExtra(VoiceControlForPlexApplication.Intent.EXTRA_SERVER), PlexServer.class);
32+
if(server != null) {
33+
if (server.connections.size() == 0) {
34+
server.connections = new ArrayList<Connection>();
35+
server.connections.add(new Connection("http", server.address, server.port));
36+
}
37+
}
38+
39+
serviceIntent.putExtra(VoiceControlForPlexApplication.Intent.EXTRA_SERVER, gson.toJson(server));
2440
serviceIntent.putExtra(VoiceControlForPlexApplication.Intent.EXTRA_CLIENT, getIntent().getStringExtra(VoiceControlForPlexApplication.Intent.EXTRA_CLIENT));
25-
41+
serviceIntent.putExtra(VoiceControlForPlexApplication.Intent.EXTRA_RESUME, getIntent().getBooleanExtra(VoiceControlForPlexApplication.Intent.EXTRA_RESUME, false));
2642

2743
SecureRandom random = new SecureRandom();
2844
serviceIntent.setData(Uri.parse(new BigInteger(130, random).toString(32)));

Voice Control For Plex/src/main/java/com/atomjack/vcfp/activities/ShortcutProviderActivity.java

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class ShortcutProviderActivity extends Activity {
4141
ConcurrentHashMap<String, PlexServer> servers;
4242
HashMap<String, PlexClient> clients;
4343

44+
private boolean resume = false;
45+
4446
@Override
4547
protected void onCreate(Bundle savedInstanceState) {
4648
super.onCreate(savedInstanceState);
@@ -51,13 +53,15 @@ protected void onCreate(Bundle savedInstanceState) {
5153

5254
localScan = new LocalScan(this, ShortcutProviderActivity.class, null, new ScanHandler() {
5355
@Override
54-
public void onDeviceSelected(PlexDevice device) {
56+
public void onDeviceSelected(PlexDevice device, boolean _resume) {
5557
Logger.d("chose %s", device.name);
5658
if(device instanceof PlexServer) {
5759
server = (PlexServer)device;
58-
localScan.showPlexClients(clients);
60+
localScan.showPlexClients(clients, true);
5961
} else if(device instanceof PlexClient) {
6062
client = (PlexClient)device;
63+
resume = _resume;
64+
Logger.d("checked: %s", resume);
6165
createShortcut(false);
6266
}
6367
}
@@ -83,7 +87,6 @@ public void onDeviceSelected(PlexDevice device) {
8387
public void onClick(DialogInterface dialog, int id) {
8488
dialog.dismiss();
8589
localScan.showPlexServers(servers);
86-
// selectServer();
8790
}
8891
});
8992
}
@@ -113,6 +116,7 @@ private void createShortcut(boolean use_current) {
113116
Logger.d("setting client to %s", client.name);
114117
launchIntent.putExtra(VoiceControlForPlexApplication.Intent.EXTRA_SERVER, gson.toJson(server));
115118
launchIntent.putExtra(VoiceControlForPlexApplication.Intent.EXTRA_CLIENT, gson.toJson(client));
119+
launchIntent.putExtra(VoiceControlForPlexApplication.Intent.EXTRA_RESUME, resume);
116120
String label = server.name.equals(client.name) ? server.name : (server.owned ? server.name : server.sourceTitle) + "/" + client.name;
117121
sendIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
118122
} else
@@ -126,27 +130,6 @@ private void createShortcut(boolean use_current) {
126130

127131
finish();
128132
}
129-
/*
130-
@Override
131-
public void onNewIntent(Intent intent) {
132-
Logger.d("on new intent in ShortcutProvider");
133-
Logger.d("Got " + VoiceControlForPlexApplication.getPlexMediaServers().size() + " servers");
134-
135-
Intent.ShortcutIconResource icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
136-
137-
Intent sendIntent = new Intent();
138-
139-
Intent launchIntent = new Intent(this, ShortcutActivity.class);
140-
141-
sendIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
142-
sendIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name));
143-
sendIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
144-
145-
setResult(RESULT_OK, sendIntent);
146-
147-
finish();
148-
}
149-
*/
150133

151134
@Override
152135
protected void onDestroy() {

Voice Control For Plex/src/main/java/com/atomjack/vcfp/adapters/PlexListAdapter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.util.Map;
44
import java.util.concurrent.ConcurrentHashMap;
55

6-
import android.app.Dialog;
76
import android.content.Context;
87
import android.view.LayoutInflater;
98
import android.view.View;

0 commit comments

Comments
 (0)