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.deskclock 18 19 import android.view.KeyEvent 20 import android.widget.Button 21 import android.widget.ImageView 22 import androidx.annotation.ColorInt 23 import androidx.fragment.app.Fragment 24 25 import com.android.deskclock.FabContainer.UpdateFabFlag 26 import com.android.deskclock.uidata.UiDataModel 27 28 abstract class DeskClockFragment( 29 /** The tab associated with this fragment. */ 30 private val mTab: UiDataModel.Tab 31 ) : Fragment(), FabContainer, FabController { 32 33 /** The container that houses the fab and its left and right buttons. */ 34 private var mFabContainer: FabContainer? = null 35 onResumenull36 override fun onResume() { 37 super.onResume() 38 39 // Update the fab and buttons in case their state changed while the fragment was paused. 40 if (isTabSelected) { 41 updateFab(FabContainer.FAB_AND_BUTTONS_IMMEDIATE) 42 } 43 } 44 onKeyDownnull45 open fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean { 46 // By default return false so event continues to propagate 47 return false 48 } 49 onLeftButtonClicknull50 override fun onLeftButtonClick(left: Button) { 51 // Do nothing here, only in derived classes 52 } 53 onRightButtonClicknull54 override fun onRightButtonClick(right: Button) { 55 // Do nothing here, only in derived classes 56 } 57 onMorphFabnull58 override fun onMorphFab(fab: ImageView) { 59 // Do nothing here, only in derived classes 60 } 61 62 /** 63 * @param color the newly installed app window color 64 */ onAppColorChangednull65 protected open fun onAppColorChanged(@ColorInt color: Int) { 66 // Do nothing here, only in derived classes 67 } 68 69 /** 70 * @param fabContainer the container that houses the fab and its left and right buttons 71 */ setFabContainernull72 fun setFabContainer(fabContainer: FabContainer?) { 73 mFabContainer = fabContainer 74 } 75 76 /** 77 * Requests that the parent activity update the fab and buttons. 78 * 79 * @param updateTypes the manner in which the fab container should be updated 80 */ updateFabnull81 override fun updateFab(@UpdateFabFlag updateTypes: Int) { 82 mFabContainer?.updateFab(updateTypes) 83 } 84 85 /** 86 * @return `true` iff the currently selected tab displays this fragment 87 */ 88 val isTabSelected: Boolean 89 get() = UiDataModel.uiDataModel.selectedTab == mTab 90 91 /** 92 * Select the tab that displays this fragment. 93 */ selectTabnull94 fun selectTab() { 95 UiDataModel.uiDataModel.selectedTab = mTab 96 } 97 98 /** 99 * Updates the scrolling state in the [UiDataModel] for this tab. 100 * 101 * @param scrolledToTop `true` iff the vertical scroll position of this tab is at the top 102 */ setTabScrolledToTopnull103 fun setTabScrolledToTop(scrolledToTop: Boolean) { 104 UiDataModel.uiDataModel.setTabScrolledToTop(mTab, scrolledToTop) 105 } 106 }