Move to Notification.Builder progress API.
Jeff Sharkey [Fri, 5 Aug 2011 21:52:17 +0000 (14:52 -0700)]
Instead of using custom layout to surface progress information, use
new Builder API.  Also use resources to build percent string.

Bug: 4022082
Change-Id: I556a666771e9103ce5d7ddb60faa879b8777b284

res/layout/status_bar_ongoing_event_progress_bar.xml [deleted file]
res/values/strings.xml
src/com/android/providers/downloads/DownloadNotification.java

diff --git a/res/layout/status_bar_ongoing_event_progress_bar.xml b/res/layout/status_bar_ongoing_event_progress_bar.xml
deleted file mode 100644 (file)
index 5b19a92..0000000
+++ /dev/null
@@ -1,114 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-/*
-** Copyright 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.
-*/
--->
-
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:orientation="vertical"
-    android:background="@android:drawable/status_bar_item_app_background"
-    >
-
-    <LinearLayout
-        android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:orientation="horizontal"
-        >
-
-        <LinearLayout
-            android:layout_width="40dp"
-            android:layout_height="match_parent"
-            android:orientation="vertical"
-            android:paddingTop="8dp"
-            android:focusable="true"
-            >
-            <ImageView
-                android:id="@+id/appIcon"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:layout_gravity="center"
-                android:src="@android:drawable/sym_def_app_icon"
-                android:paddingLeft="15dp"
-                />
-            <TextView android:id="@+id/progress_text"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                style="@android:style/TextAppearance.StatusBar.EventContent"
-                android:singleLine="true"
-                android:layout_gravity="center_horizontal"
-                />
-        </LinearLayout>
-
-        <RelativeLayout
-            android:layout_width="match_parent"
-            android:layout_height="match_parent"
-            android:orientation="vertical"
-            android:focusable="true"
-            >
-            <LinearLayout
-                android:layout_width="match_parent"
-                android:layout_height="wrap_content"
-                android:orientation="horizontal"
-                android:focusable="true"
-                android:layout_alignParentTop="true"
-                android:paddingTop="10dp"
-                >
-                <TextView android:id="@+id/title"
-                    android:layout_width="wrap_content"
-                    android:layout_height="wrap_content"
-                    style="@android:style/TextAppearance.StatusBar.EventContent.Title"
-                    android:singleLine="true"
-                    android:paddingLeft="20dp"
-                    />
-                <TextView android:id="@+id/description"
-                    android:layout_width="wrap_content"
-                    android:layout_height="wrap_content"
-                    style="@android:style/TextAppearance.StatusBar.EventContent"
-                    android:singleLine="true"
-                    android:paddingLeft="20dp"
-                    />
-            </LinearLayout>
-            <!-- Only one of progress_bar and paused_text will be visible. -->
-            <ProgressBar android:id="@+id/progress_bar"
-                style="?android:attr/progressBarStyleHorizontal"
-                android:layout_width="match_parent"
-                android:layout_height="wrap_content"
-                android:layout_alignParentBottom="true"
-                android:paddingBottom="8dp"
-                android:paddingRight="25dp"
-                />
-            <TextView android:id="@+id/paused_text"
-                android:layout_width="match_parent"
-                android:layout_height="wrap_content"
-                android:layout_alignParentBottom="true"
-                android:paddingBottom="8dp"
-                android:singleLine="true"
-                style="@android:style/TextAppearance.StatusBar.EventContent"
-                android:paddingLeft="5dp"
-                />
-        </RelativeLayout>
-    </LinearLayout>
-
-    <ImageView
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:src="@android:drawable/divider_horizontal_bright"
-        />
-
-</LinearLayout>
-
index 270a54c..24a3f34 100644 (file)
          [CHAR LIMIT=25] -->
     <string name="button_start_now">Start now</string>
 
+    <!-- Short representation of download progress percentage. [CHAR LIMIT=8] -->
+    <string name="download_percent"><xliff:g id="number">%d</xliff:g><xliff:g id="percent">%%</xliff:g></string>
 
 </resources>
index df14abb..81209b2 100644 (file)
@@ -23,8 +23,7 @@ import android.content.Context;
 import android.content.Intent;
 import android.net.Uri;
 import android.provider.Downloads;
-import android.view.View;
-import android.widget.RemoteViews;
+import android.text.TextUtils;
 
 import java.util.Collection;
 import java.util.HashMap;
