2013-05-10 21:29:09 -07:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
2013-11-11 10:41:16 -08:00
|
|
|
import org.mozilla.gecko.preferences.GeckoPreferences;
|
2013-05-10 21:29:09 -07:00
|
|
|
|
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2014-04-09 19:06:00 -07:00
|
|
|
import android.content.res.Resources;
|
2013-05-13 21:31:04 -07:00
|
|
|
import android.graphics.Typeface;
|
2013-05-10 21:29:09 -07:00
|
|
|
import android.os.Build;
|
|
|
|
import android.support.v4.app.NotificationCompat;
|
2013-05-13 21:31:04 -07:00
|
|
|
import android.text.Spannable;
|
|
|
|
import android.text.SpannableString;
|
2014-04-09 19:06:00 -07:00
|
|
|
import android.text.TextUtils;
|
2013-05-13 21:31:04 -07:00
|
|
|
import android.text.style.StyleSpan;
|
2013-05-10 21:29:09 -07:00
|
|
|
|
|
|
|
public class DataReportingNotification {
|
|
|
|
|
|
|
|
private static final String LOGTAG = "DataReportNotification";
|
|
|
|
|
|
|
|
public static final String ALERT_NAME_DATAREPORTING_NOTIFICATION = "datareporting-notification";
|
|
|
|
|
|
|
|
private static final String PREFS_POLICY_NOTIFIED_TIME = "datareporting.policy.dataSubmissionPolicyNotifiedTime";
|
|
|
|
private static final String PREFS_POLICY_VERSION = "datareporting.policy.dataSubmissionPolicyVersion";
|
2014-04-09 19:06:00 -07:00
|
|
|
private static final int DATA_REPORTING_VERSION = 2;
|
2013-05-10 21:29:09 -07:00
|
|
|
|
|
|
|
public static void checkAndNotifyPolicy(Context context) {
|
2014-04-01 07:02:14 -07:00
|
|
|
SharedPreferences dataPrefs = GeckoSharedPrefs.forApp(context);
|
2014-04-09 19:06:00 -07:00
|
|
|
final int currentVersion = dataPrefs.getInt(PREFS_POLICY_VERSION, -1);
|
2013-05-10 21:29:09 -07:00
|
|
|
|
2014-04-09 19:06:00 -07:00
|
|
|
if (currentVersion < 1) {
|
|
|
|
// This is a first run, so notify user about data policy.
|
|
|
|
notifyDataPolicy(context, dataPrefs);
|
2013-05-10 21:29:09 -07:00
|
|
|
|
|
|
|
// If healthreport is enabled, set default preference value.
|
|
|
|
if (AppConstants.MOZ_SERVICES_HEALTHREPORT) {
|
2014-04-09 19:06:00 -07:00
|
|
|
SharedPreferences.Editor editor = dataPrefs.edit();
|
2013-05-10 21:29:09 -07:00
|
|
|
editor.putBoolean(GeckoPreferences.PREFS_HEALTHREPORT_UPLOAD_ENABLED, true);
|
2014-04-09 19:06:00 -07:00
|
|
|
editor.commit();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentVersion == 1) {
|
|
|
|
// Redisplay notification only for Beta because version 2 updates Beta policy and update version.
|
|
|
|
if (TextUtils.equals("beta", AppConstants.MOZ_UPDATE_CHANNEL)) {
|
|
|
|
notifyDataPolicy(context, dataPrefs);
|
|
|
|
} else {
|
|
|
|
// Silently update the version.
|
|
|
|
SharedPreferences.Editor editor = dataPrefs.edit();
|
|
|
|
editor.putInt(PREFS_POLICY_VERSION, DATA_REPORTING_VERSION);
|
|
|
|
editor.commit();
|
2013-05-10 21:29:09 -07:00
|
|
|
}
|
2014-04-09 19:06:00 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentVersion >= DATA_REPORTING_VERSION) {
|
|
|
|
// Do nothing, we're at a current (or future) version.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-05-10 21:29:09 -07:00
|
|
|
|
2014-04-09 19:06:00 -07:00
|
|
|
/**
|
|
|
|
* Launch a notification of the data policy, and record notification time and version.
|
|
|
|
*/
|
|
|
|
private static void notifyDataPolicy(Context context, SharedPreferences sharedPrefs) {
|
2014-04-09 19:06:01 -07:00
|
|
|
boolean result = false;
|
|
|
|
try {
|
|
|
|
// Launch main App to launch Data choices when notification is clicked.
|
|
|
|
Intent prefIntent = new Intent(GeckoApp.ACTION_LAUNCH_SETTINGS);
|
|
|
|
prefIntent.setClassName(AppConstants.ANDROID_PACKAGE_NAME, AppConstants.BROWSER_INTENT_CLASS_NAME);
|
|
|
|
|
|
|
|
GeckoPreferences.setResourceToOpen(prefIntent, "preferences_vendor");
|
|
|
|
prefIntent.putExtra(ALERT_NAME_DATAREPORTING_NOTIFICATION, true);
|
|
|
|
|
|
|
|
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, prefIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
final Resources resources = context.getResources();
|
|
|
|
|
|
|
|
// Create and send notification.
|
|
|
|
String notificationTitle = resources.getString(R.string.datareporting_notification_title);
|
|
|
|
String notificationSummary;
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
|
|
|
|
notificationSummary = resources.getString(R.string.datareporting_notification_action);
|
|
|
|
} else {
|
|
|
|
// Display partial version of Big Style notification for supporting devices.
|
|
|
|
notificationSummary = resources.getString(R.string.datareporting_notification_summary);
|
|
|
|
}
|
|
|
|
String notificationAction = resources.getString(R.string.datareporting_notification_action);
|
|
|
|
String notificationBigSummary = resources.getString(R.string.datareporting_notification_summary);
|
|
|
|
|
|
|
|
// Make styled ticker text for display in notification bar.
|
|
|
|
String tickerString = resources.getString(R.string.datareporting_notification_ticker_text);
|
|
|
|
SpannableString tickerText = new SpannableString(tickerString);
|
|
|
|
// Bold the notification title of the ticker text, which is the same string as notificationTitle.
|
|
|
|
tickerText.setSpan(new StyleSpan(Typeface.BOLD), 0, notificationTitle.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
|
|
|
|
|
|
|
|
Notification notification = new NotificationCompat.Builder(context)
|
|
|
|
.setContentTitle(notificationTitle)
|
|
|
|
.setContentText(notificationSummary)
|
|
|
|
.setSmallIcon(R.drawable.ic_status_logo)
|
|
|
|
.setAutoCancel(true)
|
|
|
|
.setContentIntent(contentIntent)
|
|
|
|
.setStyle(new NotificationCompat.BigTextStyle()
|
|
|
|
.bigText(notificationBigSummary))
|
|
|
|
.addAction(R.drawable.firefox_settings_alert, notificationAction, contentIntent)
|
|
|
|
.setTicker(tickerText)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
int notificationID = ALERT_NAME_DATAREPORTING_NOTIFICATION.hashCode();
|
|
|
|
notificationManager.notify(notificationID, notification);
|
|
|
|
|
|
|
|
// Record version and notification time.
|
|
|
|
SharedPreferences.Editor editor = sharedPrefs.edit();
|
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
editor.putLong(PREFS_POLICY_NOTIFIED_TIME, now);
|
|
|
|
editor.putInt(PREFS_POLICY_VERSION, DATA_REPORTING_VERSION);
|
|
|
|
editor.commit();
|
|
|
|
result = true;
|
|
|
|
} finally {
|
|
|
|
// We want to track any errors, so record notification outcome.
|
2014-05-30 16:53:28 -07:00
|
|
|
Telemetry.sendUIEvent(TelemetryContract.Event.POLICY_NOTIFICATION_SUCCESS, result);
|
2013-05-10 21:29:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|