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