@@ -148,50 +147,41 @@ class DownloadNotification {
         // Add the notifications
         for (NotificationItem item : mNotifications.values()) {
             // Build the notification object
-            Notification n = new Notification();
+            final Notification.Builder builder = new Notification.Builder(mContext);
 
             boolean hasPausedText = (item.mPausedText != null);
-            int iconResource = android.R.drawable.stat_sys_download;
+            int iconResource = android.R.drawable.stat_sys_download_done;
             if (hasPausedText) {
                 iconResource = android.R.drawable.stat_sys_warning;
             }
-            n.icon = iconResource;
+            builder.setSmallIcon(iconResource);
+            builder.setOngoing(true);
 
-            n.flags |= Notification.FLAG_ONGOING_EVENT;
-
-            // Build the RemoteView object
-            RemoteViews expandedView = new RemoteViews(
-                    "com.android.providers.downloads",
-                    R.layout.status_bar_ongoing_event_progress_bar);
+            boolean hasContentText = false;
             StringBuilder title = new StringBuilder(item.mTitles[0]);
             if (item.mTitleCount > 1) {
                 title.append(mContext.getString(R.string.notification_filename_separator));
                 title.append(item.mTitles[1]);
-                n.number = item.mTitleCount;
                 if (item.mTitleCount > 2) {
                     title.append(mContext.getString(R.string.notification_filename_extras,
                             new Object[] { Integer.valueOf(item.mTitleCount - 2) }));
                 }
-            } else {
-                expandedView.setTextViewText(R.id.description,
-                        item.mDescription);
+            } else if (!TextUtils.isEmpty(item.mDescription)) {
+                builder.setContentText(item.mDescription);
+                hasContentText = true;
             }
-            expandedView.setTextViewText(R.id.title, title);
+            builder.setContentTitle(title);
 
             if (hasPausedText) {
-                expandedView.setViewVisibility(R.id.progress_bar, View.GONE);
-                expandedView.setTextViewText(R.id.paused_text, item.mPausedText);
+                builder.setContentText(item.mPausedText);
             } else {
-                expandedView.setViewVisibility(R.id.paused_text, View.GONE);
-                expandedView.setProgressBar(R.id.progress_bar,
-                        (int) item.mTotalTotal,
-                        (int) item.mTotalCurrent,
-                        item.mTotalTotal == -1);
+                builder.setProgress(
+                        (int) item.mTotalTotal, (int) item.mTotalCurrent, item.mTotalTotal == -1);
+                if (hasContentText) {
+                    builder.setContentInfo(
+                            buildPercentageLabel(mContext, item.mTotalTotal, item.mTotalCurrent));
+                }
             }
-            expandedView.setTextViewText(R.id.progress_text,
-                    getDownloadingText(item.mTotalTotal, item.mTotalCurrent));
-            expandedView.setImageViewResource(R.id.appIcon, iconResource);
-            n.contentView = expandedView;
 
             Intent intent = new Intent(Constants.ACTION_LIST);
             intent.setClassName("com.android.providers.downloads",
@@ -200,9 +190,9 @@ class DownloadNotification {
                     ContentUris.withAppendedId(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, item.mId));
             intent.putExtra("multiple", item.mTitleCount > 1);
 
-            n.contentIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
+            builder.setContentIntent(PendingIntent.getBroadcast(mContext, 0, intent, 0));
 
-            mSystemFacade.postNotification(item.mId, n);
+            mSystemFacade.postNotification(item.mId, builder.getNotification());
 
         }
     }
@@ -219,8 +209,8 @@ class DownloadNotification {
     void notificationForCompletedDownload(long id, String title, int status,
             int destination, long lastMod) {
         // Add the notifications
-        Notification n = new Notification();
-        n.icon = android.R.drawable.stat_sys_download_done;
+        Notification.Builder builder = new Notification.Builder(mContext);
+        builder.setSmallIcon(android.R.drawable.stat_sys_download_done);
         if (title == null || title.length() == 0) {
             title = mContext.getResources().getString(
                     R.string.download_unknown_title);
@@ -246,17 +236,18 @@ class DownloadNotification {
                 DownloadReceiver.class.getName());
         intent.setData(contentUri);
 
-        n.when = lastMod;
-        n.setLatestEventInfo(mContext, title, caption,
-                PendingIntent.getBroadcast(mContext, 0, intent, 0));
+        builder.setWhen(lastMod);
+        builder.setContentTitle(title);
+        builder.setContentText(caption);
+        builder.setContentIntent(PendingIntent.getBroadcast(mContext, 0, intent, 0));
 
         intent = new Intent(Constants.ACTION_HIDE);
         intent.setClassName("com.android.providers.downloads",
                 DownloadReceiver.class.getName());
         intent.setData(contentUri);
-        n.deleteIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
+        builder.setDeleteIntent(PendingIntent.getBroadcast(mContext, 0, intent, 0));
 
-        mSystemFacade.postNotification(id, n);
+        mSystemFacade.postNotification(id, builder.getNotification());
     }
 
     private boolean isActiveAndVisible(DownloadInfo download) {
@@ -269,18 +260,13 @@ class DownloadNotification {
                 && download.mVisibility == Downloads.Impl.VISIBILITY_VISIBLE_NOTIFY_COMPLETED;
     }
 
-    /*
-     * Helper function to build the downloading text.
-     */
-    private String getDownloadingText(long totalBytes, long currentBytes) {
+    private static String buildPercentageLabel(
+            Context context, long totalBytes, long currentBytes) {
         if (totalBytes <= 0) {
-            return "";
+            return null;
+        } else {
+            final int percent = (int) (100 * currentBytes / totalBytes);
+            return context.getString(R.string.download_percent, percent);
         }
-        long progress = currentBytes * 100 / totalBytes;
-        StringBuilder sb = new StringBuilder();
-        sb.append(progress);
-        sb.append('%');
-        return sb.toString();
     }
-
 }