-
Notifications
You must be signed in to change notification settings - Fork 0
/
MusicPlayingActivity.java
185 lines (134 loc) · 6.27 KB
/
MusicPlayingActivity.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.example.mark.SongFeed;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
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 android.widget.Toast;
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;
public class MusicPlayingActivity extends AppCompatActivity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
public static final String TAG = "track";
public static final String SERVICECMD = "com.android.music.musicservicecommand";
GoogleApiClient mGoogleApiClient;
Message mActiveMessage;
Typeface typeface;
TextView artist;
TextView album;
TextView title;
private void publish(String message) {
try {
Log.i(TAG, "Publishing message: " + message);
mActiveMessage = new Message(message.getBytes());
Nearby.Messages.publish(mGoogleApiClient, mActiveMessage);
}catch(RuntimeException r){
Context context = getApplicationContext();
CharSequence text = r+" Cannot publish. Try launch music player first, then open SongFeed!";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
private void unpublish() {
Log.i(TAG, "Unpublishing.");
if (mActiveMessage != null)
{
Nearby.Messages.unpublish(mGoogleApiClient, mActiveMessage);
mActiveMessage = null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Nearby.MESSAGES_API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.enableAutoManage(this, this)
.build();
setTitle("SongFeed");
typeface = Typeface.createFromAsset(getAssets(), "fonts/Sawasdee.ttf");
setContentView(R.layout.activity_music_playing);
artist = (TextView) findViewById(R.id.artist);
album = (TextView) findViewById(R.id.album);
title = (TextView) findViewById(R.id.title);
artist.setTypeface(typeface);
album.setTypeface(typeface);
title.setTypeface(typeface);
IntentFilter iF = new IntentFilter();
iF.addAction("com.android.music.metachanged");
iF.addAction("com.android.music.playstatechanged");
iF.addAction("com.android.music.playbackcomplete"); //intentFilters assigned to music-playing activities in other applications
iF.addAction("com.android.music.queuechanged");
iF.addAction("com.android.music.musicservicecommand.next");
iF.addAction("com.android.music.musicservicecommand.previous");
/*
iF.addAction("com.htc.music.metachanged");
iF.addAction("fm.last.android.metachanged");
iF.addAction("com.sec.android.app.music.metachanged");
iF.addAction("com.nullsoft.winamp.metachanged");
iF.addAction("com.amazon.mp3.metachanged");
iF.addAction("com.miui.player.metachanged");
iF.addAction("com.real.IMP.metachanged");
iF.addAction("com.sonyericsson.music.metachanged");
iF.addAction("com.rdio.android.metachanged");
iF.addAction("com.samsung.sec.android.MusicPlayer.metachanged");
iF.addAction("com.andrew.apollo.metachanged");
*/
registerReceiver(mReceiver, iF); //BroadcastReceiver listens for data broadcast by other activities
}
public BroadcastReceiver mReceiver = new BroadcastReceiver() { //new instance of BroadcastReceiver
//@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
String cmd = intent.getStringExtra("command");
Log.v("tag ",action+" / "+cmd);
String artists = intent.getStringExtra("artist"); //receiving data through IntentFilters, converting received data to String
String albums = intent.getStringExtra("album");
String tracks = intent.getStringExtra("track");
Log.v("music ", artists + ":" + albums + ":" + tracks);
artist.setText(artists);
album.setText(albums); //setting String data to TextView
title.setText(tracks);
publish("Someone is listening to "+tracks+" from "+albums+" by "+artists);
unregisterReceiver(mReceiver);
}
};
@Override
public void onConnected(Bundle connectionHint) {
}
@Override
public void onStop() {
unpublish();
super.onStop();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
protected void onSaveInstanceState(Bundle savedInstanceState){
MusicPlayingActivity.super.onSaveInstanceState(savedInstanceState); //saves data before screen orientation changes
savedInstanceState.putString("saved_message1", artist.getText().toString());
savedInstanceState.putString("saved_message2", album.getText().toString());
savedInstanceState.putString("saved_message3", title.getText().toString());
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
MusicPlayingActivity.super.onRestoreInstanceState(savedInstanceState);
artist.setText(savedInstanceState.getString("saved_message1")); //loads saved data after screen orientation changes
album.setText(savedInstanceState.getString("saved_message2"));
title.setText(savedInstanceState.getString("saved_message3"));
}
}