• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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.model.DocumentInfo;
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 
show(FragmentManager fm)54     public static void show(FragmentManager fm) {
55         final CreateDirectoryFragment dialog = new CreateDirectoryFragment();
56         dialog.show(fm, TAG_CREATE_DIRECTORY);
57     }
58 
59     @Override
onCreateDialog(Bundle savedInstanceState)60     public Dialog onCreateDialog(Bundle savedInstanceState) {
61         final Context context = getActivity();
62         final ContentResolver resolver = context.getContentResolver();
63 
64         final AlertDialog.Builder builder = new AlertDialog.Builder(context);
65         final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
66 
67         final View view = dialogInflater.inflate(R.layout.dialog_file_name, null, false);
68         final EditText editText = (EditText) view.findViewById(android.R.id.text1);
69 
70         builder.setTitle(R.string.menu_create_dir);
71         builder.setView(view);
72 
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                 });
81 
82         builder.setNegativeButton(android.R.string.cancel, null);
83         final AlertDialog dialog = builder.create();
84 
85         // Workaround for the problem - virtual keyboard doesn't show on the phone.
86         Shared.ensureKeyboardPresent(context, dialog);
87 
88         editText.setOnEditorActionListener(
89                 new OnEditorActionListener() {
90                     @Override
91                     public boolean onEditorAction(
92                             TextView view, int actionId, @Nullable KeyEvent event) {
93                         if ((actionId == EditorInfo.IME_ACTION_DONE) || (event != null
94                                 && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
95                                 && event.hasNoModifiers())) {
96                             createDirectory(editText.getText().toString());
97                             dialog.dismiss();
98                             return true;
99                         }
100                         return false;
101                     }
102                 });
103 
104 
105         return dialog;
106     }
107 
createDirectory(String name)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));
114     }
115 
116     private class CreateDirectoryTask extends AsyncTask<Void, Void, DocumentInfo> {
117         private final BaseActivity mActivity;
118         private final DocumentInfo mCwd;
119         private final String mDisplayName;
120 
CreateDirectoryTask( BaseActivity activity, DocumentInfo cwd, String displayName)121         public CreateDirectoryTask(
122                 BaseActivity activity, DocumentInfo cwd, String displayName) {
123             mActivity = activity;
124             mCwd = cwd;
125             mDisplayName = displayName;
126         }
127 
128         @Override
onPreExecute()129         protected void onPreExecute() {
130             mActivity.setPending(true);
131         }
132 
133         @Override
doInBackground(Void... params)134         protected DocumentInfo doInBackground(Void... params) {
135             final ContentResolver resolver = mActivity.getContentResolver();
136             ContentProviderClient client = null;
137             try {
138                 client = DocumentsApplication.acquireUnstableProviderOrThrow(
139                         resolver, mCwd.derivedUri.getAuthority());
140                 final Uri childUri = DocumentsContract.createDocument(
141                         client, mCwd.derivedUri, Document.MIME_TYPE_DIR, mDisplayName);
142                 return DocumentInfo.fromUri(resolver, childUri);
143             } catch (Exception e) {
144                 Log.w(TAG, "Failed to create directory", e);
145                 return null;
146             } finally {
147                 ContentProviderClient.releaseQuietly(client);
148             }
149         }
150 
151         @Override
onPostExecute(DocumentInfo result)152         protected void onPostExecute(DocumentInfo result) {
153             if (result != null) {
154                 // Navigate into newly created child
155                 mActivity.onDirectoryCreated(result);
156                 Metrics.logCreateDirOperation(getContext());
157             } else {
158                 Snackbars.makeSnackbar(mActivity, R.string.create_error, Snackbar.LENGTH_SHORT)
159                         .show();
160                 Metrics.logCreateDirError(getContext());
161             }
162             mActivity.setPending(false);
163         }
164     }
165 }
166