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; |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame^] | 28 | import android.os.AsyncTask; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 29 | import android.os.Bundle; |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 30 | import android.provider.DocumentsContract; |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 31 | import android.provider.DocumentsContract.Document; |
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 | |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 37 | import com.android.documentsui.model.DocumentInfo; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 38 | |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame^] | 39 | import java.io.FileNotFoundException; |
| 40 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Dialog to create a new directory. |
| 43 | */ |
| 44 | public class CreateDirectoryFragment extends DialogFragment { |
| 45 | private static final String TAG_CREATE_DIRECTORY = "create_directory"; |
| 46 | |
| 47 | public static void show(FragmentManager fm) { |
| 48 | final CreateDirectoryFragment dialog = new CreateDirectoryFragment(); |
| 49 | dialog.show(fm, TAG_CREATE_DIRECTORY); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 54 | final Context context = getActivity(); |
| 55 | final ContentResolver resolver = context.getContentResolver(); |
| 56 | |
| 57 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 58 | final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); |
| 59 | |
| 60 | final View view = dialogInflater.inflate(R.layout.dialog_create_dir, null, false); |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 61 | final EditText text1 = (EditText) view.findViewById(android.R.id.text1); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 62 | |
| 63 | builder.setTitle(R.string.menu_create_dir); |
| 64 | builder.setView(view); |
| 65 | |
| 66 | builder.setPositiveButton(android.R.string.ok, new OnClickListener() { |
| 67 | @Override |
| 68 | public void onClick(DialogInterface dialog, int which) { |
| 69 | final String displayName = text1.getText().toString(); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame^] | 70 | new CreateDirectoryTask(displayName).execute(); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 71 | } |
| 72 | }); |
| 73 | builder.setNegativeButton(android.R.string.cancel, null); |
| 74 | |
| 75 | return builder.create(); |
| 76 | } |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame^] | 77 | |
| 78 | private class CreateDirectoryTask extends AsyncTask<Void, Void, DocumentInfo> { |
| 79 | private final String mDisplayName; |
| 80 | |
| 81 | public CreateDirectoryTask(String displayName) { |
| 82 | mDisplayName = displayName; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | protected DocumentInfo doInBackground(Void... params) { |
| 87 | final DocumentsActivity activity = (DocumentsActivity) getActivity(); |
| 88 | final ContentResolver resolver = activity.getContentResolver(); |
| 89 | |
| 90 | final DocumentInfo cwd = activity.getCurrentDirectory(); |
| 91 | final Uri childUri = DocumentsContract.createDocument( |
| 92 | resolver, cwd.derivedUri, Document.MIME_TYPE_DIR, mDisplayName); |
| 93 | try { |
| 94 | return DocumentInfo.fromUri(resolver, childUri); |
| 95 | } catch (FileNotFoundException e) { |
| 96 | return null; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | protected void onPostExecute(DocumentInfo result) { |
| 102 | final DocumentsActivity activity = (DocumentsActivity) getActivity(); |
| 103 | if (result != null) { |
| 104 | // Navigate into newly created child |
| 105 | activity.onDocumentPicked(result); |
| 106 | } else { |
| 107 | Toast.makeText(activity, R.string.create_error, Toast.LENGTH_SHORT).show(); |
| 108 | } |
| 109 | } |
| 110 | } |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 111 | } |