mobile/android/base/AlertNotification.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/AlertNotification.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,125 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko;
    1.10 +
    1.11 +import org.mozilla.gecko.gfx.BitmapUtils;
    1.12 +
    1.13 +import android.app.Notification;
    1.14 +import android.app.NotificationManager;
    1.15 +import android.content.Context;
    1.16 +import android.graphics.Bitmap;
    1.17 +import android.net.Uri;
    1.18 +import android.util.Log;
    1.19 +import android.widget.RemoteViews;
    1.20 +
    1.21 +import java.text.NumberFormat;
    1.22 +
    1.23 +public class AlertNotification
    1.24 +    extends Notification
    1.25 +{
    1.26 +    private static final String LOGTAG = "GeckoAlertNotification";
    1.27 +
    1.28 +    private final int mId;
    1.29 +    private final int mIcon;
    1.30 +    private final String mTitle;
    1.31 +    private final String mText;
    1.32 +    private final NotificationManager mNotificationManager;
    1.33 +
    1.34 +    private boolean mProgressStyle;
    1.35 +    private double mPrevPercent  = -1;
    1.36 +    private String mPrevAlertText = "";
    1.37 +
    1.38 +    private static final double UPDATE_THRESHOLD = .01;
    1.39 +    private Context mContext;
    1.40 +
    1.41 +    public AlertNotification(Context aContext, int aNotificationId, int aIcon,
    1.42 +                             String aTitle, String aText, long aWhen, Uri aIconUri) {
    1.43 +        super(aIcon, (aText.length() > 0) ? aText : aTitle, aWhen);
    1.44 +
    1.45 +        mIcon = aIcon;
    1.46 +        mTitle = aTitle;
    1.47 +        mText = aText;
    1.48 +        mId = aNotificationId;
    1.49 +        mContext = aContext;
    1.50 +
    1.51 +        mNotificationManager = (NotificationManager) aContext.getSystemService(Context.NOTIFICATION_SERVICE);
    1.52 +
    1.53 +        if (aIconUri == null || aIconUri.getScheme() == null)
    1.54 +            return;
    1.55 +
    1.56 +        // Custom view
    1.57 +        int layout = R.layout.notification_icon_text;
    1.58 +        RemoteViews view = new RemoteViews(mContext.getPackageName(), layout);
    1.59 +        try {
    1.60 +            Bitmap bm = BitmapUtils.decodeUrl(aIconUri);
    1.61 +            if (bm == null) {
    1.62 +                Log.e(LOGTAG, "failed to decode icon");
    1.63 +                return;
    1.64 +            }
    1.65 +            view.setImageViewBitmap(R.id.notification_image, bm);
    1.66 +            view.setTextViewText(R.id.notification_title, mTitle);
    1.67 +            if (mText.length() > 0) {
    1.68 +                view.setTextViewText(R.id.notification_text, mText);
    1.69 +            }
    1.70 +            contentView = view;
    1.71 +        } catch (Exception e) {
    1.72 +            Log.e(LOGTAG, "failed to create bitmap", e);
    1.73 +        }
    1.74 +    }
    1.75 +
    1.76 +    public int getId() {
    1.77 +        return mId;
    1.78 +    }
    1.79 +
    1.80 +    public synchronized boolean isProgressStyle() {
    1.81 +        return mProgressStyle;
    1.82 +    }
    1.83 +
    1.84 +    public void show() {
    1.85 +        mNotificationManager.notify(mId, this);
    1.86 +    }
    1.87 +
    1.88 +    public void cancel() {
    1.89 +        mNotificationManager.cancel(mId);
    1.90 +    }
    1.91 +
    1.92 +    public synchronized void updateProgress(String aAlertText, long aProgress, long aProgressMax) {
    1.93 +        if (!mProgressStyle) {
    1.94 +            // Custom view
    1.95 +            int layout =  aAlertText.length() > 0 ? R.layout.notification_progress_text : R.layout.notification_progress;
    1.96 +
    1.97 +            RemoteViews view = new RemoteViews(mContext.getPackageName(), layout);
    1.98 +            view.setImageViewResource(R.id.notification_image, mIcon);
    1.99 +            view.setTextViewText(R.id.notification_title, mTitle);
   1.100 +            contentView = view;
   1.101 +            flags |= FLAG_ONGOING_EVENT | FLAG_ONLY_ALERT_ONCE;
   1.102 +
   1.103 +            mProgressStyle = true;
   1.104 +        }
   1.105 +
   1.106 +        String text;
   1.107 +        double percent = 0;
   1.108 +        if (aProgressMax > 0)
   1.109 +            percent = ((double)aProgress / (double)aProgressMax);
   1.110 +
   1.111 +        if (aAlertText.length() > 0)
   1.112 +            text = aAlertText;
   1.113 +        else
   1.114 +            text = NumberFormat.getPercentInstance().format(percent); 
   1.115 +
   1.116 +        if (mPrevAlertText.equals(text) && Math.abs(mPrevPercent - percent) < UPDATE_THRESHOLD)
   1.117 +            return;
   1.118 +
   1.119 +        contentView.setTextViewText(R.id.notification_text, text);
   1.120 +        contentView.setProgressBar(R.id.notification_progressbar, (int)aProgressMax, (int)aProgress, false);
   1.121 +
   1.122 +        // Update the notification
   1.123 +        mNotificationManager.notify(mId, this);
   1.124 +
   1.125 +        mPrevPercent = percent;
   1.126 +        mPrevAlertText = text;
   1.127 +    }
   1.128 +}

mercurial