-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubActivity.java
153 lines (107 loc) · 4.4 KB
/
SubActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.example.mark.SongFeed;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.nearby.Nearby;
import com.google.android.gms.nearby.messages.Message;
import com.google.android.gms.nearby.messages.MessageListener;
import com.google.android.gms.nearby.messages.SubscribeOptions;
import static com.example.mark.SongFeed.MusicPlayingActivity.TAG;
public class SubActivity extends AppCompatActivity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
GoogleApiClient nGoogleApiClient;
MessageListener mMessageListener;
SubscribeOptions options;
TextView track1;
TextView album1;
TextView artist1;
Typeface typeface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pub_sub);
setTitle("SongFeed");
/*
the Google API Client allows integration with Google Play Services which]
in turn, allow access to all the Google APIs including Nearby Messages
*/
nGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Nearby.MESSAGES_API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.enableAutoManage(this, this)
.build();
options = new SubscribeOptions.Builder()
//.setCallback(myCallback)
.build();
typeface = Typeface.createFromAsset(getAssets(), "fonts/Sawasdee.ttf"); //changes the font of the text
//data to receive
track1 = (TextView) findViewById(R.id.track1);
album1 = (TextView) findViewById(R.id.album1); //defining the text fields as variables
artist1 = (TextView) findViewById(R.id.artist1);
track1.setTypeface(typeface);
album1.setTypeface(typeface); //sets the text to the font above
artist1.setTypeface(typeface);
///////////////////////////////////////////////////////////////////////////////////////////////
//Track
mMessageListener = new MessageListener() {
@Override
public void onFound(Message message) {
String messageAsString = new String(message.getContent());
Log.i(TAG, "Found message: " + messageAsString);
track1.setText(messageAsString);
}
@Override
public void onLost(Message message) {
String messageAsString = new String(message.getContent());
Log.i(TAG, "Lost sight of message: " + messageAsString);
}
};
}
// Subscribe to receive messages.
private void subscribe() {
try{
Log.i(TAG, "Subscribing.");
Nearby.Messages.subscribe(nGoogleApiClient, mMessageListener, options );
}
catch (NullPointerException n){
n.printStackTrace();
}
}
private void unsubscribe() {
Log.i(TAG, "Unsubscribing.");
Nearby.Messages.unsubscribe(nGoogleApiClient, mMessageListener);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//put subscribed data in try/catch statement- catch unknown track/album/artist display as textview
protected void onSaveInstanceState(Bundle savedInstanceState){
SubActivity.super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString("saved_message", track1.getText().toString());
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
SubActivity.super.onRestoreInstanceState(savedInstanceState);
track1.setText(savedInstanceState.getString("saved_message"));
}
@Override
public void onConnected(Bundle b) {
subscribe();
}
@Override
public void onStop() {
unsubscribe();
super.onStop();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}