1 /* 2 * Copyright (C) 2018 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.dialer.ui.common; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.view.View; 22 23 import androidx.annotation.CallSuper; 24 import androidx.annotation.NonNull; 25 import androidx.annotation.Nullable; 26 import androidx.fragment.app.Fragment; 27 import androidx.lifecycle.LiveData; 28 import androidx.lifecycle.MutableLiveData; 29 import androidx.lifecycle.ViewModelProviders; 30 31 import com.android.car.dialer.R; 32 import com.android.car.dialer.ui.TelecomActivity; 33 import com.android.car.dialer.ui.TelecomActivityViewModel; 34 import com.android.car.ui.toolbar.Toolbar; 35 36 /** 37 * The base class for top level dialer content {@link Fragment}s. 38 */ 39 public abstract class DialerBaseFragment extends Fragment { 40 41 private MutableLiveData<Integer> mToolbarHeight; 42 43 /** 44 * Interface for Dialer top level fragment's parent to implement. 45 */ 46 public interface DialerFragmentParent { 47 48 /** 49 * Push a fragment to the back stack. Update action bar accordingly. 50 */ pushContentFragment(Fragment fragment, String fragmentTag)51 void pushContentFragment(Fragment fragment, String fragmentTag); 52 } 53 54 @Override onCreate(@ullable Bundle savedInstanceState)55 public void onCreate(@Nullable Bundle savedInstanceState) { 56 super.onCreate(savedInstanceState); 57 mToolbarHeight = new MutableLiveData<>(); 58 } 59 60 @CallSuper 61 @Override onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)62 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { 63 super.onViewCreated(view, savedInstanceState); 64 Toolbar toolbar = getActivity().findViewById(R.id.car_ui_toolbar); 65 // Null check for unit tests to pass 66 if (toolbar != null) { 67 setupToolbar(toolbar); 68 } 69 } 70 71 @Override onStart()72 public void onStart() { 73 super.onStart(); 74 mToolbarHeight.observe(this, this::onToolbarHeightChange); 75 } 76 77 /** 78 * Customizes the tool bar. Can be overridden in subclasses. 79 */ setupToolbar(@onNull Toolbar toolbar)80 protected void setupToolbar(@NonNull Toolbar toolbar) { 81 TelecomActivityViewModel viewModel = ViewModelProviders.of(getActivity()).get( 82 TelecomActivityViewModel.class); 83 LiveData<String> toolbarTitleLiveData = viewModel.getToolbarTitle(); 84 toolbarTitleLiveData.observe(this, 85 toolbarTitle -> toolbar.setTitle(toolbarTitle)); 86 87 toolbar.setState(getToolbarState()); 88 toolbar.setLogo(getToolbarState() == Toolbar.State.HOME ? getActivity().getDrawable( 89 R.drawable.ic_app_icon) : null); 90 91 toolbar.setMenuItems(R.xml.menuitems); 92 93 setShowToolbarBackground(true); 94 95 setToolbarHeight(toolbar); 96 } 97 98 /** 99 * Push a fragment to the back stack. Update action bar accordingly. 100 */ pushContentFragment(@onNull Fragment fragment, String fragmentTag)101 protected void pushContentFragment(@NonNull Fragment fragment, String fragmentTag) { 102 Activity parentActivity = getActivity(); 103 if (parentActivity instanceof DialerFragmentParent) { 104 ((DialerFragmentParent) parentActivity).pushContentFragment(fragment, fragmentTag); 105 } 106 } 107 getToolbarState()108 protected Toolbar.State getToolbarState() { 109 return Toolbar.State.HOME; 110 } 111 setShowToolbarBackground(boolean showBackground)112 protected final void setShowToolbarBackground(boolean showBackground) { 113 Activity activity = getActivity(); 114 if (activity instanceof TelecomActivity) { 115 ((TelecomActivity) activity).setShowToolbarBackground(showBackground); 116 } 117 } 118 119 /** 120 * Sets the toolbar height. 121 */ setToolbarHeight(Toolbar toolbar)122 protected final void setToolbarHeight(Toolbar toolbar) { 123 int toolbarFirstRowHeight = getResources().getDimensionPixelSize( 124 R.dimen.car_ui_toolbar_first_row_height); 125 int toolbarHeight = toolbar.isTabsInSecondRow() && getToolbarState() == Toolbar.State.HOME 126 ? toolbarFirstRowHeight + getResources().getDimensionPixelSize( 127 R.dimen.car_ui_toolbar_second_row_height) 128 : toolbarFirstRowHeight; 129 mToolbarHeight.setValue(toolbarHeight); 130 } 131 132 /** 133 * Handles the toolbar height. 134 */ onToolbarHeightChange(int toolbarHeight)135 public abstract void onToolbarHeightChange(int toolbarHeight); 136 } 137