Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Commit 95dc718

Browse files
committed
moved broadcast receivers to onCreate, improved the error dialog, bigger touch target on "more" menu
1 parent baa33bb commit 95dc718

File tree

24 files changed

+145
-91
lines changed

24 files changed

+145
-91
lines changed

app/app.iml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@
114114
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
115115
<orderEntry type="sourceFolder" forTests="false" />
116116
<orderEntry type="library" exported="" name="design-23.4.0" level="project" />
117-
<orderEntry type="library" exported="" name="appcompat-v7-23.4.0" level="project" />
118117
<orderEntry type="library" exported="" name="support-annotations-23.4.0" level="project" />
119118
<orderEntry type="library" exported="" name="animated-vector-drawable-23.4.0" level="project" />
120-
<orderEntry type="library" exported="" name="support-vector-drawable-23.4.0" level="project" />
121-
<orderEntry type="library" exported="" name="recyclerview-v7-23.4.0" level="project" />
122119
<orderEntry type="library" exported="" name="support-v4-23.4.0" level="project" />
120+
<orderEntry type="library" exported="" name="recyclerview-v7-23.4.0" level="project" />
121+
<orderEntry type="library" exported="" name="support-vector-drawable-23.4.0" level="project" />
122+
<orderEntry type="library" exported="" name="appcompat-v7-23.4.0" level="project" />
123123
</component>
124124
</module>

app/src/main/java/org/y20k/transistor/MainActivity.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public final class MainActivity extends AppCompatActivity {
5353
private File mFolder;
5454
private Collection mCollection;
5555
private View mContainer;
56+
private BroadcastReceiver mCollectionChangedReceiver;
5657

5758

5859
@Override
@@ -71,19 +72,20 @@ protected void onCreate(Bundle savedInstanceState) {
7172
mIntent.putExtra(TransistorKeys.EXTRA_COLLECTION, mCollection);
7273
}
7374

74-
// initialize broadcast receivers
75-
initializeBroadcastReceivers();
76-
7775
// set layout
7876
setContentView(R.layout.activity_main);
7977

78+
// initialize broadcast receivers
79+
initializeBroadcastReceivers();
80+
8081
}
8182

8283

