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.annotation.StringRes;
20 import android.app.Activity;
21 import android.support.design.widget.Snackbar;
22 import android.view.Gravity;
23 import android.view.View;
24 import android.widget.TextView;
25 
26 import com.android.documentsui.R;
27 import com.android.documentsui.base.Shared;
28 
29 public final class Snackbars {
Snackbars()30     private Snackbars() {}
31 
showDocumentsClipped(Activity activity, int docCount)32     public static final void showDocumentsClipped(Activity activity, int docCount) {
33         String msg = Shared.getQuantityString(
34                 activity, R.plurals.clipboard_files_clipped, docCount);
35         Snackbars.makeSnackbar(activity, msg, Snackbar.LENGTH_SHORT).show();
36     }
37 
showMove(Activity activity, int docCount)38     public static final void showMove(Activity activity, int docCount) {
39         CharSequence message = Shared.getQuantityString(activity, R.plurals.move_begin, docCount);
40         makeSnackbar(activity, message, Snackbar.LENGTH_SHORT).show();
41     }
42 
showCopy(Activity activity, int docCount)43     public static final void showCopy(Activity activity, int docCount) {
44         CharSequence message = Shared.getQuantityString(activity, R.plurals.copy_begin, docCount);
45         makeSnackbar(activity, message, Snackbar.LENGTH_SHORT).show();
46     }
47 
showCompress(Activity activity, int docCount)48     public static final void showCompress(Activity activity, int docCount) {
49         CharSequence message = Shared.getQuantityString(activity, R.plurals.compress_begin, docCount);
50         makeSnackbar(activity, message, Snackbar.LENGTH_SHORT).show();
51     }
52 
showExtract(Activity activity, int docCount)53     public static final void showExtract(Activity activity, int docCount) {
54         CharSequence message = Shared.getQuantityString(activity, R.plurals.extract_begin, docCount);
55         makeSnackbar(activity, message, Snackbar.LENGTH_SHORT).show();
56     }
57 
showDelete(Activity activity, int docCount)58     public static final void showDelete(Activity activity, int docCount) {
59         CharSequence message = Shared.getQuantityString(activity, R.plurals.deleting, docCount);
60         makeSnackbar(activity, message, Snackbar.LENGTH_SHORT).show();
61     }
62 
showOperationRejected(Activity activity)63     public static final void showOperationRejected(Activity activity) {
64         makeSnackbar(activity, R.string.file_operation_rejected, Snackbar.LENGTH_SHORT).show();
65     }
66 
showOperationFailed(Activity activity)67     public static final void showOperationFailed(Activity activity) {
68         makeSnackbar(activity, R.string.file_operation_error, Snackbar.LENGTH_SHORT).show();
69     }
70 
showRenameFailed(Activity activity)71     public static final void showRenameFailed(Activity activity) {
72         makeSnackbar(activity, R.string.rename_error, Snackbar.LENGTH_SHORT).show();
73     }
74 
showCustomTextWithImage(Activity activity, String text, int imageRes)75     public static final void showCustomTextWithImage(Activity activity, String text, int imageRes) {
76         Snackbar snackbar = makeSnackbar(activity, text, Snackbar.LENGTH_SHORT);
77         View snackbarLayout = snackbar.getView();
78         TextView textView = (TextView)snackbarLayout.findViewById(
79                 android.support.design.R.id.snackbar_text);
80         textView.setGravity(Gravity.CENTER_HORIZONTAL);
81         textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
82         textView.setCompoundDrawablesWithIntrinsicBounds(imageRes, 0, 0, 0);
83         snackbar.show();
84     }
85 
makeSnackbar(Activity activity, @StringRes int messageId, int duration)86     public static final Snackbar makeSnackbar(Activity activity, @StringRes int messageId,
87             int duration) {
88         return Snackbars.makeSnackbar(
89                 activity, activity.getResources().getText(messageId), duration);
90     }
91 
makeSnackbar( Activity activity, CharSequence message, int duration)92     public static final Snackbar makeSnackbar(
93             Activity activity, CharSequence message, int duration) {
94         final View view = activity.findViewById(R.id.coordinator_layout);
95         return Snackbar.make(view, message, duration);
96     }
97 }
98