OSDN Git Service

MediaFile.UNKNOWN_STRING -> MediaStore.UNKNOWN_STRING
[android-x86/packages-apps-Music.git] / src / com / android / music / MediaPickerActivity.java
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.internal.database.SortCursor;
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 {
41
42     public MediaPickerActivity()
43     {
44     }
45
46     /** Called when the activity is first created. */
47     @Override
48     public void onCreate(Bundle icicle)
49     {
50         super.onCreate(icicle);
51
52         mFirstYear = getIntent().getStringExtra("firstyear");
53         mLastYear = getIntent().getStringExtra("lastyear");
54
55         if (mFirstYear == null) {
56             setTitle(R.string.all_title);
57         } else if (mFirstYear.equals(mLastYear)) {
58             setTitle(mFirstYear);
59         } else {
60             setTitle(mFirstYear + "-" + mLastYear);
61         }
62         MusicUtils.bindToService(this);
63         init();
64     }
65
66     @Override
67     public void onDestroy() {
68         MusicUtils.unbindFromService(this);
69         super.onDestroy();
70         if (mCursor != null) {
71             mCursor.close();
72         }
73     }
74
75     public void init() {
76
77         setContentView(R.layout.media_picker_activity);
78
79         MakeCursor();
80         if (null == mCursor || 0 == mCursor.getCount()) {
81             return;
82         }
83
84         PickListAdapter adapter = new PickListAdapter(
85                 this,
86                 R.layout.track_list_item,
87                 mCursor,
88                 new String[] {},
89                 new int[] {});
90
91         setListAdapter(adapter);
92     }
93
94     @Override
95     protected void onListItemClick(ListView l, View v, int position, long id)
96     {
97         mCursor.moveToPosition(position);
98         String type = mCursor.getString(mCursor.getColumnIndexOrThrow(
99                 MediaStore.Audio.Media.MIME_TYPE));
100
101         String action = getIntent().getAction();
102         if (Intent.ACTION_GET_CONTENT.equals(action)) {
103             Uri uri;
104
105             long mediaId;
106             if (type.startsWith("video")) {
107                 uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
108                 mediaId = mCursor.getLong(mCursor.getColumnIndexOrThrow(
109                         MediaStore.Video.Media._ID));
110             } else {
111                 uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
112                 mediaId = mCursor.getLong(mCursor.getColumnIndexOrThrow(
113                         MediaStore.Audio.Media._ID));
114             }
115
116             setResult(RESULT_OK, new Intent().setData(ContentUris.withAppendedId(uri, mediaId)));
117             finish();
118             return;
119         }
120
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) {
124             try {
125                 MusicUtils.sService.stop();
126             } catch (RemoteException ex) {
127             }
128         }
129         Intent intent = new Intent(Intent.ACTION_VIEW);
130         intent.setDataAndType(ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id), type);
131
132         startActivity(intent);
133     }
134
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
144         };
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
153         };
154
155         Cursor[] cs;
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();
162
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/*")) {
167                 mCursor = null;
168                 return;
169             }
170
171             mWhereClause = MediaStore.Audio.Media.YEAR + ">=" + mFirstYear + " AND " +
172                            MediaStore.Audio.Media.YEAR + "<=" + mLastYear;
173         }
174
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
177         // Array.
178
179         Cursor c;
180         if (type.equals("video/*")) {
181             // Only video.
182             c = MusicUtils.query(this, MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
183                     videocols, null , null, mSortOrder);
184             if (c != null) {
185                 cList.add(c);
186             }
187         } else {
188             c = MusicUtils.query(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
189                     audiocols, mWhereClause , null, mSortOrder);
190
191             if (c != null) {
192                 cList.add(c);
193             }
194
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);
199                 if (c != null) {
200                     cList.add(c);
201                 }
202             }
203         }
204
205         // Get the ArrayList size.
206         int size = cList.size();
207         if (0 == size) {
208             // If no video/audio/SDCard exist, return.
209             mCursor = null;
210             return;
211         }
212
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);
217     }
218
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;
224
225     static class PickListAdapter extends SimpleCursorAdapter {
226         int mTitleIdx;
227         int mArtistIdx;
228         int mAlbumIdx;
229         int mMimeIdx;
230
231         PickListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
232             super(context, layout, cursor, from, to);
233
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);
238         }
239         
240         @Override
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;
248
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);
253            
254            return v;
255         }
256
257         @Override
258         public void bindView(View view, Context context, Cursor cursor) {
259
260             TextView tv = (TextView) view.findViewById(R.id.line1);
261             String name = cursor.getString(mTitleIdx);
262             tv.setText(name);
263             
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));
269             } else {
270                 builder.append(name);
271             }
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));
276             } else {
277                 builder.append(name);
278             }
279             tv.setText(builder.toString());
280
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);
291             } else {
292                 iv.setImageResource(0);
293             }
294         }
295     }
296 }