mobile/android/base/fxa/sync/FxAccountNotificationManager.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.fxa.sync;
michael@0 6
michael@0 7 import org.mozilla.gecko.BrowserLocaleManager;
michael@0 8 import org.mozilla.gecko.R;
michael@0 9 import org.mozilla.gecko.background.common.log.Logger;
michael@0 10 import org.mozilla.gecko.fxa.FxAccountConstants;
michael@0 11 import org.mozilla.gecko.fxa.activities.FxAccountStatusActivity;
michael@0 12 import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
michael@0 13 import org.mozilla.gecko.fxa.login.State;
michael@0 14 import org.mozilla.gecko.fxa.login.State.Action;
michael@0 15
michael@0 16 import android.app.NotificationManager;
michael@0 17 import android.app.PendingIntent;
michael@0 18 import android.content.Context;
michael@0 19 import android.content.Intent;
michael@0 20 import android.support.v4.app.NotificationCompat;
michael@0 21 import android.support.v4.app.NotificationCompat.Builder;
michael@0 22
michael@0 23 /**
michael@0 24 * Abstraction that manages notifications shown or hidden for a Firefox Account.
michael@0 25 * <p>
michael@0 26 * In future, we anticipate this tracking things like:
michael@0 27 * <ul>
michael@0 28 * <li>new engines to offer to Sync;</li>
michael@0 29 * <li>service interruption updates;</li>
michael@0 30 * <li>messages from other clients.</li>
michael@0 31 * </ul>
michael@0 32 */
michael@0 33 public class FxAccountNotificationManager {
michael@0 34 private static final String LOG_TAG = FxAccountNotificationManager.class.getSimpleName();
michael@0 35
michael@0 36 protected final int notificationId;
michael@0 37
michael@0 38 // We're lazy about updating our locale info, because most syncs don't notify.
michael@0 39 private volatile boolean localeUpdated;
michael@0 40
michael@0 41 public FxAccountNotificationManager(int notificationId) {
michael@0 42 this.notificationId = notificationId;
michael@0 43 }
michael@0 44
michael@0 45 /**
michael@0 46 * Reflect new Firefox Account state to the notification manager: show or hide
michael@0 47 * notifications reflecting the state of a Firefox Account.
michael@0 48 *
michael@0 49 * @param context
michael@0 50 * Android context.
michael@0 51 * @param fxAccount
michael@0 52 * Firefox Account to reflect to the notification manager.
michael@0 53 */
michael@0 54 protected void update(Context context, AndroidFxAccount fxAccount) {
michael@0 55 final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
michael@0 56
michael@0 57 final State state = fxAccount.getState();
michael@0 58 final Action action = state.getNeededAction();
michael@0 59 if (action == Action.None) {
michael@0 60 Logger.info(LOG_TAG, "State " + state.getStateLabel() + " needs no action; cancelling any existing notification.");
michael@0 61 notificationManager.cancel(notificationId);
michael@0 62 return;
michael@0 63 }
michael@0 64
michael@0 65 if (!localeUpdated) {
michael@0 66 localeUpdated = true;
michael@0 67 BrowserLocaleManager.getInstance().getAndApplyPersistedLocale(context);
michael@0 68 }
michael@0 69
michael@0 70 final String title = context.getResources().getString(R.string.fxaccount_sync_sign_in_error_notification_title);
michael@0 71 final String text = context.getResources().getString(R.string.fxaccount_sync_sign_in_error_notification_text, state.email);
michael@0 72 Logger.info(LOG_TAG, "State " + state.getStateLabel() + " needs action; offering notification with title: " + title);
michael@0 73 FxAccountConstants.pii(LOG_TAG, "And text: " + text);
michael@0 74
michael@0 75 final Intent notificationIntent = new Intent(context, FxAccountStatusActivity.class);
michael@0 76 final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
michael@0 77
michael@0 78 final Builder builder = new NotificationCompat.Builder(context);
michael@0 79 builder
michael@0 80 .setContentTitle(title)
michael@0 81 .setContentText(text)
michael@0 82 .setSmallIcon(R.drawable.ic_status_logo)
michael@0 83 .setAutoCancel(true)
michael@0 84 .setContentIntent(pendingIntent);
michael@0 85 notificationManager.notify(notificationId, builder.build());
michael@0 86 }
michael@0 87 }

mercurial