1 /*
2  * Copyright (C) 2016 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.Dialog;
20 import android.content.DialogInterface;
21 import android.net.Uri;
22 import android.os.Bundle;
23 import android.text.Html;
24 
25 import androidx.annotation.IntDef;
26 import androidx.fragment.app.DialogFragment;
27 import androidx.fragment.app.FragmentManager;
28 import androidx.fragment.app.FragmentTransaction;
29 
30 import com.android.documentsui.base.DocumentInfo;
31 import com.android.documentsui.base.DocumentStack;
32 import com.android.documentsui.services.FileOperationService;
33 import com.android.documentsui.services.FileOperationService.OpType;
34 import com.android.documentsui.ui.MessageBuilder;
35 
36 import com.google.android.material.dialog.MaterialAlertDialogBuilder;
37 
38 import java.lang.annotation.Retention;
39 import java.lang.annotation.RetentionPolicy;
40 import java.util.ArrayList;
41 
42 /**
43  * Alert dialog for operation dialogs.
44  */
45 public class OperationDialogFragment extends DialogFragment {
46 
47     public static final int DIALOG_TYPE_UNKNOWN = 0;
48     public static final int DIALOG_TYPE_FAILURE = 1;
49     public static final int DIALOG_TYPE_CONVERTED = 2;
50 
51     @IntDef(flag = true, value = {
52         DIALOG_TYPE_UNKNOWN,
53         DIALOG_TYPE_FAILURE,
54         DIALOG_TYPE_CONVERTED
55     })
56 
57     @Retention(RetentionPolicy.SOURCE)
58     public @interface DialogType {}
59 
60     private static final String TAG = "OperationDialogFragment";
61 
show( FragmentManager fm, @DialogType int dialogType, ArrayList<DocumentInfo> failedSrcList, ArrayList<Uri> uriList, DocumentStack dstStack, @OpType int operationType)62     public static void show(
63             FragmentManager fm,
64             @DialogType int dialogType,
65             ArrayList<DocumentInfo> failedSrcList,
66             ArrayList<Uri> uriList,
67             DocumentStack dstStack,
68             @OpType int operationType) {
69 
70         final Bundle args = new Bundle();
71         args.putInt(FileOperationService.EXTRA_DIALOG_TYPE, dialogType);
72         args.putInt(FileOperationService.EXTRA_OPERATION_TYPE, operationType);
73         args.putParcelableArrayList(FileOperationService.EXTRA_FAILED_DOCS, failedSrcList);
74         args.putParcelableArrayList(FileOperationService.EXTRA_FAILED_URIS, uriList);
75 
76         final FragmentTransaction ft = fm.beginTransaction();
77         final OperationDialogFragment fragment = new OperationDialogFragment();
78         fragment.setArguments(args);
79 
80         ft.add(fragment, TAG);
81         ft.commitAllowingStateLoss();
82     }
83 
84     @Override
onCreateDialog(Bundle inState)85     public Dialog onCreateDialog(Bundle inState) {
86         super.onCreate(inState);
87 
88         final @DialogType int dialogType =
89               getArguments().getInt(FileOperationService.EXTRA_DIALOG_TYPE);
90         final @OpType int operationType =
91               getArguments().getInt(FileOperationService.EXTRA_OPERATION_TYPE);
92         final ArrayList<Uri> uriList = getArguments().getParcelableArrayList(
93                 FileOperationService.EXTRA_FAILED_URIS);
94         final ArrayList<DocumentInfo> docList = getArguments().getParcelableArrayList(
95                 FileOperationService.EXTRA_FAILED_DOCS);
96 
97         final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getActivity());
98         final String message = new MessageBuilder(getContext()).generateListMessage(
99                 dialogType, operationType, docList, uriList);
100 
101         builder.setMessage(Html.fromHtml(message));
102         builder.setPositiveButton(
103                 R.string.close,
104                 new DialogInterface.OnClickListener() {
105                     @Override
106                     public void onClick(DialogInterface dialog, int id) {
107                         dialog.dismiss();
108                     }
109                 });
110 
111         return builder.create();
112     }
113 }
114