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.car.portraitlauncher.panel; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.View; 22 import android.widget.ImageView; 23 import android.widget.RelativeLayout; 24 import android.widget.TextView; 25 26 import com.android.car.portraitlauncher.R; 27 28 /** The toolbar shows on top of a TaskViewPanel */ 29 public class ToolBarView extends RelativeLayout { 30 31 private static final String TAG = ToolBarView.class.getSimpleName(); 32 33 private TextView mClock; 34 private ImageView mBackButton; 35 private Callback mCallback; 36 ToolBarView(Context context)37 public ToolBarView(Context context) { 38 this(context, null); 39 } 40 ToolBarView(Context context, AttributeSet attrs)41 public ToolBarView(Context context, AttributeSet attrs) { 42 this(context, attrs, /* defStyleAttr= */ 0); 43 } 44 ToolBarView(Context context, AttributeSet attrs, int defStyleAttr)45 public ToolBarView(Context context, AttributeSet attrs, int defStyleAttr) { 46 this(context, attrs, defStyleAttr, /* defStyleRes= */ 0); 47 } 48 ToolBarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)49 public ToolBarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 50 super(context, attrs, defStyleAttr, defStyleRes); 51 } 52 53 @Override onFinishInflate()54 protected void onFinishInflate() { 55 super.onFinishInflate(); 56 mBackButton = findViewById(R.id.back_button); 57 mClock = findViewById(R.id.clock); 58 } 59 registerToolbarCallback(Callback callback)60 void registerToolbarCallback(Callback callback) { 61 mCallback = callback; 62 mBackButton.setOnClickListener(v -> mCallback.onClick()); 63 } 64 65 /** 66 * Show/hide toolbar content with animation. 67 */ updateToolBarContentVisibility(boolean isVisible)68 public void updateToolBarContentVisibility(boolean isVisible) { 69 updateViewVisibilityWithAnimation(isVisible, mClock); 70 updateViewVisibilityWithAnimation(isVisible, mBackButton); 71 } 72 updateViewVisibilityWithAnimation(boolean isVisible, View view)73 private void updateViewVisibilityWithAnimation(boolean isVisible, View view) { 74 if (view == null) { 75 return; 76 } 77 float translationY = isVisible ? 0 : -getHeight(); 78 view.animate().translationY(translationY); 79 } 80 81 /** 82 * Callback interface for {@link ToolBarView} actions 83 */ 84 public interface Callback { 85 /** 86 * Callback triggered when the {@link mBackButton} is clicked 87 */ onClick()88 void onClick(); 89 } 90 } 91