OSDN Git Service

c06ea29a26d2d4fe29f2de280161c38d3bb51626
[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 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;
34
35 import java.util.ArrayList;
36
37 public class MediaPickerActivity extends ListActivity implements MusicUtils.Defs
38 {
39
40     public MediaPickerActivity()
41     {
42     }
43
44     /** Called when the activity is first created. */
45     @Override
46     public void onCreate(Bundle icicle)
47     {
48         super.onCreate(icicle);
49
50         mFirstYear = getIntent().getStringExtra("firstyear");
51         mLastYear = getIntent().getStringExtra("lastyear");
52
53         if (mFirstYear == null) {
54             setTitle(R.string.all_title);
55         } else if (mFirstYear.equals(mLastYear)) {
56             setTitle(mFirstYear);
57         } else {
58             setTitle(mFirstYear + "-" + mLastYear);
59         }
60         MusicUtils.bindToService(this);
61         init();
62     }
63
64     @Override
65     public void onDestroy() {
66         MusicUtils.unbindFromService(this);
67         super.onDestroy();
68         if (mCursor != null) {
69             mCursor.close();
70         }
71     }
72
73     public void init() {
74
75         setContentView(R.layout.media_picker_activity);
76
77         MakeCursor();
78         if (null == mCursor || 0 == mCursor.getCount()) {
79             return;
80         }
81
82         PickListAdapter adapter = new PickListAdapter(
83                 this,
84                 R.layout.track_list_item,
85                 mCursor,
86                 new String[] {},
87                 new int[] {});
88
89         setListAdapter(adapter);
90     }
91
92     @Override
93     protected void onListItemClick(ListView l, View v, int position, long id)
94     {
95         mCursor.moveToPosition(position);
96         String type = mCursor.getString(mCursor.getColumnIndexOrThrow(
97                 MediaStore.Audio.Media.MIME_TYPE));
98
99         String action = getIntent().getAction();
100         if (Intent.ACTION_GET_CONTENT.equals(action)) {
101             Uri uri;
102
103             long mediaId;
104             if (type.startsWith("video")) {
105                 uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
106                 mediaId = mCursor.getLong(mCursor.getColumnIndexOrThrow(
107                         MediaStore.Video.Media._ID));
108             } else {
109                 uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
110                 mediaId = mCursor.getLong(mCursor.getColumnIndexOrThrow(
111                         MediaStore.Audio.Media._ID));
112             }
113
114             setResult(RESULT_OK, new Intent().setData(ContentUris.withAppendedId(uri, mediaId)));
115             finish();
116             return;
117         }
118
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) {
122             try {
123                 MusicUtils.sService.stop();
124             } catch (RemoteException ex) {
125             }
126         }
127         Intent intent = new Intent(Intent.ACTION_VIEW);
128         intent.setDataAndType(ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id), type);
129
130         startActivity(intent);
131     }
132
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
142         };
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
151         };
152
153         Cursor[] cs;
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();
160
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/*")) {
165                 mCursor = null;
166                 return;
167             }
168
169             mWhereClause = MediaStore.Audio.Media.YEAR + ">=" + mFirstYear + " AND " +
170                            MediaStore.Audio.Media.YEAR + "<=" + mLastYear;
171         }
172
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
175         // Array.
176
177         Cursor c;
178         if (type.equals("video/*")) {
179             // Only video.
180             c = MusicUtils.query(this, MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
181                     videocols, null , null, mSortOrder);
182             if (c != null) {
183                 cList.add(c);
184             }
185         } else {
186             c = MusicUtils.query(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
187                     audiocols, mWhereClause , null, mSortOrder);
188
189             if (c != null) {
190                 cList.add(c);
191             }
192
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);
197                 if (c != null) {
198                     cList.add(c);
199                 }
200             }
201         }
202
203         // Get the ArrayList size.
204         int size = cList.size();
205         if (0 == size) {
206             // If no video/audio/SDCard exist, return.
207             mCursor = null;
208             return;
209         }
210
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);
215     }
216
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;
222
223     static class PickListAdapter extends SimpleCursorAdapter {
224         int mTitleIdx;
225         int mArtistIdx;
226         int mAlbumIdx;
227         int mMimeIdx;
228
229         PickListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
230             super(context, layout, cursor, from, to);
231
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);
236         }
237         
238         @Override
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;
246
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);
251            
252            return v;
253         }
254
255         @Override
256         public void bindView(View view, Context context, Cursor cursor) {
257
258             TextView tv = (TextView) view.findViewById(R.id.line1);
259             String name = cursor.getString(mTitleIdx);
260             tv.setText(name);
261             
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));
267             } else {
268                 builder.append(name);
269             }
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));
274             } else {
275                 builder.append(name);
276             }
277             tv.setText(builder.toString());
278
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);
289             } else {
290                 iv.setImageResource(0);
291             }
292         }
293     }
294 }