Search in sources :

Example 36 with FacebookException

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;
}
Also used : FacebookException(com.facebook.FacebookException) Method(java.lang.reflect.Method) FacebookException(com.facebook.FacebookException)

Example 37 with FacebookException

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;
}
Also used : WebDialog(com.facebook.widget.WebDialog) Bundle(android.os.Bundle) FacebookException(com.facebook.FacebookException) Intent(android.content.Intent)

Example 38 with FacebookException

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) FacebookException(com.facebook.FacebookException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URL(java.net.URL) Bitmap(android.graphics.Bitmap) HttpURLConnection(java.net.HttpURLConnection) FacebookException(com.facebook.FacebookException)

Example 39 with FacebookException

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;
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) FacebookException(com.facebook.FacebookException) JSONArray(org.json.JSONArray) GraphObject(com.facebook.model.GraphObject) JSONObject(org.json.JSONObject)

Example 40 with FacebookException

use of com.facebook.FacebookException in project Klyph by jonathangerbaud.

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) FacebookException(com.facebook.FacebookException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URL(java.net.URL) Bitmap(android.graphics.Bitmap) HttpURLConnection(java.net.HttpURLConnection) FacebookException(com.facebook.FacebookException)

Aggregations

FacebookException (com.facebook.FacebookException)70 Bundle (android.os.Bundle)24 JSONObject (org.json.JSONObject)24 JSONException (org.json.JSONException)19 Bitmap (android.graphics.Bitmap)13 GraphRequest (com.facebook.GraphRequest)10 JSONArray (org.json.JSONArray)10 Intent (android.content.Intent)9 Uri (android.net.Uri)8 GraphResponse (com.facebook.GraphResponse)8 URISyntaxException (java.net.URISyntaxException)8 AccessToken (com.facebook.AccessToken)7 FacebookOperationCanceledException (com.facebook.FacebookOperationCanceledException)5 FacebookRequestError (com.facebook.FacebookRequestError)5 LoginResult (com.facebook.login.LoginResult)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 InputStreamReader (java.io.InputStreamReader)5 HttpURLConnection (java.net.HttpURLConnection)5 URL (java.net.URL)5