blob: 2cef8d31ad1a733acf03dd6e30a1e7aaed6deafa [file] [log] [blame]
Tomasz Mikolajewskidd2b31c2016-01-22 16:22:51 +09001/*
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
Tomasz Mikolajewskidd2b31c2016-01-22 16:22:51 +090019import 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;
Steve McKay946f5612016-02-18 16:08:36 -080032import com.android.documentsui.services.FileOperationService.OpType;
Tomasz Mikolajewskidd2b31c2016-01-22 16:22:51 +090033
34import java.lang.annotation.Retention;
35import java.lang.annotation.RetentionPolicy;
36import java.util.ArrayList;
Tomasz Mikolajewskidd2b31c2016-01-22 16:22:51 +090037
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_MOVE:
99 messageFormat = getString(R.string.move_failure_alert_content);
100 break;
101 default:
102 throw new UnsupportedOperationException();
103 }
104 break;
105
106 default:
107 throw new UnsupportedOperationException();
108 }
109
110 final StringBuilder list = new StringBuilder("<p>");
111 for (DocumentInfo documentInfo : srcList) {
Steve McKay946f5612016-02-18 16:08:36 -0800112 list.append(String.format("&#8226; %s<br>", Html.escapeHtml(documentInfo.displayName)));
Tomasz Mikolajewskidd2b31c2016-01-22 16:22:51 +0900113 }
114 list.append("</p>");
115 builder.setMessage(Html.fromHtml(String.format(messageFormat, list.toString())));
116 builder.setPositiveButton(
117 R.string.close,
118 new DialogInterface.OnClickListener() {
119 @Override
120 public void onClick(DialogInterface dialog, int id) {
121 dialog.dismiss();
122 }
123 });
124
125 return builder.create();
126 }
127}