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; |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 23 | import android.content.ContentProviderClient; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 24 | import android.content.ContentResolver; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.DialogInterface; |
| 27 | import android.content.DialogInterface.OnClickListener; |
| 28 | import android.net.Uri; |
| 29 | import android.os.Bundle; |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 30 | import android.provider.DocumentsContract; |
Jeff Sharkey | f339f25 | 2013-08-15 16:17:41 -0700 | [diff] [blame] | 31 | import android.provider.DocumentsContract.Documents; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 32 | import android.view.LayoutInflater; |
| 33 | import android.view.View; |
| 34 | import android.widget.EditText; |
| 35 | import android.widget.Toast; |
| 36 | |
| 37 | import com.android.documentsui.model.Document; |
| 38 | |
| 39 | /** |
| 40 | * Dialog to create a new directory. |
| 41 | */ |
| 42 | public class CreateDirectoryFragment extends DialogFragment { |
| 43 | private static final String TAG_CREATE_DIRECTORY = "create_directory"; |
| 44 | |
| 45 | public static void show(FragmentManager fm) { |
| 46 | final CreateDirectoryFragment dialog = new CreateDirectoryFragment(); |
| 47 | dialog.show(fm, TAG_CREATE_DIRECTORY); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 52 | final Context context = getActivity(); |
| 53 | final ContentResolver resolver = context.getContentResolver(); |
| 54 | |
| 55 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 56 | final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); |
| 57 | |
| 58 | final View view = dialogInflater.inflate(R.layout.dialog_create_dir, null, false); |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 59 | final EditText text1 = (EditText) view.findViewById(android.R.id.text1); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 60 | |
| 61 | builder.setTitle(R.string.menu_create_dir); |
| 62 | builder.setView(view); |
| 63 | |
| 64 | builder.setPositiveButton(android.R.string.ok, new OnClickListener() { |
| 65 | @Override |
| 66 | public void onClick(DialogInterface dialog, int which) { |
| 67 | final String displayName = text1.getText().toString(); |
| 68 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 69 | final DocumentsActivity activity = (DocumentsActivity) getActivity(); |
| 70 | final Document cwd = activity.getCurrentDirectory(); |
| 71 | |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 72 | final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( |
| 73 | cwd.uri.getAuthority()); |
Jeff Sharkey | 744dbbd | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 74 | try { |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 75 | final String docId = DocumentsContract.createDocument(client, |
| 76 | DocumentsContract.getDocId(cwd.uri), Documents.MIME_TYPE_DIR, |
| 77 | displayName); |
| 78 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 79 | // Navigate into newly created child |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 80 | final Uri childUri = DocumentsContract.buildDocumentUri( |
| 81 | cwd.uri.getAuthority(), docId); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 82 | final Document childDoc = Document.fromUri(resolver, childUri); |
| 83 | activity.onDocumentPicked(childDoc); |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 84 | } catch (Exception e) { |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 85 | Toast.makeText(context, R.string.save_error, Toast.LENGTH_SHORT).show(); |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame^] | 86 | } finally { |
| 87 | ContentProviderClient.closeQuietly(client); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | }); |
| 91 | builder.setNegativeButton(android.R.string.cancel, null); |
| 92 | |
| 93 | return builder.create(); |
| 94 | } |
| 95 | } |