michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0:
michael@0: package org.mozilla.gecko.fxa.sync;
michael@0:
michael@0: import org.mozilla.gecko.BrowserLocaleManager;
michael@0: import org.mozilla.gecko.R;
michael@0: import org.mozilla.gecko.background.common.log.Logger;
michael@0: import org.mozilla.gecko.fxa.FxAccountConstants;
michael@0: import org.mozilla.gecko.fxa.activities.FxAccountStatusActivity;
michael@0: import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
michael@0: import org.mozilla.gecko.fxa.login.State;
michael@0: import org.mozilla.gecko.fxa.login.State.Action;
michael@0:
michael@0: import android.app.NotificationManager;
michael@0: import android.app.PendingIntent;
michael@0: import android.content.Context;
michael@0: import android.content.Intent;
michael@0: import android.support.v4.app.NotificationCompat;
michael@0: import android.support.v4.app.NotificationCompat.Builder;
michael@0:
michael@0: /**
michael@0: * Abstraction that manages notifications shown or hidden for a Firefox Account.
michael@0: *
michael@0: * In future, we anticipate this tracking things like:
michael@0: *
michael@0: * - new engines to offer to Sync;
michael@0: * - service interruption updates;
michael@0: * - messages from other clients.
michael@0: *
michael@0: */
michael@0: public class FxAccountNotificationManager {
michael@0: private static final String LOG_TAG = FxAccountNotificationManager.class.getSimpleName();
michael@0:
michael@0: protected final int notificationId;
michael@0:
michael@0: // We're lazy about updating our locale info, because most syncs don't notify.
michael@0: private volatile boolean localeUpdated;
michael@0:
michael@0: public FxAccountNotificationManager(int notificationId) {
michael@0: this.notificationId = notificationId;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Reflect new Firefox Account state to the notification manager: show or hide
michael@0: * notifications reflecting the state of a Firefox Account.
michael@0: *
michael@0: * @param context
michael@0: * Android context.
michael@0: * @param fxAccount
michael@0: * Firefox Account to reflect to the notification manager.
michael@0: */
michael@0: protected void update(Context context, AndroidFxAccount fxAccount) {
michael@0: final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
michael@0:
michael@0: final State state = fxAccount.getState();
michael@0: final Action action = state.getNeededAction();
michael@0: if (action == Action.None) {
michael@0: Logger.info(LOG_TAG, "State " + state.getStateLabel() + " needs no action; cancelling any existing notification.");
michael@0: notificationManager.cancel(notificationId);
michael@0: return;
michael@0: }
michael@0:
michael@0: if (!localeUpdated) {
michael@0: localeUpdated = true;
michael@0: BrowserLocaleManager.getInstance().getAndApplyPersistedLocale(context);
michael@0: }
michael@0:
michael@0: final String title = context.getResources().getString(R.string.fxaccount_sync_sign_in_error_notification_title);
michael@0: final String text = context.getResources().getString(R.string.fxaccount_sync_sign_in_error_notification_text, state.email);
michael@0: Logger.info(LOG_TAG, "State " + state.getStateLabel() + " needs action; offering notification with title: " + title);
michael@0: FxAccountConstants.pii(LOG_TAG, "And text: " + text);
michael@0:
michael@0: final Intent notificationIntent = new Intent(context, FxAccountStatusActivity.class);
michael@0: final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
michael@0:
michael@0: final Builder builder = new NotificationCompat.Builder(context);
michael@0: builder
michael@0: .setContentTitle(title)
michael@0: .setContentText(text)
michael@0: .setSmallIcon(R.drawable.ic_status_logo)
michael@0: .setAutoCancel(true)
michael@0: .setContentIntent(pendingIntent);
michael@0: notificationManager.notify(notificationId, builder.build());
michael@0: }
michael@0: }