Fix RemoteViewException.

Bug: 2801654
Change-Id: I335e40b56981b55dac35a8d35e357d1d30c0ce9a
diff --git a/res/layout/status_bar_ongoing_event_progress_bar.xml b/res/layout/status_bar_ongoing_event_progress_bar.xml
index d1bce1a..5eb4e9d 100644
--- a/res/layout/status_bar_ongoing_event_progress_bar.xml
+++ b/res/layout/status_bar_ongoing_event_progress_bar.xml
@@ -31,7 +31,6 @@
         android:layout_height="match_parent"
         android:orientation="horizontal"
         >
-        
         <LinearLayout
             android:layout_width="40dp"
             android:layout_height="match_parent"
@@ -40,14 +39,14 @@
             android:focusable="true"
             android:clickable="true"
             >
-            <com.android.server.status.AnimatedImageView 
-                android:id="@+id/appIcon"
+            <com.android.contacts.vcard.AnimatedImageView
+                android:id="@+id/status_icon"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_gravity="center"
                 android:src="@android:drawable/sym_def_app_icon"
                 />
-            <TextView android:id="@+id/progress_text" 
+            <TextView android:id="@+id/status_progress_text"
                 android:layout_width="wrap_content" 
                 android:layout_height="wrap_content"
                 android:textColor="#ff000000"
@@ -73,7 +72,7 @@
                 android:layout_alignParentTop="true"
                 android:paddingTop="10dp"
                 >
-                <TextView android:id="@+id/title"
+                <TextView android:id="@+id/status_title"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:singleLine="true"
@@ -81,7 +80,7 @@
                     android:textColor="#ff000000"
                     android:paddingLeft="2dp"
                     />
-                <TextView android:id="@+id/description"
+                <TextView android:id="@+id/status_description"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:textColor="#ff000000"
@@ -90,7 +89,7 @@
                     android:paddingLeft="5dp"
                     />
             </LinearLayout>
-            <ProgressBar android:id="@+id/progress_bar"
+            <ProgressBar android:id="@+id/status_progress_bar"
                 style="?android:attr/progressBarStyleHorizontal"
                 android:layout_width="match_parent" 
                 android:layout_height="wrap_content"
@@ -101,7 +100,7 @@
         </RelativeLayout>
     </LinearLayout>
         
-    <com.android.server.status.AnimatedImageView 
+    <com.android.contacts.vcard.AnimatedImageView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:src="@android:drawable/divider_horizontal_bright"
diff --git a/src/com/android/contacts/vcard/AnimatedImageView.java b/src/com/android/contacts/vcard/AnimatedImageView.java
new file mode 100644
index 0000000..8911b52
--- /dev/null
+++ b/src/com/android/contacts/vcard/AnimatedImageView.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2008 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.
+ */
+
+package com.android.contacts.vcard;
+
+import android.content.Context;
+import android.graphics.drawable.AnimationDrawable;
+import android.graphics.drawable.Drawable;
+import android.util.AttributeSet;
+import android.widget.ImageView;
+import android.widget.RemoteViews.RemoteView;
+
+/**
+ * Just a bare copy from SystemUI. Do not edit. 
+ */
+public class AnimatedImageView extends ImageView {
+    AnimationDrawable mAnim;
+    boolean mAttached;
+
+    public AnimatedImageView(Context context) {
+        super(context);
+    }
+
+    public AnimatedImageView(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    private void updateAnim() {
+        Drawable drawable = getDrawable();
+        if (mAttached && mAnim != null) {
+            mAnim.stop();
+        }
+        if (drawable instanceof AnimationDrawable) {
+            mAnim = (AnimationDrawable)drawable;
+            if (mAttached) {
+                mAnim.start();
+            }
+        } else {
+            mAnim = null;
+        }
+    }
+
+    @Override
+    public void setImageDrawable(Drawable drawable) {
+        super.setImageDrawable(drawable);
+        updateAnim();
+    }
+
+    @Override
+    @android.view.RemotableViewMethod
+    public void setImageResource(int resid) {
+        super.setImageResource(resid);
+        updateAnim();
+    }
+
+    @Override
+    public void onAttachedToWindow() {
+        super.onAttachedToWindow();
+        if (mAnim != null) {
+            mAnim.start();
+        }
+        mAttached = true;
+    }
+
+    @Override
+    public void onDetachedFromWindow() {
+        super.onDetachedFromWindow();
+        if (mAnim != null) {
+            mAnim.stop();
+        }
+        mAttached = false;
+    }
+}
+
diff --git a/src/com/android/contacts/vcard/ExportProcessor.java b/src/com/android/contacts/vcard/ExportProcessor.java
index 7e7c7c8..fd3498a 100644
--- a/src/com/android/contacts/vcard/ExportProcessor.java
+++ b/src/com/android/contacts/vcard/ExportProcessor.java
@@ -224,13 +224,13 @@
 
         final RemoteViews remoteViews = new RemoteViews(mService.getPackageName(),
                 R.layout.status_bar_ongoing_event_progress_bar);
-        remoteViews.setTextViewText(R.id.description, message);
-        remoteViews.setProgressBar(R.id.progress_bar, total, current, (total == -1));
+        remoteViews.setTextViewText(R.id.status_description, message);
+        remoteViews.setProgressBar(R.id.status_progress_bar, total, current, (total == -1));
 
         final String percentage = mService.getString(R.string.percentage,
                 String.valueOf((current * 100)/total));
-        remoteViews.setTextViewText(R.id.progress_text, percentage);
-        remoteViews.setImageViewResource(R.id.appIcon, android.R.drawable.stat_sys_upload);
+        remoteViews.setTextViewText(R.id.status_progress_text, percentage);
+        remoteViews.setImageViewResource(R.id.status_icon, android.R.drawable.stat_sys_upload);
 
         final Notification notification = new Notification();
         notification.icon = android.R.drawable.stat_sys_upload;
diff --git a/src/com/android/contacts/vcard/ImportProgressNotifier.java b/src/com/android/contacts/vcard/ImportProgressNotifier.java
index eec75f6..afd6c3a 100644
--- a/src/com/android/contacts/vcard/ImportProgressNotifier.java
+++ b/src/com/android/contacts/vcard/ImportProgressNotifier.java
@@ -20,8 +20,6 @@
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
-import android.util.Log;
-import android.widget.RemoteViews;
 
 import com.android.contacts.ContactsListActivity;
 import com.android.contacts.R;
@@ -58,11 +56,6 @@
         // - There's high probability where name comes soon after the beginning of entry, so
         //   we don't need to hurry to show something.
 
-        // TODO: should not create this every time?
-        final RemoteViews remoteViews =
-                new RemoteViews(mContext.getPackageName(),
-                R.layout.status_bar_ongoing_event_progress_bar);
-
         final String title = mContext.getString(R.string.reading_vcard_title);
 
         String totalCountString;
@@ -74,29 +67,47 @@
                 totalCountString,
                 contactStruct.getDisplayName());
 
