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 17 package com.android.documentsui.picker; 18 19 import static com.android.documentsui.base.Shared.getCallingAppName; 20 21 import android.app.Dialog; 22 import android.content.DialogInterface; 23 import android.net.Uri; 24 import android.os.Bundle; 25 26 import androidx.fragment.app.DialogFragment; 27 import androidx.fragment.app.Fragment; 28 import androidx.fragment.app.FragmentManager; 29 import androidx.fragment.app.FragmentTransaction; 30 31 import com.android.documentsui.BaseActivity; 32 import com.android.documentsui.R; 33 import com.android.documentsui.base.DocumentInfo; 34 import com.android.documentsui.base.Shared; 35 36 import com.google.android.material.dialog.MaterialAlertDialogBuilder; 37 38 /** 39 * Used to confirm with user that it's OK to overwrite an existing file. 40 */ 41 public class ConfirmFragment extends DialogFragment { 42 43 private static final String TAG = "ConfirmFragment"; 44 45 public static final String CONFIRM_TYPE = "type"; 46 public static final int TYPE_OVERWRITE = 1; 47 public static final int TYPE_OEPN_TREE = 2; 48 49 private ActionHandler<PickActivity> mActions; 50 private DocumentInfo mTarget; 51 private int mType; 52 53 @Override onActivityCreated(Bundle savedInstanceState)54 public void onActivityCreated(Bundle savedInstanceState) { 55 super.onActivityCreated(savedInstanceState); 56 57 mActions = ((PickActivity) getActivity()).getInjector().actions; 58 } 59 60 @Override onCreateDialog(Bundle savedInstanceState)61 public Dialog onCreateDialog(Bundle savedInstanceState) { 62 Bundle arg = (getArguments() != null) ? getArguments() : savedInstanceState; 63 64 mTarget = arg.getParcelable(Shared.EXTRA_DOC); 65 mType = arg.getInt(CONFIRM_TYPE); 66 final PickResult pickResult = ((PickActivity) getActivity()).getInjector().pickResult; 67 68 final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getActivity()); 69 switch (mType) { 70 case TYPE_OVERWRITE: 71 String message = String.format( 72 getString(R.string.overwrite_file_confirmation_message), 73 mTarget.displayName); 74 builder.setMessage(message); 75 builder.setPositiveButton( 76 android.R.string.ok, 77 (DialogInterface dialog, int id) -> { 78 pickResult.increaseActionCount(); 79 mActions.finishPicking(mTarget.getDocumentUri()); 80 }); 81 break; 82 case TYPE_OEPN_TREE: 83 final Uri treeUri = mTarget.getTreeDocumentUri(); 84 final BaseActivity activity = (BaseActivity) getActivity(); 85 final String target = activity.getCurrentTitle(); 86 final String text = getString(R.string.open_tree_dialog_title, 87 getCallingAppName(getActivity()), target); 88 message = getString(R.string.open_tree_dialog_message, 89 getCallingAppName(getActivity()), target); 90 91 builder.setTitle(text); 92 builder.setMessage(message); 93 builder.setPositiveButton( 94 R.string.allow, 95 (DialogInterface dialog, int id) -> { 96 pickResult.increaseActionCount(); 97 mActions.finishPicking(treeUri); 98 }); 99 break; 100 101 } 102 builder.setNegativeButton(android.R.string.cancel, 103 (DialogInterface dialog, int id) -> pickResult.increaseActionCount()); 104 105 return builder.create(); 106 } 107 108 @Override onSaveInstanceState(Bundle outState)109 public void onSaveInstanceState(Bundle outState) { 110 super.onSaveInstanceState(outState); 111 112 outState.putParcelable(Shared.EXTRA_DOC, mTarget); 113 outState.putInt(CONFIRM_TYPE, mType); 114 } 115 show(FragmentManager fm, DocumentInfo overwriteTarget, int type)116 public static void show(FragmentManager fm, DocumentInfo overwriteTarget, int type) { 117 Bundle arg = new Bundle(); 118 arg.putParcelable(Shared.EXTRA_DOC, overwriteTarget); 119 arg.putInt(CONFIRM_TYPE, type); 120 121 FragmentTransaction ft = fm.beginTransaction(); 122 Fragment f = new ConfirmFragment(); 123 f.setArguments(arg); 124 ft.add(f, TAG); 125 ft.commitAllowingStateLoss(); 126 } 127 } 128