Skip to content
This repository was archived by the owner on Oct 16, 2018. It is now read-only.

Commit 6cf78ff

Browse files
fixed leaking singleton
1 parent 6370f7f commit 6cf78ff

File tree

8 files changed

+101
-227
lines changed

8 files changed

+101
-227
lines changed

app/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ dependencies {
4343
compile 'com.github.pchmn:MaterialChipsInput:1.0.8'
4444
compile 'com.google.firebase:firebase-core:10.0.1'
4545
compile 'com.google.firebase:firebase-crash:10.0.1'
46+
47+
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.4'
48+
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
49+
4650
testCompile 'junit:junit:4.12'
4751
}
4852

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
android:label="@string/app_name"
1212
android:roundIcon="@drawable/sedaily_logo"
1313
android:supportsRtl="true"
14-
android:theme="@style/AppTheme">
14+
android:theme="@style/AppTheme"
15+
android:name=".SEDailyApplication">
1516

1617
<activity android:name=".MainActivity">
1718
<intent-filter>

app/src/main/java/com/koalatea/thehollidayinn/softwareengineeringdaily/MainActivity.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@
3333

3434
public class MainActivity extends PlaybackControllerActivity implements SearchView.OnQueryTextListener {
3535
private UserRepository userRepository;
36-
private RecentPodcastFragment firstFragment;
3736
private FilterRepository filterRepository;
3837

38+
private RecentPodcastFragment firstFragment;
39+
private PodListFragment secondPage;
40+
private PodListFragment thirdPage;
41+
42+
3943
@Override
4044
protected void onCreate(Bundle savedInstanceState) {
4145
super.onCreate(savedInstanceState);
@@ -54,7 +58,9 @@ protected void onCreate(Bundle savedInstanceState) {
5458
}
5559

5660
private void showInitialPage () {
57-
firstFragment = RecentPodcastFragment.newInstance();
61+
if (firstFragment == null) {
62+
firstFragment = RecentPodcastFragment.newInstance();
63+
}
5864
this.getSupportFragmentManager()
5965
.beginTransaction()
6066
.replace(R.id.fragment_container, firstFragment)
@@ -80,17 +86,21 @@ private Boolean navigationItemSelected(@NonNull MenuItem item) {
8086
showInitialPage();
8187
break;
8288
case R.id.action_schedules:
83-
PodListFragment second = PodListFragment.newInstance("Greatest Hits", "");
89+
if (secondPage == null) {
90+
secondPage = PodListFragment.newInstance("Greatest Hits", "");
91+
}
8492
this.getSupportFragmentManager()
8593
.beginTransaction()
86-
.replace(R.id.fragment_container, second)
94+
.replace(R.id.fragment_container, secondPage)
8795
.commit();
8896
break;
8997
case R.id.action_music:
90-
PodListFragment third = PodListFragment.newInstance("Just For You", "");
98+
if (thirdPage == null) {
99+
thirdPage = PodListFragment.newInstance("Just For You", "");
100+
}
91101
this.getSupportFragmentManager()
92102
.beginTransaction()
93-
.replace(R.id.fragment_container, third)
103+
.replace(R.id.fragment_container, thirdPage)
94104
.commit();
95105
break;
96106
}

app/src/main/java/com/koalatea/thehollidayinn/softwareengineeringdaily/PlaybackControllerActivity.java

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.content.ComponentName;
44
import android.os.Bundle;
55
import android.os.RemoteException;
6+
import android.support.annotation.Nullable;
67
import android.support.v4.media.MediaBrowserCompat;
78
import android.support.v4.media.MediaMetadataCompat;
89
import android.support.v4.media.session.MediaControllerCompat;
@@ -25,6 +26,9 @@ public class PlaybackControllerActivity extends AppCompatActivity {
2526
private MediaBrowserCompat mMediaBrowser;
2627
private PlaybackControlsFragment mControlsFragment;
2728

29+
private PlaybackStateCompat state;
30+
private String mCurrentMediaId;
31+
2832
private final MediaBrowserCompat.ConnectionCallback mConnectionCallbacks =
2933
new MediaBrowserCompat.ConnectionCallback() {
3034
@Override
@@ -47,12 +51,10 @@ public void onConnectionFailed() {
4751
}
4852
};
4953

50-
MediaControllerCompat.Callback controllerCallback =
54+
private MediaControllerCompat.Callback controllerCallback =
5155
new MediaControllerCompat.Callback() {
5256
@Override
5357
public void onMetadataChanged(MediaMetadataCompat metadata) {
54-
if (metadata != null) {
55-
}
5658
}
5759

5860
@Override
@@ -72,26 +74,37 @@ protected void onCreate(Bundle savedInstanceState) {
7274
}
7375

7476
protected void setUp() {
75-
mMediaBrowser = new MediaBrowserCompat(this,
76-
new ComponentName(this, MusicService.class),
77-
mConnectionCallbacks,
78-
null); // optional Bundle
77+
if (mMediaBrowser == null) {
78+
mMediaBrowser = new MediaBrowserCompat(this,
79+
new ComponentName(this, MusicService.class),
80+
mConnectionCallbacks,
81+
null); // optional Bundle
82+
}
7983
}
8084

8185
@Override
8286
public void onStart() {
8387
super.onStart();
84-
mControlsFragment = (PlaybackControlsFragment) getSupportFragmentManager()
85-
.findFragmentById(R.id.fragment_playback_controls);
88+
if (mControlsFragment == null) {
89+
mControlsFragment = (PlaybackControlsFragment) getSupportFragmentManager()
90+
.findFragmentById(R.id.fragment_playback_controls);
91+
}
8692
mMediaBrowser.connect();
8793

8894
}
8995

96+
@Override
97+
public void onPause() {
98+
super.onPause();
99+
if (MediaControllerCompat.getMediaController(this) != null) {
100+
MediaControllerCompat.getMediaController(this).unregisterCallback(controllerCallback);
101+
}
102+
mMediaBrowser.disconnect();
103+
}
104+
90105
@Override
91106
public void onResume() {
92107
super.onResume();
93-
94-
95108
}
96109

97110
@Override
@@ -105,20 +118,17 @@ public void onStop() {
105118
}
106119

107120
protected void showPlaybackControls() {
108-
// if (NetworkHelper.isOnline(this)) {
121+
// @TODO: check for network
109122
getSupportFragmentManager().beginTransaction()
110-
// .setCustomAnimations(
111-
// R.animator.slide_in_from_bottom, R.animator.slide_out_to_bottom,
112-
// R.animator.slide_in_from_bottom, R.animator.slide_out_to_bottom)
113123
.show(mControlsFragment)
114-
.commit();
115-
// }
124+
.commitAllowingStateLoss();
125+
116126
}
117127

118128
protected void hidePlaybackControls() {
119129
getSupportFragmentManager().beginTransaction()
120130
.hide(mControlsFragment)
121-
.commit();
131+
.commitAllowingStateLoss();
122132
}
123133

124134
/**
@@ -144,7 +154,7 @@ protected boolean shouldShowControls() {
144154
}
145155
}
146156

147-
private void connectToSession(MediaSessionCompat.Token token) throws RemoteException {
157+
private void connectToSession(MediaSessionCompat.Token token) throws RemoteException {
148158
MediaControllerCompat mediaController = new MediaControllerCompat(
149159
this, token);
150160

@@ -161,4 +171,24 @@ private void connectToSession(MediaSessionCompat.Token token) throws RemoteExcep
161171
mControlsFragment.onConnected();
162172
}
163173
}
174+
175+
public void onMediaItemSelected(MediaBrowserCompat.MediaItem item, boolean isPlaying) {
176+
if (item.isPlayable()) {
177+
MediaControllerCompat controller = MediaControllerCompat.getMediaController(this);
178+
MediaControllerCompat.TransportControls controls = controller.getTransportControls();
179+
180+
if (isPlaying) {
181+
controls.pause();
182+
} else {
183+
controls.playFromMediaId(item.getMediaId(), null);
184+
}
185+
}
186+
}
187+
188+
@Nullable
189+
public String getPlayingMediaId() {
190+
boolean isPlaying = state != null
191+
&& state.getState() == PlaybackStateCompat.STATE_PLAYING;
192+
return isPlaying ? mCurrentMediaId : null;
193+
}
164194
}

app/src/main/java/com/koalatea/thehollidayinn/softwareengineeringdaily/audio/MediaPlayer.java

Lines changed: 0 additions & 156 deletions
This file was deleted.

app/src/main/java/com/koalatea/thehollidayinn/softwareengineeringdaily/podcast/PodListFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public View onCreateView(LayoutInflater inflater,
5050
ViewGroup container, Bundle savedInstanceState) {
5151
View rootView = (View) inflater.inflate(
5252
R.layout.fragment_podcast_horizontal, container, false);
53-
53+
Log.v("keithtest", "onCreateView");
5454
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
5555

5656
recyclerView.setHasFixedSize(true);

app/src/main/java/com/koalatea/thehollidayinn/softwareengineeringdaily/podcast/PodcastAdapter.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@
33
import android.content.Intent;
44
import android.support.v4.app.Fragment;
55
import android.support.v4.media.MediaBrowserCompat;
6-
import android.support.v4.media.MediaMetadataCompat;
76
import android.support.v7.widget.RecyclerView;
87
import android.view.LayoutInflater;
98
import android.view.View;
109
import android.view.ViewGroup;
11-
import android.widget.Button;
1210
import android.widget.ImageView;
1311
import android.widget.TextView;
1412

1513
import com.koalatea.thehollidayinn.softwareengineeringdaily.R;
16-
import com.koalatea.thehollidayinn.softwareengineeringdaily.audio.MediaPlayer;
17-
import com.koalatea.thehollidayinn.softwareengineeringdaily.audio.MusicProvider;
1814
import com.koalatea.thehollidayinn.softwareengineeringdaily.data.models.Post;
1915
import com.squareup.picasso.Picasso;
2016

0 commit comments

Comments
 (0)