blob: 0f4ef88556f643117aa78a82b97d21380a2adde9 [file] [log] [blame]
The Android Open Source Project792a2202009-03-03 19:32:30 -08001/*
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
17package com.android.music;
18
19import android.app.Activity;
20import android.content.ContentResolver;
21import android.content.ContentUris;
22import android.content.ContentValues;
23import android.content.Intent;
24import android.database.Cursor;
25import android.media.AudioManager;
26import android.net.Uri;
27import android.os.Bundle;
28import android.provider.MediaStore;
29import android.text.Editable;
30import android.text.TextWatcher;
The Android Open Source Project792a2202009-03-03 19:32:30 -080031import android.view.View;
32import android.view.Window;
33import android.view.WindowManager;
34import android.widget.Button;
35import android.widget.EditText;
36import android.widget.TextView;
37
Jack Heb0fba8b2017-01-26 15:54:38 -080038public class CreatePlaylist extends Activity {
The Android Open Source Project792a2202009-03-03 19:32:30 -080039 private EditText mPlaylist;
40 private TextView mPrompt;
41 private Button mSaveButton;
42
43 @Override
44 public void onCreate(Bundle icicle) {
45 super.onCreate(icicle);
46 setVolumeControlStream(AudioManager.STREAM_MUSIC);
47
48 requestWindowFeature(Window.FEATURE_NO_TITLE);
49 setContentView(R.layout.create_playlist);
Jack Heb0fba8b2017-01-26 15:54:38 -080050 getWindow().setLayout(
51 WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
The Android Open Source Project792a2202009-03-03 19:32:30 -080052
Jack Heb0fba8b2017-01-26 15:54:38 -080053 mPrompt = (TextView) findViewById(R.id.prompt);
54 mPlaylist = (EditText) findViewById(R.id.playlist);
The Android Open Source Project792a2202009-03-03 19:32:30 -080055 mSaveButton = (Button) findViewById(R.id.create);
56 mSaveButton.setOnClickListener(mOpenClicked);
57
Jack Heb0fba8b2017-01-26 15:54:38 -080058 ((Button) findViewById(R.id.cancel)).setOnClickListener(new View.OnClickListener() {
The Android Open Source Project792a2202009-03-03 19:32:30 -080059 public void onClick(View v) {
60 finish();
61 }
62 });
Jack Heb0fba8b2017-01-26 15:54:38 -080063
The Android Open Source Project792a2202009-03-03 19:32:30 -080064 String defaultname = icicle != null ? icicle.getString("defaultname") : makePlaylistName();
65 if (defaultname == null) {
66 finish();
67 return;
68 }
69 String promptformat = getString(R.string.create_playlist_create_text_prompt);
70 String prompt = String.format(promptformat, defaultname);
71 mPrompt.setText(prompt);
72 mPlaylist.setText(defaultname);
73 mPlaylist.setSelection(defaultname.length());
74 mPlaylist.addTextChangedListener(mTextWatcher);
75 }
Jack Heb0fba8b2017-01-26 15:54:38 -080076
The Android Open Source Project792a2202009-03-03 19:32:30 -080077 TextWatcher mTextWatcher = new TextWatcher() {
78 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
79 // don't care about this one
80 }
81 public void onTextChanged(CharSequence s, int start, int before, int count) {
Marco Nelissen538837b2010-02-12 09:46:07 -080082 String newText = mPlaylist.getText().toString();
83 if (newText.trim().length() == 0) {
84 mSaveButton.setEnabled(false);
The Android Open Source Project792a2202009-03-03 19:32:30 -080085 } else {
Marco Nelissen538837b2010-02-12 09:46:07 -080086 mSaveButton.setEnabled(true);
87 // check if playlist with current name exists already, and warn the user if so.
88 if (idForplaylist(newText) >= 0) {
89 mSaveButton.setText(R.string.create_playlist_overwrite_text);
90 } else {
91 mSaveButton.setText(R.string.create_playlist_create_text);
92 }
The Android Open Source Project792a2202009-03-03 19:32:30 -080093 }
94 };
95 public void afterTextChanged(Editable s) {
96 // don't care about this one
97 }
98 };
Jack Heb0fba8b2017-01-26 15:54:38 -080099
The Android Open Source Project792a2202009-03-03 19:32:30 -0800100 private int idForplaylist(String name) {
101 Cursor c = MusicUtils.query(this, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
Jack Heb0fba8b2017-01-26 15:54:38 -0800102 new String[] {MediaStore.Audio.Playlists._ID},
103 MediaStore.Audio.Playlists.NAME + "=?", new String[] {name},
The Android Open Source Project792a2202009-03-03 19:32:30 -0800104 MediaStore.Audio.Playlists.NAME);
105 int id = -1;
106 if (c != null) {
107 c.moveToFirst();
108 if (!c.isAfterLast()) {
109 id = c.getInt(0);
110 }
Marco Nelissen45c9ca32010-02-12 15:07:38 -0800111 c.close();
The Android Open Source Project792a2202009-03-03 19:32:30 -0800112 }
The Android Open Source Project792a2202009-03-03 19:32:30 -0800113 return id;
114 }
Jack Heb0fba8b2017-01-26 15:54:38 -0800115
The Android Open Source Project792a2202009-03-03 19:32:30 -0800116 @Override
117 public void onSaveInstanceState(Bundle outcicle) {
118 outcicle.putString("defaultname", mPlaylist.getText().toString());
119 }
Jack Heb0fba8b2017-01-26 15:54:38 -0800120
The Android Open Source Project792a2202009-03-03 19:32:30 -0800121 @Override
122 public void onResume() {
123 super.onResume();
124 }
125
126 private String makePlaylistName() {
The Android Open Source Project792a2202009-03-03 19:32:30 -0800127 String template = getString(R.string.new_playlist_name_template);
128 int num = 1;
129
Jack Heb0fba8b2017-01-26 15:54:38 -0800130 String[] cols = new String[] {MediaStore.Audio.Playlists.NAME};
The Android Open Source Project792a2202009-03-03 19:32:30 -0800131 ContentResolver resolver = getContentResolver();
132 String whereclause = MediaStore.Audio.Playlists.NAME + " != ''";
Jack Heb0fba8b2017-01-26 15:54:38 -0800133 Cursor c = resolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, cols,
134 whereclause, null, MediaStore.Audio.Playlists.NAME);
The Android Open Source Project792a2202009-03-03 19:32:30 -0800135
136 if (c == null) {
137 return null;
138 }
Jack Heb0fba8b2017-01-26 15:54:38 -0800139
The Android Open Source Project792a2202009-03-03 19:32:30 -0800140 String suggestedname;
141 suggestedname = String.format(template, num++);
Jack Heb0fba8b2017-01-26 15:54:38 -0800142
The Android Open Source Project792a2202009-03-03 19:32:30 -0800143 // Need to loop until we've made 1 full pass through without finding a match.
144 // Looping more than once shouldn't happen very often, but will happen if
145 // you have playlists named "New Playlist 1"/10/2/3/4/5/6/7/8/9, where
146 // making only one pass would result in "New Playlist 10" being erroneously
147 // picked for the new name.
148 boolean done = false;
149 while (!done) {
150 done = true;
151 c.moveToFirst();
Jack Heb0fba8b2017-01-26 15:54:38 -0800152 while (!c.isAfterLast()) {
The Android Open Source Project792a2202009-03-03 19:32:30 -0800153 String playlistname = c.getString(0);
154 if (playlistname.compareToIgnoreCase(suggestedname) == 0) {
155 suggestedname = String.format(template, num++);
156 done = false;
157 }
158 c.moveToNext();
159 }
160 }
161 c.close();
162 return suggestedname;
163 }
Jack Heb0fba8b2017-01-26 15:54:38 -0800164
The Android Open Source Project792a2202009-03-03 19:32:30 -0800165 private View.OnClickListener mOpenClicked = new View.OnClickListener() {
166 public void onClick(View v) {
167 String name = mPlaylist.getText().toString();
168 if (name != null && name.length() > 0) {
169 ContentResolver resolver = getContentResolver();
170 int id = idForplaylist(name);
171 Uri uri;
172 if (id >= 0) {
Jack Heb0fba8b2017-01-26 15:54:38 -0800173 uri = ContentUris.withAppendedId(
174 MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, id);
The Android Open Source Project792a2202009-03-03 19:32:30 -0800175 MusicUtils.clearPlaylist(CreatePlaylist.this, id);
176 } else {
177 ContentValues values = new ContentValues(1);
178 values.put(MediaStore.Audio.Playlists.NAME, name);
179 uri = resolver.insert(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, values);
180 }
181 setResult(RESULT_OK, (new Intent()).setData(uri));
182 finish();
183 }
184 }
185 };
186}