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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/fxa/sync/FxAccountNotificationManager.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,87 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.fxa.sync;
     1.9 +
    1.10 +import org.mozilla.gecko.BrowserLocaleManager;
    1.11 +import org.mozilla.gecko.R;
    1.12 +import org.mozilla.gecko.background.common.log.Logger;
    1.13 +import org.mozilla.gecko.fxa.FxAccountConstants;
    1.14 +import org.mozilla.gecko.fxa.activities.FxAccountStatusActivity;
    1.15 +import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
    1.16 +import org.mozilla.gecko.fxa.login.State;
    1.17 +import org.mozilla.gecko.fxa.login.State.Action;
    1.18 +
    1.19 +import android.app.NotificationManager;
    1.20 +import android.app.PendingIntent;
    1.21 +import android.content.Context;
    1.22 +import android.content.Intent;
    1.23 +import android.support.v4.app.NotificationCompat;
    1.24 +import android.support.v4.app.NotificationCompat.Builder;
    1.25 +
    1.26 +/**
    1.27 + * Abstraction that manages notifications shown or hidden for a Firefox Account.
    1.28 + * <p>
    1.29 + * In future, we anticipate this tracking things like:
    1.30 + * <ul>
    1.31 + * <li>new engines to offer to Sync;</li>
    1.32 + * <li>service interruption updates;</li>
    1.33 + * <li>messages from other clients.</li>
    1.34 + * </ul>
    1.35 + */
    1.36 +public class FxAccountNotificationManager {
    1.37 +  private static final String LOG_TAG = FxAccountNotificationManager.class.getSimpleName();
    1.38 +
    1.39 +  protected final int notificationId;
    1.40 +
    1.41 +  // We're lazy about updating our locale info, because most syncs don't notify.
    1.42 +  private volatile boolean localeUpdated;
    1.43 +
    1.44 +  public FxAccountNotificationManager(int notificationId) {
    1.45 +    this.notificationId = notificationId;
    1.46 +  }
    1.47 +
    1.48 +  /**
    1.49 +   * Reflect new Firefox Account state to the notification manager: show or hide
    1.50 +   * notifications reflecting the state of a Firefox Account.
    1.51 +   *
    1.52 +   * @param context
    1.53 +   *          Android context.
    1.54 +   * @param fxAccount
    1.55 +   *          Firefox Account to reflect to the notification manager.
    1.56 +   */
    1.57 +  protected void update(Context context, AndroidFxAccount fxAccount) {
    1.58 +    final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    1.59 +
    1.60 +    final State state = fxAccount.getState();
    1.61 +    final Action action = state.getNeededAction();
    1.62 +    if (action == Action.None) {
    1.63 +      Logger.info(LOG_TAG, "State " + state.getStateLabel() + " needs no action; cancelling any existing notification.");
    1.64 +      notificationManager.cancel(notificationId);
    1.65 +      return;
    1.66 +    }
    1.67 +
    1.68 +    if (!localeUpdated) {
    1.69 +      localeUpdated = true;
    1.70 +      BrowserLocaleManager.getInstance().getAndApplyPersistedLocale(context);
    1.71 +    }
    1.72 +
    1.73 +    final String title = context.getResources().getString(R.string.fxaccount_sync_sign_in_error_notification_title);
    1.74 +    final String text = context.getResources().getString(R.string.fxaccount_sync_sign_in_error_notification_text, state.email);
    1.75 +    Logger.info(LOG_TAG, "State " + state.getStateLabel() + " needs action; offering notification with title: " + title);
    1.76 +    FxAccountConstants.pii(LOG_TAG, "And text: " + text);
    1.77 +
    1.78 +    final Intent notificationIntent = new Intent(context, FxAccountStatusActivity.class);
    1.79 +    final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    1.80 +
    1.81 +    final Builder builder = new NotificationCompat.Builder(context);
    1.82 +    builder
    1.83 +    .setContentTitle(title)
    1.84 +    .setContentText(text)
    1.85 +    .setSmallIcon(R.drawable.ic_status_logo)
    1.86 +    .setAutoCancel(true)
    1.87 +    .setContentIntent(pendingIntent);
    1.88 +    notificationManager.notify(notificationId, builder.build());
    1.89 +  }
    1.90 +}

mercurial