• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ActionBar;
20 import android.app.Activity;
21 import android.graphics.drawable.ColorDrawable;
22 import android.graphics.drawable.Drawable;
23 import android.os.Bundle;
24 import android.view.View;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 import androidx.fragment.app.Fragment;
29 
30 import com.android.car.apps.common.util.Themes;
31 import com.android.car.dialer.R;
32 import com.android.car.dialer.ui.TelecomActivity;
33 
34 /** The base class for top level dialer content {@link Fragment}s. */
35 public abstract class DialerBaseFragment extends Fragment {
36 
37     /**
38      * Interface for Dialer top level fragment's parent to implement.
39      */
40     public interface DialerFragmentParent {
41 
42         /** Sets the background drawable. */
setBackground(Drawable background)43         void setBackground(Drawable background);
44 
45         /** Push a fragment to the back stack. Update action bar accordingly. */
pushContentFragment(Fragment fragment, String fragmentTag)46         void pushContentFragment(Fragment fragment, String fragmentTag);
47     }
48 
49     @Override
onCreate(Bundle savedInstanceState)50     public void onCreate(Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52         setHasOptionsMenu(true);
53     }
54 
55     @Override
onResume()56     public void onResume() {
57         setFullScreenBackground();
58 
59         Activity parentActivity = getActivity();
60         ActionBar actionBar = parentActivity.getActionBar();
61         if (actionBar != null) {
62             setupActionBar(actionBar);
63         }
64 
65         super.onResume();
66     }
67 
68     /**
69      * Sets a fullscreen background to its parent Activity.
70      */
setFullScreenBackground()71     protected void setFullScreenBackground() {
72         Activity parentActivity = getActivity();
73         if (parentActivity instanceof DialerFragmentParent) {
74             ((DialerFragmentParent) parentActivity).setBackground(getFullScreenBackgroundColor());
75         }
76     }
77 
78     /** Customizes the action bar. Can be overridden in subclasses. */
setupActionBar(@onNull ActionBar actionBar)79     protected void setupActionBar(@NonNull ActionBar actionBar) {
80         actionBar.setTitle(getActionBarTitle());
81         actionBar.setCustomView(null);
82         setActionBarBackground(getContext().getDrawable(R.color.app_bar_background_color));
83     }
84 
85     /**
86      * Returns the full screen background for its parent Activity. Override this function to
87      * change the background.
88      */
getFullScreenBackgroundColor()89     protected Drawable getFullScreenBackgroundColor() {
90         return new ColorDrawable(Themes.getAttrColor(getContext(), android.R.attr.background));
91     }
92 
93     /** Push a fragment to the back stack. Update action bar accordingly. */
pushContentFragment(@onNull Fragment fragment, String fragmentTag)94     protected void pushContentFragment(@NonNull Fragment fragment, String fragmentTag) {
95         Activity parentActivity = getActivity();
96         if (parentActivity instanceof DialerFragmentParent) {
97             ((DialerFragmentParent) parentActivity).pushContentFragment(fragment, fragmentTag);
98         }
99     }
100 
101     /** Return the action bar title. */
getActionBarTitle()102     protected CharSequence getActionBarTitle() {
103         return getString(R.string.default_toolbar_title);
104     }
105 
getTopBarHeight()106     protected int getTopBarHeight() {
107         View toolbar = getActivity().findViewById(R.id.car_toolbar);
108 
109         int backStackEntryCount =
110                 getActivity().getSupportFragmentManager().getBackStackEntryCount();
111         int topBarHeight = Themes.getAttrDimensionPixelSize(getContext(),
112                 android.R.attr.actionBarSize);
113         // Tabs are not child of the toolbar and tabs are visible.
114         if (toolbar.findViewById(R.id.tab_layout) == null && backStackEntryCount == 1) {
115             topBarHeight += topBarHeight;
116         }
117         return topBarHeight;
118     }
119 
setActionBarBackground(@ullable Drawable drawable)120     protected final void setActionBarBackground(@Nullable Drawable drawable) {
121         Activity activity = getActivity();
122         if (activity instanceof TelecomActivity) {
123             ((TelecomActivity) activity).setActionBarBackground(drawable);
124         }
125     }
126 }
127