Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 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 android.app.AlertDialog; |
| 20 | import android.app.Dialog; |
| 21 | import android.app.DialogFragment; |
| 22 | import android.app.FragmentManager; |
| 23 | import android.content.ContentResolver; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.DialogInterface; |
| 26 | import android.content.DialogInterface.OnClickListener; |
| 27 | import android.net.Uri; |
| 28 | import android.os.Bundle; |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 29 | import android.provider.DocumentsContract; |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame^] | 30 | import android.provider.DocumentsContract.Document; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 31 | import android.view.LayoutInflater; |
| 32 | import android.view.View; |
| 33 | import android.widget.EditText; |
| 34 | import android.widget.Toast; |
| 35 | |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame^] | 36 | import com.android.documentsui.model.DocumentInfo; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Dialog to create a new directory. |
| 40 | */ |
| 41 | public 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 Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 58 | final EditText text1 = (EditText) view.findViewById(android.R.id.text1); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 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 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 68 | final DocumentsActivity activity = (DocumentsActivity) getActivity(); |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame^] | 69 | final DocumentInfo cwd = activity.getCurrentDirectory(); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 70 | |
Jeff Sharkey | 744dbbd | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 71 | try { |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame^] | 72 | final Uri childUri = DocumentsContract.createDocument( |
| 73 | resolver, cwd.uri, Document.MIME_TYPE_DIR, displayName); |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 74 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 75 | // Navigate into newly created child |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame^] | 76 | final DocumentInfo childDoc = DocumentInfo.fromUri(resolver, childUri); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 77 | activity.onDocumentPicked(childDoc); |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 78 | } catch (Exception e) { |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 79 | Toast.makeText(context, R.string.save_error, Toast.LENGTH_SHORT).show(); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | }); |
| 83 | builder.setNegativeButton(android.R.string.cancel, null); |
| 84 | |
| 85 | return builder.create(); |
| 86 | } |
| 87 | } |