| Index: chrome/android/java/src/org/chromium/chrome/browser/physicalweb/NearbyForegroundSubscription.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/NearbyForegroundSubscription.java b/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/NearbyForegroundSubscription.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..d32a4b557b91dec8989974ce417bbc9c8388fb17
|
| --- /dev/null
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/NearbyForegroundSubscription.java
|
| @@ -0,0 +1,58 @@
|
| +// Copyright 2016 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.content.Context;
|
| +
|
| +import com.google.android.gms.nearby.Nearby;
|
| +import com.google.android.gms.nearby.messages.MessageListener;
|
| +
|
| +
|
| +/**
|
| + * This class represents a connection to Google Play Services that does foreground
|
| + * subscription/unsubscription to Nearby Eddystone-URLs.
|
| + * To use this class, one should:
|
| + * 1. connect,
|
| + * 2. subscribe,
|
| + * 3. unsubscribe,
|
| + * 4. repeat steps 2-3 as desired, and
|
| + * 5. disconnect.
|
| + */
|
| +class NearbyForegroundSubscription extends NearbySubscription {
|
| + private static final String TAG = "PhysicalWeb";
|
| + private final MessageListener mMessageListener;
|
| + private boolean mShouldSubscribe;
|
| +
|
| + NearbyForegroundSubscription(Context context) {
|
| + super(context);
|
| + mMessageListener = PhysicalWebBleClient.getInstance().createForegroundMessageListener();
|
| + mShouldSubscribe = false;
|
| + }
|
| +
|
| + @Override
|
| + protected void onConnected() {
|
| + if (mShouldSubscribe) {
|
| + subscribe();
|
| + }
|
| + }
|
| +
|
| + void subscribe() {
|
| + if (!getGoogleApiClient().isConnected()) {
|
| + mShouldSubscribe = true;
|
| + return;
|
| + }
|
| + Nearby.Messages.subscribe(getGoogleApiClient(), mMessageListener, createSubscribeOptions())
|
| + .setResultCallback(new SimpleResultCallback("foreground subscribe"));
|
| + }
|
| +
|
| + void unsubscribe() {
|
| + if (!getGoogleApiClient().isConnected()) {
|
| + mShouldSubscribe = false;
|
| + return;
|
| + }
|
| + Nearby.Messages.unsubscribe(getGoogleApiClient(), mMessageListener)
|
| + .setResultCallback(new SimpleResultCallback("foreground unsubscribe"));
|
| + }
|
| +}
|
|
|