Skip to content

Commit b9669dd

Browse files
author
Yaniv Inbar
committed
[api 1.12] add GooglePlayServicesAvailabilityIOException
https://codereview.appspot.com/6770043/
1 parent 9d901a8 commit b9669dd

3 files changed

Lines changed: 83 additions & 3 deletions

File tree

google-api-client-android/src/main/java/com/google/api/client/googleapis/extensions/android/gms/auth/GoogleAccountCredential.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import com.google.android.gms.auth.GoogleAuthException;
1616
import com.google.android.gms.auth.GoogleAuthUtil;
17+
import com.google.android.gms.auth.GooglePlayServicesAvailabilityException;
1718
import com.google.android.gms.auth.UserRecoverableAuthException;
1819
import com.google.android.gms.common.AccountPicker;
1920
import com.google.api.client.googleapis.extensions.android.accounts.GoogleAccountManager;
@@ -38,6 +39,8 @@
3839
* <p>
3940
* When fetching a token, any thrown {@link GoogleAuthException} would be wrapped:
4041
* <ul>
42+
* <li>{@link GooglePlayServicesAvailabilityException} would be wrapped inside of
43+
* {@link GooglePlayServicesAvailabilityIOException}</li>
4144
* <li>{@link UserRecoverableAuthException} would be wrapped inside of
4245
* {@link UserRecoverableAuthIOException}</li>
4346
* <li>{@link GoogleAuthException} when be wrapped inside of {@link GoogleAuthIOException}</li>
@@ -213,6 +216,8 @@ public void intercept(HttpRequest request) throws IOException {
213216
try {
214217
token = getToken();
215218
request.getHeaders().setAuthorization("Bearer " + token);
219+
} catch (GooglePlayServicesAvailabilityException e) {
220+
throw new GooglePlayServicesAvailabilityIOException(e);
216221
} catch (UserRecoverableAuthException e) {
217222
throw new UserRecoverableAuthIOException(e);
218223
} catch (GoogleAuthException e) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
3+
* in compliance with the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License
8+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
9+
* or implied. See the License for the specific language governing permissions and limitations under
10+
* the License.
11+
*/
12+
13+
package com.google.api.client.googleapis.extensions.android.gms.auth;
14+
15+
import com.google.android.gms.auth.GooglePlayServicesAvailabilityException;
16+
import com.google.android.gms.common.GooglePlayServicesUtil;
17+
18+
import android.app.Activity;
19+
20+
import java.io.IOException;
21+
22+
/**
23+
* Wraps a {@link GooglePlayServicesAvailabilityException} into an {@link IOException} so it can be
24+
* caught directly.
25+
*
26+
* <p>
27+
* Use {@link #getConnectionStatusCode()} to display the error dialog. Alternatively, use
28+
* {@link #getCause()} to get the wrapped {@link GooglePlayServicesAvailabilityException}. Example
29+
* usage:
30+
* </p>
31+
*
32+
* <pre>
33+
} catch (final GooglePlayServicesAvailabilityIOException availabilityException) {
34+
myActivity.runOnUiThread(new Runnable() {
35+
public void run() {
36+
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
37+
availabilityException.getConnectionStatusCode(),
38+
myActivity,
39+
MyActivity.AUTH_REQUEST_CODE);
40+
dialog.show();
41+
}
42+
});
43+
* </pre>
44+
*
45+
* @since 1.12
46+
* @author Yaniv Inbar
47+
*/
48+
public class GooglePlayServicesAvailabilityIOException extends UserRecoverableAuthIOException {
49+
50+
private static final long serialVersionUID = 1L;
51+
52+
GooglePlayServicesAvailabilityIOException(GooglePlayServicesAvailabilityException wrapped) {
53+
super(wrapped);
54+
}
55+
56+
@Override
57+
public GooglePlayServicesAvailabilityException getCause() {
58+
return (GooglePlayServicesAvailabilityException) super.getCause();
59+
}
60+
61+
/**
62+
* Returns the error code to use with
63+
* {@link GooglePlayServicesUtil#getErrorDialog(int, Activity, int)}.
64+
*/
65+
public final int getConnectionStatusCode() {
66+
return getCause().getConnectionStatusCode();
67+
}
68+
}

google-api-client-android/src/main/java/com/google/api/client/googleapis/extensions/android/gms/auth/UserRecoverableAuthIOException.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@
2424
* directly.
2525
*
2626
* <p>
27-
* Use {@link #getCause()} to get the wrapped {@link UserRecoverableAuthException}. Use
28-
* {@link #getIntent()} to allow user interaction to recover.
27+
* Use {@link #getIntent()} to allow user interaction to recover. Alternatively, use
28+
* {@link #getCause()} to get the wrapped {@link UserRecoverableAuthException}. Example usage:
2929
* </p>
3030
*
31+
* <pre>
32+
} catch (UserRecoverableAuthIOException userRecoverableException) {
33+
myActivity.startActivityForResult(
34+
userRecoverableException.getIntent(), MyActivity.AUTH_REQUEST_CODE);
35+
}
36+
* </pre>
37+
*
3138
* @since 1.12
3239
* @author Yaniv Inbar
3340
*/
@@ -48,7 +55,7 @@ public UserRecoverableAuthException getCause() {
4855
* Returns the {@link Intent} that when supplied to
4956
* {@link Activity#startActivityForResult(Intent, int)} will allow user intervention.
5057
*/
51-
public Intent getIntent() {
58+
public final Intent getIntent() {
5259
return getCause().getIntent();
5360
}
5461
}

0 commit comments

Comments
 (0)