Added 'new' badge to navigation drawer.

Added 'new' badge to navigation drawer for
assistant. The badge will show until the
welcome assistant card is dismissed.

Test: Manual, verified badge is displayed until user dismisses the
welcome card.

Bug: 32542932
Change-Id: I5145ebaefc88444773a1d25ff2bfb11e6e6b0678
diff --git a/res/drawable/new_badge_background.xml b/res/drawable/new_badge_background.xml
new file mode 100644
index 0000000..be18ade
--- /dev/null
+++ b/res/drawable/new_badge_background.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<shape xmlns:android="http://schemas.android.com/apk/res/android">
+    <solid android:color="@color/dialtacts_secondary_text_color"/>
+    <corners android:radius="@dimen/nav_new_badge_corners"/>
+</shape>
\ No newline at end of file
diff --git a/res/layout/assistant_new_badge.xml b/res/layout/assistant_new_badge.xml
new file mode 100644
index 0000000..6ca8cc5
--- /dev/null
+++ b/res/layout/assistant_new_badge.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/assistant_new_badge"
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
+    android:layout_gravity="center_vertical"
+    android:paddingLeft="@dimen/nav_new_badge_horizontal_pad"
+    android:paddingTop="@dimen/nav_new_badge_vertical_pad"
+    android:paddingRight="@dimen/nav_new_badge_horizontal_pad"
+    android:paddingBottom="@dimen/nav_new_badge_vertical_pad"
+    android:background="@drawable/new_badge_background"
+    android:textSize="@dimen/nav_new_badge_font_size"
+    android:textAllCaps="true"
+    android:textColor="#FFFFFF"
+    android:text="@string/menu_assistant_new_badge"/>
\ No newline at end of file
diff --git a/res/menu/activity_main_drawer.xml b/res/menu/activity_main_drawer.xml
index 75deb8b..877c726 100644
--- a/res/menu/activity_main_drawer.xml
+++ b/res/menu/activity_main_drawer.xml
@@ -16,7 +16,8 @@
   limitations under the License.
   -->
 
-<menu xmlns:android="http://schemas.android.com/apk/res/android">
+<menu xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto">
 
     <group android:id="@+id/nav_utilities">
         <item
@@ -25,6 +26,7 @@
             android:title="@string/contactsList" />
         <item
             android:id="@+id/nav_assistant"
+            app:actionViewClass="android.widget.LinearLayout"
             android:icon="@drawable/ic_assistant"
             android:title="@string/menu_assistant"/>
         <item
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index 896d4e6..d3431fb 100644
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -248,6 +248,11 @@
     <dimen name="nav_activity_horizontal_margin">16dp</dimen>
     <dimen name="nav_activity_vertical_margin">16dp</dimen>
 
+    <dimen name="nav_new_badge_corners">3dp</dimen>
+    <dimen name="nav_new_badge_vertical_pad">2dp</dimen>
+    <dimen name="nav_new_badge_horizontal_pad">5dp</dimen>
+    <dimen name="nav_new_badge_font_size">11sp</dimen>
+
     <!-- Top padding for the image for empty group view -->
     <dimen name="empty_group_view_image_padding_top">8dp</dimen>
 
diff --git a/res/values/strings.xml b/res/values/strings.xml
index d8f3362..29c92c2 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -926,6 +926,9 @@
     <!-- The menu item to open the link/merge duplicates activity. [CHAR LIMIT=20]-->
     <string name="menu_duplicates">Duplicates</string>
 
+    <!-- Assistant 'NEW' badge text. Badge shown next to Assistant in left drawer menu when user has not visited it yet. [CHAR LIMIT=10] -->
+    <string name="menu_assistant_new_badge">New</string>
+
     <!-- Open drawer content descriptions [CHAR LIMIT=40] -->
     <string name="navigation_drawer_open">Open navigation drawer</string>
 
diff --git a/src/com/android/contacts/ContactsDrawerActivity.java b/src/com/android/contacts/ContactsDrawerActivity.java
index caaa326..29ac4f8 100644
--- a/src/com/android/contacts/ContactsDrawerActivity.java
+++ b/src/com/android/contacts/ContactsDrawerActivity.java
@@ -28,11 +28,13 @@
 import android.support.annotation.LayoutRes;
 import android.support.design.widget.NavigationView;
 import android.support.v4.view.GravityCompat;
