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 = "navigation_bar.xml"; 42 private static final String LAYOUT_600DP_XML = "navigation_bar600dp.xml"; 43 NavigationBar(BridgeContext context, Density density, int orientation, boolean isRtl, boolean rtlEnabled, int simulatedPlatformVersion, boolean quickStepEnabled)44 public NavigationBar(BridgeContext context, Density density, int orientation, boolean isRtl, 45 boolean rtlEnabled, int simulatedPlatformVersion, boolean quickStepEnabled) { 46 this(context, density, orientation, isRtl, rtlEnabled, simulatedPlatformVersion, 47 getShortestWidth(context)>= 600 ? LAYOUT_600DP_XML : LAYOUT_XML, quickStepEnabled); 48 } 49 NavigationBar(BridgeContext context, Density density, int orientation, boolean isRtl, boolean rtlEnabled, int simulatedPlatformVersion, String layoutPath, boolean quickStepEnabled)50 protected NavigationBar(BridgeContext context, Density density, int orientation, boolean isRtl, 51 boolean rtlEnabled, int simulatedPlatformVersion, String layoutPath, boolean quickStepEnabled) { 52 super(context, orientation, layoutPath, 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, 71 quickStepEnabled ? "ic_sysbar_back_quick_step.png" : "ic_sysbar_back.png", 72 density, isRtl); 73 //noinspection SpellCheckingInspection 74 loadIcon(3, quickStepEnabled ? "ic_sysbar_home_quick_step.png" : "ic_sysbar_home.png", 75 density, 76 isRtl); 77 if (!quickStepEnabled) { 78 //noinspection SpellCheckingInspection 79 loadIcon(recent, "ic_sysbar_recent.png", density, isRtl); 80 } 81 setupNavBar(context, orientation); 82 } 83 setupNavBar(BridgeContext context, int orientation)84 private void setupNavBar(BridgeContext context, int orientation) { 85 float sw = getShortestWidth(context); 86 View leftPadding = getChildAt(0); 87 View rightPadding = getChildAt(6); 88 setSize(context, leftPadding, orientation, getSidePadding(sw)); 89 setSize(context, rightPadding, orientation, getSidePadding(sw)); 90 int navButtonWidth = getWidth(sw); 91 for (int i = 1; i < 6; i += 2) { 92 View navButton = getChildAt(i); 93 setSize(context, navButton, orientation, navButtonWidth); 94 } 95 if (sw >= 600) { 96 setSize(context, getChildAt(2), orientation, 128); 97 setSize(context, getChildAt(4), orientation, 128); 98 } 99 } 100 setSize(BridgeContext context, View view, int orientation, int size)101 private static void setSize(BridgeContext context, View view, int orientation, int size) { 102 size *= context.getMetrics().density; 103 LayoutParams layoutParams = (LayoutParams) view.getLayoutParams(); 104 if (orientation == HORIZONTAL) { 105 layoutParams.width = size; 106 } else { 107 layoutParams.height = size; 108 } 109 view.setLayoutParams(layoutParams); 110 } 111 getSidePadding(float sw)112 protected int getSidePadding(float sw) { 113 if (sw >= 400) { 114 return PADDING_WIDTH_SW400; 115 } 116 if (sw >= 360) { 117 return PADDING_WIDTH_SW360; 118 } 119 return PADDING_WIDTH_DEFAULT; 120 } 121 getWidth(float sw)122 private static int getWidth(float sw) { 123 if (sw >= 600) { 124 return WIDTH_SW600; 125 } 126 if (sw >= 360) { 127 return WIDTH_SW360; 128 } 129 return WIDTH_DEFAULT; 130 } 131 getShortestWidth(BridgeContext context)132 private static float getShortestWidth(BridgeContext context) { 133 DisplayMetrics metrics = context.getMetrics(); 134 float sw = metrics.widthPixels < metrics.heightPixels ? 135 metrics.widthPixels : metrics.heightPixels; 136 sw /= metrics.density; 137 return sw; 138 } 139 140 @Override 141 protected TextView getStyleableTextView() { 142 return null; 143 } 144 } 145