blob: 48bfaf0d7c3493fc247764de74153314b8ffecb4 [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
19import android.app.AlertDialog;
20import android.app.Dialog;
21import android.app.DialogFragment;
22import android.app.FragmentManager;
23import android.content.ContentResolver;
Jeff Sharkey76112212013-08-06 11:26:10 -070024import android.content.Context;
25import android.content.DialogInterface;
26import android.content.DialogInterface.OnClickListener;
27import android.net.Uri;
Jeff Sharkey9dd02622013-09-27 16:44:11 -070028import android.os.AsyncTask;
Jeff Sharkey76112212013-08-06 11:26:10 -070029import android.os.Bundle;
Jeff Sharkeyb6a7f2c2013-08-27 18:26:48 -070030import android.provider.DocumentsContract;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070031import android.provider.DocumentsContract.Document;
Jeff Sharkey76112212013-08-06 11:26:10 -070032import android.view.LayoutInflater;
33import android.view.View;
34import android.widget.EditText;
35import android.widget.Toast;
36
Jeff Sharkey724deeb2013-08-31 15:02:20 -070037import com.android.documentsui.model.DocumentInfo;
Jeff Sharkey76112212013-08-06 11:26:10 -070038
Jeff Sharkey9dd02622013-09-27 16:44:11 -070039import java.io.FileNotFoundException;
40
Jeff Sharkey76112212013-08-06 11:26:10 -070041/**
42 * Dialog to create a new directory.
43 */
44public class CreateDirectoryFragment extends DialogFragment {
45 private static final String TAG_CREATE_DIRECTORY = "create_directory";
46
47 public static void show(FragmentManager fm) {
48 final CreateDirectoryFragment dialog = new CreateDirectoryFragment();
49 dialog.show(fm, TAG_CREATE_DIRECTORY);
50 }
51
52 @Override
53 public Dialog onCreateDialog(Bundle savedInstanceState) {
54 final Context context = getActivity();
55 final ContentResolver resolver = context.getContentResolver();
56
57 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
58 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
59
60 final View view = dialogInflater.inflate(R.layout.dialog_create_dir, null, false);
Jeff Sharkeyb6a7f2c2013-08-27 18:26:48 -070061 final EditText text1 = (EditText) view.findViewById(android.R.id.text1);
Jeff Sharkey76112212013-08-06 11:26:10 -070062
63 builder.setTitle(R.string.menu_create_dir);
64 builder.setView(view);
65
66 builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
67 @Override
68 public void onClick(DialogInterface dialog, int which) {
69 final String displayName = text1.getText().toString();
Jeff Sharkey9dd02622013-09-27 16:44:11 -070070 new CreateDirectoryTask(displayName).execute();
Jeff Sharkey76112212013-08-06 11:26:10 -070071 }
72 });
73 builder.setNegativeButton(android.R.string.cancel, null);
74
75 return builder.create();
76 }
Jeff Sharkey9dd02622013-09-27 16:44:11 -070077
78 private class CreateDirectoryTask extends AsyncTask<Void, Void, DocumentInfo> {
79 private final String mDisplayName;
80
81 public CreateDirectoryTask(String displayName) {
82 mDisplayName = displayName;
83 }
84
85 @Override
86 protected DocumentInfo doInBackground(Void... params) {
87 final DocumentsActivity activity = (DocumentsActivity) getActivity();
88 final ContentResolver resolver = activity.getContentResolver();
89
90 final DocumentInfo cwd = activity.getCurrentDirectory();
91 final Uri childUri = DocumentsContract.createDocument(
92 resolver, cwd.derivedUri, Document.MIME_TYPE_DIR, mDisplayName);
93 try {
94 return DocumentInfo.fromUri(resolver, childUri);
95 } catch (FileNotFoundException e) {
96 return null;
97 }
98 }
99
100 @Override
101 protected void onPostExecute(DocumentInfo result) {
102 final DocumentsActivity activity = (DocumentsActivity) getActivity();
103 if (result != null) {
104 // Navigate into newly created child
105 activity.onDocumentPicked(result);
106 } else {
107 Toast.makeText(activity, R.string.create_error, Toast.LENGTH_SHORT).show();
108 }
109 }
110 }
Jeff Sharkey76112212013-08-06 11:26:10 -0700111}