1 /* 2 * Copyright (C) 2007 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 com.android.music.MusicUtils.ServiceToken; 20 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; 36 37 import java.util.ArrayList; 38 39 public class MediaPickerActivity extends ListActivity implements MusicUtils.Defs { 40 private ServiceToken mToken; 41 MediaPickerActivity()42 public MediaPickerActivity() {} 43 44 /** Called when the activity is first created. */ 45 @Override onCreate(Bundle icicle)46 public void onCreate(Bundle icicle) { 47 super.onCreate(icicle); 48 49 mFirstYear = getIntent().getStringExtra("firstyear"); 50 mLastYear = getIntent().getStringExtra("lastyear"); 51 52 if (mFirstYear == null) { 53 setTitle(R.string.all_title); 54 } else if (mFirstYear.equals(mLastYear)) { 55 setTitle(mFirstYear); 56 } else { 57 setTitle(mFirstYear + "-" + mLastYear); 58 } 59 mToken = MusicUtils.bindToService(this); 60 init(); 61 } 62 63 @Override onDestroy()64 public void onDestroy() { 65 MusicUtils.unbindFromService(mToken); 66 super.onDestroy(); 67 if (mCursor != null) { 68 mCursor.close(); 69 } 70 } 71 init()72 public void init() { 73 setContentView(R.layout.media_picker_activity); 74 75 MakeCursor(); 76 if (null == mCursor || 0 == mCursor.getCount()) { 77 return; 78 } 79 80 PickListAdapter adapter = new PickListAdapter( 81 this, R.layout.track_list_item, mCursor, new String[] {}, new int[] {}); 82 83 setListAdapter(adapter); 84 } 85 86 @Override onListItemClick(ListView l, View v, int position, long id)87 protected void onListItemClick(ListView l, View v, int position, long id) { 88 mCursor.moveToPosition(position); 89 String type = 90 mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE)); 91 92 String action = getIntent().getAction(); 93 if (Intent.ACTION_GET_CONTENT.equals(action)) { 94 Uri uri; 95 96 long mediaId; 97 if (type.startsWith("video")) { 98 uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; 99 mediaId = 100 mCursor.getLong(mCursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID)); 101 } else { 102 uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 103 mediaId = 104 mCursor.getLong(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)); 105 } 106 107 setResult(RESULT_OK, new Intent().setData(ContentUris.withAppendedId(uri, mediaId))); 108 finish(); 109 return; 110 } 111 112 // Need to stop the playbackservice, in case it is busy playing audio 113 // and the user selected a video. 114 if (MusicUtils.sService != null) { 115 try { 116 MusicUtils.sService.stop(); 117 } catch (RemoteException ex) { 118 } 119 } 120 Intent intent = new Intent(Intent.ACTION_VIEW); 121 intent.setDataAndType( 122 ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id), type); 123 124 startActivity(intent); 125 } 126 MakeCursor()127 private void MakeCursor() { 128 String[] audiocols = new String[] {MediaStore.Audio.Media._ID, 129 MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, 130 MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA, 131 MediaStore.Audio.Media.MIME_TYPE, MediaStore.Audio.Media.YEAR}; 132 String[] videocols = new String[] {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE, 133 MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, 134 MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA, 135 MediaStore.Audio.Media.MIME_TYPE}; 136 137 Cursor[] cs; 138 // Use ArrayList for the moment, since we don't know the size of 139 // Cursor[]. If the length of Corsor[] larger than really used, 140 // a NPE will come up when access the content of Corsor[]. 141 ArrayList<Cursor> cList = new ArrayList<Cursor>(); 142 Intent intent = getIntent(); 143 String type = intent.getType(); 144 145 if (mFirstYear != null) { 146 // If mFirstYear is not null, the picker only for audio because 147 // video has no year column. 148 if (type.equals("video/*")) { 149 mCursor = null; 150 return; 151 } 152 153 mWhereClause = MediaStore.Audio.Media.YEAR + ">=" + mFirstYear + " AND " 154 + MediaStore.Audio.Media.YEAR + "<=" + mLastYear; 155 } 156 157 // If use Cursor[] as before, the Cursor[i] could be null when there is 158 // no video/audio/sdcard. Then a NPE will come up when access the content of the 159 // Array. 160 161 Cursor c; 162 if (type.equals("video/*")) { 163 // Only video. 164 c = MusicUtils.query(this, MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videocols, null, 165 null, mSortOrder); 166 if (c != null) { 167 cList.add(c); 168 } 169 } else { 170 c = MusicUtils.query(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, audiocols, 171 mWhereClause, null, mSortOrder); 172 173 if (c != null) { 174 cList.add(c); 175 } 176 177 if (mFirstYear == null && intent.getType().equals("media/*")) { 178 // video has no year column 179 c = MusicUtils.query(this, MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videocols, 180 null, null, mSortOrder); 181 if (c != null) { 182 cList.add(c); 183 } 184 } 185 } 186 187 // Get the ArrayList size. 188 int size = cList.size(); 189 if (0 == size) { 190 // If no video/audio/SDCard exist, return. 191 mCursor = null; 192 return; 193 } 194 195 // The size is known now, we're sure each item of Cursor[] is not null. 196 cs = new Cursor[size]; 197 cs = cList.toArray(cs); 198 mCursor = new SortCursor(cs, MediaStore.Audio.Media.TITLE); 199 } 200 201 private Cursor mCursor; 202 private String mSortOrder = MediaStore.Audio.Media.TITLE + " COLLATE UNICODE"; 203 private String mFirstYear; 204 private String mLastYear; 205 private String mWhereClause; 206 207 static class PickListAdapter extends SimpleCursorAdapter { 208 int mTitleIdx; 209 int mArtistIdx; 210 int mAlbumIdx; 211 int mMimeIdx; 212 PickListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to)213 PickListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) { 214 super(context, layout, cursor, from, to); 215 216 mTitleIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE); 217 mArtistIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST); 218 mAlbumIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM); 219 mMimeIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE); 220 } 221 222 @Override newView(Context context, Cursor cursor, ViewGroup parent)223 public View newView(Context context, Cursor cursor, ViewGroup parent) { 224 View v = super.newView(context, cursor, parent); 225 ImageView iv = (ImageView) v.findViewById(R.id.icon); 226 iv.setVisibility(View.VISIBLE); 227 ViewGroup.LayoutParams p = iv.getLayoutParams(); 228 p.width = ViewGroup.LayoutParams.WRAP_CONTENT; 229 p.height = ViewGroup.LayoutParams.WRAP_CONTENT; 230 231 TextView tv = (TextView) v.findViewById(R.id.duration); 232 tv.setVisibility(View.GONE); 233 iv = (ImageView) v.findViewById(R.id.play_indicator); 234 iv.setVisibility(View.GONE); 235 236 return v; 237 } 238 239 @Override bindView(View view, Context context, Cursor cursor)240 public void bindView(View view, Context context, Cursor cursor) { 241 TextView tv = (TextView) view.findViewById(R.id.line1); 242 String name = cursor.getString(mTitleIdx); 243 tv.setText(name); 244 245 tv = (TextView) view.findViewById(R.id.line2); 246 name = cursor.getString(mAlbumIdx); 247 StringBuilder builder = new StringBuilder(); 248 if (name == null || name.equals(MediaStore.UNKNOWN_STRING)) { 249 builder.append(context.getString(R.string.unknown_album_name)); 250 } else { 251 builder.append(name); 252 } 253 builder.append("\n"); 254 name = cursor.getString(mArtistIdx); 255 if (name == null || name.equals(MediaStore.UNKNOWN_STRING)) { 256 builder.append(context.getString(R.string.unknown_artist_name)); 257 } else { 258 builder.append(name); 259 } 260 tv.setText(builder.toString()); 261 262 String text = cursor.getString(mMimeIdx); 263 ImageView iv = (ImageView) view.findViewById(R.id.icon); 264 ; 265 if ("audio/midi".equals(text)) { 266 iv.setImageResource(R.drawable.midi); 267 } else if (text != null && (text.startsWith("audio") || text.equals("application/ogg") 268 || text.equals("application/x-ogg"))) { 269 iv.setImageResource(R.drawable.ic_search_category_music_song); 270 } else if (text != null && text.startsWith("video")) { 271 iv.setImageResource(R.drawable.movie); 272 } else { 273 iv.setImageResource(0); 274 } 275 } 276 } 277 } 278