|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko; |
|
7 |
|
8 import org.mozilla.gecko.preferences.GeckoPreferences; |
|
9 |
|
10 import android.app.Notification; |
|
11 import android.app.NotificationManager; |
|
12 import android.app.PendingIntent; |
|
13 import android.content.Context; |
|
14 import android.content.Intent; |
|
15 import android.content.SharedPreferences; |
|
16 import android.content.res.Resources; |
|
17 import android.graphics.Typeface; |
|
18 import android.os.Build; |
|
19 import android.support.v4.app.NotificationCompat; |
|
20 import android.text.Spannable; |
|
21 import android.text.SpannableString; |
|
22 import android.text.TextUtils; |
|
23 import android.text.style.StyleSpan; |
|
24 |
|
25 public class DataReportingNotification { |
|
26 |
|
27 private static final String LOGTAG = "DataReportNotification"; |
|
28 |
|
29 public static final String ALERT_NAME_DATAREPORTING_NOTIFICATION = "datareporting-notification"; |
|
30 |
|
31 private static final String PREFS_POLICY_NOTIFIED_TIME = "datareporting.policy.dataSubmissionPolicyNotifiedTime"; |
|
32 private static final String PREFS_POLICY_VERSION = "datareporting.policy.dataSubmissionPolicyVersion"; |
|
33 private static final int DATA_REPORTING_VERSION = 2; |
|
34 |
|
35 public static void checkAndNotifyPolicy(Context context) { |
|
36 SharedPreferences dataPrefs = GeckoSharedPrefs.forApp(context); |
|
37 final int currentVersion = dataPrefs.getInt(PREFS_POLICY_VERSION, -1); |
|
38 |
|
39 if (currentVersion < 1) { |
|
40 // This is a first run, so notify user about data policy. |
|
41 notifyDataPolicy(context, dataPrefs); |
|
42 |
|
43 // If healthreport is enabled, set default preference value. |
|
44 if (AppConstants.MOZ_SERVICES_HEALTHREPORT) { |
|
45 SharedPreferences.Editor editor = dataPrefs.edit(); |
|
46 editor.putBoolean(GeckoPreferences.PREFS_HEALTHREPORT_UPLOAD_ENABLED, true); |
|
47 editor.commit(); |
|
48 } |
|
49 return; |
|
50 } |
|
51 |
|
52 if (currentVersion == 1) { |
|
53 // Redisplay notification only for Beta because version 2 updates Beta policy and update version. |
|
54 if (TextUtils.equals("beta", AppConstants.MOZ_UPDATE_CHANNEL)) { |
|
55 notifyDataPolicy(context, dataPrefs); |
|
56 } else { |
|
57 // Silently update the version. |
|
58 SharedPreferences.Editor editor = dataPrefs.edit(); |
|
59 editor.putInt(PREFS_POLICY_VERSION, DATA_REPORTING_VERSION); |
|
60 editor.commit(); |
|
61 } |
|
62 return; |
|
63 } |
|
64 |
|
65 if (currentVersion >= DATA_REPORTING_VERSION) { |
|
66 // Do nothing, we're at a current (or future) version. |
|
67 return; |
|
68 } |
|
69 } |
|
70 |
|
71 /** |
|
72 * Launch a notification of the data policy, and record notification time and version. |
|
73 */ |
|
74 private static void notifyDataPolicy(Context context, SharedPreferences sharedPrefs) { |
|
75 boolean result = false; |
|
76 try { |
|
77 // Launch main App to launch Data choices when notification is clicked. |
|
78 Intent prefIntent = new Intent(GeckoApp.ACTION_LAUNCH_SETTINGS); |
|
79 prefIntent.setClassName(AppConstants.ANDROID_PACKAGE_NAME, AppConstants.BROWSER_INTENT_CLASS_NAME); |
|
80 |
|
81 GeckoPreferences.setResourceToOpen(prefIntent, "preferences_vendor"); |
|
82 prefIntent.putExtra(ALERT_NAME_DATAREPORTING_NOTIFICATION, true); |
|
83 |
|
84 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, prefIntent, PendingIntent.FLAG_UPDATE_CURRENT); |
|
85 final Resources resources = context.getResources(); |
|
86 |
|
87 // Create and send notification. |
|
88 String notificationTitle = resources.getString(R.string.datareporting_notification_title); |
|
89 String notificationSummary; |
|
90 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { |
|
91 notificationSummary = resources.getString(R.string.datareporting_notification_action); |
|
92 } else { |
|
93 // Display partial version of Big Style notification for supporting devices. |
|
94 notificationSummary = resources.getString(R.string.datareporting_notification_summary); |
|
95 } |
|
96 String notificationAction = resources.getString(R.string.datareporting_notification_action); |
|
97 String notificationBigSummary = resources.getString(R.string.datareporting_notification_summary); |
|
98 |
|
99 // Make styled ticker text for display in notification bar. |
|
100 String tickerString = resources.getString(R.string.datareporting_notification_ticker_text); |
|
101 SpannableString tickerText = new SpannableString(tickerString); |
|
102 // Bold the notification title of the ticker text, which is the same string as notificationTitle. |
|
103 tickerText.setSpan(new StyleSpan(Typeface.BOLD), 0, notificationTitle.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); |
|
104 |
|
105 Notification notification = new NotificationCompat.Builder(context) |
|
106 .setContentTitle(notificationTitle) |
|
107 .setContentText(notificationSummary) |
|
108 .setSmallIcon(R.drawable.ic_status_logo) |
|
109 .setAutoCancel(true) |
|
110 .setContentIntent(contentIntent) |
|
111 .setStyle(new NotificationCompat.BigTextStyle() |
|
112 .bigText(notificationBigSummary)) |
|
113 .addAction(R.drawable.firefox_settings_alert, notificationAction, contentIntent) |
|
114 .setTicker(tickerText) |
|
115 .build(); |
|
116 |
|
117 NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
|
118 int notificationID = ALERT_NAME_DATAREPORTING_NOTIFICATION.hashCode(); |
|
119 notificationManager.notify(notificationID, notification); |
|
120 |
|
121 // Record version and notification time. |
|
122 SharedPreferences.Editor editor = sharedPrefs.edit(); |
|
123 long now = System.currentTimeMillis(); |
|
124 editor.putLong(PREFS_POLICY_NOTIFIED_TIME, now); |
|
125 editor.putInt(PREFS_POLICY_VERSION, DATA_REPORTING_VERSION); |
|
126 editor.commit(); |
|
127 result = true; |
|
128 } finally { |
|
129 // We want to track any errors, so record notification outcome. |
|
130 final String notificationEvent = TelemetryContract.Event.POLICY_NOTIFICATION_SUCCESS + result; |
|
131 Telemetry.sendUIEvent(notificationEvent); |
|
132 } |
|
133 } |
|
134 } |