blob: ebc9082185b1b400b12bceead23abe75abe9d367 [file] [log] [blame]
Jeff Sharkey66516692013-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 McKay58efce32015-08-20 16:19:38 +000019import static com.android.documentsui.Shared.TAG;
Jeff Sharkey7aa76012013-09-30 14:26:27 -070020
Jeff Sharkey66516692013-08-06 11:26:10 -070021import android.app.AlertDialog;
22import android.app.Dialog;
23import android.app.DialogFragment;
24import android.app.FragmentManager;
Jeff Sharkey7aa76012013-09-30 14:26:27 -070025import android.content.ContentProviderClient;
Jeff Sharkey66516692013-08-06 11:26:10 -070026import android.content.ContentResolver;
Jeff Sharkey66516692013-08-06 11:26:10 -070027import android.content.Context;
28import android.content.DialogInterface;
29import android.content.DialogInterface.OnClickListener;
30import android.net.Uri;
Jeff Sharkey6efba222013-09-27 16:44:11 -070031import android.os.AsyncTask;
Jeff Sharkey66516692013-08-06 11:26:10 -070032import android.os.Bundle;
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -070033import android.provider.DocumentsContract;
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -070034import android.provider.DocumentsContract.Document;
Steve McKay7e392b72015-12-01 15:00:43 -080035import android.support.annotation.Nullable;
Ben Kwa94b486d2015-09-30 10:00:10 -070036import android.support.design.widget.Snackbar;
Jeff Sharkey7aa76012013-09-30 14:26:27 -070037import android.util.Log;
Steve McKaya521d0d2015-05-19 16:10:25 -070038import android.view.KeyEvent;
Aga Wronska40416802016-02-08 09:14:25 -080039import android.view.inputmethod.EditorInfo;
Jeff Sharkey66516692013-08-06 11:26:10 -070040import android.view.LayoutInflater;
41import android.view.View;
42import android.widget.EditText;
Steve McKaya521d0d2015-05-19 16:10:25 -070043import android.widget.TextView;
44import android.widget.TextView.OnEditorActionListener;
Jeff Sharkey66516692013-08-06 11:26:10 -070045
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -070046import com.android.documentsui.model.DocumentInfo;
Jeff Sharkey66516692013-08-06 11:26:10 -070047
48/**
49 * Dialog to create a new directory.
50 */
51public 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 Mikolajewski5527f642015-08-03 19:32:34 +090064 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey66516692013-08-06 11:26:10 -070065 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
66
Aga Wronska3b327ef2016-01-20 16:32:33 -080067 final View view = dialogInflater.inflate(R.layout.dialog_file_name, null, false);
Steve McKaya521d0d2015-05-19 16:10:25 -070068 final EditText editText = (EditText) view.findViewById(android.R.id.text1);
Jeff Sharkey66516692013-08-06 11:26:10 -070069
70 builder.setTitle(R.string.menu_create_dir);
71 builder.setView(view);
72
Steve McKaya521d0d2015-05-19 16:10:25 -070073 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 Sharkeyd01571e2013-10-01 17:57:41 -070081
Jeff Sharkey66516692013-08-06 11:26:10 -070082 builder.setNegativeButton(android.R.string.cancel, null);
Steve McKaya521d0d2015-05-19 16:10:25 -070083 final AlertDialog dialog = builder.create();
Jeff Sharkey66516692013-08-06 11:26:10 -070084
Aga Wronska02c532f2016-03-02 16:00:22 -080085 // Workaround for the problem - virtual keyboard doesn't show on the phone.
86 Shared.ensureKeyboardPresent(context, dialog);
87
Steve McKaya521d0d2015-05-19 16:10:25 -070088 editText.setOnEditorActionListener(
89 new OnEditorActionListener() {
90 @Override
Steve McKay7e392b72015-12-01 15:00:43 -080091 public boolean onEditorAction(
92 TextView view, int actionId, @Nullable KeyEvent event) {
Aga Wronska40416802016-02-08 09:14:25 -080093 if ((actionId == EditorInfo.IME_ACTION_DONE) || (event != null
Steve McKay7e392b72015-12-01 15:00:43 -080094 && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
Aga Wronska40416802016-02-08 09:14:25 -080095 && event.hasNoModifiers())) {
Steve McKaya521d0d2015-05-19 16:10:25 -070096 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 Sharkey66516692013-08-06 11:26:10 -0700114 }
Jeff Sharkey6efba222013-09-27 16:44:11 -0700115
116 private class CreateDirectoryTask extends AsyncTask<Void, Void, DocumentInfo> {
Steve McKaye934ce62015-03-25 14:35:33 -0700117 private final BaseActivity mActivity;
Jeff Sharkey78b13702013-10-08 17:09:18 -0700118 private final DocumentInfo mCwd;
Jeff Sharkey6efba222013-09-27 16:44:11 -0700119 private final String mDisplayName;
120
Jeff Sharkey78b13702013-10-08 17:09:18 -0700121 public CreateDirectoryTask(
Steve McKaye934ce62015-03-25 14:35:33 -0700122 BaseActivity activity, DocumentInfo cwd, String displayName) {
Jeff Sharkey78b13702013-10-08 17:09:18 -0700123 mActivity = activity;
124 mCwd = cwd;
Jeff Sharkey6efba222013-09-27 16:44:11 -0700125 mDisplayName = displayName;
126 }
127
128 @Override
Jeff Sharkey04d45a02013-10-23 15:46:38 -0700129 protected void onPreExecute() {
130 mActivity.setPending(true);
131 }
132
133 @Override
Jeff Sharkey6efba222013-09-27 16:44:11 -0700134 protected DocumentInfo doInBackground(Void... params) {
Jeff Sharkey78b13702013-10-08 17:09:18 -0700135 final ContentResolver resolver = mActivity.getContentResolver();
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700136 ContentProviderClient client = null;
Jeff Sharkey6efba222013-09-27 16:44:11 -0700137 try {
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700138 client = DocumentsApplication.acquireUnstableProviderOrThrow(
Jeff Sharkey78b13702013-10-08 17:09:18 -0700139 resolver, mCwd.derivedUri.getAuthority());
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700140 final Uri childUri = DocumentsContract.createDocument(
Jeff Sharkey78b13702013-10-08 17:09:18 -0700141 client, mCwd.derivedUri, Document.MIME_TYPE_DIR, mDisplayName);
Jeff Sharkey6efba222013-09-27 16:44:11 -0700142 return DocumentInfo.fromUri(resolver, childUri);
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700143 } catch (Exception e) {
144 Log.w(TAG, "Failed to create directory", e);
Jeff Sharkey6efba222013-09-27 16:44:11 -0700145 return null;
Jeff Sharkey7aa76012013-09-30 14:26:27 -0700146 } finally {
147 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey6efba222013-09-27 16:44:11 -0700148 }
149 }
150
151 @Override
152 protected void onPostExecute(DocumentInfo result) {
Jeff Sharkey6efba222013-09-27 16:44:11 -0700153 if (result != null) {
154 // Navigate into newly created child
Steve McKay6eaf38632015-08-04 10:11:01 -0700155 mActivity.onDirectoryCreated(result);
Jeff Sharkey6efba222013-09-27 16:44:11 -0700156 } else {
Steve McKay5bbae102015-10-01 11:39:24 -0700157 Snackbars.makeSnackbar(mActivity, R.string.create_error, Snackbar.LENGTH_SHORT).show();
Jeff Sharkey6efba222013-09-27 16:44:11 -0700158 }
Jeff Sharkey04d45a02013-10-23 15:46:38 -0700159
160 mActivity.setPending(false);
Jeff Sharkey6efba222013-09-27 16:44:11 -0700161 }
162 }
Jeff Sharkey66516692013-08-06 11:26:10 -0700163}