blob: 8a480faada8a620ec44d58591cae0b0d9471f432 [file] [log] [blame]
Tomasz Mikolajewski332d8192015-04-13 19:38:43 +09001/*
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
17package com.android.documentsui;
18
Tomasz Mikolajewski332d8192015-04-13 19:38:43 +090019import android.app.AlertDialog;
20import android.app.Dialog;
21import android.app.DialogFragment;
22import android.app.FragmentManager;
23import android.app.FragmentTransaction;
24import android.content.DialogInterface;
Tomasz Mikolajewski332d8192015-04-13 19:38:43 +090025import android.os.Bundle;
26import android.text.Html;
27
28import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +090029import com.android.documentsui.model.DocumentStack;
Tomasz Mikolajewski332d8192015-04-13 19:38:43 +090030
Tomasz Mikolajewski332d8192015-04-13 19:38:43 +090031import java.util.ArrayList;
32
33/**
34 * Alert dialog for failed operations.
35 */
36public class FailureDialogFragment extends DialogFragment
37 implements DialogInterface.OnClickListener {
38 private static final String TAG = "FailureDialogFragment";
39
40 private int mFailure;
Ben Kwacb4461f2015-05-05 11:50:11 -070041 private int mTransferMode;
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +090042 private ArrayList<DocumentInfo> mFailedSrcList;
Tomasz Mikolajewski332d8192015-04-13 19:38:43 +090043
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +090044 public static void show(FragmentManager fm, int failure,
Ben Kwacb4461f2015-05-05 11:50:11 -070045 ArrayList<DocumentInfo> failedSrcList, DocumentStack dstStack, int transferMode) {
Tomasz Mikolajewski332d8192015-04-13 19:38:43 +090046 // 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 Kwacb4461f2015-05-05 11:50:11 -070053 args.putInt(CopyService.EXTRA_TRANSFER_MODE, transferMode);
Tomasz Mikolajewski332d8192015-04-13 19:38:43 +090054 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 Kwacb4461f2015-05-05 11:50:11 -070066 if (whichButton == DialogInterface.BUTTON_POSITIVE) {
67 CopyService.start(getActivity(), mFailedSrcList,
68 (DocumentStack) getActivity().getIntent().getParcelableExtra(
69 CopyService.EXTRA_STACK),
70 mTransferMode);
71 }
Tomasz Mikolajewski332d8192015-04-13 19:38:43 +090072 }
73
74 @Override
75 public Dialog onCreateDialog(Bundle inState) {
76 super.onCreate(inState);
77
78 mFailure = getArguments().getInt(CopyService.EXTRA_FAILURE);
Ben Kwacb4461f2015-05-05 11:50:11 -070079 mTransferMode = getArguments().getInt(CopyService.EXTRA_TRANSFER_MODE);
Tomasz Mikolajewski332d8192015-04-13 19:38:43 +090080 mFailedSrcList = getArguments().getParcelableArrayList(CopyService.EXTRA_SRC_LIST);
81
82 final StringBuilder list = new StringBuilder("<p>");
Tomasz Mikolajewski9452c442015-04-14 16:32:41 +090083 for (DocumentInfo documentInfo : mFailedSrcList) {
84 list.append(String.format("&#8226; %s<br>", documentInfo.displayName));
Tomasz Mikolajewski332d8192015-04-13 19:38:43 +090085 }
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 Kwacb4461f2015-05-05 11:50:11 -070091 .setMessage(Html.fromHtml(message))
92 .setPositiveButton(R.string.retry, this)
93 .setNegativeButton(android.R.string.cancel, this)
94 .create();
Tomasz Mikolajewski332d8192015-04-13 19:38:43 +090095 }
96}