OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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.gcore; |
| 6 |
| 7 import android.os.Bundle; |
| 8 import android.os.Handler; |
| 9 |
| 10 import com.google.android.gms.common.ConnectionResult; |
| 11 import com.google.android.gms.common.api.GoogleApiClient; |
| 12 import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; |
| 13 import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListe
ner; |
| 14 |
| 15 /** |
| 16 * Helper that allows to manage connection failures when using {@link GoogleApiC
lient}. |
| 17 * |
| 18 * When the connection fails, it will try to reconnect {@link ConnectedTask#RETR
Y_NUMBER_LIMIT} |
| 19 * times in the background, waiting {@link ConnectedTask#CONNECTION_RETRY_TIME_M
S} milliseconds |
| 20 * between each attempt. |
| 21 */ |
| 22 public class GoogleApiClientConnectionHelper |
| 23 implements OnConnectionFailedListener, ConnectionCallbacks { |
| 24 private int mResolutionAttempts = 0; |
| 25 private Handler mHandler; |
| 26 private final GoogleApiClient mClient; |
| 27 |
| 28 public GoogleApiClientConnectionHelper(GoogleApiClient client) { |
| 29 mClient = client; |
| 30 mClient.registerConnectionCallbacks(this); |
| 31 mClient.registerConnectionFailedListener(this); |
| 32 } |
| 33 |
| 34 @Override |
| 35 public void onConnectionFailed(ConnectionResult result) { |
| 36 if (mResolutionAttempts++ < ConnectedTask.RETRY_NUMBER_LIMIT) { |
| 37 if (mHandler == null) mHandler = new Handler(); |
| 38 mHandler.postDelayed(new Runnable() { |
| 39 @Override |
| 40 public void run() { |
| 41 mClient.connect(); |
| 42 } |
| 43 }, ConnectedTask.CONNECTION_RETRY_TIME_MS); |
| 44 } |
| 45 } |
| 46 |
| 47 @Override |
| 48 public void onConnected(Bundle connectionHint) { |
| 49 mResolutionAttempts = 0; |
| 50 } |
| 51 |
| 52 @Override |
| 53 public void onConnectionSuspended(int cause) {} |
| 54 } |
OLD | NEW |