-        remoteViews.setTextViewText(R.id.title, title);
-        remoteViews.setTextViewText(R.id.description, description);
-        remoteViews.setProgressBar(R.id.progress_bar, mTotalCount, mCurrentCount,
+        final Context context = mContext.getApplicationContext();
+
+        /* TODO: fix this.
+        final RemoteViews remoteViews =
+                new RemoteViews(context.getPackageName(),
+                R.layout.status_bar_ongoing_event_progress_bar);
+        remoteViews.setTextViewText(R.id.status_title, title);
+        remoteViews.setTextViewText(R.id.status_description, description);
+        remoteViews.setProgressBar(R.id.status_progress_bar, mTotalCount, mCurrentCount,
                 mTotalCount == -1);
         final String percentage;
         if (mTotalCount > 0) {
-            percentage = mContext.getString(R.string.percentage,
+            percentage = context.getString(R.string.percentage,
                     String.valueOf(mCurrentCount * 100/mTotalCount));
         } else {
             percentage = "";
         }
-
-        remoteViews.setTextViewText(R.id.progress_text, percentage);
-        remoteViews.setImageViewResource(R.id.appIcon, android.R.drawable.stat_sys_download);
+        remoteViews.setTextViewText(R.id.status_progress_text, percentage);
+        remoteViews.setImageViewResource(R.id.status_icon, android.R.drawable.stat_sys_download);
 
         final Notification notification = new Notification();
         notification.icon = android.R.drawable.stat_sys_download;
+        notification.tickerText = description;
         notification.flags |= Notification.FLAG_ONGOING_EVENT;
         notification.tickerText = description;
-        notification.contentView = remoteViews;
-        notification.contentIntent =
-                PendingIntent.getActivity(mContext, 0,
-                        new Intent(mContext, ContactsListActivity.class), 0);
+        notification.contentView = remoteViews;*/
+
+        final long when = System.currentTimeMillis();
+        final Notification notification = new Notification(
+                android.R.drawable.stat_sys_download,
+                description,
+                when);
+
+        final PendingIntent pendingIntent =
+            PendingIntent.getActivity(context, 0,
+                    new Intent(context, ContactsListActivity.class),
+                    PendingIntent.FLAG_UPDATE_CURRENT);
+
+        // notification.contentIntent = pendingIntent;
+        notification.setLatestEventInfo(context, title, description, pendingIntent);
+
         mNotificationManager.notify(VCardService.IMPORT_NOTIFICATION_ID, notification);
     }