The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 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.Activity; |
| 20 | import android.content.ContentResolver; |
| 21 | import android.content.ContentUris; |
| 22 | import android.content.ContentValues; |
| 23 | import android.content.Intent; |
| 24 | import android.database.Cursor; |
| 25 | import android.media.AudioManager; |
| 26 | import android.net.Uri; |
| 27 | import android.os.Bundle; |
| 28 | import android.provider.MediaStore; |
| 29 | import android.text.Editable; |
| 30 | import android.text.TextWatcher; |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 31 | import android.view.View; |
| 32 | import android.view.Window; |
| 33 | import android.view.WindowManager; |
| 34 | import android.widget.Button; |
| 35 | import android.widget.EditText; |
| 36 | import android.widget.TextView; |
| 37 | |
Jack He | b0fba8b | 2017-01-26 15:54:38 -0800 | [diff] [blame] | 38 | public class CreatePlaylist extends Activity { |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 39 | 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 He | b0fba8b | 2017-01-26 15:54:38 -0800 | [diff] [blame] | 50 | getWindow().setLayout( |
| 51 | WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT); |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 52 | |
Jack He | b0fba8b | 2017-01-26 15:54:38 -0800 | [diff] [blame] | 53 | mPrompt = (TextView) findViewById(R.id.prompt); |
| 54 | mPlaylist = (EditText) findViewById(R.id.playlist); |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 55 | mSaveButton = (Button) findViewById(R.id.create); |
| 56 | mSaveButton.setOnClickListener(mOpenClicked); |
| 57 | |
Jack He | b0fba8b | 2017-01-26 15:54:38 -0800 | [diff] [blame] | 58 | ((Button) findViewById(R.id.cancel)).setOnClickListener(new View.OnClickListener() { |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 59 | public void onClick(View v) { |
| 60 | finish(); |
| 61 | } |
| 62 | }); |
Jack He | b0fba8b | 2017-01-26 15:54:38 -0800 | [diff] [blame] | 63 | |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 64 | 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 He | b0fba8b | 2017-01-26 15:54:38 -0800 | [diff] [blame] | 76 | |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 77 | 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 Nelissen | 538837b | 2010-02-12 09:46:07 -0800 | [diff] [blame] | 82 | String newText = mPlaylist.getText().toString(); |
| 83 | if (newText.trim().length() == 0) { |
| 84 | mSaveButton.setEnabled(false); |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 85 | } else { |
Marco Nelissen | 538837b | 2010-02-12 09:46:07 -0800 | [diff] [blame] | 86 | 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 Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 93 | } |
| 94 | }; |
| 95 | public void afterTextChanged(Editable s) { |
| 96 | // don't care about this one |
| 97 | } |
| 98 | }; |
Jack He | b0fba8b | 2017-01-26 15:54:38 -0800 | [diff] [blame] | 99 | |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 100 | private int idForplaylist(String name) { |
| 101 | Cursor c = MusicUtils.query(this, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, |
Jack He | b0fba8b | 2017-01-26 15:54:38 -0800 | [diff] [blame] | 102 | new String[] {MediaStore.Audio.Playlists._ID}, |
| 103 | MediaStore.Audio.Playlists.NAME + "=?", new String[] {name}, |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 104 | 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 Nelissen | 45c9ca3 | 2010-02-12 15:07:38 -0800 | [diff] [blame] | 111 | c.close(); |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 112 | } |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 113 | return id; |
| 114 | } |
Jack He | b0fba8b | 2017-01-26 15:54:38 -0800 | [diff] [blame] | 115 | |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 116 | @Override |
| 117 | public void onSaveInstanceState(Bundle outcicle) { |
| 118 | outcicle.putString("defaultname", mPlaylist.getText().toString()); |
| 119 | } |
Jack He | b0fba8b | 2017-01-26 15:54:38 -0800 | [diff] [blame] | 120 | |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 121 | @Override |
| 122 | public void onResume() { |
| 123 | super.onResume(); |
| 124 | } |
| 125 | |
| 126 | private String makePlaylistName() { |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 127 | String template = getString(R.string.new_playlist_name_template); |
| 128 | int num = 1; |
| 129 | |
Jack He | b0fba8b | 2017-01-26 15:54:38 -0800 | [diff] [blame] | 130 | String[] cols = new String[] {MediaStore.Audio.Playlists.NAME}; |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 131 | ContentResolver resolver = getContentResolver(); |
| 132 | String whereclause = MediaStore.Audio.Playlists.NAME + " != ''"; |
Jack He | b0fba8b | 2017-01-26 15:54:38 -0800 | [diff] [blame] | 133 | Cursor c = resolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, cols, |
| 134 | whereclause, null, MediaStore.Audio.Playlists.NAME); |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 135 | |
| 136 | if (c == null) { |
| 137 | return null; |
| 138 | } |
Jack He | b0fba8b | 2017-01-26 15:54:38 -0800 | [diff] [blame] | 139 | |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 140 | String suggestedname; |
| 141 | suggestedname = String.format(template, num++); |
Jack He | b0fba8b | 2017-01-26 15:54:38 -0800 | [diff] [blame] | 142 | |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 143 | // 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 He | b0fba8b | 2017-01-26 15:54:38 -0800 | [diff] [blame] | 152 | while (!c.isAfterLast()) { |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 153 | 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 He | b0fba8b | 2017-01-26 15:54:38 -0800 | [diff] [blame] | 164 | |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 165 | 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 He | b0fba8b | 2017-01-26 15:54:38 -0800 | [diff] [blame] | 173 | uri = ContentUris.withAppendedId( |
| 174 | MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, id); |
The Android Open Source Project | 792a220 | 2009-03-03 19:32:30 -0800 | [diff] [blame] | 175 | 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 | } |