Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3106)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PhysicalWebBroadcastService.java

Issue 2741103002: Create Broadcast Notification (Closed)
Patch Set: Stylizing Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/android/java/strings/android_chrome_strings.grd » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PhysicalWebBroadcastService.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PhysicalWebBroadcastService.java b/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PhysicalWebBroadcastService.java
index dfaac76650cefb2508cc95d78ff86c2f6bacf072..d18e18f141f948abd9d86ee90db7668874a25719 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PhysicalWebBroadcastService.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PhysicalWebBroadcastService.java
@@ -5,13 +5,25 @@
package org.chromium.chrome.browser.physicalweb;
import android.annotation.TargetApi;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
import android.app.Service;
+import android.content.BroadcastReceiver;
+import android.content.Context;
import android.content.Intent;
+import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.IBinder;
import org.chromium.base.ContextUtils;
+import org.chromium.base.Log;
+import org.chromium.chrome.R;
+import org.chromium.chrome.browser.AppHooks;
+import org.chromium.chrome.browser.notifications.ChromeNotificationBuilder;
+import org.chromium.chrome.browser.notifications.NotificationConstants;
+import org.chromium.chrome.browser.notifications.NotificationManagerProxy;
+import org.chromium.chrome.browser.notifications.NotificationManagerProxyImpl;
/**
* Broadcasts Physical Web URLs via Bluetooth Low Energy (BLE).
@@ -26,8 +38,8 @@ import org.chromium.base.ContextUtils;
* service and will be the only way for the user to stop the service.
*
* If the user terminates Chrome or the OS shuts down this service mid-broadcasting, when the
- * service get's restarted it will restart broadcasting as well by gathering the URL from
- * shared preferences. This will create a seemless experience to the user by behaving as
+ * service gets restarted it will restart broadcasting as well by gathering the URL from
+ * shared preferences. This will create a seamless experience to the user by behaving as
* though broadcasting is not connected to the Chrome application.
*
* bluetooth.le.AdvertiseCallback and bluetooth.BluetoothAdapter require API level 21.
@@ -39,25 +51,52 @@ public class PhysicalWebBroadcastService extends Service {
public static final String DISPLAY_URL_KEY = "display_url";
public static final String PREVIOUS_BROADCAST_URL_KEY = "previousUrl";
+ private static final String STOP_SERVICE =
+ "org.chromium.chrome.browser.physicalweb.stop_service";
+ private static final String TAG = "PhysicalWebSharing";
private boolean mStartedByRestart;
+ private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ String action = intent.getAction();
+
+ if (STOP_SERVICE.equals(action)) {
+ stopSelf();
+ } else {
+ Log.e(TAG, "Unrecognized Broadcast Received");
+ }
+ }
+ };
+
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String displayUrl = fetchDisplayUrl(intent);
// This should never happen.
if (displayUrl == null) {
+ Log.e(TAG, "Given null display URL");
stopSelf();
return START_STICKY;
}
- // TODO(iankc): implement parsing, broadcasting, and notifications.
- stopSelf();
+ IntentFilter filter = new IntentFilter(STOP_SERVICE);
+ registerReceiver(mBroadcastReceiver, filter);
+
+ // TODO(iankc): implement parsing, broadcasting, Url Shortener.
+ createBroadcastNotification(displayUrl);
return START_STICKY;
}
@Override
+ public void onDestroy() {
+ unregisterReceiver(mBroadcastReceiver);
+ disableUrlBroadcasting();
+ super.onDestroy();
+ }
+
+ @Override
public IBinder onBind(Intent intent) {
return null;
}
@@ -73,7 +112,42 @@ public class PhysicalWebBroadcastService extends Service {
ContextUtils.getAppSharedPreferences()
.edit()
.putString(PREVIOUS_BROADCAST_URL_KEY, displayUrl)
- .commit();
+ .apply();
return displayUrl;
}
+
+ /**
+ * Surfaces a notification to the user that the Physical Web is broadcasting.
+ * The notification specifies the URL that is being broadcast. It cannot be swiped away,
+ * but broadcasting can be terminated with the stop button on the notification.
+ */
+ private void createBroadcastNotification(String displayUrl) {
+ Context context = getApplicationContext();
+ PendingIntent stopPendingIntent = PendingIntent.getBroadcast(
+ context, 0, new Intent(STOP_SERVICE), PendingIntent.FLAG_UPDATE_CURRENT);
+ NotificationManagerProxy notificationManager = new NotificationManagerProxyImpl(
+ (NotificationManager) getSystemService(NOTIFICATION_SERVICE));
+ ChromeNotificationBuilder notificationBuilder =
+ AppHooks.get()
+ .createChromeNotificationBuilder(true /* preferCompat */,
+ NotificationConstants.CATEGORY_ID_BROWSER,
+ context.getString(R.string.notification_category_browser),
+ NotificationConstants.CATEGORY_GROUP_ID_GENERAL,
+ context.getString(R.string.notification_category_group_general))
+ .setSmallIcon(R.drawable.ic_image_white_24dp)
+ .setContentTitle(getString(R.string.physical_web_broadcast_notification))
+ .setContentText(displayUrl)
+ .setOngoing(true)
+ .addAction(android.R.drawable.ic_menu_close_clear_cancel,
+ getString(R.string.physical_web_stop_broadcast), stopPendingIntent);
+ notificationManager.notify(
+ NotificationConstants.NOTIFICATION_ID_PHYSICAL_WEB, notificationBuilder.build());
+ }
+
+ /** Turns off URL broadcasting. */
+ private void disableUrlBroadcasting() {
+ NotificationManagerProxy notificationManager = new NotificationManagerProxyImpl(
+ (NotificationManager) getSystemService(NOTIFICATION_SERVICE));
+ notificationManager.cancel(NotificationConstants.NOTIFICATION_ID_PHYSICAL_WEB);
+ }
}
« no previous file with comments | « no previous file | chrome/android/java/strings/android_chrome_strings.grd » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698