3
3
4
4
import android .content .Intent ;
5
5
import android .graphics .Bitmap ;
6
- import android .net . Uri ;
6
+ import android .os . Handler ;
7
7
import android .speech .RecognizerIntent ;
8
8
9
9
import com .atomjack .shared .Logger ;
10
10
import com .atomjack .shared .SendToDataLayerThread ;
11
- import com .atomjack .shared .UriSerializer ;
12
11
import com .atomjack .shared .WearConstants ;
13
- import com .atomjack .shared .UriDeserializer ;
14
12
import com .atomjack .vcfp .CastPlayerManager ;
15
13
import com .atomjack .shared .PlayerState ;
16
- import com .atomjack .vcfp .PlexHeaders ;
17
14
import com .atomjack .vcfp .PlexSubscription ;
18
15
import com .atomjack .vcfp .VoiceControlForPlexApplication ;
16
+ import com .atomjack .vcfp .activities .CastActivity ;
19
17
import com .atomjack .vcfp .activities .MainActivity ;
20
18
import com .atomjack .vcfp .activities .NowPlayingActivity ;
21
19
import com .atomjack .vcfp .activities .VCFPActivity ;
22
20
import com .atomjack .vcfp .interfaces .BitmapHandler ;
23
- import com .atomjack .vcfp .model .MediaContainer ;
24
21
import com .atomjack .vcfp .model .PlexClient ;
25
- import com .atomjack .shared .model .Timeline ;
26
22
import com .atomjack .vcfp .model .PlexMedia ;
27
- import com .atomjack .vcfp .net .PlexHttpClient ;
28
23
import com .google .android .gms .common .api .GoogleApiClient ;
29
- import com .google .android .gms .wearable .Asset ;
30
- import com .google .android .gms .wearable .DataEvent ;
31
- import com .google .android .gms .wearable .DataEventBuffer ;
32
24
import com .google .android .gms .wearable .DataMap ;
33
- import com .google .android .gms .wearable .DataMapItem ;
34
25
import com .google .android .gms .wearable .MessageEvent ;
35
26
import com .google .android .gms .wearable .Wearable ;
36
27
import com .google .android .gms .wearable .WearableListenerService ;
37
- import com .google .gson .Gson ;
38
- import com .google .gson .GsonBuilder ;
39
- import com .google .gson .reflect .TypeToken ;
40
-
41
- import org .apache .http .Header ;
42
- import org .apache .http .message .BasicHeader ;
43
-
44
- import java .io .ByteArrayOutputStream ;
45
- import java .lang .reflect .Type ;
46
- import java .util .ArrayList ;
47
- import java .util .Hashtable ;
48
28
49
29
public class WearListenerService extends WearableListenerService {
50
30
51
- protected Gson gsonRead = new GsonBuilder ()
52
- .registerTypeAdapter (Uri .class , new UriDeserializer ())
53
- .create ();
54
-
55
- protected Gson gsonWrite = new GsonBuilder ()
56
- .registerTypeAdapter (Uri .class , new UriSerializer ())
57
- .create ();
58
-
59
31
GoogleApiClient googleApiClient ;
60
32
33
+ Handler handler = new Handler ();
34
+ // How many times we've had the wear request playback state.
35
+ int playbackStateRetries = 0 ;
36
+
61
37
@ Override
62
38
public void onCreate () {
63
39
super .onCreate ();
@@ -67,39 +43,38 @@ public void onCreate() {
67
43
googleApiClient .connect ();
68
44
}
69
45
70
- // @Override
71
- // public void onDataChanged(DataEventBuffer dataEvents) {
72
- // Logger.d("[WearListenerService] onDataChanged");
73
- // DataMap dataMap;
74
- // for (DataEvent event : dataEvents) {
75
- // dataMap = DataMapItem.fromDataItem(event.getDataItem()).getDataMap();
76
- // // Check the data type
77
- // if (event.getType() == DataEvent.TYPE_CHANGED) {
78
- // // Check the data path
79
- // String path = event.getDataItem().getUri().getPath();
80
- // if (path.equals(WearConstants.GET_PLAYBACK_STATE)) {
81
- // Logger.d("GET_PLAYBACK_STATE");
82
- // }
83
- // }
84
- // }
85
- //
86
- // }
87
-
88
46
@ Override
89
47
public void onMessageReceived (MessageEvent messageEvent ) {
90
48
String message = messageEvent .getPath () == null ? "" : messageEvent .getPath ();
91
49
Logger .d ("[WearListenerService] onMessageReceived: %s" , message );
50
+ if (!VoiceControlForPlexApplication .getInstance ().getInventoryQueried () && message .equals (WearConstants .GET_PLAYBACK_STATE )) {
51
+ // This message was received before we've had a chance to check with Google on whether or not Wear support
52
+ // has been purchased. After a delay of 500ms, send a message back to the Wearable to get playback state again.
53
+ // By then, it should have had time to see if Wear Support has been purchased.
54
+ playbackStateRetries ++;
55
+ if (playbackStateRetries < 4 ) {
56
+ handler .postDelayed (new Runnable () {
57
+ @ Override
58
+ public void run () {
59
+ new SendToDataLayerThread (WearConstants .RETRY_GET_PLAYBACK_STATE , WearListenerService .this ).start ();
60
+ }
61
+ }, 500 );
62
+ return ;
63
+ }
64
+ }
92
65
if (!VoiceControlForPlexApplication .getInstance ().hasWear () && !message .equals (WearConstants .PONG )) {
93
66
// Wear support has not been purchased, so send a message back to the wear device, and show the purchase required
94
67
// popup on the handheld. However, if the message is 'pong', a response to a 'ping', skip since we want to react to a pong
95
68
// even if wear support has not been purchased (so we can alert the user to the option to purchase)
96
69
new SendToDataLayerThread (WearConstants .WEAR_UNAUTHORIZED , this ).start ();
97
- Intent intent = new Intent (this , MainActivity .class );
98
- intent .setAction (com .atomjack .shared .Intent .SHOW_WEAR_PURCHASE_REQUIRED );
99
- intent .addFlags (Intent .FLAG_FROM_BACKGROUND );
100
- intent .addFlags (Intent .FLAG_ACTIVITY_SINGLE_TOP );
101
- intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
102
- startActivity (intent );
70
+ if (VoiceControlForPlexApplication .isApplicationVisible ()) {
71
+ Intent intent = new Intent (this , MainActivity .class );
72
+ intent .setAction (com .atomjack .shared .Intent .SHOW_WEAR_PURCHASE_REQUIRED );
73
+ intent .addFlags (Intent .FLAG_FROM_BACKGROUND );
74
+ intent .addFlags (Intent .FLAG_ACTIVITY_SINGLE_TOP );
75
+ intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
76
+ startActivity (intent );
77
+ }
103
78
return ;
104
79
}
105
80
if (messageEvent .getPath () != null ) {
@@ -176,22 +151,26 @@ public void onSuccess(Bitmap bitmap) {
176
151
177
152
} else if (message .equals (WearConstants .GET_PLAYING_MEDIA )) {
178
153
// Send an intent to NowPlayingActivity to tell it to forward on information about the currently playing media back to the wear device
179
- Intent intent = new Intent (this , NowPlayingActivity .class );
180
- intent .setAction (com .atomjack .shared .Intent .GET_PLAYING_MEDIA );
181
- intent .addFlags (Intent .FLAG_FROM_BACKGROUND );
182
- intent .addFlags (Intent .FLAG_ACTIVITY_SINGLE_TOP );
183
- intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
184
- startActivity (intent );
154
+ if (VoiceControlForPlexApplication .isApplicationVisible ()) {
155
+ Class theClass = castPlayerManager .isSubscribed () ? CastActivity .class : NowPlayingActivity .class ;
156
+ Intent intent = new Intent (this , theClass );
157
+ intent .setAction (com .atomjack .shared .Intent .GET_PLAYING_MEDIA );
158
+ intent .addFlags (Intent .FLAG_FROM_BACKGROUND );
159
+ intent .addFlags (Intent .FLAG_ACTIVITY_SINGLE_TOP );
160
+ intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
161
+ startActivity (intent );
162
+ }
185
163
} else if (message .equals (WearConstants .PONG )) {
186
164
// Received a pong back from the user, so show a popup allowing the user to purchase wear support.
187
165
Logger .d ("[WearListenerService] Received pong" );
188
-
189
- Intent intent = new Intent (this , MainActivity .class );
190
- intent .setAction (com .atomjack .shared .Intent .SHOW_WEAR_PURCHASE );
191
- intent .addFlags (Intent .FLAG_FROM_BACKGROUND );
192
- intent .addFlags (Intent .FLAG_ACTIVITY_SINGLE_TOP );
193
- intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
194
- startActivity (intent );
166
+ if (VoiceControlForPlexApplication .isApplicationVisible ()) {
167
+ Intent intent = new Intent (this , MainActivity .class );
168
+ intent .setAction (com .atomjack .shared .Intent .SHOW_WEAR_PURCHASE );
169
+ intent .addFlags (Intent .FLAG_FROM_BACKGROUND );
170
+ intent .addFlags (Intent .FLAG_ACTIVITY_SINGLE_TOP );
171
+ intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
172
+ startActivity (intent );
173
+ }
195
174
} else if (message .equals (WearConstants .ACTION_PAUSE ) || message .equals (WearConstants .ACTION_PLAY ) || message .equals (WearConstants .ACTION_STOP )) {
196
175
Intent intent = new android .content .Intent (this , PlexControlService .class );
197
176
intent .setAction (message );
0 commit comments