1 /*
2  * Copyright (C) 2012 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.app.Fragment;
20 import android.support.annotation.ColorInt;
21 import android.support.annotation.NonNull;
22 import android.view.KeyEvent;
23 import android.widget.Button;
24 import android.widget.ImageView;
25 
26 import com.android.deskclock.uidata.UiDataModel;
27 import com.android.deskclock.uidata.UiDataModel.Tab;
28 
29 public abstract class DeskClockFragment extends Fragment implements FabContainer, FabController {
30 
31     /** The tab associated with this fragment. */
32     private final Tab mTab;
33 
34     /** The container that houses the fab and its left and right buttons. */
35     private FabContainer mFabContainer;
36 
DeskClockFragment(Tab tab)37     public DeskClockFragment(Tab tab) {
38         mTab = tab;
39     }
40 
41     @Override
onResume()42     public void onResume() {
43         super.onResume();
44 
45         // Update the fab and buttons in case their state changed while the fragment was paused.
46         if (isTabSelected()) {
47             updateFab(FAB_AND_BUTTONS_IMMEDIATE);
48         }
49     }
50 
onKeyDown(int keyCode, KeyEvent event)51     public boolean onKeyDown(int keyCode, KeyEvent event) {
52         // By default return false so event continues to propagate
53         return false;
54     }
55 
56     @Override
onLeftButtonClick(@onNull Button left)57     public void onLeftButtonClick(@NonNull Button left) {
58         // Do nothing here, only in derived classes
59     }
60 
61     @Override
onRightButtonClick(@onNull Button right)62     public void onRightButtonClick(@NonNull Button right) {
63         // Do nothing here, only in derived classes
64     }
65 
66     @Override
onMorphFab(@onNull ImageView fab)67     public void onMorphFab(@NonNull ImageView fab) {
68         // Do nothing here, only in derived classes
69     }
70 
71     /**
72      * @param color the newly installed app window color
73      */
onAppColorChanged(@olorInt int color)74     protected void onAppColorChanged(@ColorInt int color) {
75         // Do nothing here, only in derived classes
76     }
77 
78     /**
79      * @param fabContainer the container that houses the fab and its left and right buttons
80      */
setFabContainer(FabContainer fabContainer)81     public final void setFabContainer(FabContainer fabContainer) {
82         mFabContainer = fabContainer;
83     }
84 
85     /**
86      * Requests that the parent activity update the fab and buttons.
87      *
88      * @param updateTypes the manner in which the fab container should be updated
89      */
90     @Override
updateFab(@pdateFabFlag int updateTypes)91     public final void updateFab(@UpdateFabFlag int updateTypes) {
92         if (mFabContainer != null) {
93             mFabContainer.updateFab(updateTypes);
94         }
95     }
96 
97     /**
98      * @return {@code true} iff the currently selected tab displays this fragment
99      */
isTabSelected()100     public final boolean isTabSelected() {
101         return UiDataModel.getUiDataModel().getSelectedTab() == mTab;
102     }
103 
104     /**
105      * Select the tab that displays this fragment.
106      */
selectTab()107     public final void selectTab() {
108         UiDataModel.getUiDataModel().setSelectedTab(mTab);
109     }
110 
111     /**
112      * Updates the scrolling state in the {@link UiDataModel} for this tab.
113      *
114      * @param scrolledToTop {@code true} iff the vertical scroll position of this tab is at the top
115      */
setTabScrolledToTop(boolean scrolledToTop)116     public final void setTabScrolledToTop(boolean scrolledToTop) {
117         UiDataModel.getUiDataModel().setTabScrolledToTop(mTab, scrolledToTop);
118     }
119 }