8384
@Override
8485
protected void onResume() {
8586
super.onResume();
8687

88+
8789
// if player_container is present two-pane layout has been loaded
8890
mContainer = findViewById(R.id.player_container);
8991

@@ -122,6 +124,13 @@ protected void onResume() {
122124
}
123125

124126

127+
@Override
128+
protected void onDestroy() {
129+
super.onDestroy();
130+
unregisterBroadcastReceivers();
131+
}
132+
133+
125134
@Override
126135
protected void onNewIntent(Intent intent) {
127136
super.onNewIntent(intent);
@@ -235,10 +244,16 @@ private void saveAppState(Context context) {
235244
}
236245

237246

247+
/* Unregisters broadcast receivers */
248+
private void unregisterBroadcastReceivers() {
249+
LocalBroadcastManager.getInstance(this).unregisterReceiver(mCollectionChangedReceiver);
250+
}
251+
252+
238253
/* Initializes broadcast receivers for onCreate */
239254
private void initializeBroadcastReceivers() {
240255
// RECEIVER: station added, deleted, or changed
241-
BroadcastReceiver collectionChangedReceiver = new BroadcastReceiver() {
256+
mCollectionChangedReceiver = new BroadcastReceiver() {
242257
@Override
243258
public void onReceive(Context context, Intent intent) {
244259

@@ -264,7 +279,7 @@ public void onReceive(Context context, Intent intent) {
264279
}
265280
};
266281
IntentFilter collectionChangedIntentFilter = new IntentFilter(TransistorKeys.ACTION_COLLECTION_CHANGED);
267-
LocalBroadcastManager.getInstance(this).registerReceiver(collectionChangedReceiver, collectionChangedIntentFilter);
282+
LocalBroadcastManager.getInstance(this).registerReceiver(mCollectionChangedReceiver, collectionChangedIntentFilter);
268283
}
269284

270285
}

app/src/main/java/org/y20k/transistor/MainActivityFragment.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ public void onCreate(Bundle savedInstanceState) {
144144
mStationImages = new ArrayList<>();
145145
mCollectionAdapter = new CollectionAdapter(mActivity, mCollection, mStationNames, mStationImages);
146146

147-
// // initialize broadcast receivers
148-
// initializeBroadcastReceivers();
147+
// initialize broadcast receivers
148+
initializeBroadcastReceivers();
149149

150150
}
151151

@@ -192,9 +192,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
192192
public void onResume() {
193193
super.onResume();
194194

195-
// initialize broadcast receivers
196-
initializeBroadcastReceivers();
197-
198195
// handle incoming intent
199196
handleIncomingIntent();
200197

@@ -208,14 +205,6 @@ public void onResume() {
208205
}
209206

210207

211-
@Override
212-
public void onPause() {
213-
super.onPause();
214-
// unregister broadcast receivers
215-
unregisterBroadcastReceivers();
216-
}
217-
218-
219208
@Override
220209
public void onStart() {
221210
super.onStart();
@@ -225,6 +214,13 @@ public void onStart() {
225214
}
226215

227216

217+
@Override
218+
public void onDestroy() {
219+
super.onDestroy();
220+
unregisterBroadcastReceivers();
221+
}
222+
223+
228224
@Override
229225
public boolean onOptionsItemSelected(MenuItem item) {
230226

app/src/main/java/org/y20k/transistor/PlayerActivity.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,13 @@ protected void onCreate(Bundle savedInstanceState) {
6767
if (intent.hasExtra(TransistorKeys.EXTRA_PLAYBACK_STATE)) {
6868
startPlayback = intent.getBooleanExtra(TransistorKeys.EXTRA_PLAYBACK_STATE, false);
6969

70-
// Get a support ActionBar corresponding to this toolbar
71-
ActionBar ab = getSupportActionBar();
70+
// get a support ActionBar corresponding to this toolbar
71+
ActionBar actionBar = getSupportActionBar();
7272

73-
// Enable the Up button
74-
ab.setDisplayHomeAsUpEnabled(true);
75-
76-
// // artificial back stack.
77-
// TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
78-
// stackBuilder.addParentStack(MainActivity.class);
79-
// stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
73+
// enable the Up button
74+
if (actionBar != null ) {
75+
actionBar.setDisplayHomeAsUpEnabled(true);
76+
}
8077

8178

8279
} else {

app/src/main/java/org/y20k/transistor/PlayerActivityFragment.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public void onClick(View view) {
232232
public void onViewCreated(View view, Bundle savedInstanceState) {
233233
super.onViewCreated(view, savedInstanceState);
234234
// initialize broadcast receivers
235-
// initializeBroadcastReceivers();
235+
initializeBroadcastReceivers();
236236

237237
}
238238

@@ -243,9 +243,6 @@ public void onResume() {
243243
// set fragment visibility
244244
mVisibility = true;
245245

246-
// initialize broadcast receivers
247-
initializeBroadcastReceivers();
248-
249246
// refresh playback state
250247
loadAppState(mActivity);
251248

@@ -279,10 +276,13 @@ public void onPause() {
279276
super.onPause();
280277
// set fragment visibility
281278
mVisibility = false;
279+
}
282280

283-
// unregister broadcast receivers
284-
unregisterBroadcastReceivers();
285281

282+
@Override
283+
public void onDestroy() {
284+
super.onDestroy();
285+
unregisterBroadcastReceivers();
286286
}
287287

288288

app/src/main/java/org/y20k/transistor/PlayerService.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,6 @@ public void onPrepared(MediaPlayer mp) {
288288
@Override
289289
public boolean onError(MediaPlayer mp, int what, int extra) {
290290

291-
// restart playback on error
292-
Log.i(LOG_TAG, "Trying to resume playback after error. Player instance count: " + mPlayerInstanceCounter);
293-
mp.reset();
294-
mPlayerInstanceCounter++;
295-
initializeMediaPlayer();
296-
297291
switch (what) {
298292
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
299293
Log.e(LOG_TAG, "Unknown media playback error");
@@ -324,6 +318,11 @@ public boolean onError(MediaPlayer mp, int what, int extra) {
324318
break;
325319
}
326320

321+
322+
// stop playback
323+
stopPlayback();
324+
// TODO try to reconnect to stream - limited to three attempts
325+
327326
return true;
328327
}
329328

app/src/main/java/org/y20k/transistor/core/Station.java

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ public Station(File folder, URL fileLocation) {
111111
mStreamUri = Uri.parse(fileLocation.toString().trim());
112112
mStationName = getStationName(fileLocation);
113113
// save results
114-
mStationFetchResults.putString(TransistorKeys.RESULT_PLAYLIST_TYPE, "none");
115-
mStationFetchResults.putString(TransistorKeys.RESULT_STREAM_TYPE, contentType.toString());
114+
mStationFetchResults.putParcelable(TransistorKeys.RESULT_STREAM_TYPE, contentType);
116115
mStationFetchResults.putBoolean(TransistorKeys.RESULT_FETCH_ERROR, false);
117116
}
118117

@@ -125,18 +124,26 @@ else if (isPlaylist(contentType)) {
125124
if (parse(mPlaylistFileContent) && mStreamUri != null) {
126125
mStationName = getStationName(fileLocation);
127126
// save results
128-
mStationFetchResults.putString(TransistorKeys.RESULT_PLAYLIST_TYPE, contentType.toString());
129-
mStationFetchResults.putString(TransistorKeys.RESULT_STREAM_TYPE, getContentType(mStreamUri).toString());
127+
mStationFetchResults.putParcelable(TransistorKeys.RESULT_PLAYLIST_TYPE, contentType);
128+
mStationFetchResults.putParcelable(TransistorKeys.RESULT_STREAM_TYPE, getContentType(mStreamUri));
130129
mStationFetchResults.putString(TransistorKeys.RESULT_FILE_CONTENT, mPlaylistFileContent);
131130
mStationFetchResults.putBoolean(TransistorKeys.RESULT_FETCH_ERROR, false);
132131

133132
} else {
134133
// save error flag and file content in results
134+
mStationFetchResults.putParcelable(TransistorKeys.RESULT_PLAYLIST_TYPE, contentType);
135135
mStationFetchResults.putString(TransistorKeys.RESULT_FILE_CONTENT, "\n[File probably does not contain a valid streaming URL.]");
136136
mStationFetchResults.putBoolean(TransistorKeys.RESULT_FETCH_ERROR, true);
137137
}
138138

139139
// content type is none of the above
140+
} else if (contentType != null) {
141+
// save results and return
142+
mStationFetchResults.putParcelable(TransistorKeys.RESULT_STREAM_TYPE, contentType);
143+
mStationFetchResults.putBoolean(TransistorKeys.RESULT_FETCH_ERROR, true);
144+
return;
145+
146+
// no content type
140147
} else {
141148
// save error flag in results and return
142149
mStationFetchResults.putBoolean(TransistorKeys.RESULT_FETCH_ERROR, true);
@@ -171,7 +178,6 @@ public Station(File folder, Uri fileLocation) {
171178
// parse the raw content of playlist file (mPlaylistFileContent)
172179
if (parse(mPlaylistFileContent) && mStreamUri != null) {
173180
// save results
174-
mStationFetchResults.putString(TransistorKeys.RESULT_PLAYLIST_TYPE, "local");
175181
mStationFetchResults.putString(TransistorKeys.RESULT_STREAM_TYPE, getContentType(mStreamUri).toString());
176182
mStationFetchResults.putString(TransistorKeys.RESULT_FILE_CONTENT, mPlaylistFileContent);
177183
mStationFetchResults.putBoolean(TransistorKeys.RESULT_FETCH_ERROR, false);
@@ -618,14 +624,51 @@ public void writeToParcel(Parcel dest, int flags) {
618624
* Container class representing the content-type and charset string
619625
* received from the response header of an HTTP server.
620626
*/
621-
private class ContentType {
627+
public class ContentType implements Parcelable {
622628
String type;
623629
String charset;
624630

631+
632+
/* Constructor (default) */
633+
public ContentType() {
634+
}
635+
636+
/* Constructor used by CREATOR */
637+
protected ContentType(Parcel in) {
638+
type = in.readString();
639+
charset = in.readString();
640+
}
641+
642+
/* CREATOR for ContentType object used to do parcel related operations */
643+
public final Creator<ContentType> CREATOR = new Creator<ContentType>() {
644+
@Override
645+
public ContentType createFromParcel(Parcel in) {
646+
return new ContentType(in);
647+
}
648+
649+
@Override
650+
public ContentType[] newArray(int size) {
651+
return new ContentType[size];
652+
}
653+
};
654+
655+
@Override
656+
public int describeContents() {
657+
return 0;
658+
}
659+
660+
@Override
661+
public void writeToParcel(Parcel dest, int flags) {
662+
dest.writeString(type);
663+
dest.writeString(charset);
664+
}
665+
625666
@Override
626667
public String toString() {
627668
return "ContentType{type='" + type + "'" + ", charset='" + charset + "'}";
628669
}
670+
671+
629672
}
630673

631674
}

app/src/main/java/org/y20k/transistor/helpers/StationFetcher.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,10 @@ public StationFetcher(Activity activity, Collection collection, Uri stationUri)
6262
mFolder = mCollection.getFolder();
6363
mFolderExists = mFolder.exists();
6464

65-
// notify user
66-
String stationUriString = stationUri.toString();
67-
if (stationUriString.length() >= 48) {
68-
stationUriString = stationUriString.substring(0,45) + "...";
69-
}
7065
if (stationUri != null && mStationUriScheme != null && mStationUriScheme.startsWith("http")) {
71-
Toast.makeText(mActivity, mActivity.getString(R.string.toastmessage_add_download_started) + " " + stationUriString, Toast.LENGTH_LONG).show();
66+
Toast.makeText(mActivity, mActivity.getString(R.string.toastmessage_add_download_started), Toast.LENGTH_LONG).show();
7267
} else if (stationUri != null && mStationUriScheme != null && mStationUriScheme.startsWith("file")) {
73-
Toast.makeText(mActivity, mActivity.getString(R.string.toastmessage_add_open_file_started) + " " + stationUriString, Toast.LENGTH_LONG).show();
68+
Toast.makeText(mActivity, mActivity.getString(R.string.toastmessage_add_open_file_started), Toast.LENGTH_LONG).show();
7469
}
7570

7671
}
@@ -130,13 +125,13 @@ protected void onPostExecute(Station station) {
130125
}
131126

132127
// inform user that aac might not work properly
133-
if (fetchResults.containsKey(TransistorKeys.RESULT_STREAM_TYPE) && fetchResults.getString(TransistorKeys.RESULT_STREAM_TYPE) != null && fetchResults.getString(TransistorKeys.RESULT_STREAM_TYPE).contains("aac")) {
128+
if (fetchResults.containsKey(TransistorKeys.RESULT_STREAM_TYPE) && fetchResults.getParcelable(TransistorKeys.RESULT_STREAM_TYPE) != null && fetchResults.getParcelable(TransistorKeys.RESULT_STREAM_TYPE).toString().contains("aac")) {
134129
Toast.makeText(mActivity, mActivity.getString(R.string.toastmessage_stream_may_not_work), Toast.LENGTH_LONG).show();
135130
}
136131

137132
}
138133

139-
// an error occured
134+
// an error occurred
140135
if (station == null || (fetchResults != null && fetchResults.getBoolean(TransistorKeys.RESULT_FETCH_ERROR)) || !mFolderExists || !stationAdded) {
141136

142137
String errorTitle;
@@ -190,8 +185,18 @@ private boolean urlCleanup() {
190185
private String buildDownloadErrorDetails(Bundle fetchResults) {
191186

192187
String fileContent = fetchResults.getString(TransistorKeys.RESULT_FILE_CONTENT);
193-
String playListType = fetchResults.getString(TransistorKeys.RESULT_PLAYLIST_TYPE);
194-
String streamType = fetchResults.getString(TransistorKeys.RESULT_STREAM_TYPE);
188+
String playListType;
189+
String streamType;
190+
if (fetchResults.containsKey(TransistorKeys.RESULT_PLAYLIST_TYPE) && fetchResults.getParcelable(TransistorKeys.RESULT_PLAYLIST_TYPE) != null) {
191+
playListType = fetchResults.getParcelable(TransistorKeys.RESULT_PLAYLIST_TYPE).toString();
192+
} else {
193+
playListType = "unknown";
194+
}
195+
if (fetchResults.containsKey(TransistorKeys.RESULT_STREAM_TYPE) && fetchResults.getParcelable(TransistorKeys.RESULT_STREAM_TYPE) != null) {
196+
streamType = fetchResults.getParcelable(TransistorKeys.RESULT_STREAM_TYPE).toString();
197+
} else {
198+
streamType = "unknown";
199+
}
195200

196201
// construct details string
197202
StringBuilder sb = new StringBuilder("");
-60 Bytes
Loading

app/src/main/res/layout/dialog_error.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@
2424
android:layout_height="wrap_content"
2525
android:layout_marginTop="20dp"
2626
android:text="@string/dialog_error_details_button_default"
27-
android:textIsSelectable="true"
2827
android:layout_gravity="center_horizontal"
2928
android:scrollbars="vertical"
3029
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Widget.Button" />
3130
<TextView
3231
android:id="@+id/dialog_error_details"
33-
android:layout_width="match_parent"
32+
android:layout_width="wrap_content"
3433
android:layout_height="wrap_content"
3534
android:layout_marginTop="10dp"
3635
android:text="@string/dialog_error_details_default"

0 commit comments

Comments
 (0)