blob: 19730b348653e894b16299521416439861973328 [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
Steve McKayd0805062016-09-15 14:30:38 -070029import com.android.documentsui.base.DocumentInfo;
30import com.android.documentsui.base.DocumentStack;
Tomasz Mikolajewskidd2b31c2016-01-22 16:22:51 +090031import 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);
Garfield, Tan48334772016-06-28 17:17:38 -070063 args.putInt(FileOperationService.EXTRA_OPERATION_TYPE, operationType);
Tomasz Mikolajewskidd2b31c2016-01-22 16:22:51 +090064 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 =
Garfield, Tan48334772016-06-28 17:17:38 -070081 getArguments().getInt(FileOperationService.EXTRA_OPERATION_TYPE);
Tomasz Mikolajewskidd2b31c2016-01-22 16:22:51 +090082 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;
Steve McKay49413922016-04-15 09:51:31 -070098 case FileOperationService.OPERATION_DELETE:
99 messageFormat = getString(R.string.delete_failure_alert_content);
100 break;
Tomasz Mikolajewskidd2b31c2016-01-22 16:22:51 +0900101 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) {
Steve McKay946f5612016-02-18 16:08:36 -0800115 list.append(String.format("&#8226; %s<br>", Html.escapeHtml(documentInfo.displayName)));
Tomasz Mikolajewskidd2b31c2016-01-22 16:22:51 +0900116 }
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}