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 com.android.internal.database.SortCursor;
21 import android.app.ListActivity;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.ContentUris;
25 import android.database.Cursor;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.os.RemoteException;
29 import android.provider.MediaStore;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.ImageView;
33 import android.widget.ListView;
34 import android.widget.SimpleCursorAdapter;
35 import android.widget.TextView;
37 import java.util.ArrayList;
39 public class MediaPickerActivity extends ListActivity implements MusicUtils.Defs
42 public MediaPickerActivity()
46 /** Called when the activity is first created. */
48 public void onCreate(Bundle icicle)
50 super.onCreate(icicle);
52 mFirstYear = getIntent().getStringExtra("firstyear");
53 mLastYear = getIntent().getStringExtra("lastyear");
55 if (mFirstYear == null) {
56 setTitle(R.string.all_title);
57 } else if (mFirstYear.equals(mLastYear)) {
60 setTitle(mFirstYear + "-" + mLastYear);
62 MusicUtils.bindToService(this);
67 public void onDestroy() {
68 MusicUtils.unbindFromService(this);
70 if (mCursor != null) {
77 setContentView(R.layout.media_picker_activity);
80 if (null == mCursor || 0 == mCursor.getCount()) {
84 PickListAdapter adapter = new PickListAdapter(
86 R.layout.track_list_item,
91 setListAdapter(adapter);
95 protected void onListItemClick(ListView l, View v, int position, long id)
97 mCursor.moveToPosition(position);
98 String type = mCursor.getString(mCursor.getColumnIndexOrThrow(
99 MediaStore.Audio.Media.MIME_TYPE));
101 String action = getIntent().getAction();
102 if (Intent.ACTION_GET_CONTENT.equals(action)) {
106 if (type.startsWith("video")) {
107 uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
108 mediaId = mCursor.getLong(mCursor.getColumnIndexOrThrow(
109 MediaStore.Video.Media._ID));
111 uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
112 mediaId = mCursor.getLong(mCursor.getColumnIndexOrThrow(
113 MediaStore.Audio.Media._ID));
116 setResult(RESULT_OK, new Intent().setData(ContentUris.withAppendedId(uri, mediaId)));
121 // Need to stop the playbackservice, in case it is busy playing audio
122 // and the user selected a video.
123 if (MusicUtils.sService != null) {
125 MusicUtils.sService.stop();
126 } catch (RemoteException ex) {
129 Intent intent = new Intent(Intent.ACTION_VIEW);
130 intent.setDataAndType(ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id), type);
132 startActivity(intent);
135 private void MakeCursor() {
136 String[] audiocols = new String[] {
137 MediaStore.Audio.Media._ID,
138 MediaStore.Audio.Media.ARTIST,
139 MediaStore.Audio.Media.ALBUM,
140 MediaStore.Audio.Media.TITLE,
141 MediaStore.Audio.Media.DATA,
142 MediaStore.Audio.Media.MIME_TYPE,
143 MediaStore.Audio.Media.YEAR
145 String[] videocols = new String[] {
146 MediaStore.Audio.Media._ID,
147 MediaStore.Audio.Media.TITLE,
148 MediaStore.Audio.Media.ARTIST,
149 MediaStore.Audio.Media.ALBUM,
150 MediaStore.Audio.Media.TITLE,
151 MediaStore.Audio.Media.DATA,
152 MediaStore.Audio.Media.MIME_TYPE
156 // Use ArrayList for the moment, since we don't know the size of
157 // Cursor[]. If the length of Corsor[] larger than really used,
158 // a NPE will come up when access the content of Corsor[].
159 ArrayList<Cursor> cList = new ArrayList<Cursor>();
160 Intent intent = getIntent();
161 String type = intent.getType();
163 if (mFirstYear != null) {
164 // If mFirstYear is not null, the picker only for audio because
165 // video has no year column.
166 if(type.equals("video/*")) {
171 mWhereClause = MediaStore.Audio.Media.YEAR + ">=" + mFirstYear + " AND " +
172 MediaStore.Audio.Media.YEAR + "<=" + mLastYear;
175 // If use Cursor[] as before, the Cursor[i] could be null when there is
176 // no video/audio/sdcard. Then a NPE will come up when access the content of the
180 if (type.equals("video/*")) {
182 c = MusicUtils.query(this, MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
183 videocols, null , null, mSortOrder);
188 c = MusicUtils.query(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
189 audiocols, mWhereClause , null, mSortOrder);
195 if (mFirstYear == null && intent.getType().equals("media/*")) {
196 // video has no year column
197 c = MusicUtils.query(this, MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
198 videocols, null , null, mSortOrder);
205 // Get the ArrayList size.
206 int size = cList.size();
208 // If no video/audio/SDCard exist, return.
213 // The size is known now, we're sure each item of Cursor[] is not null.
214 cs = new Cursor[size];
215 cs = cList.toArray(cs);
216 mCursor = new SortCursor(cs, MediaStore.Audio.Media.TITLE);
219 private Cursor mCursor;
220 private String mSortOrder = MediaStore.Audio.Media.TITLE + " COLLATE UNICODE";
221 private String mFirstYear;
222 private String mLastYear;
223 private String mWhereClause;
225 static class PickListAdapter extends SimpleCursorAdapter {
231 PickListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
232 super(context, layout, cursor, from, to);
234 mTitleIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
235 mArtistIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
236 mAlbumIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);
237 mMimeIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE);
241 public View newView(Context context, Cursor cursor, ViewGroup parent) {
242 View v = super.newView(context, cursor, parent);
243 ImageView iv = (ImageView) v.findViewById(R.id.icon);
244 iv.setVisibility(View.VISIBLE);
245 ViewGroup.LayoutParams p = iv.getLayoutParams();
246 p.width = ViewGroup.LayoutParams.WRAP_CONTENT;
247 p.height = ViewGroup.LayoutParams.WRAP_CONTENT;
249 TextView tv = (TextView) v.findViewById(R.id.duration);
250 tv.setVisibility(View.GONE);
251 iv = (ImageView) v.findViewById(R.id.play_indicator);
252 iv.setVisibility(View.GONE);
258 public void bindView(View view, Context context, Cursor cursor) {
260 TextView tv = (TextView) view.findViewById(R.id.line1);
261 String name = cursor.getString(mTitleIdx);
264 tv = (TextView) view.findViewById(R.id.line2);
265 name = cursor.getString(mAlbumIdx);
266 StringBuilder builder = new StringBuilder();
267 if (name == null || name.equals(MediaStore.UNKNOWN_STRING)) {
268 builder.append(context.getString(R.string.unknown_album_name));
270 builder.append(name);
272 builder.append("\n");
273 name = cursor.getString(mArtistIdx);
274 if (name == null || name.equals(MediaStore.UNKNOWN_STRING)) {
275 builder.append(context.getString(R.string.unknown_artist_name));
277 builder.append(name);
279 tv.setText(builder.toString());
281 String text = cursor.getString(mMimeIdx);
282 ImageView iv = (ImageView) view.findViewById(R.id.icon);;
283 if("audio/midi".equals(text)) {
284 iv.setImageResource(R.drawable.midi);
285 } else if(text != null && (text.startsWith("audio") ||
286 text.equals("application/ogg") ||
287 text.equals("application/x-ogg"))) {
288 iv.setImageResource(R.drawable.ic_search_category_music_song);
289 } else if(text != null && text.startsWith("video")) {
290 iv.setImageResource(R.drawable.movie);
292 iv.setImageResource(0);