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 
19 import static com.android.documentsui.base.Shared.TAG;
20 
21 import android.app.AlertDialog;
22 import android.app.Dialog;
23 import android.app.DialogFragment;
24 import android.app.FragmentManager;
25 import android.content.ContentProviderClient;
26 import android.content.ContentResolver;
27 import android.content.Context;
28 import android.content.DialogInterface;
29 import android.content.DialogInterface.OnClickListener;
30 import android.net.Uri;
31 import android.os.AsyncTask;
32 import android.os.Bundle;
33 import android.provider.DocumentsContract;
34 import android.provider.DocumentsContract.Document;
35 import android.support.annotation.Nullable;
36 import android.support.design.widget.Snackbar;
37 import android.util.Log;
38 import android.view.KeyEvent;
39 import android.view.inputmethod.EditorInfo;
40 import android.view.LayoutInflater;
41 import android.view.View;
42 import android.widget.EditText;
43 import android.widget.TextView;
44 import android.widget.TextView.OnEditorActionListener;
45 
46 import com.android.documentsui.base.DocumentInfo;
47 import com.android.documentsui.base.Shared;
48 import com.android.documentsui.ui.Snackbars;
49 
50 /**
51  * Dialog to create a new directory.
52  */
53 public class CreateDirectoryFragment extends DialogFragment {
54     private static final String TAG_CREATE_DIRECTORY = "create_directory";
55 
show(FragmentManager fm)56     public static void show(FragmentManager fm) {
57         final CreateDirectoryFragment dialog = new CreateDirectoryFragment();
58         dialog.show(fm, TAG_CREATE_DIRECTORY);
59     }
60 
61     @Override
onCreateDialog(Bundle savedInstanceState)62     public Dialog onCreateDialog(Bundle savedInstanceState) {
63         final Context context = getActivity();
64         final ContentResolver resolver = context.getContentResolver();
65 
66         final AlertDialog.Builder builder = new AlertDialog.Builder(context);
67         final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
68 
69         final View view = dialogInflater.inflate(R.layout.dialog_file_name, null, false);
70         final EditText editText = (EditText) view.findViewById(android.R.id.text1);
71 
72         builder.setTitle(R.string.menu_create_dir);
73         builder.setView(view);
74 
75         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                 });
83 
84         builder.setNegativeButton(android.R.string.cancel, null);
85         final AlertDialog dialog = builder.create();
86 
87         // Workaround for the problem - virtual keyboard doesn't show on the phone.
88         Shared.ensureKeyboardPresent(context, dialog);
89 
90         editText.setOnEditorActionListener(
91                 new OnEditorActionListener() {
92                     @Override
93                     public boolean onEditorAction(
94                             TextView view, int actionId, @Nullable KeyEvent event) {
95                         if ((actionId == EditorInfo.IME_ACTION_DONE) || (event != null
96                                 && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
97                                 && event.hasNoModifiers())) {
98                             createDirectory(editText.getText().toString());
99                             dialog.dismiss();
100                             return true;
101                         }
102                         return false;
103                     }
104                 });
105 
106 
107         return dialog;
108     }
109 
createDirectory(String name)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));
116     }
117 
118     private class CreateDirectoryTask extends AsyncTask<Void, Void, DocumentInfo> {
119         private final BaseActivity mActivity;
120         private final DocumentInfo mCwd;
121         private final String mDisplayName;
122 
CreateDirectoryTask( BaseActivity activity, DocumentInfo cwd, String displayName)123         public CreateDirectoryTask(
124                 BaseActivity activity, DocumentInfo cwd, String displayName) {
125             mActivity = activity;
126             mCwd = cwd;
127             mDisplayName = displayName;
128         }
129 
130         @Override
onPreExecute()131         protected void onPreExecute() {
132             mActivity.setPending(true);
133         }
134 
135         @Override
doInBackground(Void... params)136         protected DocumentInfo doInBackground(Void... params) {
137             final ContentResolver resolver = mActivity.getContentResolver();
138             ContentProviderClient client = null;
139             try {
140                 client = DocumentsApplication.acquireUnstableProviderOrThrow(
141                         resolver, mCwd.derivedUri.getAuthority());
142                 final Uri childUri = DocumentsContract.createDocument(
143                         client, mCwd.derivedUri, Document.MIME_TYPE_DIR, mDisplayName);
144                 return DocumentInfo.fromUri(resolver, childUri);
145             } catch (Exception e) {
146                 Log.w(TAG, "Failed to create directory", e);
147                 return null;
148             } finally {
149                 ContentProviderClient.releaseQuietly(client);
150             }
151         }
152 
153         @Override
onPostExecute(DocumentInfo result)154         protected void onPostExecute(DocumentInfo result) {
155             if (result != null) {
156                 // Navigate into newly created child
157                 mActivity.onDirectoryCreated(result);
158                 Metrics.logCreateDirOperation(getContext());
159             } else {
160                 Snackbars.makeSnackbar(mActivity, R.string.create_error, Snackbar.LENGTH_SHORT)
161                         .show();
162                 Metrics.logCreateDirError(getContext());
163             }
164             mActivity.setPending(false);
165         }
166     }
167 }
168