Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 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 | |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 19 | import android.annotation.IntDef; |
| 20 | import android.app.AlertDialog; |
| 21 | import android.app.Dialog; |
| 22 | import android.app.DialogFragment; |
| 23 | import android.app.FragmentManager; |
| 24 | import android.app.FragmentTransaction; |
| 25 | import android.content.DialogInterface; |
| 26 | import android.os.Bundle; |
| 27 | import android.text.Html; |
| 28 | |
| 29 | import com.android.documentsui.model.DocumentInfo; |
| 30 | import com.android.documentsui.model.DocumentStack; |
| 31 | import com.android.documentsui.services.FileOperationService; |
Steve McKay | 946f561 | 2016-02-18 16:08:36 -0800 | [diff] [blame] | 32 | import com.android.documentsui.services.FileOperationService.OpType; |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 33 | |
| 34 | import java.lang.annotation.Retention; |
| 35 | import java.lang.annotation.RetentionPolicy; |
| 36 | import java.util.ArrayList; |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Alert dialog for operation dialogs. |
| 40 | */ |
| 41 | public 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 McKay | 946f561 | 2016-02-18 16:08:36 -0800 | [diff] [blame] | 112 | list.append(String.format("• %s<br>", Html.escapeHtml(documentInfo.displayName))); |
Tomasz Mikolajewski | dd2b31c | 2016-01-22 16:22:51 +0900 | [diff] [blame] | 113 | } |
| 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 | } |