Use pushed-state on "star"

On phone layouts, it is now a proper actionbar button (not custom anymore)
On tablet layouts, it is now an ImageView, so that we can center the icon

Bug:6008899

Change-Id: Ida56f959d129480e799fa8a4c477272e4de70eac
diff --git a/res/drawable-hdpi/btn_star_off_normal_holo_light.png b/res/drawable-hdpi/btn_star_off_normal_holo_light.png
new file mode 100644
index 0000000..cc17004
--- /dev/null
+++ b/res/drawable-hdpi/btn_star_off_normal_holo_light.png
Binary files differ
diff --git a/res/drawable-hdpi/btn_star_on_normal_holo_light.png b/res/drawable-hdpi/btn_star_on_normal_holo_light.png
new file mode 100644
index 0000000..ce97262
--- /dev/null
+++ b/res/drawable-hdpi/btn_star_on_normal_holo_light.png
Binary files differ
diff --git a/res/drawable-mdpi/btn_star_off_normal_holo_light.png b/res/drawable-mdpi/btn_star_off_normal_holo_light.png
new file mode 100644
index 0000000..70483d9
--- /dev/null
+++ b/res/drawable-mdpi/btn_star_off_normal_holo_light.png
Binary files differ
diff --git a/res/drawable-mdpi/btn_star_on_normal_holo_light.png b/res/drawable-mdpi/btn_star_on_normal_holo_light.png
new file mode 100644
index 0000000..3a52c18
--- /dev/null
+++ b/res/drawable-mdpi/btn_star_on_normal_holo_light.png
Binary files differ
diff --git a/res/drawable-xhdpi/btn_star_off_normal_holo_light.png b/res/drawable-xhdpi/btn_star_off_normal_holo_light.png
new file mode 100644
index 0000000..ff718a7
--- /dev/null
+++ b/res/drawable-xhdpi/btn_star_off_normal_holo_light.png
Binary files differ
diff --git a/res/drawable-xhdpi/btn_star_on_normal_holo_light.png b/res/drawable-xhdpi/btn_star_on_normal_holo_light.png
new file mode 100644
index 0000000..282145f
--- /dev/null
+++ b/res/drawable-xhdpi/btn_star_on_normal_holo_light.png
Binary files differ
diff --git a/res/layout-sw580dp/favorites_star.xml b/res/layout-sw580dp/favorites_star.xml
index 7959df3..22b9339 100644
--- a/res/layout-sw580dp/favorites_star.xml
+++ b/res/layout-sw580dp/favorites_star.xml
@@ -14,19 +14,12 @@
      limitations under the License.
 -->
 
-<FrameLayout
+<!-- The favorite star, shown outside of the ActionBar -->
+<ImageView
     xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="wrap_content"
-    android:layout_height="wrap_content"
-    android:minHeight="50dip">
-
-    <CheckBox
-        android:id="@+id/star"
-        android:duplicateParentState="true"
-        android:layout_width="wrap_content"
-        android:layout_height="match_parent"
-        android:layout_gravity="center_vertical"
-        android:contentDescription="@string/description_star"
-        style="?android:attr/starStyle"/>
-
-</FrameLayout>
+    android:id="@+id/star"
+    android:layout_width="48dip"
+    android:layout_height="48dip"
+    android:scaleType="center"
+    android:background="?android:attr/selectableItemBackground"
+    android:src="@drawable/btn_star_off_normal_holo_light" />
diff --git a/res/layout/favorites_star.xml b/res/layout/favorites_star.xml
deleted file mode 100644
index 2ac3039..0000000
--- a/res/layout/favorites_star.xml
+++ /dev/null
@@ -1,33 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2011 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.
--->
-
-<FrameLayout
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="wrap_content"
-    android:layout_height="wrap_content"
-    android:paddingLeft="10dip"
-    android:paddingRight="10dip">
-
-    <CheckBox
-        android:id="@+id/star"
-        android:duplicateParentState="true"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_gravity="center_vertical"
-        android:contentDescription="@string/description_star"
-        android:visibility="invisible"
-        android:button="@drawable/btn_star_holo_dark"/>
-</FrameLayout>
diff --git a/src/com/android/contacts/ContactSaveService.java b/src/com/android/contacts/ContactSaveService.java
index fb549d2..f475609 100644
--- a/src/com/android/contacts/ContactSaveService.java
+++ b/src/com/android/contacts/ContactSaveService.java
@@ -35,13 +35,12 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.OperationApplicationException;
-import android.content.res.AssetFileDescriptor;
 import android.database.Cursor;
 import android.net.Uri;
