1 /*
2  * Copyright (C) 2023 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.systemui.car.userpicker;
18 
19 import android.annotation.NonNull;
20 import android.content.Context;
21 import android.graphics.Color;
22 import android.util.Log;
23 import android.util.Slog;
24 import android.view.View;
25 
26 import androidx.annotation.IdRes;
27 
28 import com.android.systemui.R;
29 
30 import com.google.android.material.snackbar.Snackbar;
31 
32 import javax.inject.Inject;
33 
34 @UserPickerScope
35 final class SnackbarManager {
36     private static final String TAG = SnackbarManager.class.getSimpleName();
37     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
38 
39     private int mDisplayId;
40     private View mRootView;
41     @IdRes
42     private int mAnchorViewId = View.NO_ID;
43     private int mSnackbarBackgroundTint;
44 
45     @Inject
SnackbarManager()46     SnackbarManager() {}
47 
setRootView(View rootView, @IdRes int anchorViewId)48     void setRootView(View rootView, @IdRes int anchorViewId) {
49         Context context = rootView.getContext();
50         mRootView = rootView;
51         if (mRootView.findViewById(anchorViewId) != null) {
52             // ensure view exists
53             mAnchorViewId = anchorViewId;
54         }
55         mDisplayId = context.getDisplayId();
56         mSnackbarBackgroundTint = context.getColor(R.color.user_picker_snack_bar_background_color);
57     }
58 
showSnackbar(@onNull String message)59     void showSnackbar(@NonNull String message) {
60         if (DEBUG) {
61             Slog.d(TAG, "showSnackBar: displayId=" + mDisplayId);
62         }
63         Snackbar snackbar = Snackbar.make(mRootView, message, Snackbar.LENGTH_SHORT);
64         snackbar.setBackgroundTint(mSnackbarBackgroundTint);
65         snackbar.setTextColor(Color.WHITE);
66         if (mAnchorViewId != View.NO_ID) {
67             snackbar.setAnchorView(mAnchorViewId);
68         }
69         snackbar.show();
70     }
71 }
72