Jeff Sharkey | 6651669 | 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 | |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 19 | import static com.android.documentsui.Shared.TAG; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 20 | |
Jeff Sharkey | 6651669 | 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 | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 25 | import android.content.ContentProviderClient; |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 26 | import android.content.ContentResolver; |
Jeff Sharkey | 6651669 | 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 | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 31 | import android.os.AsyncTask; |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 32 | import android.os.Bundle; |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 33 | import android.provider.DocumentsContract; |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 34 | import android.provider.DocumentsContract.Document; |
Steve McKay | 7e392b7 | 2015-12-01 15:00:43 -0800 | [diff] [blame] | 35 | import android.support.annotation.Nullable; |
Ben Kwa | 94b486d | 2015-09-30 10:00:10 -0700 | [diff] [blame] | 36 | import android.support.design.widget.Snackbar; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 37 | import android.util.Log; |
Steve McKay | a521d0d | 2015-05-19 16:10:25 -0700 | [diff] [blame] | 38 | import android.view.KeyEvent; |
Aga Wronska | 4041680 | 2016-02-08 09:14:25 -0800 | [diff] [blame] | 39 | import android.view.inputmethod.EditorInfo; |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 40 | import android.view.LayoutInflater; |
| 41 | import android.view.View; |
| 42 | import android.widget.EditText; |
Steve McKay | a521d0d | 2015-05-19 16:10:25 -0700 | [diff] [blame] | 43 | import android.widget.TextView; |
| 44 | import android.widget.TextView.OnEditorActionListener; |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 45 | |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 46 | import com.android.documentsui.model.DocumentInfo; |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Dialog to create a new directory. |
| 50 | */ |
| 51 | public class CreateDirectoryFragment extends DialogFragment { |
| 52 | private static final String TAG_CREATE_DIRECTORY = "create_directory"; |
| 53 | |
| 54 | public static void show(FragmentManager fm) { |
| 55 | final CreateDirectoryFragment dialog = new CreateDirectoryFragment(); |
| 56 | dialog.show(fm, TAG_CREATE_DIRECTORY); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 61 | final Context context = getActivity(); |
| 62 | final ContentResolver resolver = context.getContentResolver(); |
| 63 | |
Tomasz Mikolajewski | 5527f64 | 2015-08-03 19:32:34 +0900 | [diff] [blame] | 64 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 65 | final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); |
| 66 | |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 67 | final View view = dialogInflater.inflate(R.layout.dialog_file_name, null, false); |
Steve McKay | a521d0d | 2015-05-19 16:10:25 -0700 | [diff] [blame] | 68 | final EditText editText = (EditText) view.findViewById(android.R.id.text1); |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 69 | |
| 70 | builder.setTitle(R.string.menu_create_dir); |
| 71 | builder.setView(view); |
| 72 | |
Steve McKay | a521d0d | 2015-05-19 16:10:25 -0700 | [diff] [blame] | 73 | builder.setPositiveButton( |
| 74 | android.R.string.ok, |
| 75 | new OnClickListener() { |
| 76 | @Override |
| 77 | public void onClick(DialogInterface dialog, int which) { |
| 78 | createDirectory(editText.getText().toString()); |
| 79 | } |
| 80 | }); |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 81 | |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 82 | builder.setNegativeButton(android.R.string.cancel, null); |
Steve McKay | a521d0d | 2015-05-19 16:10:25 -0700 | [diff] [blame] | 83 | final AlertDialog dialog = builder.create(); |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 84 | |
Aga Wronska | 02c532f | 2016-03-02 16:00:22 -0800 | [diff] [blame] | 85 | // Workaround for the problem - virtual keyboard doesn't show on the phone. |
| 86 | Shared.ensureKeyboardPresent(context, dialog); |
| 87 | |
Steve McKay | a521d0d | 2015-05-19 16:10:25 -0700 | [diff] [blame] | 88 | editText.setOnEditorActionListener( |
| 89 | new OnEditorActionListener() { |
| 90 | @Override |
Steve McKay | 7e392b7 | 2015-12-01 15:00:43 -0800 | [diff] [blame] | 91 | public boolean onEditorAction( |
| 92 | TextView view, int actionId, @Nullable KeyEvent event) { |
Aga Wronska | 4041680 | 2016-02-08 09:14:25 -0800 | [diff] [blame] | 93 | if ((actionId == EditorInfo.IME_ACTION_DONE) || (event != null |
Steve McKay | 7e392b7 | 2015-12-01 15:00:43 -0800 | [diff] [blame] | 94 | && event.getKeyCode() == KeyEvent.KEYCODE_ENTER |
Aga Wronska | 4041680 | 2016-02-08 09:14:25 -0800 | [diff] [blame] | 95 | && event.hasNoModifiers())) { |
Steve McKay | a521d0d | 2015-05-19 16:10:25 -0700 | [diff] [blame] | 96 | createDirectory(editText.getText().toString()); |
| 97 | dialog.dismiss(); |
| 98 | return true; |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | }); |
| 103 | |
| 104 | |
| 105 | return dialog; |
| 106 | } |
| 107 | |
| 108 | private void createDirectory(String name) { |
| 109 | final BaseActivity activity = (BaseActivity) getActivity(); |
| 110 | final DocumentInfo cwd = activity.getCurrentDirectory(); |
| 111 | |
| 112 | new CreateDirectoryTask(activity, cwd, name).executeOnExecutor( |
| 113 | ProviderExecutor.forAuthority(cwd.authority)); |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 114 | } |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 115 | |
| 116 | private class CreateDirectoryTask extends AsyncTask<Void, Void, DocumentInfo> { |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 117 | private final BaseActivity mActivity; |
Jeff Sharkey | 78b1370 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 118 | private final DocumentInfo mCwd; |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 119 | private final String mDisplayName; |
| 120 | |
Jeff Sharkey | 78b1370 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 121 | public CreateDirectoryTask( |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 122 | BaseActivity activity, DocumentInfo cwd, String displayName) { |
Jeff Sharkey | 78b1370 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 123 | mActivity = activity; |
| 124 | mCwd = cwd; |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 125 | mDisplayName = displayName; |
| 126 | } |
| 127 | |
| 128 | @Override |
Jeff Sharkey | 04d45a0 | 2013-10-23 15:46:38 -0700 | [diff] [blame] | 129 | protected void onPreExecute() { |
| 130 | mActivity.setPending(true); |
| 131 | } |
| 132 | |
| 133 | @Override |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 134 | protected DocumentInfo doInBackground(Void... params) { |
Jeff Sharkey | 78b1370 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 135 | final ContentResolver resolver = mActivity.getContentResolver(); |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 136 | ContentProviderClient client = null; |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 137 | try { |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 138 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
Jeff Sharkey | 78b1370 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 139 | resolver, mCwd.derivedUri.getAuthority()); |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 140 | final Uri childUri = DocumentsContract.createDocument( |
Jeff Sharkey | 78b1370 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 141 | client, mCwd.derivedUri, Document.MIME_TYPE_DIR, mDisplayName); |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 142 | return DocumentInfo.fromUri(resolver, childUri); |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 143 | } catch (Exception e) { |
| 144 | Log.w(TAG, "Failed to create directory", e); |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 145 | return null; |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 146 | } finally { |
| 147 | ContentProviderClient.releaseQuietly(client); |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | protected void onPostExecute(DocumentInfo result) { |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 153 | if (result != null) { |
| 154 | // Navigate into newly created child |
Steve McKay | 6eaf3863 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 155 | mActivity.onDirectoryCreated(result); |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 156 | } else { |
Steve McKay | 5bbae10 | 2015-10-01 11:39:24 -0700 | [diff] [blame] | 157 | Snackbars.makeSnackbar(mActivity, R.string.create_error, Snackbar.LENGTH_SHORT).show(); |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 158 | } |
Jeff Sharkey | 04d45a0 | 2013-10-23 15:46:38 -0700 | [diff] [blame] | 159 | |
| 160 | mActivity.setPending(false); |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 163 | } |