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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..48ee78f7ca4baaf1e5964e48967f0c72c826ae5b |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PhysicalWebBroadcastService.java |
| @@ -0,0 +1,82 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.physicalweb; |
| + |
| +import android.annotation.TargetApi; |
| +import android.app.Service; |
| +import android.content.Intent; |
| +import android.content.SharedPreferences; |
| +import android.os.Build; |
| +import android.os.IBinder; |
| + |
| +import org.chromium.base.ContextUtils; |
| + |
| +/** |
| + * Broadcasts Physical Web URLs via Bluetooth Low Energy (BLE). |
| + * |
| + * This background service will start when the user activates the Physical Web Sharing item in the |
| + * sharing chooser intent. If the URL received from the triggering Tab is a validated |
|
nyquist
2017/03/07 06:58:33
Remove " a "
iankc
2017/03/07 18:03:08
Done.
|
| + * by the Physical Web Service then it will encode the URL into an Eddystone URL and |
| + * broadcast the URL through BLE. |
| + * |
| + * To notify and provide the user with the ability to terminate broadcasting |
| + * a persistent notification with a cancel button will be created. This cancel button will stop the |
| + * service and will be the only way for the user to stop the serive. |
| + * |
| + * If the user terminates Chrome or the OS shutsdown this service mid-broadcasting, when the service |
|
nyquist
2017/03/07 06:58:33
"shuts down"
iankc
2017/03/07 18:03:08
Done.
|
| + * 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 though |
| + * broadcasting is not connected to the Chrome application. |
| + * |
| + * bluetooth.le.AdvertiseCallback and bluetooth.BluetoothAdapter require API level 21. |
| + * This will only be run on M and above. |
| + **/ |
| + |
| +@TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| +public class PhysicalWebBroadcastService extends Service { |
| + public static final String DISPLAY_URL_KEY = "display_url"; |
| + |
| + public static final String PREVIOUS_BROADCAST_URL_KEY = "previousUrl"; |
| + |
| + private boolean mStartedByRestart; |
|
nyquist
2017/03/07 06:58:33
Is the plan to use this for something later? Other
iankc
2017/03/07 18:03:08
Yeah, there are minor changes to the procedure whe
|
| + |
| + @Override |
| + public int onStartCommand(Intent intent, int flags, int startId) { |
| + String displayUrl = intent.getStringExtra(DISPLAY_URL_KEY); |
|
nyquist
2017/03/07 06:58:33
I think you want to call fetchBroadcastData() here
iankc
2017/03/07 18:03:08
Done.
|
| + |
| + // This should never happen. |
| + if (displayUrl == null) { |
| + stopSelf(); |
| + return START_STICKY; |
| + } |
| + |
| + // TODO(iankc): implement parsing, broadcasting, and notifications. |
| + stopSelf(); |
| + return START_STICKY; |
| + } |
| + |
| + @Override |
| + public IBinder onBind(Intent intent) { |
| + return null; |
| + } |
| + |
| + private String fetchBroadcastData(Intent intent) { |
|
nyquist
2017/03/07 06:58:33
For now, this just retrieves the display URL, so m
iankc
2017/03/07 18:03:08
Done.
|
| + String displayUrl; |
| + |
| + mStartedByRestart = intent == null; |
|
nyquist
2017/03/07 06:58:33
I like that you split this out to a variable to si
iankc
2017/03/07 18:03:08
Done.
|
| + if (mStartedByRestart) { |
| + SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences(); |
| + displayUrl = sharedPrefs.getString(PREVIOUS_BROADCAST_URL_KEY, null); |
|
nyquist
2017/03/07 06:58:33
You can probably just return this directly here, e
iankc
2017/03/07 18:03:08
Done.
|
| + return displayUrl; |
| + } |
| + |
| + displayUrl = intent.getStringExtra(DISPLAY_URL_KEY); |
|
nyquist
2017/03/07 06:58:33
I'd just declare displayUrl here.
iankc
2017/03/07 18:03:08
Done.
|
| + ContextUtils.getAppSharedPreferences() |
| + .edit() |
| + .putString(PREVIOUS_BROADCAST_URL_KEY, displayUrl) |
| + .commit(); |
| + return displayUrl; |
| + } |
| +} |