Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 19 | import android.app.AlertDialog; |
| 20 | import android.app.Dialog; |
| 21 | import android.app.DialogFragment; |
| 22 | import android.app.FragmentManager; |
| 23 | import android.app.FragmentTransaction; |
| 24 | import android.content.DialogInterface; |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 25 | import android.os.Bundle; |
| 26 | import android.text.Html; |
| 27 | |
| 28 | import com.android.documentsui.model.DocumentInfo; |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 29 | import com.android.documentsui.model.DocumentStack; |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 30 | |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 31 | import java.util.ArrayList; |
| 32 | |
| 33 | /** |
| 34 | * Alert dialog for failed operations. |
| 35 | */ |
| 36 | public class FailureDialogFragment extends DialogFragment |
| 37 | implements DialogInterface.OnClickListener { |
| 38 | private static final String TAG = "FailureDialogFragment"; |
| 39 | |
| 40 | private int mFailure; |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 41 | private int mTransferMode; |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 42 | private ArrayList<DocumentInfo> mFailedSrcList; |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 43 | |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 44 | public static void show(FragmentManager fm, int failure, |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 45 | ArrayList<DocumentInfo> failedSrcList, DocumentStack dstStack, int transferMode) { |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 46 | // TODO: Add support for other failures than copy. |
| 47 | if (failure != CopyService.FAILURE_COPY) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | final Bundle args = new Bundle(); |
| 52 | args.putInt(CopyService.EXTRA_FAILURE, failure); |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 53 | args.putInt(CopyService.EXTRA_TRANSFER_MODE, transferMode); |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 54 | args.putParcelableArrayList(CopyService.EXTRA_SRC_LIST, failedSrcList); |
| 55 | |
| 56 | final FragmentTransaction ft = fm.beginTransaction(); |
| 57 | final FailureDialogFragment fragment = new FailureDialogFragment(); |
| 58 | fragment.setArguments(args); |
| 59 | |
| 60 | ft.add(fragment, TAG); |
| 61 | ft.commitAllowingStateLoss(); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void onClick(DialogInterface dialog, int whichButton) { |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 66 | if (whichButton == DialogInterface.BUTTON_POSITIVE) { |
| 67 | CopyService.start(getActivity(), mFailedSrcList, |
| 68 | (DocumentStack) getActivity().getIntent().getParcelableExtra( |
| 69 | CopyService.EXTRA_STACK), |
| 70 | mTransferMode); |
| 71 | } |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public Dialog onCreateDialog(Bundle inState) { |
| 76 | super.onCreate(inState); |
| 77 | |
| 78 | mFailure = getArguments().getInt(CopyService.EXTRA_FAILURE); |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 79 | mTransferMode = getArguments().getInt(CopyService.EXTRA_TRANSFER_MODE); |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 80 | mFailedSrcList = getArguments().getParcelableArrayList(CopyService.EXTRA_SRC_LIST); |
| 81 | |
| 82 | final StringBuilder list = new StringBuilder("<p>"); |
Tomasz Mikolajewski | 9452c44 | 2015-04-14 16:32:41 +0900 | [diff] [blame] | 83 | for (DocumentInfo documentInfo : mFailedSrcList) { |
| 84 | list.append(String.format("• %s<br>", documentInfo.displayName)); |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 85 | } |
| 86 | list.append("</p>"); |
| 87 | final String message = String.format(getString(R.string.copy_failure_alert_content), |
| 88 | list.toString()); |
| 89 | |
| 90 | return new AlertDialog.Builder(getActivity()) |
Ben Kwa | cb4461f | 2015-05-05 11:50:11 -0700 | [diff] [blame^] | 91 | .setMessage(Html.fromHtml(message)) |
| 92 | .setPositiveButton(R.string.retry, this) |
| 93 | .setNegativeButton(android.R.string.cancel, this) |
| 94 | .create(); |
Tomasz Mikolajewski | 332d819 | 2015-04-13 19:38:43 +0900 | [diff] [blame] | 95 | } |
| 96 | } |