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.ui;
18 
19 import android.app.Activity;
20 import android.view.Gravity;
21 import android.view.View;
22 import android.widget.TextView;
23 
24 import androidx.annotation.StringRes;
25 
26 import com.android.documentsui.R;
27 import com.android.documentsui.base.Shared;
28 
29 import com.google.android.material.snackbar.Snackbar;
30 
31 import java.util.function.Consumer;
32 
33 public final class Snackbars {
34 
Snackbars()35     private Snackbars() {}
36 
showDocumentsClipped(Activity activity, int docCount)37     public static final void showDocumentsClipped(Activity activity, int docCount) {
38         String msg = Shared.getQuantityString(
39                 activity, R.plurals.clipboard_files_clipped, docCount);
40         Snackbars.makeSnackbar(activity, msg, Snackbar.LENGTH_LONG).show();
41     }
42 
showMove(Activity activity, int docCount)43     public static final void showMove(Activity activity, int docCount) {
44         CharSequence message = Shared.getQuantityString(activity, R.plurals.move_begin, docCount);
45         makeSnackbar(activity, message, Snackbar.LENGTH_LONG).show();
46     }
47 
showCopy(Activity activity, int docCount)48     public static final void showCopy(Activity activity, int docCount) {
49         CharSequence message = Shared.getQuantityString(activity, R.plurals.copy_begin, docCount);
50         makeSnackbar(activity, message, Snackbar.LENGTH_LONG).show();
51     }
52 
showCompress(Activity activity, int docCount)53     public static final void showCompress(Activity activity, int docCount) {
54         CharSequence message = Shared.getQuantityString(activity, R.plurals.compress_begin, docCount);
55         makeSnackbar(activity, message, Snackbar.LENGTH_LONG).show();
56     }
57 
showExtract(Activity activity, int docCount)58     public static final void showExtract(Activity activity, int docCount) {
59         CharSequence message = Shared.getQuantityString(activity, R.plurals.extract_begin, docCount);
60         makeSnackbar(activity, message, Snackbar.LENGTH_LONG).show();
61     }
62 
showDelete(Activity activity, int docCount)63     public static final void showDelete(Activity activity, int docCount) {
64         CharSequence message = Shared.getQuantityString(activity, R.plurals.deleting, docCount);
65         makeSnackbar(activity, message, Snackbar.LENGTH_LONG).show();
66     }
67 
showOperationRejected(Activity activity)68     public static final void showOperationRejected(Activity activity) {
69         makeSnackbar(activity, R.string.file_operation_rejected, Snackbar.LENGTH_LONG).show();
70     }
71 
showOperationFailed(Activity activity)72     public static final void showOperationFailed(Activity activity) {
73         makeSnackbar(activity, R.string.file_operation_error, Snackbar.LENGTH_LONG).show();
74     }
75 
showRenameFailed(Activity activity)76     public static final void showRenameFailed(Activity activity) {
77         makeSnackbar(activity, R.string.rename_error, Snackbar.LENGTH_LONG).show();
78     }
79 
showInspectorError(Activity activity)80     public static final void showInspectorError(Activity activity) {
81         //Document Inspector uses a different view from other files app activities.
82         final View view = activity.findViewById(R.id.inspector_root);
83         Snackbar.make(view, R.string.inspector_load_error, Snackbar.LENGTH_INDEFINITE).show();
84     }
85 
showCustomTextWithImage(Activity activity, String text, int imageRes)86     public static final void showCustomTextWithImage(Activity activity, String text, int imageRes) {
87         Snackbar snackbar = makeSnackbar(activity, text, Snackbar.LENGTH_LONG);
88         View snackbarLayout = snackbar.getView();
89         TextView textView = (TextView)snackbarLayout.findViewById(
90                 com.google.android.material.R.id.snackbar_text);
91         textView.setGravity(Gravity.CENTER_HORIZONTAL);
92         textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
93         textView.setCompoundDrawablesWithIntrinsicBounds(imageRes, 0, 0, 0);
94         snackbar.show();
95     }
96 
makeSnackbarWithAction(Activity activity, int docCount, CharSequence message, int duration, CharSequence actionText, Consumer<View> action, final Snackbar.Callback callback)97     public static final Snackbar makeSnackbarWithAction(Activity activity, int docCount,
98             CharSequence message, int duration, CharSequence actionText,
99             Consumer<View> action, final Snackbar.Callback callback) {
100         return makeSnackbar(activity, message, duration)
101                 .setAction(actionText, action::accept)
102                 .addCallback(callback);
103     }
104 
makeSnackbar(Activity activity, @StringRes int messageId, int duration)105     public static final Snackbar makeSnackbar(Activity activity, @StringRes int messageId,
106             int duration) {
107         return Snackbars.makeSnackbar(
108                 activity, activity.getResources().getText(messageId), duration);
109     }
110 
makeSnackbar( Activity activity, CharSequence message, int duration)111     public static final Snackbar makeSnackbar(
112             Activity activity, CharSequence message, int duration) {
113         final View view = activity.findViewById(R.id.container_save);
114         return Snackbar.make(view, message, duration);
115     }
116 }
117