blob: 23a3f22f4226de2afeb8678ac0320b1c38746634 [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
Jeff Sharkey3fd11772013-09-30 14:26:27 -070019import static com.android.documentsui.DocumentsActivity.TAG;
20
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;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070035import android.util.Log;
Jeff Sharkey76112212013-08-06 11:26:10 -070036import android.view.LayoutInflater;
37import android.view.View;
38import android.widget.EditText;
39import android.widget.Toast;
40
Jeff Sharkey724deeb2013-08-31 15:02:20 -070041import com.android.documentsui.model.DocumentInfo;
Jeff Sharkey76112212013-08-06 11:26:10 -070042
43/**
44 * Dialog to create a new directory.
45 */
46public class CreateDirectoryFragment extends DialogFragment {
47 private static final String TAG_CREATE_DIRECTORY = "create_directory";
48
49 public static void show(FragmentManager fm) {
50 final CreateDirectoryFragment dialog = new CreateDirectoryFragment();
51 dialog.show(fm, TAG_CREATE_DIRECTORY);
52 }
53
54 @Override
55 public Dialog onCreateDialog(Bundle savedInstanceState) {
56 final Context context = getActivity();
57 final ContentResolver resolver = context.getContentResolver();
58
59 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
60 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
61
62 final View view = dialogInflater.inflate(R.layout.dialog_create_dir, null, false);
Jeff Sharkeyb6a7f2c2013-08-27 18:26:48 -070063 final EditText text1 = (EditText) view.findViewById(android.R.id.text1);
Jeff Sharkey76112212013-08-06 11:26:10 -070064
65 builder.setTitle(R.string.menu_create_dir);
66 builder.setView(view);
67
68 builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
69 @Override
70 public void onClick(DialogInterface dialog, int which) {
71 final String displayName = text1.getText().toString();
Jeff Sharkey9dd02622013-09-27 16:44:11 -070072 new CreateDirectoryTask(displayName).execute();
Jeff Sharkey76112212013-08-06 11:26:10 -070073 }
74 });
75 builder.setNegativeButton(android.R.string.cancel, null);
76
77 return builder.create();
78 }
Jeff Sharkey9dd02622013-09-27 16:44:11 -070079
80 private class CreateDirectoryTask extends AsyncTask<Void, Void, DocumentInfo> {
81 private final String mDisplayName;
82
83 public CreateDirectoryTask(String displayName) {
84 mDisplayName = displayName;
85 }
86
87 @Override
88 protected DocumentInfo doInBackground(Void... params) {
89 final DocumentsActivity activity = (DocumentsActivity) getActivity();
90 final ContentResolver resolver = activity.getContentResolver();
91
92 final DocumentInfo cwd = activity.getCurrentDirectory();
Jeff Sharkey3fd11772013-09-30 14:26:27 -070093
94 ContentProviderClient client = null;
Jeff Sharkey9dd02622013-09-27 16:44:11 -070095 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -070096 client = DocumentsApplication.acquireUnstableProviderOrThrow(
97 resolver, cwd.derivedUri.getAuthority());
98 final Uri childUri = DocumentsContract.createDocument(
99 client, cwd.derivedUri, Document.MIME_TYPE_DIR, mDisplayName);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700100 return DocumentInfo.fromUri(resolver, childUri);
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700101 } catch (Exception e) {
102 Log.w(TAG, "Failed to create directory", e);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700103 return null;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700104 } finally {
105 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700106 }
107 }
108
109 @Override
110 protected void onPostExecute(DocumentInfo result) {
111 final DocumentsActivity activity = (DocumentsActivity) getActivity();
112 if (result != null) {
113 // Navigate into newly created child
114 activity.onDocumentPicked(result);
115 } else {
116 Toast.makeText(activity, R.string.create_error, Toast.LENGTH_SHORT).show();
117 }
118 }
119 }
Jeff Sharkey76112212013-08-06 11:26:10 -0700120}