use of com.facebook.FacebookException in project openkit-android by OpenKit.
the class ImageDownloader method download.
private static void download(RequestKey key, Context context) {
HttpURLConnection connection = null;
InputStream stream = null;
Exception error = null;
Bitmap bitmap = null;
boolean issueResponse = true;
try {
URL url = new URL(key.uri.toString());
connection = (HttpURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(false);
switch(connection.getResponseCode()) {
case HttpURLConnection.HTTP_MOVED_PERM:
case HttpURLConnection.HTTP_MOVED_TEMP:
// redirect. So we need to perform further requests
issueResponse = false;
String redirectLocation = connection.getHeaderField("location");
if (!Utility.isNullOrEmpty(redirectLocation)) {
URI redirectUri = new URI(redirectLocation);
UrlRedirectCache.cacheUriRedirect(context, key.uri, redirectUri);
// Once the old downloader context is removed, we are thread-safe since this is the
// only reference to it
DownloaderContext downloaderContext = removePendingRequest(key);
if (downloaderContext != null && !downloaderContext.isCancelled) {
enqueueCacheRead(downloaderContext.request, new RequestKey(redirectUri, key.tag), false);
}
}
break;
case HttpURLConnection.HTTP_OK:
// image should be available
stream = ImageResponseCache.interceptAndCacheImageStream(context, connection);
bitmap = BitmapFactory.decodeStream(stream);
break;
default:
stream = connection.getErrorStream();
InputStreamReader reader = new InputStreamReader(stream);
char[] buffer = new char[128];
int bufferLength;
StringBuilder errorMessageBuilder = new StringBuilder();
while ((bufferLength = reader.read(buffer, 0, buffer.length)) > 0) {
errorMessageBuilder.append(buffer, 0, bufferLength);
}
Utility.closeQuietly(reader);
error = new FacebookException(errorMessageBuilder.toString());
break;
}
} catch (IOException e) {
error = e;
} catch (URISyntaxException e) {
error = e;
} finally {
Utility.closeQuietly(stream);
Utility.disconnectQuietly(connection);
}
if (issueResponse) {
issueResponse(key, error, bitmap, false);
}
}
use of com.facebook.FacebookException in project phonegap-facebook-plugin by Wizcorp.
the class AttributionIdentifiers method getAndroidId.
private static AttributionIdentifiers getAndroidId(Context context) {
AttributionIdentifiers identifiers = new AttributionIdentifiers();
try {
// freeze, if this is the case throw:
if (Looper.myLooper() == Looper.getMainLooper()) {
throw new FacebookException("getAndroidId cannot be called on the main thread.");
}
Method isGooglePlayServicesAvailable = Utility.getMethodQuietly("com.google.android.gms.common.GooglePlayServicesUtil", "isGooglePlayServicesAvailable", Context.class);
if (isGooglePlayServicesAvailable == null) {
return identifiers;
}
Object connectionResult = Utility.invokeMethodQuietly(null, isGooglePlayServicesAvailable, context);
if (!(connectionResult instanceof Integer) || (Integer) connectionResult != CONNECTION_RESULT_SUCCESS) {
return identifiers;
}
Method getAdvertisingIdInfo = Utility.getMethodQuietly("com.google.android.gms.ads.identifier.AdvertisingIdClient", "getAdvertisingIdInfo", Context.class);
if (getAdvertisingIdInfo == null) {
return identifiers;
}
Object advertisingInfo = Utility.invokeMethodQuietly(null, getAdvertisingIdInfo, context);
if (advertisingInfo == null) {
return identifiers;
}
Method getId = Utility.getMethodQuietly(advertisingInfo.getClass(), "getId");
Method isLimitAdTrackingEnabled = Utility.getMethodQuietly(advertisingInfo.getClass(), "isLimitAdTrackingEnabled");
if (getId == null || isLimitAdTrackingEnabled == null) {
return identifiers;
}
identifiers.androidAdvertiserId = (String) Utility.invokeMethodQuietly(advertisingInfo, getId);
identifiers.limitTracking = (Boolean) Utility.invokeMethodQuietly(advertisingInfo, isLimitAdTrackingEnabled);
} catch (Exception e) {
Utility.logd("android_id", e);
}
return identifiers;
}
use of com.facebook.FacebookException in project phonegap-facebook-plugin by Wizcorp.
the class FacebookWebFallbackDialog method presentWebFallback.
public static boolean presentWebFallback(final Context context, String dialogUrl, String applicationId, final FacebookDialog.PendingCall appCall, final FacebookDialog.Callback callback) {
if (Utility.isNullOrEmpty(dialogUrl)) {
return false;
}
String redirectUrl = String.format("fb%s://bridge/", applicationId);
// Show the webdialog.
FacebookWebFallbackDialog fallbackWebDialog = new FacebookWebFallbackDialog(context, dialogUrl, redirectUrl);
fallbackWebDialog.setOnCompleteListener(new WebDialog.OnCompleteListener() {
@Override
public void onComplete(Bundle values, FacebookException error) {
Intent dummyIntent = new Intent();
dummyIntent.putExtras(values == null ? new Bundle() : values);
FacebookDialog.handleActivityResult(context, appCall, appCall.getRequestCode(), dummyIntent, callback);
}
});
fallbackWebDialog.show();
return true;
}
use of com.facebook.FacebookException in project phonegap-facebook-plugin by Wizcorp.
the class ImageDownloader method download.
private static void download(RequestKey key, Context context) {
HttpURLConnection connection = null;
InputStream stream = null;
Exception error = null;
Bitmap bitmap = null;
boolean issueResponse = true;
try {
URL url = new URL(key.uri.toString());
connection = (HttpURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(false);
switch(connection.getResponseCode()) {
case HttpURLConnection.HTTP_MOVED_PERM:
case HttpURLConnection.HTTP_MOVED_TEMP:
// redirect. So we need to perform further requests
issueResponse = false;
String redirectLocation = connection.getHeaderField("location");
if (!Utility.isNullOrEmpty(redirectLocation)) {
URI redirectUri = new URI(redirectLocation);
UrlRedirectCache.cacheUriRedirect(context, key.uri, redirectUri);
// Once the old downloader context is removed, we are thread-safe since this is the
// only reference to it
DownloaderContext downloaderContext = removePendingRequest(key);
if (downloaderContext != null && !downloaderContext.isCancelled) {
enqueueCacheRead(downloaderContext.request, new RequestKey(redirectUri, key.tag), false);
}
}
break;
case HttpURLConnection.HTTP_OK:
// image should be available
stream = ImageResponseCache.interceptAndCacheImageStream(context, connection);
bitmap = BitmapFactory.decodeStream(stream);
break;
default:
stream = connection.getErrorStream();
StringBuilder errorMessageBuilder = new StringBuilder();
if (stream != null) {
InputStreamReader reader = new InputStreamReader(stream);
char[] buffer = new char[128];
int bufferLength;
while ((bufferLength = reader.read(buffer, 0, buffer.length)) > 0) {
errorMessageBuilder.append(buffer, 0, bufferLength);
}
Utility.closeQuietly(reader);
} else {
errorMessageBuilder.append(context.getString(R.string.com_facebook_image_download_unknown_error));
}
error = new FacebookException(errorMessageBuilder.toString());
break;
}
} catch (IOException e) {
error = e;
} catch (URISyntaxException e) {
error = e;
} finally {
Utility.closeQuietly(stream);
Utility.disconnectQuietly(connection);
}
if (issueResponse) {
issueResponse(key, error, bitmap, false);
}
}
use of com.facebook.FacebookException in project phonegap-facebook-plugin by Wizcorp.
the class Utility method getStringPropertyAsJSON.
// Returns either a JSONObject or JSONArray representation of the 'key' property of 'jsonObject'.
public static Object getStringPropertyAsJSON(JSONObject jsonObject, String key, String nonJSONPropertyKey) throws JSONException {
Object value = jsonObject.opt(key);
if (value != null && value instanceof String) {
JSONTokener tokener = new JSONTokener((String) value);
value = tokener.nextValue();
}
if (value != null && !(value instanceof JSONObject || value instanceof JSONArray)) {
if (nonJSONPropertyKey != null) {
// Facebook sometimes gives us back a non-JSON value such as
// literal "true" or "false" as a result.
// If we got something like that, we present it to the caller as
// a GraphObject with a single
// property. We only do this if the caller wants that behavior.
jsonObject = new JSONObject();
jsonObject.putOpt(nonJSONPropertyKey, value);
return jsonObject;
} else {
throw new FacebookException("Got an unexpected non-JSON object.");
}
}
return value;
}
Aggregations