• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.music;
18 
19 import android.app.Activity;
20 import android.content.BroadcastReceiver;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.content.ServiceConnection;
26 import android.media.AudioManager;
27 import android.net.Uri;
28 import android.os.Bundle;
29 import android.os.IBinder;
30 import android.os.RemoteException;
31 import android.view.Window;
32 import android.widget.TextView;
33 import android.widget.Toast;
34 
35 public class StreamStarter extends Activity
36 {
37     @Override
onCreate(Bundle icicle)38     public void onCreate(Bundle icicle) {
39         super.onCreate(icicle);
40         setVolumeControlStream(AudioManager.STREAM_MUSIC);
41 
42         requestWindowFeature(Window.FEATURE_NO_TITLE);
43         setContentView(R.layout.streamstarter);
44 
45         TextView tv = (TextView) findViewById(R.id.streamloading);
46 
47         Uri uri = getIntent().getData();
48         String msg = getString(R.string.streamloadingtext, uri.getHost());
49         tv.setText(msg);
50     }
51 
52     @Override
onResume()53     public void onResume() {
54         super.onResume();
55 
56         MusicUtils.bindToService(this, new ServiceConnection() {
57             public void onServiceConnected(ComponentName classname, IBinder obj) {
58                 try {
59                     IntentFilter f = new IntentFilter();
60                     f.addAction(MediaPlaybackService.ASYNC_OPEN_COMPLETE);
61                     f.addAction(MediaPlaybackService.PLAYBACK_COMPLETE);
62                     registerReceiver(mStatusListener, new IntentFilter(f));
63                     MusicUtils.sService.openFileAsync(getIntent().getData().toString());
64                 } catch (RemoteException ex) {
65                 }
66             }
67 
68             public void onServiceDisconnected(ComponentName classname) {
69             }
70         });
71     }
72 
73     private BroadcastReceiver mStatusListener = new BroadcastReceiver() {
74         @Override
75         public void onReceive(Context context, Intent intent) {
76             String action = intent.getAction();
77             if (action.equals(MediaPlaybackService.PLAYBACK_COMPLETE)) {
78                 // You would come here only in case of a failure in the
79                 // MediaPlayerService before PrepareAsync completes
80                 String msg = getString(R.string.fail_to_start_stream);
81                 Toast mt = Toast.makeText(StreamStarter.this, msg, Toast.LENGTH_SHORT);
82                 mt.show();
83                 finish();
84                 return;
85             }
86             try {
87                 MusicUtils.sService.play();
88                 intent = new Intent("com.android.music.PLAYBACK_VIEWER");
89                 intent.putExtra("oneshot", true);
90                 startActivity(intent);
91             } catch (RemoteException ex) {
92             }
93             finish();
94         }
95     };
96 
97     @Override
onPause()98     public void onPause() {
99         if (MusicUtils.sService != null) {
100             try {
101                 // This looks a little weird (when it's not playing, stop playing)
102                 // but it is correct. When nothing is playing, it means that this
103                 // was paused before a connection was established, in which case
104                 // we stop trying to connect/play.
105                 // Otherwise, this call to onPause() was a result of the call to
106                 // finish() above, and we should let playback continue.
107                 if (! MusicUtils.sService.isPlaying()) {
108                     MusicUtils.sService.stop();
109                 }
110             } catch (RemoteException ex) {
111             }
112         }
113         unregisterReceiver(mStatusListener);
114         MusicUtils.unbindFromService(this);
115         super.onPause();
116     }
117 }
118