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.media.MediaFile;
27 import android.net.Uri;
28 import android.os.Bundle;
29 import android.os.RemoteException;
30 import android.provider.MediaStore;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.ImageView;
34 import android.widget.ListView;
35 import android.widget.SimpleCursorAdapter;
36 import android.widget.TextView;
38 import java.util.ArrayList;
40 public class MediaPickerActivity extends ListActivity implements MusicUtils.Defs
43 public MediaPickerActivity()
47 /** Called when the activity is first created. */
49 public void onCreate(Bundle icicle)
51 super.onCreate(icicle);
53 mFirstYear = getIntent().getStringExtra("firstyear");
54 mLastYear = getIntent().getStringExtra("lastyear");
56 if (mFirstYear == null) {
57 setTitle(R.string.all_title);
58 } else if (mFirstYear.equals(mLastYear)) {
61 setTitle(mFirstYear + "-" + mLastYear);
63 MusicUtils.bindToService(this);
68 public void onDestroy() {
69 MusicUtils.unbindFromService(this);
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.getColumnIndex(MediaStore.Audio.Media.MIME_TYPE));
98 String action = getIntent().getAction();
99 if (Intent.ACTION_GET_CONTENT.equals(action)) {
103 if (type.startsWith("video")) {
104 uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
105 mediaId = mCursor.getLong(mCursor.getColumnIndex(MediaStore.Video.Media._ID));
107 uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
108 mediaId = mCursor.getLong(mCursor.getColumnIndex(MediaStore.Audio.Media._ID));
111 setResult(RESULT_OK, new Intent().setData(ContentUris.withAppendedId(uri, mediaId)));
116 // Need to stop the playbackservice, in case it is busy playing audio
117 // and the user selected a video.
118 if (MusicUtils.sService != null) {
120 MusicUtils.sService.stop();
121 } catch (RemoteException ex) {
124 Intent intent = new Intent(Intent.ACTION_VIEW);
125 intent.setDataAndType(ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id), type);
127 startActivity(intent);
130 private void MakeCursor() {
131 String[] audiocols = new String[] {
132 MediaStore.Audio.Media._ID,
133 MediaStore.Audio.Media.ARTIST,
134 MediaStore.Audio.Media.ALBUM,
135 MediaStore.Audio.Media.TITLE,
136 MediaStore.Audio.Media.DATA,
137 MediaStore.Audio.Media.MIME_TYPE,
138 MediaStore.Audio.Media.YEAR
140 String[] videocols = new String[] {
141 MediaStore.Audio.Media._ID,
142 MediaStore.Audio.Media.TITLE,
143 MediaStore.Audio.Media.ARTIST,
144 MediaStore.Audio.Media.ALBUM,
145 MediaStore.Audio.Media.TITLE,
146 MediaStore.Audio.Media.DATA,
147 MediaStore.Audio.Media.MIME_TYPE
151 // Use ArrayList for the moment, since we don't know the size of
152 // Cursor[]. If the length of Corsor[] larger than really used,
153 // a NPE will come up when access the content of Corsor[].
154 ArrayList<Cursor> cList = new ArrayList<Cursor>();
155 Intent intent = getIntent();
156 String type = intent.getType();
158 if (mFirstYear != null) {
159 // If mFirstYear is not null, the picker only for audio because
160 // video has no year column.
161 if(type.equals("video/*")) {
166 mWhereClause = MediaStore.Audio.Media.YEAR + ">=" + mFirstYear + " AND " +
167 MediaStore.Audio.Media.YEAR + "<=" + mLastYear;
170 // If use Cursor[] as before, the Cursor[i] could be null when there is
171 // no video/audio/sdcard. Then a NPE will come up when access the content of the
175 if (type.equals("video/*")) {
177 c = MusicUtils.query(this, MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
178 videocols, null , null, mSortOrder);
183 c = MusicUtils.query(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
184 audiocols, mWhereClause , null, mSortOrder);
190 if (mFirstYear == null && intent.getType().equals("media/*")) {
191 // video has no year column
192 c = MusicUtils.query(this, MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
193 videocols, null , null, mSortOrder);
200 // Get the ArrayList size.
201 int size = cList.size();
203 // If no video/audio/SDCard exist, return.
208 // The size is known now, we're sure each item of Cursor[] is not null.
209 cs = new Cursor[size];
210 cs = cList.toArray(cs);
211 mCursor = new SortCursor(cs, MediaStore.Audio.Media.TITLE);
214 private Cursor mCursor;
215 private String mSortOrder = MediaStore.Audio.Media.TITLE + " COLLATE UNICODE";
216 private String mFirstYear;
217 private String mLastYear;
218 private String mWhereClause;
220 class PickListAdapter extends SimpleCursorAdapter {
226 PickListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
227 super(context, layout, cursor, from, to);
229 mTitleIdx = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
230 mArtistIdx = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
231 mAlbumIdx = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
232 mMimeIdx = cursor.getColumnIndex(MediaStore.Audio.Media.MIME_TYPE);
236 public View newView(Context context, Cursor cursor, ViewGroup parent) {
237 View v = super.newView(context, cursor, parent);
238 ImageView iv = (ImageView) v.findViewById(R.id.icon);
239 iv.setVisibility(View.VISIBLE);
240 ViewGroup.LayoutParams p = iv.getLayoutParams();
241 p.width = ViewGroup.LayoutParams.WRAP_CONTENT;
242 p.height = ViewGroup.LayoutParams.WRAP_CONTENT;
244 TextView tv = (TextView) v.findViewById(R.id.duration);
245 tv.setVisibility(View.GONE);
246 iv = (ImageView) v.findViewById(R.id.play_indicator);
247 iv.setVisibility(View.GONE);
253 public void bindView(View view, Context context, Cursor cursor) {
255 TextView tv = (TextView) view.findViewById(R.id.line1);
256 String name = cursor.getString(mTitleIdx);
259 tv = (TextView) view.findViewById(R.id.line2);
260 name = cursor.getString(mAlbumIdx);
261 StringBuilder builder = new StringBuilder();
262 if (name == null || name.equals(MediaFile.UNKNOWN_STRING)) {
263 builder.append(context.getString(R.string.unknown_album_name));
265 builder.append(name);
267 builder.append("\n");
268 name = cursor.getString(mArtistIdx);
269 if (name == null || name.equals(MediaFile.UNKNOWN_STRING)) {
270 builder.append(context.getString(R.string.unknown_artist_name));
272 builder.append(name);
274 tv.setText(builder.toString());
276 String text = cursor.getString(mMimeIdx);
277 ImageView iv = (ImageView) view.findViewById(R.id.icon);;
278 if("audio/midi".equals(text)) {
279 iv.setImageResource(R.drawable.midi);
280 } else if(text != null && (text.startsWith("audio") ||
281 text.equals("application/ogg") ||
282 text.equals("application/x-ogg"))) {
283 iv.setImageResource(R.drawable.ic_search_category_music_song);
284 } else if(text != null && text.startsWith("video")) {
285 iv.setImageResource(R.drawable.movie);
287 iv.setImageResource(0);