CreateDirectoryFragment.java revision ae9b51bfa313c51a31af30875a71255d7b6d2e61
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 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; 24import android.content.Context; 25import android.content.DialogInterface; 26import android.content.DialogInterface.OnClickListener; 27import android.net.Uri; 28import android.os.Bundle; 29import android.provider.DocumentsContract; 30import android.provider.DocumentsContract.Document; 31import android.view.LayoutInflater; 32import android.view.View; 33import android.widget.EditText; 34import android.widget.Toast; 35 36import com.android.documentsui.model.DocumentInfo; 37 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); 58 final EditText text1 = (EditText) view.findViewById(android.R.id.text1); 59 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 68 final DocumentsActivity activity = (DocumentsActivity) getActivity(); 69 final DocumentInfo cwd = activity.getCurrentDirectory(); 70 71 try { 72 final Uri childUri = DocumentsContract.createDocument( 73 resolver, cwd.uri, Document.MIME_TYPE_DIR, displayName); 74 75 // Navigate into newly created child 76 final DocumentInfo childDoc = DocumentInfo.fromUri(resolver, childUri); 77 activity.onDocumentPicked(childDoc); 78 } catch (Exception e) { 79 Toast.makeText(context, R.string.save_error, Toast.LENGTH_SHORT).show(); 80 } 81 } 82 }); 83 builder.setNegativeButton(android.R.string.cancel, null); 84 85 return builder.create(); 86 } 87} 88