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