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