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