1 /*
2  * Copyright (C) 2020 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.navigationbar.buttons;
18 
19 import android.annotation.DrawableRes;
20 import android.annotation.IdRes;
21 import android.content.Context;
22 import android.view.View;
23 
24 import com.android.systemui.shared.rotation.RotationButton;
25 import com.android.systemui.shared.rotation.RotationButtonController;
26 
27 /** Containing logic for the rotation button in nav bar. */
28 public class RotationContextButton extends ContextualButton implements RotationButton {
29     public static final boolean DEBUG_ROTATION = false;
30 
31     private RotationButtonController mRotationButtonController;
32 
33     /**
34      * @param lightContext the context to use to load the icon resource
35      */
RotationContextButton(@dRes int buttonResId, Context lightContext, @DrawableRes int iconResId)36     public RotationContextButton(@IdRes int buttonResId, Context lightContext,
37             @DrawableRes int iconResId) {
38         super(buttonResId, lightContext, iconResId);
39     }
40 
41     @Override
setRotationButtonController(RotationButtonController rotationButtonController)42     public void setRotationButtonController(RotationButtonController rotationButtonController) {
43         mRotationButtonController = rotationButtonController;
44     }
45 
46     @Override
setUpdatesCallback(RotationButtonUpdatesCallback updatesCallback)47     public void setUpdatesCallback(RotationButtonUpdatesCallback updatesCallback) {
48         setListener((button, visible) -> {
49             if (updatesCallback != null) {
50                 updatesCallback.onVisibilityChanged(visible);
51             }
52         });
53     }
54 
55     @Override
setVisibility(int visibility)56     public void setVisibility(int visibility) {
57         super.setVisibility(visibility);
58 
59         // Start the rotation animation once it becomes visible
60         final KeyButtonDrawable currentDrawable = getImageDrawable();
61         if (visibility == View.VISIBLE && currentDrawable != null) {
62             currentDrawable.resetAnimation();
63             currentDrawable.startAnimation();
64         }
65     }
66 
67     @Override
getNewDrawable(int lightIconColor, int darkIconColor)68     protected KeyButtonDrawable getNewDrawable(int lightIconColor, int darkIconColor) {
69         return KeyButtonDrawable.create(mRotationButtonController.getContext(),
70                 lightIconColor, darkIconColor, mRotationButtonController.getIconResId(),
71                 false /* shadow */, null /* ovalBackgroundColor */);
72     }
73 
74     @Override
acceptRotationProposal()75     public boolean acceptRotationProposal() {
76         View currentView = getCurrentView();
77         return currentView != null && currentView.isAttachedToWindow();
78     }
79 }
80