| Index: chrome/android/java/src/org/chromium/chrome/browser/physicalweb/NearbyBackgroundSubscription.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/NearbyBackgroundSubscription.java b/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/NearbyBackgroundSubscription.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a14520589cee16573c2b0be2902ea8ef58a2d9e9
|
| --- /dev/null
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/NearbyBackgroundSubscription.java
|
| @@ -0,0 +1,72 @@
|
| +// 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.app.PendingIntent;
|
| +import android.content.Intent;
|
| +
|
| +import com.google.android.gms.common.api.PendingResult;
|
| +import com.google.android.gms.common.api.Status;
|
| +import com.google.android.gms.nearby.Nearby;
|
| +
|
| +import org.chromium.base.ContextUtils;
|
| +
|
| +
|
| +/**
|
| + * This class represents a connection to Google Play Services that does background
|
| + * subscription/unsubscription to Nearby Eddystone-URLs.
|
| + */
|
| +class NearbyBackgroundSubscription extends NearbySubscription {
|
| + private static final String TAG = "PhysicalWeb";
|
| + private final int mAction;
|
| + private final Runnable mCallback;
|
| +
|
| + NearbyBackgroundSubscription(int action, Runnable callback) {
|
| + super(ContextUtils.getApplicationContext());
|
| + mAction = action;
|
| + mCallback = callback;
|
| + }
|
| +
|
| + NearbyBackgroundSubscription(int action) {
|
| + this(action, null);
|
| + }
|
| +
|
| + private PendingIntent createNearbySubscribeIntent() {
|
| + Intent intent =
|
| + new Intent(ContextUtils.getApplicationContext(), NearbyMessageIntentService.class);
|
| + PendingIntent pendingIntent = PendingIntent.getService(
|
| + ContextUtils.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
| + return pendingIntent;
|
| + }
|
| +
|
| + @Override
|
| + protected void onConnected() {
|
| + PendingResult<Status> pendingResult = null;
|
| + String actionStr = null;
|
| + if (mAction == SUBSCRIBE) {
|
| + pendingResult = Nearby.Messages.subscribe(
|
| + getGoogleApiClient(), createNearbySubscribeIntent(), createSubscribeOptions());
|
| + actionStr = "background subscribe";
|
| + } else {
|
| + pendingResult = Nearby.Messages.unsubscribe(
|
| + getGoogleApiClient(), createNearbySubscribeIntent());
|
| + actionStr = "background unsubscribe";
|
| + }
|
| + pendingResult.setResultCallback(new SimpleResultCallback(actionStr) {
|
| + @Override
|
| + public void onResult(final Status status) {
|
| + super.onResult(status);
|
| + disconnect();
|
| + if (mCallback != null) {
|
| + mCallback.run();
|
| + }
|
| + }
|
| + });
|
| + }
|
| +
|
| + void run() {
|
| + connect();
|
| + }
|
| +}
|
|
|