@@ -97,8 +97,48 @@ protected void onCreate(final Bundle savedInstanceState) {
9797 getSupportActionBar ().setTitle (R .string .title_activity_play_queue );
9898 }
9999
100- serviceConnection = getServiceConnection ();
101- bind ();
100+ serviceConnection = new ServiceConnection () {
101+ @ Override
102+ public void onServiceDisconnected (final ComponentName name ) {
103+ Log .d (TAG , "Player service is disconnected" );
104+ }
105+
106+ @ Override
107+ public void onServiceConnected (final ComponentName name , final IBinder binder ) {
108+ Log .d (TAG , "Player service is connected" );
109+
110+ if (binder instanceof PlayerService .LocalBinder ) {
111+ @ Nullable final PlayerService s =
112+ ((PlayerService .LocalBinder ) binder ).getService ();
113+ if (s == null ) {
114+ throw new IllegalArgumentException (
115+ "PlayerService.LocalBinder.getService() must never be"
116+ + "null after the service connects" );
117+ }
118+ player = s .getPlayer ();
119+ }
120+
121+ if (player == null || player .getPlayQueue () == null || player .exoPlayerIsNull ()) {
122+ unbind ();
123+ } else {
124+ onQueueUpdate (player .getPlayQueue ());
125+ buildComponents ();
126+ if (player != null ) {
127+ player .setActivityListener (PlayQueueActivity .this );
128+ }
129+ }
130+ }
131+ };
132+
133+ // Note: this code should not really exist, and PlayerHolder should be used instead, but
134+ // it will be rewritten when NewPlayer will replace the current player.
135+ final Intent bindIntent = new Intent (this , PlayerService .class );
136+ bindIntent .setAction (PlayerService .BIND_PLAYER_HOLDER_ACTION );
137+ final boolean success = bindService (bindIntent , serviceConnection , BIND_AUTO_CREATE );
138+ if (!success ) {
139+ unbindService (serviceConnection );
140+ }
141+ serviceBound = success ;
102142 }
103143
104144 @ Override
@@ -180,19 +220,6 @@ protected void onDestroy() {
180220
181221 ////////////////////////////////////////////////////////////////////////////
182222 // Service Connection
183- ////////////////////////////////////////////////////////////////////////////
184-
185- private void bind () {
186- // Note: this code should not really exist, and PlayerHolder should be used instead, but
187- // it will be rewritten when NewPlayer will replace the current player.
188- final Intent bindIntent = new Intent (this , PlayerService .class );
189- bindIntent .setAction (PlayerService .BIND_PLAYER_HOLDER_ACTION );
190- final boolean success = bindService (bindIntent , serviceConnection , BIND_AUTO_CREATE );
191- if (!success ) {
192- unbindService (serviceConnection );
193- }
194- serviceBound = success ;
195- }
196223
197224 private void unbind () {
198225 if (serviceBound ) {
@@ -212,41 +239,6 @@ private void unbind() {
212239 }
213240 }
214241
215- private ServiceConnection getServiceConnection () {
216- return new ServiceConnection () {
217- @ Override
218- public void onServiceDisconnected (final ComponentName name ) {
219- Log .d (TAG , "Player service is disconnected" );
220- }
221-
222- @ Override
223- public void onServiceConnected (final ComponentName name , final IBinder binder ) {
224- Log .d (TAG , "Player service is connected" );
225-
226- if (binder instanceof PlayerService .LocalBinder ) {
227- @ Nullable final PlayerService s =
228- ((PlayerService .LocalBinder ) binder ).getService ();
229- if (s == null ) {
230- throw new IllegalArgumentException (
231- "PlayerService.LocalBinder.getService() must never be"
232- + "null after the service connects" );
233- }
234- player = s .getPlayer ();
235- }
236-
237- if (player == null || player .getPlayQueue () == null || player .exoPlayerIsNull ()) {
238- unbind ();
239- } else {
240- onQueueUpdate (player .getPlayQueue ());
241- buildComponents ();
242- if (player != null ) {
243- player .setActivityListener (PlayQueueActivity .this );
244- }
245- }
246- }
247- };
248- }
249-
250242 ////////////////////////////////////////////////////////////////////////////
251243 // Component Building
252244 ////////////////////////////////////////////////////////////////////////////
0 commit comments