+import android.os.Bundle;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.Parcelable;
-import android.os.Bundle;
 import android.os.RemoteException;
 import android.provider.ContactsContract;
 import android.provider.ContactsContract.AggregationExceptions;
@@ -55,15 +54,14 @@
 import android.util.Log;
 import android.widget.Toast;
 
-import java.lang.Long;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.concurrent.CopyOnWriteArrayList;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
 
 /**
  * A service responsible for saving changes to the content provider.
diff --git a/src/com/android/contacts/activities/ContactDetailActivity.java b/src/com/android/contacts/activities/ContactDetailActivity.java
index 601e9fb..dd3b03b 100644
--- a/src/com/android/contacts/activities/ContactDetailActivity.java
+++ b/src/com/android/contacts/activities/ContactDetailActivity.java
@@ -45,11 +45,8 @@
 import android.view.MenuItem;
 import android.view.MenuItem.OnMenuItemClickListener;
 import android.view.View;
-import android.view.View.OnClickListener;
-import android.view.ViewGroup;
 import android.view.accessibility.AccessibilityEvent;
 import android.view.accessibility.AccessibilityManager;
-import android.widget.CheckBox;
 import android.widget.Toast;
 
 import java.util.ArrayList;
@@ -151,27 +148,37 @@
 
     @Override
     public boolean onPrepareOptionsMenu(Menu menu) {
-        MenuItem starredMenuItem = menu.findItem(R.id.menu_star);
-        ViewGroup starredContainer = (ViewGroup) getLayoutInflater().inflate(
-                R.layout.favorites_star, null, false);
-        final CheckBox starredView = (CheckBox) starredContainer.findViewById(R.id.star);
-        starredView.setOnClickListener(new OnClickListener() {
+        final MenuItem starredMenuItem = menu.findItem(R.id.menu_star);
+        starredMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
             @Override
-            public void onClick(View v) {
+            public boolean onMenuItemClick(MenuItem item) {
                 // Toggle "starred" state
                 // Make sure there is a contact
                 if (mLookupUri != null) {
+                    // Read the current starred value from the UI instead of using the last
+                    // loaded state. This allows rapid tapping without writing the same
+                    // value several times
+                    final boolean isStarred = starredMenuItem.isChecked();
+
+                    // To improve responsiveness, swap out the picture (and tag) in the UI already
+                    ContactDetailDisplayUtils.configureStarredMenuItem(starredMenuItem,
+                            mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
+                            !isStarred);
+
+                    // Now perform the real save
                     Intent intent = ContactSaveService.createSetStarredIntent(
-                            ContactDetailActivity.this, mLookupUri, starredView.isChecked());
+                            ContactDetailActivity.this, mLookupUri, !isStarred);
                     ContactDetailActivity.this.startService(intent);
                 }
+                return true;
             }
         });
         // If there is contact data, update the starred state
         if (mContactData != null) {
-            ContactDetailDisplayUtils.setStarred(mContactData, starredView);
+            ContactDetailDisplayUtils.configureStarredMenuItem(starredMenuItem,
+                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
+                    mContactData.getStarred());
         }
-        starredMenuItem.setActionView(starredContainer);
         return true;
     }
 
diff --git a/src/com/android/contacts/detail/ContactDetailDisplayUtils.java b/src/com/android/contacts/detail/ContactDetailDisplayUtils.java
index 912d7fb..08e8bfe 100644
--- a/src/com/android/contacts/detail/ContactDetailDisplayUtils.java
+++ b/src/com/android/contacts/detail/ContactDetailDisplayUtils.java
@@ -54,12 +54,12 @@
 import android.text.TextUtils;
 import android.util.Log;
 import android.view.LayoutInflater;
+import android.view.MenuItem;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.ViewGroup;
 import android.view.animation.AccelerateInterpolator;
 import android.view.animation.AlphaAnimation;
-import android.widget.CheckBox;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.ListView;
@@ -283,17 +283,43 @@
     /**
      * Sets the starred state of this contact.
      */
