Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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.documentsui; |
| 18 | |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.DocumentsActivity.TAG; |
| 20 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 21 | import android.app.AlertDialog; |
| 22 | import android.app.Dialog; |
| 23 | import android.app.DialogFragment; |
| 24 | import android.app.FragmentManager; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 25 | import android.content.ContentProviderClient; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 26 | import android.content.ContentResolver; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 27 | import android.content.Context; |
| 28 | import android.content.DialogInterface; |
| 29 | import android.content.DialogInterface.OnClickListener; |
| 30 | import android.net.Uri; |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 31 | import android.os.AsyncTask; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 32 | import android.os.Bundle; |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 33 | import android.provider.DocumentsContract; |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 34 | import android.provider.DocumentsContract.Document; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 35 | import android.util.Log; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 36 | import android.view.LayoutInflater; |
| 37 | import android.view.View; |
| 38 | import android.widget.EditText; |
| 39 | import android.widget.Toast; |
| 40 | |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 41 | import com.android.documentsui.model.DocumentInfo; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Dialog to create a new directory. |
| 45 | */ |
| 46 | public class CreateDirectoryFragment extends DialogFragment { |
| 47 | private static final String TAG_CREATE_DIRECTORY = "create_directory"; |
| 48 | |
| 49 | public static void show(FragmentManager fm) { |
| 50 | final CreateDirectoryFragment dialog = new CreateDirectoryFragment(); |
| 51 | dialog.show(fm, TAG_CREATE_DIRECTORY); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 56 | final Context context = getActivity(); |
| 57 | final ContentResolver resolver = context.getContentResolver(); |
| 58 | |
| 59 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 60 | final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); |
| 61 | |
| 62 | final View view = dialogInflater.inflate(R.layout.dialog_create_dir, null, false); |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 63 | final EditText text1 = (EditText) view.findViewById(android.R.id.text1); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 64 | |
| 65 | builder.setTitle(R.string.menu_create_dir); |
| 66 | builder.setView(view); |
| 67 | |
| 68 | builder.setPositiveButton(android.R.string.ok, new OnClickListener() { |
| 69 | @Override |
| 70 | public void onClick(DialogInterface dialog, int which) { |
| 71 | final String displayName = text1.getText().toString(); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 72 | |
| 73 | final DocumentsActivity activity = (DocumentsActivity) getActivity(); |
| 74 | final DocumentInfo cwd = activity.getCurrentDirectory(); |
| 75 | |
Jeff Sharkey | 0aa2692 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 76 | new CreateDirectoryTask(activity, cwd, displayName).executeOnExecutor( |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 77 | ProviderExecutor.forAuthority(cwd.authority)); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 78 | } |
| 79 | }); |
| 80 | builder.setNegativeButton(android.R.string.cancel, null); |
| 81 | |
| 82 | return builder.create(); |
| 83 | } |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 84 | |
| 85 | private class CreateDirectoryTask extends AsyncTask<Void, Void, DocumentInfo> { |
Jeff Sharkey | 0aa2692 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 86 | private final DocumentsActivity mActivity; |
| 87 | private final DocumentInfo mCwd; |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 88 | private final String mDisplayName; |
| 89 | |
Jeff Sharkey | 0aa2692 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 90 | public CreateDirectoryTask( |
| 91 | DocumentsActivity activity, DocumentInfo cwd, String displayName) { |
| 92 | mActivity = activity; |
| 93 | mCwd = cwd; |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 94 | mDisplayName = displayName; |
| 95 | } |
| 96 | |
| 97 | @Override |
Jeff Sharkey | 4be51f1 | 2013-10-23 15:46:38 -0700 | [diff] [blame^] | 98 | protected void onPreExecute() { |
| 99 | mActivity.setPending(true); |
| 100 | } |
| 101 | |
| 102 | @Override |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 103 | protected DocumentInfo doInBackground(Void... params) { |
Jeff Sharkey | 0aa2692 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 104 | final ContentResolver resolver = mActivity.getContentResolver(); |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 105 | ContentProviderClient client = null; |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 106 | try { |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 107 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
Jeff Sharkey | 0aa2692 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 108 | resolver, mCwd.derivedUri.getAuthority()); |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 109 | final Uri childUri = DocumentsContract.createDocument( |
Jeff Sharkey | 0aa2692 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 110 | client, mCwd.derivedUri, Document.MIME_TYPE_DIR, mDisplayName); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 111 | return DocumentInfo.fromUri(resolver, childUri); |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 112 | } catch (Exception e) { |
| 113 | Log.w(TAG, "Failed to create directory", e); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 114 | return null; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 115 | } finally { |
| 116 | ContentProviderClient.releaseQuietly(client); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | protected void onPostExecute(DocumentInfo result) { |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 122 | if (result != null) { |
| 123 | // Navigate into newly created child |
Jeff Sharkey | 0aa2692 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 124 | mActivity.onDocumentPicked(result); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 125 | } else { |
Jeff Sharkey | 0aa2692 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 126 | Toast.makeText(mActivity, R.string.create_error, Toast.LENGTH_SHORT).show(); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 127 | } |
Jeff Sharkey | 4be51f1 | 2013-10-23 15:46:38 -0700 | [diff] [blame^] | 128 | |
| 129 | mActivity.setPending(false); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 132 | } |