1 /* 2 * Copyright (C) 2019 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 package com.android.documentsui.files; 17 18 import static com.android.documentsui.base.SharedMinimal.TAG; 19 20 import android.app.Dialog; 21 import android.content.Context; 22 import android.os.Bundle; 23 import android.util.Log; 24 import android.view.LayoutInflater; 25 import android.widget.Button; 26 import android.widget.TextView; 27 28 import androidx.annotation.NonNull; 29 import androidx.appcompat.app.AlertDialog; 30 import androidx.fragment.app.DialogFragment; 31 import androidx.fragment.app.FragmentManager; 32 33 import com.android.documentsui.Injector; 34 import com.android.documentsui.R; 35 import com.android.documentsui.base.DocumentInfo; 36 import com.android.documentsui.base.Shared; 37 38 import com.google.android.material.dialog.MaterialAlertDialogBuilder; 39 40 import java.util.ArrayList; 41 import java.util.List; 42 43 /** 44 * Dialog to delete file or directory. 45 */ 46 public class DeleteDocumentFragment extends DialogFragment { 47 private static final String TAG_DELETE_DOCUMENT = "delete_document"; 48 49 private List<DocumentInfo> mDocuments; 50 private DocumentInfo mSrcParent; 51 52 /** 53 * Show the dialog UI. 54 * 55 * @param fm the fragment manager 56 * @param docs the selected documents 57 * @param srcParent the parent document of the selection 58 */ show(FragmentManager fm, List<DocumentInfo> docs, DocumentInfo srcParent)59 public static void show(FragmentManager fm, List<DocumentInfo> docs, DocumentInfo srcParent) { 60 if (fm.isStateSaved()) { 61 Log.w(TAG, "Skip show delete dialog because state saved"); 62 return; 63 } 64 65 final DeleteDocumentFragment dialog = new DeleteDocumentFragment(); 66 dialog.mDocuments = docs; 67 dialog.mSrcParent = srcParent; 68 dialog.show(fm, TAG_DELETE_DOCUMENT); 69 } 70 71 /** 72 * Creates the dialog UI. 73 * 74 * @param savedInstanceState 75 * @return 76 */ 77 @Override onCreateDialog(Bundle savedInstanceState)78 public Dialog onCreateDialog(Bundle savedInstanceState) { 79 if (savedInstanceState != null) { 80 mSrcParent = savedInstanceState.getParcelable(Shared.EXTRA_DOC); 81 mDocuments = savedInstanceState.getParcelableArrayList(Shared.EXTRA_SELECTION); 82 } 83 84 Context context = getActivity(); 85 Injector<?> injector = ((FilesActivity) getActivity()).getInjector(); 86 LayoutInflater dialogInflater = LayoutInflater.from(context); 87 TextView message = (TextView) dialogInflater.inflate( 88 R.layout.dialog_delete_confirmation, null, false); 89 message.setText(injector.messages.generateDeleteMessage(mDocuments)); 90 91 final AlertDialog alertDialog = new MaterialAlertDialogBuilder(context) 92 .setView(message) 93 .setPositiveButton( 94 android.R.string.ok, 95 (dialog, id) -> 96 injector.actions.deleteSelectedDocuments(mDocuments, mSrcParent)) 97 .setNegativeButton(android.R.string.cancel, null) 98 .create(); 99 100 alertDialog.setOnShowListener( 101 (dialogInterface) -> { 102 Button positive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); 103 positive.setFocusable(true); 104 positive.requestFocus(); 105 }); 106 return alertDialog; 107 } 108 109 @Override onSaveInstanceState(@onNull Bundle outState)110 public void onSaveInstanceState(@NonNull Bundle outState) { 111 super.onSaveInstanceState(outState); 112 outState.putParcelable(Shared.EXTRA_DOC, mSrcParent); 113 outState.putParcelableArrayList(Shared.EXTRA_SELECTION, (ArrayList) mDocuments); 114 } 115 } 116