blob: 66be663d143e88a7701a2915a8e26ce28675871e [file] [log] [blame]
Jeff Sharkey76112212013-08-06 11:26:10 -07001/*
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
17package com.android.documentsui;
18
Steve McKayd9caa6a2016-09-15 16:36:45 -070019import static com.android.documentsui.base.Shared.TAG;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070020
Jeff Sharkey76112212013-08-06 11:26:10 -070021import android.app.AlertDialog;
22import android.app.Dialog;
23import android.app.DialogFragment;
24import android.app.FragmentManager;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070025import android.content.ContentProviderClient;
Jeff Sharkey76112212013-08-06 11:26:10 -070026import android.content.ContentResolver;
Jeff Sharkey76112212013-08-06 11:26:10 -070027import android.content.Context;
28import android.content.DialogInterface;
29import android.content.DialogInterface.OnClickListener;
30import android.net.Uri;
Jeff Sharkey9dd02622013-09-27 16:44:11 -070031import android.os.AsyncTask;
Jeff Sharkey76112212013-08-06 11:26:10 -070032import android.os.Bundle;
Jeff Sharkeyb6a7f2c2013-08-27 18:26:48 -070033import android.provider.DocumentsContract;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070034import android.provider.DocumentsContract.Document;
Steve McKaye9ec61e2015-12-01 15:00:43 -080035import android.support.annotation.Nullable;
Ben Kwac4693342015-09-30 10:00:10 -070036import android.support.design.widget.Snackbar;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070037import android.util.Log;
Steve McKayceeb3f72015-05-19 16:10:25 -070038import android.view.KeyEvent;
Aga Wronska279ec312016-02-08 09:14:25 -080039import android.view.inputmethod.EditorInfo;
Jeff Sharkey76112212013-08-06 11:26:10 -070040import android.view.LayoutInflater;
41import android.view.View;
42import android.widget.EditText;
Steve McKayceeb3f72015-05-19 16:10:25 -070043import android.widget.TextView;
44import android.widget.TextView.OnEditorActionListener;
Jeff Sharkey76112212013-08-06 11:26:10 -070045
Steve McKayd0805062016-09-15 14:30:38 -070046import com.android.documentsui.base.DocumentInfo;
Steve McKayd9caa6a2016-09-15 16:36:45 -070047import com.android.documentsui.base.Shared;
Steve McKayc8889af2016-09-23 11:22:41 -070048import com.android.documentsui.ui.Snackbars;
Jeff Sharkey76112212013-08-06 11:26:10 -070049
50/**
51 * Dialog to create a new directory.
52 */
53public class CreateDirectoryFragment extends DialogFragment {
54 private static final String TAG_CREATE_DIRECTORY = "create_directory";
55
56 public static void show(FragmentManager fm) {
57 final CreateDirectoryFragment dialog = new CreateDirectoryFragment();
58 dialog.show(fm, TAG_CREATE_DIRECTORY);
59 }
60
61 @Override
62 public Dialog onCreateDialog(Bundle savedInstanceState) {
63 final Context context = getActivity();
64 final ContentResolver resolver = context.getContentResolver();
65
Tomasz Mikolajewski137437e2015-08-03 19:32:34 +090066 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey76112212013-08-06 11:26:10 -070067 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
68
Aga Wronska3c237182016-01-20 16:32:33 -080069 final View view = dialogInflater.inflate(R.layout.dialog_file_name, null, false);
Steve McKayceeb3f72015-05-19 16:10:25 -070070 final EditText editText = (EditText) view.findViewById(android.R.id.text1);
Jeff Sharkey76112212013-08-06 11:26:10 -070071
72 builder.setTitle(R.string.menu_create_dir);
73 builder.setView(view);
74
Steve McKayceeb3f72015-05-19 16:10:25 -070075 builder.setPositiveButton(
76 android.R.string.ok,
77 new OnClickListener() {
78 @Override
79 public void onClick(DialogInterface dialog, int which) {
80 createDirectory(editText.getText().toString());
81 }
82 });
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070083
Jeff Sharkey76112212013-08-06 11:26:10 -070084 builder.setNegativeButton(android.R.string.cancel, null);
Steve McKayceeb3f72015-05-19 16:10:25 -070085 final AlertDialog dialog = builder.create();
Jeff Sharkey76112212013-08-06 11:26:10 -070086
Aga Wronska741ac6f2016-03-02 16:00:22 -080087 // Workaround for the problem - virtual keyboard doesn't show on the phone.
88 Shared.ensureKeyboardPresent(context, dialog);
89
Steve McKayceeb3f72015-05-19 16:10:25 -070090 editText.setOnEditorActionListener(
91 new OnEditorActionListener() {
92 @Override
Steve McKaye9ec61e2015-12-01 15:00:43 -080093 public boolean onEditorAction(
94 TextView view, int actionId, @Nullable KeyEvent event) {
Aga Wronska279ec312016-02-08 09:14:25 -080095 if ((actionId == EditorInfo.IME_ACTION_DONE) || (event != null
Steve McKaye9ec61e2015-12-01 15:00:43 -080096 && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
Aga Wronska279ec312016-02-08 09:14:25 -080097 && event.hasNoModifiers())) {
Steve McKayceeb3f72015-05-19 16:10:25 -070098 createDirectory(editText.getText().toString());
99 dialog.dismiss();
100 return true;
101 }
102 return false;
103 }
104 });
105
106
107 return dialog;
108 }
109
110 private void createDirectory(String name) {
111 final BaseActivity activity = (BaseActivity) getActivity();
112 final DocumentInfo cwd = activity.getCurrentDirectory();
113
114 new CreateDirectoryTask(activity, cwd, name).executeOnExecutor(
115 ProviderExecutor.forAuthority(cwd.authority));
Jeff Sharkey76112212013-08-06 11:26:10 -0700116 }
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700117
118 private class CreateDirectoryTask extends AsyncTask<Void, Void, DocumentInfo> {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700119 private final BaseActivity mActivity;
Jeff Sharkey0aa26922013-10-08 17:09:18 -0700120 private final DocumentInfo mCwd;
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700121 private final String mDisplayName;
122
Jeff Sharkey0aa26922013-10-08 17:09:18 -0700123 public CreateDirectoryTask(
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700124 BaseActivity activity, DocumentInfo cwd, String displayName) {
Jeff Sharkey0aa26922013-10-08 17:09:18 -0700125 mActivity = activity;
126 mCwd = cwd;
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700127 mDisplayName = displayName;
128 }
129
130 @Override
Jeff Sharkey4be51f12013-10-23 15:46:38 -0700131 protected void onPreExecute() {
132 mActivity.setPending(true);
133 }
134
135 @Override
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700136 protected DocumentInfo doInBackground(Void... params) {
Jeff Sharkey0aa26922013-10-08 17:09:18 -0700137 final ContentResolver resolver = mActivity.getContentResolver();
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700138 ContentProviderClient client = null;
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700139 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700140 client = DocumentsApplication.acquireUnstableProviderOrThrow(
Jeff Sharkey0aa26922013-10-08 17:09:18 -0700141 resolver, mCwd.derivedUri.getAuthority());
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700142 final Uri childUri = DocumentsContract.createDocument(
Jeff Sharkey0aa26922013-10-08 17:09:18 -0700143 client, mCwd.derivedUri, Document.MIME_TYPE_DIR, mDisplayName);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700144 return DocumentInfo.fromUri(resolver, childUri);
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700145 } catch (Exception e) {
146 Log.w(TAG, "Failed to create directory", e);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700147 return null;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700148 } finally {
149 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700150 }
151 }
152
153 @Override
154 protected void onPostExecute(DocumentInfo result) {
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700155 if (result != null) {
156 // Navigate into newly created child
Steve McKay351a7492015-08-04 10:11:01 -0700157 mActivity.onDirectoryCreated(result);
Aga Wronska46a868a2016-03-30 10:57:04 -0700158 Metrics.logCreateDirOperation(getContext());
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700159 } else {
Aga Wronska46a868a2016-03-30 10:57:04 -0700160 Snackbars.makeSnackbar(mActivity, R.string.create_error, Snackbar.LENGTH_SHORT)
161 .show();
162 Metrics.logCreateDirError(getContext());
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700163 }
Jeff Sharkey4be51f12013-10-23 15:46:38 -0700164 mActivity.setPending(false);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700165 }
166 }
Jeff Sharkey76112212013-08-06 11:26:10 -0700167}