Chromium Code Reviews| 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..76f2956318663de94cbe6310c9830cec2b8353d3 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,24 @@ |
| 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 android.support.v4.app.NotificationCompat; |
| import org.chromium.base.ContextUtils; |
| +import org.chromium.base.Log; |
| +import org.chromium.chrome.R; |
| +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). |
| @@ -39,9 +50,23 @@ 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 = "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 (action.equals(STOP_SERVICE)) { |
| + Log.d(TAG, "Stop Service"); |
|
cco3
2017/03/10 23:21:38
"Stop button pressed on Physical Web broadcast not
iankc
2017/03/10 23:54:18
Done.
|
| + stopSelf(); |
| + } |
| + } |
| + }; |
| + |
| @Override |
| public int onStartCommand(Intent intent, int flags, int startId) { |
| String displayUrl = fetchDisplayUrl(intent); |
| @@ -52,12 +77,23 @@ public class PhysicalWebBroadcastService extends Service { |
| return START_STICKY; |
| } |
| + IntentFilter filter = new IntentFilter(); |
| + filter.addAction(STOP_SERVICE); |
|
cco3
2017/03/10 23:21:38
Just pass this to the constructor.
registerReceiv
iankc
2017/03/10 23:54:18
Done.
|
| + registerReceiver(mBroadcastReceiver, filter); |
| + |
| // TODO(iankc): implement parsing, broadcasting, and notifications. |
|
mattreynolds
2017/03/09 22:59:42
is this TODONE now?
iankc
2017/03/10 23:54:18
Done.
|
| - stopSelf(); |
| + createBroadcastNotification(displayUrl); |
| return START_STICKY; |
| } |
| @Override |
| + public void onDestroy() { |
| + unregisterReceiver(mBroadcastReceiver); |
| + disableUrlBroadcasting(); |
| + super.onDestroy(); |
| + } |
| + |
| + @Override |
| public IBinder onBind(Intent intent) { |
| return null; |
| } |
| @@ -73,7 +109,36 @@ 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 |
|
cco3
2017/03/10 23:21:38
The first sentence should be on its own line. Sta
iankc
2017/03/10 23:54:18
Done.
|
| + * specifies the URL that is being broadcast and cannot be swiped away. |
|
cco3
2017/03/10 23:21:38
You might want to mention that it has a stop butto
iankc
2017/03/10 23:54:18
Done.
|
| + */ |
| + private void createBroadcastNotification(String displayUrl) { |
| + Context context = getApplicationContext(); |
| + context.registerReceiver(mBroadcastReceiver, new IntentFilter(STOP_SERVICE)); |
|
cco3
2017/03/10 23:21:38
Haven't you already registered this?
iankc
2017/03/10 23:54:18
Done.
|
| + PendingIntent stopPendingIntent = PendingIntent.getBroadcast( |
| + context, 0, new Intent(STOP_SERVICE), PendingIntent.FLAG_UPDATE_CURRENT); |
| + NotificationManager notificationManager = |
|
cco3
2017/03/10 23:21:38
You should be using NotificationManagerProxy
iankc
2017/03/10 23:54:18
Done.
|
| + (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
| + notificationManager.notify(NotificationConstants.NOTIFICATION_ID_PHYSICAL_WEB, |
| + new NotificationCompat.Builder(context) |
| + .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) |
| + .build()); |
| + } |
| + |
| + /** Turns off URL broadcasting. */ |
| + private void disableUrlBroadcasting() { |
| + NotificationManagerProxy notificationProxy = new NotificationManagerProxyImpl( |
|
cco3
2017/03/10 23:21:38
I think it'd be better to call the variable notifi
iankc
2017/03/10 23:54:18
Done.
|
| + (NotificationManager) getSystemService(NOTIFICATION_SERVICE)); |
| + notificationProxy.cancel(NotificationConstants.NOTIFICATION_ID_PHYSICAL_WEB); |
| + } |
| } |