summaryrefslogtreecommitdiffstats
path: root/CyanogenMod-android_packages_apps_Music-41e85d6/src/com/android/music/MediaPickerActivity.java
blob: 199ec0d83b0cb4c3d049792ade8e05e57664f2cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.music;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.android.music.MusicUtils.ServiceToken;

public class MediaPickerActivity extends ListActivity implements
		MusicUtils.Defs {
	private ServiceToken mToken;

	public MediaPickerActivity() {
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		mFirstYear = getIntent().getStringExtra("firstyear");
		mLastYear = getIntent().getStringExtra("lastyear");

		if (mFirstYear == null) {
			setTitle(R.string.all_title);
		} else if (mFirstYear.equals(mLastYear)) {
			setTitle(mFirstYear);
		} else {
			setTitle(mFirstYear + "-" + mLastYear);
		}
		mToken = MusicUtils.bindToService(this);
		init();
	}

	@Override
	public void onDestroy() {
		MusicUtils.unbindFromService(mToken);
		super.onDestroy();
		if (mCursor != null) {
			mCursor.close();
		}
	}

	public void init() {

		setContentView(R.layout.media_picker_activity);

		MakeCursor();
		if (null == mCursor || 0 == mCursor.getCount()) {
			return;
		}

		PickListAdapter adapter = new PickListAdapter(this,
				R.layout.track_list_item, mCursor, new String[] {},
				new int[] {});

		setListAdapter(adapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		mCursor.moveToPosition(position);
		String type = mCursor.getString(mCursor
				.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE));

		String action = getIntent().getAction();
		if (Intent.ACTION_GET_CONTENT.equals(action)) {
			Uri uri;

			long mediaId;
			if (type.startsWith("video")) {
				uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
				mediaId = mCursor.getLong(mCursor
						.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
			} else {
				uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
				mediaId = mCursor.getLong(mCursor
						.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
			}

			setResult(RESULT_OK, new Intent().setData(ContentUris
					.withAppendedId(uri, mediaId)));
			finish();
			return;
		}

		// Need to stop the playbackservice, in case it is busy playing audio
		// and the user selected a video.
		if (MusicUtils.sService != null) {
			try {
				MusicUtils.sService.stop();
			} catch (RemoteException ex) {
			}
		}
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setDataAndType(ContentUris.withAppendedId(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id), type);

		startActivity(intent);
	}

	private void MakeCursor() {
		String[] audiocols = new String[] { MediaStore.Audio.Media._ID,
				MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM,
				MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA,
				MediaStore.Audio.Media.MIME_TYPE, MediaStore.Audio.Media.YEAR };
		String[] videocols = new String[] { MediaStore.Audio.Media._ID,
				MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST,
				MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.TITLE,
				MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.MIME_TYPE };

		Cursor[] cs;
		// Use ArrayList for the moment, since we don't know the size of
		// Cursor[]. If the length of Corsor[] larger than really used,
		// a NPE will come up when access the content of Corsor[].
		ArrayList<Cursor> cList = new ArrayList<Cursor>();
		Intent intent = getIntent();
		String type = intent.getType();

		if (mFirstYear != null) {
			// If mFirstYear is not null, the picker only for audio because
			// video has no year column.
			if (type.equals("video/*")) {
				mCursor = null;
				return;
			}

			mWhereClause = MediaStore.Audio.Media.YEAR + ">=" + mFirstYear
					+ " AND " + MediaStore.Audio.Media.YEAR + "<=" + mLastYear;
		}

		// If use Cursor[] as before, the Cursor[i] could be null when there is
		// no video/audio/sdcard. Then a NPE will come up when access the
		// content of the
		// Array.

		Cursor c;
		if (type.equals("video/*")) {
			// Only video.
			c = MusicUtils.query(this,
					MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videocols,
					null, null, mSortOrder);
			if (c != null) {
				cList.add(c);
			}
		} else {
			c = MusicUtils.query(this,
					MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, audiocols,
					mWhereClause, null, mSortOrder);

			if (c != null) {
				cList.add(c);
			}

			if (mFirstYear == null && intent.getType().equals("media/*")) {
				// video has no year column
				c = MusicUtils.query(this,
						MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videocols,
						null, null, mSortOrder);
				if (c != null) {
					cList.add(c);
				}
			}
		}

		// Get the ArrayList size.
		int size = cList.size();
		if (0 == size) {
			// If no video/audio/SDCard exist, return.
			mCursor = null;
			return;
		}

		// The size is known now, we're sure each item of Cursor[] is not null.
		cs = new Cursor[size];
		cs = cList.toArray(cs);
		mCursor = new SortCursor(cs, MediaStore.Audio.Media.TITLE);
	}

	private Cursor mCursor;
	private String mSortOrder = MediaStore.Audio.Media.TITLE
			+ " COLLATE UNICODE";
	private String mFirstYear;
	private String mLastYear;
	private String mWhereClause;

	static class PickListAdapter extends SimpleCursorAdapter {
		int mTitleIdx;
		int mArtistIdx;
		int mAlbumIdx;
		int mMimeIdx;

		PickListAdapter(Context context, int layout, Cursor cursor,
				String[] from, int[] to) {
			super(context, layout, cursor, from, to);

			mTitleIdx = cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
			mArtistIdx = cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
			mAlbumIdx = cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);
			mMimeIdx = cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE);
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			View v = super.newView(context, cursor, parent);
			ImageView iv = (ImageView) v.findViewById(R.id.icon);
			iv.setVisibility(View.VISIBLE);
			ViewGroup.LayoutParams p = iv.getLayoutParams();
			p.width = ViewGroup.LayoutParams.WRAP_CONTENT;
			p.height = ViewGroup.LayoutParams.WRAP_CONTENT;

			TextView tv = (TextView) v.findViewById(R.id.duration);
			tv.setVisibility(View.GONE);
			iv = (ImageView) v.findViewById(R.id.play_indicator);
			iv.setVisibility(View.GONE);

			return v;
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {

			TextView tv = (TextView) view.findViewById(R.id.line1);
			String name = cursor.getString(mTitleIdx);
			tv.setText(name);

			tv = (TextView) view.findViewById(R.id.line2);
			name = cursor.getString(mAlbumIdx);
			StringBuilder builder = new StringBuilder();
			if (name == null || name.equals(MediaStore.UNKNOWN_STRING)) {
				builder.append(context.getString(R.string.unknown_album_name));
			} else {
				builder.append(name);
			}
			builder.append("\n");
			name = cursor.getString(mArtistIdx);
			if (name == null || name.equals(MediaStore.UNKNOWN_STRING)) {
				builder.append(context.getString(R.string.unknown_artist_name));
			} else {
				builder.append(name);
			}
			tv.setText(builder.toString());

			String text = cursor.getString(mMimeIdx);
			ImageView iv = (ImageView) view.findViewById(R.id.icon);
			;
			if ("audio/midi".equals(text)) {
				iv.setImageResource(R.drawable.midi);
			} else if (text != null
					&& (text.startsWith("audio")
							|| text.equals("application/ogg") || text
							.equals("application/x-ogg"))) {
			} else if (text != null && text.startsWith("video")) {
				iv.setImageResource(R.drawable.movie);
			} else {
				iv.setImageResource(0);
			}
		}
	}
}