1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/DataReportingNotification.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,134 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko; 1.10 + 1.11 +import org.mozilla.gecko.preferences.GeckoPreferences; 1.12 + 1.13 +import android.app.Notification; 1.14 +import android.app.NotificationManager; 1.15 +import android.app.PendingIntent; 1.16 +import android.content.Context; 1.17 +import android.content.Intent; 1.18 +import android.content.SharedPreferences; 1.19 +import android.content.res.Resources; 1.20 +import android.graphics.Typeface; 1.21 +import android.os.Build; 1.22 +import android.support.v4.app.NotificationCompat; 1.23 +import android.text.Spannable; 1.24 +import android.text.SpannableString; 1.25 +import android.text.TextUtils; 1.26 +import android.text.style.StyleSpan; 1.27 + 1.28 +public class DataReportingNotification { 1.29 + 1.30 + private static final String LOGTAG = "DataReportNotification"; 1.31 + 1.32 + public static final String ALERT_NAME_DATAREPORTING_NOTIFICATION = "datareporting-notification"; 1.33 + 1.34 + private static final String PREFS_POLICY_NOTIFIED_TIME = "datareporting.policy.dataSubmissionPolicyNotifiedTime"; 1.35 + private static final String PREFS_POLICY_VERSION = "datareporting.policy.dataSubmissionPolicyVersion"; 1.36 + private static final int DATA_REPORTING_VERSION = 2; 1.37 + 1.38 + public static void checkAndNotifyPolicy(Context context) { 1.39 + SharedPreferences dataPrefs = GeckoSharedPrefs.forApp(context); 1.40 + final int currentVersion = dataPrefs.getInt(PREFS_POLICY_VERSION, -1); 1.41 + 1.42 + if (currentVersion < 1) { 1.43 + // This is a first run, so notify user about data policy. 1.44 + notifyDataPolicy(context, dataPrefs); 1.45 + 1.46 + // If healthreport is enabled, set default preference value. 1.47 + if (AppConstants.MOZ_SERVICES_HEALTHREPORT) { 1.48 + SharedPreferences.Editor editor = dataPrefs.edit(); 1.49 + editor.putBoolean(GeckoPreferences.PREFS_HEALTHREPORT_UPLOAD_ENABLED, true); 1.50 + editor.commit(); 1.51 + } 1.52 + return; 1.53 + } 1.54 + 1.55 + if (currentVersion == 1) { 1.56 + // Redisplay notification only for Beta because version 2 updates Beta policy and update version. 1.57 + if (TextUtils.equals("beta", AppConstants.MOZ_UPDATE_CHANNEL)) { 1.58 + notifyDataPolicy(context, dataPrefs); 1.59 + } else { 1.60 + // Silently update the version. 1.61 + SharedPreferences.Editor editor = dataPrefs.edit(); 1.62 + editor.putInt(PREFS_POLICY_VERSION, DATA_REPORTING_VERSION); 1.63 + editor.commit(); 1.64 + } 1.65 + return; 1.66 + } 1.67 + 1.68 + if (currentVersion >= DATA_REPORTING_VERSION) { 1.69 + // Do nothing, we're at a current (or future) version. 1.70 + return; 1.71 + } 1.72 + } 1.73 + 1.74 + /** 1.75 + * Launch a notification of the data policy, and record notification time and version. 1.76 + */ 1.77 + private static void notifyDataPolicy(Context context, SharedPreferences sharedPrefs) { 1.78 + boolean result = false; 1.79 + try { 1.80 + // Launch main App to launch Data choices when notification is clicked. 1.81 + Intent prefIntent = new Intent(GeckoApp.ACTION_LAUNCH_SETTINGS); 1.82 + prefIntent.setClassName(AppConstants.ANDROID_PACKAGE_NAME, AppConstants.BROWSER_INTENT_CLASS_NAME); 1.83 + 1.84 + GeckoPreferences.setResourceToOpen(prefIntent, "preferences_vendor"); 1.85 + prefIntent.putExtra(ALERT_NAME_DATAREPORTING_NOTIFICATION, true); 1.86 + 1.87 + PendingIntent contentIntent = PendingIntent.getActivity(context, 0, prefIntent, PendingIntent.FLAG_UPDATE_CURRENT); 1.88 + final Resources resources = context.getResources(); 1.89 + 1.90 + // Create and send notification. 1.91 + String notificationTitle = resources.getString(R.string.datareporting_notification_title); 1.92 + String notificationSummary; 1.93 + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { 1.94 + notificationSummary = resources.getString(R.string.datareporting_notification_action); 1.95 + } else { 1.96 + // Display partial version of Big Style notification for supporting devices. 1.97 + notificationSummary = resources.getString(R.string.datareporting_notification_summary); 1.98 + } 1.99 + String notificationAction = resources.getString(R.string.datareporting_notification_action); 1.100 + String notificationBigSummary = resources.getString(R.string.datareporting_notification_summary); 1.101 + 1.102 + // Make styled ticker text for display in notification bar. 1.103 + String tickerString = resources.getString(R.string.datareporting_notification_ticker_text); 1.104 + SpannableString tickerText = new SpannableString(tickerString); 1.105 + // Bold the notification title of the ticker text, which is the same string as notificationTitle. 1.106 + tickerText.setSpan(new StyleSpan(Typeface.BOLD), 0, notificationTitle.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); 1.107 + 1.108 + Notification notification = new NotificationCompat.Builder(context) 1.109 + .setContentTitle(notificationTitle) 1.110 + .setContentText(notificationSummary) 1.111 + .setSmallIcon(R.drawable.ic_status_logo) 1.112 + .setAutoCancel(true) 1.113 + .setContentIntent(contentIntent) 1.114 + .setStyle(new NotificationCompat.BigTextStyle() 1.115 + .bigText(notificationBigSummary)) 1.116 + .addAction(R.drawable.firefox_settings_alert, notificationAction, contentIntent) 1.117 + .setTicker(tickerText) 1.118 + .build(); 1.119 + 1.120 + NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 1.121 + int notificationID = ALERT_NAME_DATAREPORTING_NOTIFICATION.hashCode(); 1.122 + notificationManager.notify(notificationID, notification); 1.123 + 1.124 + // Record version and notification time. 1.125 + SharedPreferences.Editor editor = sharedPrefs.edit(); 1.126 + long now = System.currentTimeMillis(); 1.127 + editor.putLong(PREFS_POLICY_NOTIFIED_TIME, now); 1.128 + editor.putInt(PREFS_POLICY_VERSION, DATA_REPORTING_VERSION); 1.129 + editor.commit(); 1.130 + result = true; 1.131 + } finally { 1.132 + // We want to track any errors, so record notification outcome. 1.133 + final String notificationEvent = TelemetryContract.Event.POLICY_NOTIFICATION_SUCCESS + result; 1.134 + Telemetry.sendUIEvent(notificationEvent); 1.135 + } 1.136 + } 1.137 +}