• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 package com.android.onemedia;
17 
18 
19 import android.app.Activity;
20 import android.app.Notification;
21 import android.app.NotificationManager;
22 import android.app.PendingIntent;
23 import android.content.ComponentName;
24 import android.content.Intent;
25 import android.graphics.Bitmap;
26 import android.graphics.BitmapFactory;
27 import android.media.MediaMetadata;
28 import android.media.session.PlaybackState;
29 import android.net.Uri;
30 import android.os.Bundle;
31 import android.provider.MediaStore;
32 import android.text.format.DateUtils;
33 import android.util.Log;
34 import android.view.Menu;
35 import android.view.View;
36 import android.widget.Button;
37 import android.widget.CheckBox;
38 import android.widget.EditText;
39 import android.widget.ImageView;
40 import android.widget.TextView;
41 
42 import java.io.IOException;
43 
44 public class OnePlayerActivity extends Activity {
45     private static final String TAG = "OnePlayerActivity";
46 
47     private static final int READ_REQUEST_CODE = 42;
48 
49     protected PlayerController mPlayer;
50 
51     private Button mStartButton;
52     private Button mPlayButton;
53     private Button mRouteButton;
54     private TextView mStatusView;
55 
56     private EditText mContentText;
57     private EditText mNextContentText;
58     private CheckBox mHasVideo;
59     private ImageView mArtView;
60 
61     private PlaybackState mPlaybackState;
62     private Bitmap mAlbumArtBitmap;
63 
64     @Override
onCreate(Bundle savedInstanceState)65     protected void onCreate(Bundle savedInstanceState) {
66         super.onCreate(savedInstanceState);
67         setContentView(R.layout.activity_one_player);
68         mPlayer = new PlayerController(this, OnePlayerService.getServiceIntent(this));
69 
70 
71         mStartButton = (Button) findViewById(R.id.start_button);
72         mPlayButton = (Button) findViewById(R.id.play_button);
73         mRouteButton = (Button) findViewById(R.id.route_button);
74         mStatusView = (TextView) findViewById(R.id.status);
75         mContentText = (EditText) findViewById(R.id.content);
76         mNextContentText = (EditText) findViewById(R.id.next_content);
77         mHasVideo = (CheckBox) findViewById(R.id.has_video);
78         mArtView = (ImageView) findViewById(R.id.art);
79 
80         final Button artPicker = (Button) findViewById(R.id.art_picker);
81         artPicker.setOnClickListener(mButtonListener);
82 
83         mStartButton.setOnClickListener(mButtonListener);
84         mPlayButton.setOnClickListener(mButtonListener);
85         mRouteButton.setOnClickListener(mButtonListener);
86 
87     }
88 
89     @Override
onCreateOptionsMenu(Menu menu)90     public boolean onCreateOptionsMenu(Menu menu) {
91         // Inflate the menu; this adds items to the action bar if it is present.
92         getMenuInflater().inflate(R.menu.main, menu);
93         return true;
94     }
95 
96     @Override
onResume()97     public void onResume() {
98         super.onResume();
99         mPlayer.onResume();
100         mPlayer.setListener(mListener);
101     }
102 
103     @Override
onPause()104     public void onPause() {
105         mPlayer.setListener(null);
106         mPlayer.onPause();
107         super.onPause();
108     }
109 
110     @Override
onActivityResult(int requestCode, int resultCode, Intent resultData)111     public void onActivityResult(int requestCode, int resultCode,
112             Intent resultData) {
113         if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
114             Uri uri = null;
115             if (resultData != null) {
116                 uri = resultData.getData();
117                 Log.i(TAG, "Uri: " + uri.toString());
118                 mAlbumArtBitmap = null;
119                 try {
120                     mAlbumArtBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
121                 } catch (IOException e) {
122                     Log.v(TAG, "Couldn't load album art", e);
123                 }
124                 mArtView.setImageBitmap(mAlbumArtBitmap);
125                 if (mAlbumArtBitmap != null) {
126                     mArtView.setVisibility(View.VISIBLE);
127                 } else {
128                     mArtView.setVisibility(View.GONE);
129                 }
130                 mPlayer.setArt(mAlbumArtBitmap);
131             }
132         }
133     }
134 
setControlsEnabled(boolean enabled)135     private void setControlsEnabled(boolean enabled) {
136         mStartButton.setEnabled(enabled);
137         mPlayButton.setEnabled(enabled);
138     }
139 
140     private View.OnClickListener mButtonListener = new View.OnClickListener() {
141         @Override
142         public void onClick(View v) {
143             final int state = mPlaybackState.getState();
144             switch (v.getId()) {
145                 case R.id.play_button:
146                     Log.d(TAG, "Play button pressed, in state " + state);
147                     if (state == PlaybackState.STATE_PAUSED
148                             || state == PlaybackState.STATE_STOPPED) {
149                         mPlayer.play();
150                     } else if (state == PlaybackState.STATE_PLAYING) {
151                         mPlayer.pause();
152                     }
153                     break;
154                 case R.id.start_button:
155                     Log.d(TAG, "Start button pressed, in state " + state);
156                     mPlayer.setContent(mContentText.getText().toString());
157                     break;
158                 case R.id.route_button:
159                     mPlayer.showRoutePicker();
160                     break;
161                 case R.id.art_picker:
162                     Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
163                     intent.addCategory(Intent.CATEGORY_OPENABLE);
164                     intent.setType("image/*");
165 
166                     startActivityForResult(intent, READ_REQUEST_CODE);
167                     break;
168             }
169 
170         }
171     };
172 
173     private PlayerController.Listener mListener = new PlayerController.Listener() {
174         public MediaMetadata mMetadata;
175 
176         @Override
177         public void onPlaybackStateChange(PlaybackState state) {
178             mPlaybackState = state;
179             boolean enablePlay = false;
180             boolean enableControls = true;
181             StringBuilder statusBuilder = new StringBuilder();
182             switch (mPlaybackState.getState()) {
183                 case PlaybackState.STATE_PLAYING:
184                     statusBuilder.append("playing");
185                     mPlayButton.setText("Pause");
186                     enablePlay = true;
187                     break;
188                 case PlaybackState.STATE_PAUSED:
189                     statusBuilder.append("paused");
190                     mPlayButton.setText("Play");
191                     enablePlay = true;
192                     break;
193                 case PlaybackState.STATE_STOPPED:
194                     statusBuilder.append("ended");
195                     mPlayButton.setText("Play");
196                     enablePlay = true;
197                     break;
198                 case PlaybackState.STATE_ERROR:
199                     statusBuilder.append("error: ").append(state.getErrorMessage());
200                     break;
201                 case PlaybackState.STATE_BUFFERING:
202                     statusBuilder.append("buffering");
203                     break;
204                 case PlaybackState.STATE_NONE:
205                     statusBuilder.append("none");
206                     break;
207                 case PlaybackState.STATE_CONNECTING:
208                     statusBuilder.append("connecting");
209                     enableControls = false;
210                     break;
211                 default:
212                     statusBuilder.append(mPlaybackState);
213             }
214             statusBuilder.append(" -- At position: ").append(state.getPosition());
215             mStatusView.setText(statusBuilder.toString());
216             mPlayButton.setEnabled(enablePlay);
217             setControlsEnabled(enableControls);
218         }
219 
220         @Override
221         public void onConnectionStateChange(int state) {
222             if (state == PlayerController.STATE_DISCONNECTED) {
223                 setControlsEnabled(false);
224             } else if (state == PlayerController.STATE_CONNECTED) {
225                 setControlsEnabled(true);
226             }
227         }
228 
229         @Override
230         public void onMetadataChange(MediaMetadata metadata) {
231             mMetadata = metadata;
232         }
233     };
234 }
235