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
17package com.android.documentsui;
18
19import android.annotation.IntDef;
20import android.app.AlertDialog;
21import android.app.Dialog;
22import android.app.DialogFragment;
23import android.app.FragmentManager;
24import android.app.FragmentTransaction;
25import android.content.DialogInterface;
26import android.os.Bundle;
27import android.text.Html;
28
29import com.android.documentsui.model.DocumentInfo;
30import com.android.documentsui.model.DocumentStack;
31import com.android.documentsui.services.FileOperationService;
32import com.android.documentsui.services.FileOperationService.OpType;
33
34import java.lang.annotation.Retention;
35import java.lang.annotation.RetentionPolicy;
36import java.util.ArrayList;
37
38/**
39 * Alert dialog for operation dialogs.
40 */
41public class OperationDialogFragment extends DialogFragment {
42
43    public static final int DIALOG_TYPE_UNKNOWN = 0;
44    public static final int DIALOG_TYPE_FAILURE = 1;
45    public static final int DIALOG_TYPE_CONVERTED = 2;
46
47    @IntDef(flag = true, value = {
48        DIALOG_TYPE_UNKNOWN,
49        DIALOG_TYPE_FAILURE,
50        DIALOG_TYPE_CONVERTED
51    })
52
53    @Retention(RetentionPolicy.SOURCE)
54    public @interface DialogType {}
55
56    private static final String TAG = "OperationDialogFragment";
57
58    public static void show(FragmentManager fm, @DialogType int dialogType,
59            ArrayList<DocumentInfo> failedSrcList, DocumentStack dstStack,
60            @OpType int operationType) {
61        final Bundle args = new Bundle();
62        args.putInt(FileOperationService.EXTRA_DIALOG_TYPE, dialogType);
63        args.putInt(FileOperationService.EXTRA_OPERATION, operationType);
64        args.putParcelableArrayList(FileOperationService.EXTRA_SRC_LIST, failedSrcList);
65
66        final FragmentTransaction ft = fm.beginTransaction();
67        final OperationDialogFragment fragment = new OperationDialogFragment();
68        fragment.setArguments(args);
69
70        ft.add(fragment, TAG);
71        ft.commitAllowingStateLoss();
72    }
73
74    @Override
75    public Dialog onCreateDialog(Bundle inState) {
76        super.onCreate(inState);
77
78        final @DialogType int dialogType =
79              getArguments().getInt(FileOperationService.EXTRA_DIALOG_TYPE);
80        final @OpType int operationType =
81              getArguments().getInt(FileOperationService.EXTRA_OPERATION);
82        final ArrayList<DocumentInfo> srcList = getArguments().getParcelableArrayList(
83                FileOperationService.EXTRA_SRC_LIST);
84
85        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
86        String messageFormat;
87
88        switch (dialogType) {
89            case DIALOG_TYPE_CONVERTED:
90                messageFormat = getString(R.string.copy_converted_warning_content);
91                break;
92
93            case DIALOG_TYPE_FAILURE:
94                switch (operationType) {
95                    case FileOperationService.OPERATION_COPY:
96                        messageFormat = getString(R.string.copy_failure_alert_content);
97                        break;
98                    case FileOperationService.OPERATION_DELETE:
99                        messageFormat = getString(R.string.delete_failure_alert_content);
100                        break;
101                    case FileOperationService.OPERATION_MOVE:
102                        messageFormat = getString(R.string.move_failure_alert_content);
103                        break;
104                    default:
105                        throw new UnsupportedOperationException();
106                }
107                break;
108
109            default:
110                throw new UnsupportedOperationException();
111        }
112
113        final StringBuilder list = new StringBuilder("<p>");
114        for (DocumentInfo documentInfo : srcList) {
115            list.append(String.format("&#8226; %s<br>", Html.escapeHtml(documentInfo.displayName)));
116        }
117        list.append("</p>");
118        builder.setMessage(Html.fromHtml(String.format(messageFormat, list.toString())));
119        builder.setPositiveButton(
120                R.string.close,
121                new DialogInterface.OnClickListener() {
122                    @Override
123                    public void onClick(DialogInterface dialog, int id) {
124                        dialog.dismiss();
125                    }
126                });
127
128        return builder.create();
129    }
130}
131