-    public static void setStarred(Result contactData, CheckBox starredView) {
+    public static void configureStarredImageView(ImageView starredView, boolean isDirectoryEntry,
+            boolean isUserProfile, boolean isStarred) {
         // Check if the starred state should be visible
-        if (!contactData.isDirectoryEntry() && !contactData.isUserProfile()) {
+        if (!isDirectoryEntry && !isUserProfile) {
             starredView.setVisibility(View.VISIBLE);
-            starredView.setChecked(contactData.getStarred());
+            final int resId = isStarred
+                    ? R.drawable.btn_star_on_normal_holo_light
+                    : R.drawable.btn_star_off_normal_holo_light;
+            starredView.setImageResource(resId);
+            starredView.setTag(isStarred);
+            starredView.setContentDescription(starredView.getResources().getString(
+                    isStarred ? R.string.menu_removeStar : R.string.menu_addStar));
         } else {
             starredView.setVisibility(View.GONE);
         }
     }
 
     /**
+     * Sets the starred state of this contact.
+     */
+    public static void configureStarredMenuItem(MenuItem starredMenuItem, boolean isDirectoryEntry,
+            boolean isUserProfile, boolean isStarred) {
+        // Check if the starred state should be visible
+        if (!isDirectoryEntry && !isUserProfile) {
+            starredMenuItem.setVisible(true);
+            final int resId = isStarred
+                    ? R.drawable.btn_star_on_normal_holo_dark
+                    : R.drawable.btn_star_off_normal_holo_dark;
+            starredMenuItem.setIcon(resId);
+            starredMenuItem.setChecked(isStarred);
+            starredMenuItem.setTitle(isStarred ? R.string.menu_removeStar : R.string.menu_addStar);
+        } else {
+            starredMenuItem.setVisible(false);
+        }
+    }
+
+    /**
      * Set the social snippet text. If there isn't one, then set the view to gone.
      */
     public static void setSocialSnippet(Context context, Result contactData, TextView statusView,
diff --git a/src/com/android/contacts/detail/ContactDetailFragment.java b/src/com/android/contacts/detail/ContactDetailFragment.java
index 7c79a4c..b0050c0 100644
--- a/src/com/android/contacts/detail/ContactDetailFragment.java
+++ b/src/com/android/contacts/detail/ContactDetailFragment.java
@@ -1356,7 +1356,7 @@
         public final TextView companyView;
         public final ImageView photoView;
         public final View photoOverlayView;
-        public final CheckBox starredView;
+        public final ImageView starredView;
         public final int layoutResourceId;
 
         public HeaderViewCache(View view, int layoutResourceInflated) {
@@ -1364,7 +1364,7 @@
             companyView = (TextView) view.findViewById(R.id.company);
             photoView = (ImageView) view.findViewById(R.id.photo);
             photoOverlayView = view.findViewById(R.id.photo_touch_intercept_overlay);
-            starredView = (CheckBox) view.findViewById(R.id.star);
+            starredView = (ImageView) view.findViewById(R.id.star);
             layoutResourceId = layoutResourceInflated;
         }
 
@@ -1505,9 +1505,11 @@
             }
 
             // Set the starred state if it should be displayed
-            final CheckBox favoritesStar = viewCache.starredView;
+            final ImageView favoritesStar = viewCache.starredView;
             if (favoritesStar != null) {
-                ContactDetailDisplayUtils.setStarred(mContactData, favoritesStar);
+                ContactDetailDisplayUtils.configureStarredImageView(favoritesStar,
+                        mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
+                        mContactData.getStarred());
                 final Uri lookupUri = mContactData.getLookupUri();
                 favoritesStar.setOnClickListener(new OnClickListener() {
                     @Override
@@ -1515,8 +1517,22 @@
                         // Toggle "starred" state
                         // Make sure there is a contact
                         if (lookupUri != null) {
+                            // Read the current starred value from the UI instead of using the last
+                            // loaded state. This allows rapid tapping without writing the same
+                            // value several times
+                            final Object tag = favoritesStar.getTag();
+                            final boolean isStarred = tag == null
+                                    ? false : (Boolean) favoritesStar.getTag();
+
+                            // To improve responsiveness, swap out the picture (and tag) in the UI
+                            // already
+                            ContactDetailDisplayUtils.configureStarredImageView(favoritesStar,
+                                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
+                                    !isStarred);
+
+                            // Now perform the real save
                             Intent intent = ContactSaveService.createSetStarredIntent(
-                                    getContext(), lookupUri, favoritesStar.isChecked());
+                                    getContext(), lookupUri, !isStarred);
                             getContext().startService(intent);
                         }
                     }