Chromium Code Reviews| 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..be5d80e3bd1df6a57d4a6547db2c9661fca62bf2 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/NearbyBackgroundSubscription.java |
| @@ -0,0 +1,86 @@ |
| +// 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 foreground |
| + * subscription/unsubscription to Nearby Eddystone-URLs. |
| + */ |
| +class NearbyBackgroundSubscription extends NearbySubscription { |
| + private static final String TAG = "PhysicalWeb"; |
| + private static final int UNSUBSCRIBING = 0; |
| + private static final int SUBSCRIBING = 1; |
| + private int mMode; |
| + private Runnable mCallback; |
| + |
| + NearbyBackgroundSubscription() { |
| + super(ContextUtils.getApplicationContext()); |
| + mMode = SUBSCRIBING; |
| + mCallback = 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 action = null; |
| + if (mMode == SUBSCRIBING) { |
| + pendingResult = Nearby.Messages.unsubscribe( |
| + getGoogleApiClient(), createNearbySubscribeIntent()); |
| + action = "background subscribe"; |
| + } else { |
| + pendingResult = Nearby.Messages.subscribe( |
| + getGoogleApiClient(), createNearbySubscribeIntent(), createSubscribeOptions()); |
|
gone
2016/07/27 17:38:47
indentation is off. Also I'm guessing it's intent
cco3
2016/07/27 18:54:59
No, good catch! I think I know why my testing did
|
| + action = "background unsubscribe"; |
| + } |
| + pendingResult.setResultCallback(new SimpleResultCallback(action) { |
| + @Override |
| + public void onResult(final Status status) { |
| + super.onResult(status); |
| + disconnect(); |
| + if (mCallback != null) { |
| + mCallback.run(); |
| + } |
| + } |
| + }); |
| + } |
| + |
| + void subscribe(Runnable callback) { |
|
gone
2016/07/27 17:38:47
This isn't something you need a queue for, is it?
cco3
2016/07/27 18:54:59
I think it's OK (see reason below if you are curio
|
| + mMode = SUBSCRIBING; |
| + mCallback = callback; |
| + connect(); |
| + } |
| + |
| + void subscribe() { |
| + subscribe(null); |
| + } |
| + |
| + void unsubscribe(Runnable callback) { |
| + mMode = UNSUBSCRIBING; |
| + mCallback = callback; |
| + connect(); |
| + } |
| + |
| + void unsubscribe() { |
| + unsubscribe(null); |
| + } |
| +} |