• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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.layoutlib.bridge.bars;
18 
19 import com.android.layoutlib.bridge.android.BridgeContext;
20 import com.android.resources.Density;
21 
22 import android.content.Context;
23 import android.content.pm.ApplicationInfo;
24 import android.util.AttributeSet;
25 import android.util.DisplayMetrics;
26 import android.view.View;
27 import android.widget.LinearLayout;
28 import android.widget.TextView;
29 
30 public class NavigationBar extends CustomBar {
31 
32     /** Navigation bar background color attribute name. */
33     private static final String ATTR_COLOR = "navigationBarColor";
34     /** Attribute for translucency property. */
35     public static final String ATTR_TRANSLUCENT = "windowTranslucentNavigation";
36     // These correspond to @dimen/navigation_side_padding in the system ui code.
37     private static final int PADDING_WIDTH_DEFAULT = 36;
38     private static final int PADDING_WIDTH_SW360 = 40;
39     private static final int PADDING_WIDTH_SW400 = 50;
40     // These corresponds to @dimen/navigation_key_width in the system ui code.
41     private static final int WIDTH_DEFAULT = 36;
42     private static final int WIDTH_SW360 = 40;
43     private static final int WIDTH_SW600 = 48;
44     private static final String LAYOUT_XML = "/bars/navigation_bar.xml";
45     private static final String LAYOUT_600DP_XML = "/bars/navigation_bar600dp.xml";
46 
47 
48     /**
49      * Constructor to be used when creating the {@link NavigationBar} as a regular control.
50      * This is currently used by the theme editor.
51      */
52     @SuppressWarnings("unused")
NavigationBar(Context context, AttributeSet attrs)53     public NavigationBar(Context context, AttributeSet attrs) {
54         this((BridgeContext) context,
55                 Density.getEnum(((BridgeContext) context).getMetrics().densityDpi),
56                 LinearLayout.HORIZONTAL, // In this mode, it doesn't need to be render vertically
57                 ((BridgeContext) context).getConfiguration().getLayoutDirection() ==
58                         View.LAYOUT_DIRECTION_RTL,
59                 (context.getApplicationInfo().flags & ApplicationInfo.FLAG_SUPPORTS_RTL) != 0,
60                 0);
61     }
62 
NavigationBar(BridgeContext context, Density density, int orientation, boolean isRtl, boolean rtlEnabled, int simulatedPlatformVersion)63     public NavigationBar(BridgeContext context, Density density, int orientation, boolean isRtl,
64             boolean rtlEnabled, int simulatedPlatformVersion) {
65         super(context, orientation, getShortestWidth(context)>= 600 ? LAYOUT_600DP_XML : LAYOUT_XML,
66                 "navigation_bar.xml", simulatedPlatformVersion);
67 
68         int color = getBarColor(ATTR_COLOR, ATTR_TRANSLUCENT);
69         setBackgroundColor(color == 0 ? 0xFF000000 : color);
70 
71         // Cannot access the inside items through id because no R.id values have been
72         // created for them.
73         // We do know the order though.
74         // 0 is a spacer.
75         int back = 1;
76         int recent = 5;
77         if (orientation == LinearLayout.VERTICAL || (isRtl && !rtlEnabled)) {
78             // If RTL is enabled, then layoutlib mirrors the layout for us.
79             back = 5;
80             recent = 1;
81         }
82 
83         //noinspection SpellCheckingInspection
84         loadIcon(back, "ic_sysbar_back.png", density, isRtl);
85         //noinspection SpellCheckingInspection
86         loadIcon(3, "ic_sysbar_home.png", density, isRtl);
87         //noinspection SpellCheckingInspection
88         loadIcon(recent, "ic_sysbar_recent.png", density, isRtl);
89         setupNavBar(context, orientation);
90     }
91 
setupNavBar(BridgeContext context, int orientation)92     private void setupNavBar(BridgeContext context, int orientation) {
93         float sw = getShortestWidth(context);
94         View leftPadding = getChildAt(0);
95         View rightPadding = getChildAt(6);
96         setSize(context, leftPadding, orientation, getSidePadding(sw));
97         setSize(context, rightPadding, orientation, getSidePadding(sw));
98         int navButtonWidth = getWidth(sw);
99         for (int i = 1; i < 6; i += 2) {
100             View navButton = getChildAt(i);
101             setSize(context, navButton, orientation, navButtonWidth);
102         }
103         if (sw >= 600) {
104             setSize(context, getChildAt(2), orientation, 128);
105             setSize(context, getChildAt(4), orientation, 128);
106         }
107     }
108 
setSize(BridgeContext context, View view, int orientation, int size)109     private static void setSize(BridgeContext context, View view, int orientation, int size) {
110         size *= context.getMetrics().density;
111         LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
112         if (orientation == HORIZONTAL) {
113             layoutParams.width = size;
114         } else {
115             layoutParams.height = size;
116         }
117         view.setLayoutParams(layoutParams);
118     }
119 
getSidePadding(float sw)120     private static int getSidePadding(float sw) {
121         if (sw >= 400) {
122             return PADDING_WIDTH_SW400;
123         }
124         if (sw >= 360) {
125             return PADDING_WIDTH_SW360;
126         }
127         return PADDING_WIDTH_DEFAULT;
128     }
129 
getWidth(float sw)130     private static int getWidth(float sw) {
131         if (sw >= 600) {
132             return WIDTH_SW600;
133         }
134         if (sw >= 360) {
135             return WIDTH_SW360;
136         }
137         return WIDTH_DEFAULT;
138     }
139 
getShortestWidth(BridgeContext context)140     private static float getShortestWidth(BridgeContext context) {
141         DisplayMetrics metrics = context.getMetrics();
142         float sw = metrics.widthPixels < metrics.heightPixels ?
143                 metrics.widthPixels : metrics.heightPixels;
144         sw /= metrics.density;
145         return sw;
146     }
147 
148     @Override
149     protected TextView getStyleableTextView() {
150         return null;
151     }
152 }
153