+import android.support.v4.view.MenuItemCompat;
 import android.support.v4.widget.DrawerLayout;
 import android.support.v7.app.ActionBarDrawerToggle;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
 import android.util.Log;
+import android.view.Gravity;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuItem;
@@ -40,6 +42,7 @@
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.LinearLayout;
+import android.widget.TextView;
 import android.widget.Toast;
 
 import com.android.contacts.activities.ActionBarAdapter;
@@ -138,6 +141,7 @@
             // overlaid by the action bar of the newly-created fragment.
             stopSearchAndSelection();
             updateStatusBarBackground();
+            initializeAssistantNewBadge();
         }
 
         private void stopSearchAndSelection() {
@@ -198,6 +202,9 @@
     // The account the new group will be created under.
     private AccountWithDataSet mNewGroupAccount;
 
+    // Recycle badge if possible
+    private TextView mAssistantNewBadge;
+
     // Checkable menu item lookup maps. Every map declared here should be added to
     // clearCheckedMenus() so that they can be cleared.
     // TODO find a better way to handle selected menu item state, when switching to fragments.
@@ -247,6 +254,7 @@
         mNavigationView.setNavigationItemSelectedListener(this);
         setUpMenu();
 
+        initializeAssistantNewBadge();
         loadGroupsAndFilters();
 
         if (savedState != null && savedState.containsKey(KEY_NEW_GROUP_ACCOUNT)) {
@@ -255,6 +263,25 @@
         }
     }
 
+    private void initializeAssistantNewBadge() {
+        if (!Flags.getInstance().getBoolean(Experiments.ASSISTANT)) {
+            return;
+        }
+        final LinearLayout newBadgeFrame = (LinearLayout) MenuItemCompat.getActionView(
+                mNavigationView.getMenu().findItem(R.id.nav_assistant));
+        final boolean showWelcomeBadge = !SharedPreferenceUtil.isWelcomeCardDismissed(this);
+        if (showWelcomeBadge && newBadgeFrame.getChildCount() == 0) {
+            if (mAssistantNewBadge == null) {
+                mAssistantNewBadge = (TextView) LayoutInflater.from(this)
+                        .inflate(R.layout.assistant_new_badge, null);
+            }
+            newBadgeFrame.setGravity(Gravity.CENTER_VERTICAL);
+            newBadgeFrame.addView(mAssistantNewBadge);
+        } else if (!showWelcomeBadge && newBadgeFrame.getChildCount() > 0) {
+            newBadgeFrame.removeAllViews();
+        }
+    }
+
     public void setDrawerLockMode(boolean enabled) {
         // Prevent drawer from being opened by sliding from the start of screen.
         mDrawer.setDrawerLockMode(enabled ? DrawerLayout.LOCK_MODE_UNLOCKED
diff --git a/src/com/android/contacts/util/SharedPreferenceUtil.java b/src/com/android/contacts/util/SharedPreferenceUtil.java
index a1767fa..14f5494 100644
--- a/src/com/android/contacts/util/SharedPreferenceUtil.java
+++ b/src/com/android/contacts/util/SharedPreferenceUtil.java
@@ -57,6 +57,9 @@
     private static final String PREFERENCE_KEY_DISMISSED_DEVICES =
             "dismissedDevices";
 
+    public static final String PREFERENCE_WELCOME_CARD_DISMISSED =
+            "welcome-reminder-card-dismissed";
+
     public static boolean getHamburgerPromoDisplayedBefore(Context context) {
         return getSharedPreferences(context)
                 .getBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_DISPLAYED_BEFORE, false);
@@ -233,6 +236,16 @@
                 .commit();
     }
 
+    public static boolean isWelcomeCardDismissed(Context context) {
+        return getSharedPreferences(context).getBoolean(PREFERENCE_WELCOME_CARD_DISMISSED,
+                false);
+    }
+
+    public static void setWelcomeCardDismissed(Context context, boolean isDismissed) {
+        getSharedPreferences(context).edit().putBoolean(PREFERENCE_WELCOME_CARD_DISMISSED,
+                isDismissed).apply();
+    }
+
     public static void clear(Context context) {
         getSharedPreferences(context).edit().clear().commit();
     }