Home | History | Annotate | Download | only in documentsui
      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 
     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;
     32 import com.android.documentsui.services.FileOperationService.OpType;
     33 
     34 import java.lang.annotation.Retention;
     35 import java.lang.annotation.RetentionPolicy;
     36 import java.util.ArrayList;
     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_DELETE:
     99                         messageFormat = getString(R.string.delete_failure_alert_content);
    100                         break;
    101                     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) {
    115             list.append(String.format("&#8226; %s<br>", Html.escapeHtml(documentInfo.displayName)));
    116         }
    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 }
    131