/* * 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 android.app.Activity; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.media.AudioManager; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class CreatePlaylist extends Activity { private EditText mPlaylist; private TextView mPrompt; private Button mSaveButton; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setVolumeControlStream(AudioManager.STREAM_MUSIC); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.create_playlist); getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT); mPrompt = (TextView)findViewById(R.id.prompt); mPlaylist = (EditText)findViewById(R.id.playlist); mSaveButton = (Button) findViewById(R.id.create); mSaveButton.setOnClickListener(mOpenClicked); ((Button)findViewById(R.id.cancel)).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { finish(); } }); String defaultname = icicle != null ? icicle.getString("defaultname") : makePlaylistName(); if (defaultname == null) { finish(); return; } String promptformat = getString(R.string.create_playlist_create_text_prompt); String prompt = String.format(promptformat, defaultname); mPrompt.setText(prompt); mPlaylist.setText(defaultname); mPlaylist.setSelection(defaultname.length()); mPlaylist.addTextChangedListener(mTextWatcher); } TextWatcher mTextWatcher = new TextWatcher() { public void beforeTextChanged(CharSequence s, int start, int count, int after) { // don't care about this one } public void onTextChanged(CharSequence s, int start, int before, int count) { String newText = mPlaylist.getText().toString(); if (newText.trim().length() == 0) { mSaveButton.setEnabled(false); } else { mSaveButton.setEnabled(true); // check if playlist with current name exists already, and warn the user if so. if (idForplaylist(newText) >= 0) { mSaveButton.setText(R.string.create_playlist_overwrite_text); } else { mSaveButton.setText(R.string.create_playlist_create_text); } } }; public void afterTextChanged(Editable s) { // don't care about this one } }; private int idForplaylist(String name) { Cursor c = MusicUtils.query(this, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Audio.Playlists._ID }, MediaStore.Audio.Playlists.NAME + "=?", new String[] { name }, MediaStore.Audio.Playlists.NAME); int id = -1; if (c != null) { c.moveToFirst(); if (!c.isAfterLast()) { id = c.getInt(0); } c.close(); } return id; } @Override public void onSaveInstanceState(Bundle outcicle) { outcicle.putString("defaultname", mPlaylist.getText().toString()); } @Override public void onResume() { super.onResume(); } private String makePlaylistName() { String template = getString(R.string.new_playlist_name_template); int num = 1; String[] cols = new String[] { MediaStore.Audio.Playlists.NAME }; ContentResolver resolver = getContentResolver(); String whereclause = MediaStore.Audio.Playlists.NAME + " != ''"; Cursor c = resolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, cols, whereclause, null, MediaStore.Audio.Playlists.NAME); if (c == null) { return null; } String suggestedname; suggestedname = String.format(template, num++); // Need to loop until we've made 1 full pass through without finding a match. // Looping more than once shouldn't happen very often, but will happen if // you have playlists named "New Playlist 1"/10/2/3/4/5/6/7/8/9, where // making only one pass would result in "New Playlist 10" being erroneously // picked for the new name. boolean done = false; while (!done) { done = true; c.moveToFirst(); while (! c.isAfterLast()) { String playlistname = c.getString(0); if (playlistname.compareToIgnoreCase(suggestedname) == 0) { suggestedname = String.format(template, num++); done = false; } c.moveToNext(); } } c.close(); return suggestedname; } private View.OnClickListener mOpenClicked = new View.OnClickListener() { public void onClick(View v) { String name = mPlaylist.getText().toString(); if (name != null && name.length() > 0) { ContentResolver resolver = getContentResolver(); int id = idForplaylist(name); Uri uri; if (id >= 0) { uri = ContentUris.withAppendedId(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, id); MusicUtils.clearPlaylist(CreatePlaylist.this, id); } else { ContentValues values = new ContentValues(1); values.put(MediaStore.Audio.Playlists.NAME, name); uri = resolver.insert(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, values); } setResult(RESULT_OK, (new Intent()).setData(uri)); finish(); } } }; }