1 /*
2  * Copyright (C) 2017 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.ui;
18 
19 import android.app.Activity;
20 import android.app.ProgressDialog;
21 import android.os.Handler;
22 import android.os.Message;
23 import android.text.format.DateUtils;
24 
25 import androidx.annotation.StringRes;
26 
27 import com.android.documentsui.R;
28 import com.android.documentsui.services.FileOperation;
29 import com.android.documentsui.services.FileOperationService;
30 import com.android.documentsui.services.FileOperations;
31 import com.android.documentsui.util.FormatUtils;
32 
33 public class OperationProgressDialog {
34 
35     private final Activity mActivity;
36     private final ProgressDialog mDialog;
37     private final String mJobId;
38 
OperationProgressDialog(Activity activity, String jobId, @StringRes int titleResId, @StringRes int prepareResId, final FileOperation operation)39     private OperationProgressDialog(Activity activity, String jobId, @StringRes int titleResId,
40             @StringRes int prepareResId, final FileOperation operation) {
41         mActivity = activity;
42         mJobId = jobId;
43         mDialog = new ProgressDialog(mActivity);
44         mDialog.setTitle(mActivity.getString(titleResId));
45         mDialog.setMessage(mActivity.getString(prepareResId));
46         mDialog.setProgress(0);
47         mDialog.setMax(100);
48         mDialog.setIndeterminate(true);
49         mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
50         mDialog.setCanceledOnTouchOutside(false);
51 
52         mDialog.setButton(ProgressDialog.BUTTON_NEGATIVE,
53                 activity.getString(android.R.string.cancel),
54                 (dialog, button) -> {
55                     FileOperations.cancel(mActivity, mJobId);
56                     mDialog.dismiss();
57                 });
58 
59         mDialog.setButton(ProgressDialog.BUTTON_NEUTRAL,
60                 activity.getString(R.string.continue_in_background),
61                 (dialog, button) -> mDialog.dismiss());
62 
63         operation.addMessageListener(new Handler.Callback() {
64             @Override
65             public boolean handleMessage(Message message) {
66                 switch (message.what) {
67                     case FileOperationService.MESSAGE_PROGRESS:
68                         mDialog.setIndeterminate(false);
69                         if (message.arg1 != -1) {
70                             mDialog.setProgress(message.arg1);
71                         }
72                         if (message.arg2 > 0) {
73                             mDialog.setMessage(mActivity.getString(R.string.copy_remaining,
74                                     FormatUtils.formatDuration(message.arg2)));
75                         }
76                         return true;
77                     case FileOperationService.MESSAGE_FINISH:
78                         operation.removeMessageListener(this);
79                         mDialog.dismiss();
80                         return true;
81                 }
82                 return false;
83             }
84         });
85     }
86 
create(Activity activity, String jobId, FileOperation operation)87     public static OperationProgressDialog create(Activity activity, String jobId,
88             FileOperation operation) {
89         int titleResId;
90         int prepareResId;
91         switch (operation.getOpType()) {
92             case FileOperationService.OPERATION_COPY:
93                 titleResId = R.string.copy_notification_title;
94                 prepareResId = R.string.copy_preparing;
95                 break;
96             case FileOperationService.OPERATION_COMPRESS:
97                 titleResId = R.string.compress_notification_title;
98                 prepareResId = R.string.compress_preparing;
99                 break;
100             case FileOperationService.OPERATION_EXTRACT:
101                 titleResId = R.string.extract_notification_title;
102                 prepareResId = R.string.extract_preparing;
103                 break;
104             case FileOperationService.OPERATION_MOVE:
105                 titleResId = R.string.move_notification_title;
106                 prepareResId = R.string.move_preparing;
107                 break;
108             case FileOperationService.OPERATION_DELETE:
109                 // Not supported yet. Pass through to default.
110             default:
111                 throw new IllegalArgumentException();
112         }
113         return new OperationProgressDialog(activity, jobId, titleResId, prepareResId, operation);
114     }
115 
dismiss()116     public void dismiss() {
117         mDialog.dismiss();
118     }
119 
show()120     public void show() {
121         mDialog.show();
122     }
123 }
124