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 valida
ted |
| 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 service. |
| 27 * |
| 28 * If the user terminates Chrome or the OS shuts down this service mid-broadcast
ing, when the |
| 29 * service get's restarted it will restart broadcasting as well by gathering the
URL from |
| 30 * shared preferences. This will create a seemless experience to the user by beh
aving as |
| 31 * though 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; |
| 44 |
| 45 @Override |
| 46 public int onStartCommand(Intent intent, int flags, int startId) { |
| 47 String displayUrl = fetchDisplayUrl(intent); |
| 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 fetchDisplayUrl(Intent intent) { |
| 66 mStartedByRestart = intent == null; |
| 67 if (mStartedByRestart) { |
| 68 SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences
(); |
| 69 return sharedPrefs.getString(PREVIOUS_BROADCAST_URL_KEY, null); |
| 70 } |
| 71 |
| 72 String displayUrl = intent.getStringExtra(DISPLAY_URL_KEY); |
| 73 ContextUtils.getAppSharedPreferences() |
| 74 .edit() |
| 75 .putString(PREVIOUS_BROADCAST_URL_KEY, displayUrl) |
| 76 .commit(); |
| 77 return displayUrl; |
| 78 } |
| 79 } |
OLD | NEW |