Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.physicalweb; | |
| 6 | |
| 7 import android.annotation.TargetApi; | |
| 8 import android.app.Service; | |
| 9 import android.content.Intent; | |
| 10 import android.content.SharedPreferences; | |
| 11 import android.os.Build; | |
| 12 import android.os.IBinder; | |
| 13 | |
| 14 import org.chromium.base.ContextUtils; | |
| 15 | |
| 16 /** | |
| 17 * Broadcasts Physical Web URLs via Bluetooth Low Energy (BLE). | |
| 18 * | |
| 19 * This background service will start when the user activates the Physical Web S haring item in the | |
| 20 * sharing chooser intent. If the URL received from the triggering Tab is a vali dated | |
|
nyquist
2017/03/07 06:58:33
Remove " a "
iankc
2017/03/07 18:03:08
Done.
| |
| 21 * by the Physical Web Service then it will encode the URL into an Eddystone URL and | |
| 22 * broadcast the URL through BLE. | |
| 23 * | |
| 24 * To notify and provide the user with the ability to terminate broadcasting | |
| 25 * a persistent notification with a cancel button will be created. This cancel b utton will stop the | |
| 26 * service and will be the only way for the user to stop the serive. | |
| 27 * | |
| 28 * If the user terminates Chrome or the OS shutsdown this service mid-broadcasti ng, when the service | |
|
nyquist
2017/03/07 06:58:33
"shuts down"
iankc
2017/03/07 18:03:08
Done.
| |
| 29 * get's restarted it will restart broadcasting as well by gathering the URL fro m shared | |
| 30 * preferences. This will create a seemless experience to the user by behaving a s though | |
| 31 * broadcasting is not connected to the Chrome application. | |
| 32 * | |
| 33 * bluetooth.le.AdvertiseCallback and bluetooth.BluetoothAdapter require API lev el 21. | |
| 34 * This will only be run on M and above. | |
| 35 **/ | |
| 36 | |
| 37 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
| 38 public class PhysicalWebBroadcastService extends Service { | |
| 39 public static final String DISPLAY_URL_KEY = "display_url"; | |
| 40 | |
| 41 public static final String PREVIOUS_BROADCAST_URL_KEY = "previousUrl"; | |
| 42 | |
| 43 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
| |
| 44 | |
| 45 @Override | |
| 46 public int onStartCommand(Intent intent, int flags, int startId) { | |
| 47 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.
| |
| 48 | |
| 49 // This should never happen. | |
| 50 if (displayUrl == null) { | |
| 51 stopSelf(); | |
| 52 return START_STICKY; | |
| 53 } | |
| 54 | |
| 55 // TODO(iankc): implement parsing, broadcasting, and notifications. | |
| 56 stopSelf(); | |
| 57 return START_STICKY; | |
| 58 } | |
| 59 | |
| 60 @Override | |
| 61 public IBinder onBind(Intent intent) { | |
| 62 return null; | |
| 63 } | |
| 64 | |
| 65 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.
| |
| 66 String displayUrl; | |
| 67 | |
| 68 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.
| |
| 69 if (mStartedByRestart) { | |
| 70 SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences (); | |
| 71 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.
| |
| 72 return displayUrl; | |
| 73 } | |
| 74 | |
| 75 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.
| |
| 76 ContextUtils.getAppSharedPreferences() | |
| 77 .edit() | |
| 78 .putString(PREVIOUS_BROADCAST_URL_KEY, displayUrl) | |
| 79 .commit(); | |
| 80 return displayUrl; | |
| 81 } | |
| 82 } | |
| OLD | NEW |