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