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.dialer.main.impl.bottomnav; 18 19 import android.content.Context; 20 import android.content.res.ColorStateList; 21 import android.support.annotation.DrawableRes; 22 import android.support.annotation.Nullable; 23 import android.support.annotation.Px; 24 import android.support.annotation.StringRes; 25 import android.util.AttributeSet; 26 import android.view.View; 27 import android.widget.FrameLayout; 28 import android.widget.ImageView; 29 import android.widget.LinearLayout; 30 import android.widget.TextView; 31 import com.android.dialer.common.Assert; 32 import com.android.dialer.configprovider.ConfigProviderComponent; 33 import com.android.dialer.theme.base.ThemeComponent; 34 35 /** Navigation item in a bottom nav. */ 36 final class BottomNavItem extends LinearLayout { 37 38 private ImageView image; 39 private TextView text; 40 private TextView notificationBadge; 41 BottomNavItem(Context context, @Nullable AttributeSet attrs)42 public BottomNavItem(Context context, @Nullable AttributeSet attrs) { 43 super(context, attrs); 44 } 45 46 @Override onFinishInflate()47 protected void onFinishInflate() { 48 super.onFinishInflate(); 49 image = findViewById(R.id.bottom_nav_item_image); 50 text = findViewById(R.id.bottom_nav_item_text); 51 notificationBadge = findViewById(R.id.notification_badge); 52 } 53 54 @Override setSelected(boolean selected)55 public void setSelected(boolean selected) { 56 super.setSelected(selected); 57 int colorId = 58 selected 59 ? ThemeComponent.get(getContext()).theme().getColorPrimary() 60 : ThemeComponent.get(getContext()).theme().getTextColorSecondary(); 61 image.setImageTintList(ColorStateList.valueOf(colorId)); 62 text.setTextColor(colorId); 63 } 64 setup(@tringRes int stringRes, @DrawableRes int drawableRes)65 void setup(@StringRes int stringRes, @DrawableRes int drawableRes) { 66 text.setText(stringRes); 67 image.setImageResource(drawableRes); 68 } 69 setNotificationCount(int count)70 void setNotificationCount(int count) { 71 Assert.checkArgument(count >= 0, "Invalid count: " + count); 72 if (count == 0) { 73 notificationBadge.setVisibility(View.INVISIBLE); 74 } else { 75 String countString = String.format(Integer.toString(count)); 76 77 boolean use99PlusCount = 78 ConfigProviderComponent.get(getContext()) 79 .getConfigProvider() 80 .getBoolean("use_99_plus", false); 81 boolean use9Plus = !use99PlusCount; 82 83 if (use99PlusCount && count > 99) { 84 countString = getContext().getString(R.string.bottom_nav_count_99_plus); 85 } else if (use9Plus && count > 9) { 86 countString = getContext().getString(R.string.bottom_nav_count_9_plus); 87 } 88 notificationBadge.setVisibility(View.VISIBLE); 89 notificationBadge.setText(countString); 90 91 @Px int margin; 92 if (countString.length() == 1) { 93 margin = getContext().getResources().getDimensionPixelSize(R.dimen.badge_margin_length_1); 94 } else if (countString.length() == 2) { 95 margin = getContext().getResources().getDimensionPixelSize(R.dimen.badge_margin_length_2); 96 } else { 97 margin = getContext().getResources().getDimensionPixelSize(R.dimen.badge_margin_length_3); 98 } 99 100 FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) image.getLayoutParams(); 101 params.setMarginStart(margin); 102 params.setMarginEnd(margin); 103 image.setLayoutParams(params); 104 } 105 } 106 } 107