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; |
| 24 | import android.content.ContentValues; |
| 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; |
| 30 | import android.provider.DocumentsContract; |
| 31 | import android.provider.DocumentsContract.DocumentColumns; |
| 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); |
| 59 | final EditText text1 = (EditText)view.findViewById(android.R.id.text1); |
| 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 | |
| 69 | final ContentValues values = new ContentValues(); |
| 70 | values.put(DocumentColumns.MIME_TYPE, DocumentsContract.MIME_TYPE_DIRECTORY); |
| 71 | values.put(DocumentColumns.DISPLAY_NAME, displayName); |
| 72 | |
| 73 | final DocumentsActivity activity = (DocumentsActivity) getActivity(); |
| 74 | final Document cwd = activity.getCurrentDirectory(); |
| 75 | |
| 76 | final Uri childUri = resolver.insert(cwd.uri, values); |
| 77 | if (childUri != null) { |
| 78 | // Navigate into newly created child |
| 79 | final Document childDoc = Document.fromUri(resolver, childUri); |
| 80 | activity.onDocumentPicked(childDoc); |
| 81 | } else { |
| 82 | Toast.makeText(context, R.string.save_error, Toast.LENGTH_SHORT).show(); |
| 83 | } |
| 84 | } |
| 85 | }); |
| 86 | builder.setNegativeButton(android.R.string.cancel, null); |
| 87 | |
| 88 | return builder.create(); |
| 89 | } |
| 90 | } |