blob: e0b8d1971a6edc6fe8ae317e4a14c4992314d999 [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;
28import android.os.Bundle;
Jeff Sharkeyb6a7f2c2013-08-27 18:26:48 -070029import android.provider.DocumentsContract;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070030import android.provider.DocumentsContract.Document;
Jeff Sharkey76112212013-08-06 11:26:10 -070031import android.view.LayoutInflater;
32import android.view.View;
33import android.widget.EditText;
34import android.widget.Toast;
35
Jeff Sharkey724deeb2013-08-31 15:02:20 -070036import com.android.documentsui.model.DocumentInfo;
Jeff Sharkey76112212013-08-06 11:26:10 -070037
38/**
39 * Dialog to create a new directory.
40 */
41public class CreateDirectoryFragment extends DialogFragment {
42 private static final String TAG_CREATE_DIRECTORY = "create_directory";
43
44 public static void show(FragmentManager fm) {
45 final CreateDirectoryFragment dialog = new CreateDirectoryFragment();
46 dialog.show(fm, TAG_CREATE_DIRECTORY);
47 }
48
49 @Override
50 public Dialog onCreateDialog(Bundle savedInstanceState) {
51 final Context context = getActivity();
52 final ContentResolver resolver = context.getContentResolver();
53
54 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
55 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
56
57 final View view = dialogInflater.inflate(R.layout.dialog_create_dir, null, false);
Jeff Sharkeyb6a7f2c2013-08-27 18:26:48 -070058 final EditText text1 = (EditText) view.findViewById(android.R.id.text1);
Jeff Sharkey76112212013-08-06 11:26:10 -070059
60 builder.setTitle(R.string.menu_create_dir);
61 builder.setView(view);
62
63 builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
64 @Override
65 public void onClick(DialogInterface dialog, int which) {
66 final String displayName = text1.getText().toString();
67
Jeff Sharkey76112212013-08-06 11:26:10 -070068 final DocumentsActivity activity = (DocumentsActivity) getActivity();
Jeff Sharkey724deeb2013-08-31 15:02:20 -070069 final DocumentInfo cwd = activity.getCurrentDirectory();
Jeff Sharkey76112212013-08-06 11:26:10 -070070
Jeff Sharkey744dbbd2013-08-07 18:33:33 -070071 try {
Jeff Sharkey724deeb2013-08-31 15:02:20 -070072 final Uri childUri = DocumentsContract.createDocument(
73 resolver, cwd.uri, Document.MIME_TYPE_DIR, displayName);
Jeff Sharkeyb6a7f2c2013-08-27 18:26:48 -070074
Jeff Sharkey76112212013-08-06 11:26:10 -070075 // Navigate into newly created child
Jeff Sharkey724deeb2013-08-31 15:02:20 -070076 final DocumentInfo childDoc = DocumentInfo.fromUri(resolver, childUri);
Jeff Sharkey76112212013-08-06 11:26:10 -070077 activity.onDocumentPicked(childDoc);
Jeff Sharkeyb6a7f2c2013-08-27 18:26:48 -070078 } catch (Exception e) {
Jeff Sharkey76112212013-08-06 11:26:10 -070079 Toast.makeText(context, R.string.save_error, Toast.LENGTH_SHORT).show();
Jeff Sharkey76112212013-08-06 11:26:10 -070080 }
81 }
82 });
83 builder.setNegativeButton(android.R.string.cancel, null);
84
85 return builder.create();
86 }
87}