2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.music;
19 import android.app.ListActivity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.ContentUris;
23 import android.database.Cursor;
24 import android.net.Uri;
25 import android.os.Bundle;
26 import android.os.RemoteException;
27 import android.provider.MediaStore;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.ImageView;
31 import android.widget.ListView;
32 import android.widget.SimpleCursorAdapter;
33 import android.widget.TextView;
35 import java.util.ArrayList;
37 public class MediaPickerActivity extends ListActivity implements MusicUtils.Defs
40 public MediaPickerActivity()
44 /** Called when the activity is first created. */
46 public void onCreate(Bundle icicle)
48 super.onCreate(icicle);
50 mFirstYear = getIntent().getStringExtra("firstyear");
51 mLastYear = getIntent().getStringExtra("lastyear");
53 if (mFirstYear == null) {
54 setTitle(R.string.all_title);
55 } else if (mFirstYear.equals(mLastYear)) {
58 setTitle(mFirstYear + "-" + mLastYear);
60 MusicUtils.bindToService(this);
65 public void onDestroy() {
66 MusicUtils.unbindFromService(this);
68 if (mCursor != null) {
75 setContentView(R.layout.media_picker_activity);
78 if (null == mCursor || 0 == mCursor.getCount()) {
82 PickListAdapter adapter = new PickListAdapter(
84 R.layout.track_list_item,
89 setListAdapter(adapter);
93 protected void onListItemClick(ListView l, View v, int position, long id)
95 mCursor.moveToPosition(position);
96 String type = mCursor.getString(mCursor.getColumnIndexOrThrow(
97 MediaStore.Audio.Media.MIME_TYPE));
99 String action = getIntent().getAction();
100 if (Intent.ACTION_GET_CONTENT.equals(action)) {
104 if (type.startsWith("video")) {
105 uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
106 mediaId = mCursor.getLong(mCursor.getColumnIndexOrThrow(
107 MediaStore.Video.Media._ID));
109 uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
110 mediaId = mCursor.getLong(mCursor.getColumnIndexOrThrow(
111 MediaStore.Audio.Media._ID));
114 setResult(RESULT_OK, new Intent().setData(ContentUris.withAppendedId(uri, mediaId)));
119 // Need to stop the playbackservice, in case it is busy playing audio
120 // and the user selected a video.
121 if (MusicUtils.sService != null) {
123 MusicUtils.sService.stop();
124 } catch (RemoteException ex) {
127 Intent intent = new Intent(Intent.ACTION_VIEW);
128 intent.setDataAndType(ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id), type);
130 startActivity(intent);
133 private void MakeCursor() {
134 String[] audiocols = new String[] {
135 MediaStore.Audio.Media._ID,
136 MediaStore.Audio.Media.ARTIST,
137 MediaStore.Audio.Media.ALBUM,
138 MediaStore.Audio.Media.TITLE,
139 MediaStore.Audio.Media.DATA,
140 MediaStore.Audio.Media.MIME_TYPE,
141 MediaStore.Audio.Media.YEAR
143 String[] videocols = new String[] {
144 MediaStore.Audio.Media._ID,
145 MediaStore.Audio.Media.TITLE,
146 MediaStore.Audio.Media.ARTIST,
147 MediaStore.Audio.Media.ALBUM,
148 MediaStore.Audio.Media.TITLE,
149 MediaStore.Audio.Media.DATA,
150 MediaStore.Audio.Media.MIME_TYPE
154 // Use ArrayList for the moment, since we don't know the size of
155 // Cursor[]. If the length of Corsor[] larger than really used,
156 // a NPE will come up when access the content of Corsor[].
157 ArrayList<Cursor> cList = new ArrayList<Cursor>();
158 Intent intent = getIntent();
159 String type = intent.getType();
161 if (mFirstYear != null) {
162 // If mFirstYear is not null, the picker only for audio because
163 // video has no year column.
164 if(type.equals("video/*")) {
169 mWhereClause = MediaStore.Audio.Media.YEAR + ">=" + mFirstYear + " AND " +
170 MediaStore.Audio.Media.YEAR + "<=" + mLastYear;
173 // If use Cursor[] as before, the Cursor[i] could be null when there is
174 // no video/audio/sdcard. Then a NPE will come up when access the content of the
178 if (type.equals("video/*")) {
180 c = MusicUtils.query(this, MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
181 videocols, null , null, mSortOrder);
186 c = MusicUtils.query(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
187 audiocols, mWhereClause , null, mSortOrder);
193 if (mFirstYear == null && intent.getType().equals("media/*")) {
194 // video has no year column
195 c = MusicUtils.query(this, MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
196 videocols, null , null, mSortOrder);
203 // Get the ArrayList size.
204 int size = cList.size();
206 // If no video/audio/SDCard exist, return.
211 // The size is known now, we're sure each item of Cursor[] is not null.
212 cs = new Cursor[size];
213 cs = cList.toArray(cs);
214 mCursor = new SortCursor(cs, MediaStore.Audio.Media.TITLE);
217 private Cursor mCursor;
218 private String mSortOrder = MediaStore.Audio.Media.TITLE + " COLLATE UNICODE";
219 private String mFirstYear;
220 private String mLastYear;
221 private String mWhereClause;
223 static class PickListAdapter extends SimpleCursorAdapter {
229 PickListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
230 super(context, layout, cursor, from, to);
232 mTitleIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
233 mArtistIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
234 mAlbumIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);
235 mMimeIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE);
239 public View newView(Context context, Cursor cursor, ViewGroup parent) {
240 View v = super.newView(context, cursor, parent);
241 ImageView iv = (ImageView) v.findViewById(R.id.icon);
242 iv.setVisibility(View.VISIBLE);
243 ViewGroup.LayoutParams p = iv.getLayoutParams();
244 p.width = ViewGroup.LayoutParams.WRAP_CONTENT;
245 p.height = ViewGroup.LayoutParams.WRAP_CONTENT;
247 TextView tv = (TextView) v.findViewById(R.id.duration);
248 tv.setVisibility(View.GONE);
249 iv = (ImageView) v.findViewById(R.id.play_indicator);
250 iv.setVisibility(View.GONE);
256 public void bindView(View view, Context context, Cursor cursor) {
258 TextView tv = (TextView) view.findViewById(R.id.line1);
259 String name = cursor.getString(mTitleIdx);
262 tv = (TextView) view.findViewById(R.id.line2);
263 name = cursor.getString(mAlbumIdx);
264 StringBuilder builder = new StringBuilder();
265 if (name == null || name.equals(MediaStore.UNKNOWN_STRING)) {
266 builder.append(context.getString(R.string.unknown_album_name));
268 builder.append(name);
270 builder.append("\n");
271 name = cursor.getString(mArtistIdx);
272 if (name == null || name.equals(MediaStore.UNKNOWN_STRING)) {
273 builder.append(context.getString(R.string.unknown_artist_name));
275 builder.append(name);
277 tv.setText(builder.toString());
279 String text = cursor.getString(mMimeIdx);
280 ImageView iv = (ImageView) view.findViewById(R.id.icon);;
281 if("audio/midi".equals(text)) {
282 iv.setImageResource(R.drawable.midi);
283 } else if(text != null && (text.startsWith("audio") ||
284 text.equals("application/ogg") ||
285 text.equals("application/x-ogg"))) {
286 iv.setImageResource(R.drawable.ic_search_category_music_song);
287 } else if(text != null && text.startsWith("video")) {
288 iv.setImageResource(R.drawable.movie);
290 iv.setImageResource(0);