OSDN Git Service

Initial Contribution
[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.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;
37
38 import java.util.ArrayList;
39
40 public class MediaPickerActivity extends ListActivity implements MusicUtils.Defs
41 {
42
43     public MediaPickerActivity()
44     {
45     }
46
47     /** Called when the activity is first created. */
48     @Override
49     public void onCreate(Bundle icicle)
50     {
51         super.onCreate(icicle);
52
53         mFirstYear = getIntent().getStringExtra("firstyear");
54         mLastYear = getIntent().getStringExtra("lastyear");
55
56         if (mFirstYear == null) {
57             setTitle(R.string.all_title);
58         } else if (mFirstYear.equals(mLastYear)) {
59             setTitle(mFirstYear);
60         } else {
61             setTitle(mFirstYear + "-" + mLastYear);
62         }
63         MusicUtils.bindToService(this);
64         init();
65     }
66
67     @Override
68     public void onDestroy() {
69         MusicUtils.unbindFromService(this);
70         super.onDestroy();
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.getColumnIndex(MediaStore.Audio.Media.MIME_TYPE));
97
98         String action = getIntent().getAction();
99         if (Intent.ACTION_GET_CONTENT.equals(action)) {
100             Uri uri;
101
102             long mediaId;
103             if (type.startsWith("video")) {
104                 uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
105                 mediaId = mCursor.getLong(mCursor.getColumnIndex(MediaStore.Video.Media._ID));
106             } else {
107                 uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
108                 mediaId = mCursor.getLong(mCursor.getColumnIndex(MediaStore.Audio.Media._ID));
109             }
110
111             setResult(RESULT_OK, new Intent().setData(ContentUris.withAppendedId(uri, mediaId)));
112             finish();
113             return;
114         }
115
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) {
119             try {
120                 MusicUtils.sService.stop();
121             } catch (RemoteException ex) {
122             }
123         }
124         Intent intent = new Intent(Intent.ACTION_VIEW);
125         intent.setDataAndType(ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id), type);
126
127         startActivity(intent);
128     }
129
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
139         };
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
148         };
149
150         Cursor[] cs;
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();
157
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/*")) {
162                 mCursor = null;
163                 return;
164             }
165
166             mWhereClause = MediaStore.Audio.Media.YEAR + ">=" + mFirstYear + " AND " +
167                            MediaStore.Audio.Media.YEAR + "<=" + mLastYear;
168         }
169
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
172         // Array.
173
174         Cursor c;
175         if (type.equals("video/*")) {
176             // Only video.
177             c = MusicUtils.query(this, MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
178                     videocols, null , null, mSortOrder);
179             if (c != null) {
180                 cList.add(c);
181             }
182         } else {
183             c = MusicUtils.query(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
184                     audiocols, mWhereClause , null, mSortOrder);
185
186             if (c != null) {
187                 cList.add(c);
188             }
189
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);
194                 if (c != null) {
195                     cList.add(c);
196                 }
197             }
198         }
199
200         // Get the ArrayList size.
201         int size = cList.size();
202         if (0 == size) {
203             // If no video/audio/SDCard exist, return.
204             mCursor = null;
205             return;
206         }
207
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);
212     }
213
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;
219
220     class PickListAdapter extends SimpleCursorAdapter {
221         int mTitleIdx;
222         int mArtistIdx;
223         int mAlbumIdx;
224         int mMimeIdx;
225
226         PickListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
227             super(context, layout, cursor, from, to);
228
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);
233         }
234         
235         @Override
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;
243
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);
248            
249            return v;
250         }
251
252         @Override
253         public void bindView(View view, Context context, Cursor cursor) {
254
255             TextView tv = (TextView) view.findViewById(R.id.line1);
256             String name = cursor.getString(mTitleIdx);
257             tv.setText(name);
258             
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));
264             } else {
265                 builder.append(name);
266             }
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));
271             } else {
272                 builder.append(name);
273             }
274             tv.setText(builder.toString());
275
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);
286             } else {
287                 iv.setImageResource(0);
288             }
289         }
290     }
291 }