Search in sources :

Example 61 with FacebookException

use of com.facebook.FacebookException in project facebook-android-sdk by facebook.

the class AppLinkData method createFromJson.

private static AppLinkData createFromJson(String jsonString) {
    if (jsonString == null) {
        return null;
    }
    try {
        // Any missing or malformed data will result in a JSONException
        JSONObject appLinkArgsJson = new JSONObject(jsonString);
        String version = appLinkArgsJson.getString(APPLINK_VERSION_KEY);
        JSONObject bridgeArgs = appLinkArgsJson.getJSONObject(APPLINK_BRIDGE_ARGS_KEY);
        String method = bridgeArgs.getString(BRIDGE_ARGS_METHOD_KEY);
        if (method.equals("applink") && version.equals("2")) {
            // We have a new deep link
            AppLinkData appLinkData = new AppLinkData();
            appLinkData.arguments = appLinkArgsJson.getJSONObject(APPLINK_METHOD_ARGS_KEY);
            // first look for the "ref" key in the top level args
            if (appLinkData.arguments.has(METHOD_ARGS_REF_KEY)) {
                appLinkData.ref = appLinkData.arguments.getString(METHOD_ARGS_REF_KEY);
            } else if (appLinkData.arguments.has(ARGUMENTS_REFERER_DATA_KEY)) {
                // if it's not in the top level args, it could be in the "referer_data" blob
                JSONObject refererData = appLinkData.arguments.getJSONObject(ARGUMENTS_REFERER_DATA_KEY);
                if (refererData.has(REFERER_DATA_REF_KEY)) {
                    appLinkData.ref = refererData.getString(REFERER_DATA_REF_KEY);
                }
            }
            if (appLinkData.arguments.has(METHOD_ARGS_TARGET_URL_KEY)) {
                appLinkData.targetUri = Uri.parse(appLinkData.arguments.getString(METHOD_ARGS_TARGET_URL_KEY));
            }
            if (appLinkData.arguments.has(ARGUMENTS_EXTRAS_KEY)) {
                JSONObject extrasData = appLinkData.arguments.getJSONObject(ARGUMENTS_EXTRAS_KEY);
                if (extrasData.has(EXTRAS_DEEPLINK_CONTEXT_KEY)) {
                    JSONObject deeplink_context = extrasData.getJSONObject(EXTRAS_DEEPLINK_CONTEXT_KEY);
                    if (deeplink_context.has(PROMOTION_CODE_KEY)) {
                        appLinkData.promotionCode = deeplink_context.getString(PROMOTION_CODE_KEY);
                    }
                }
            }
            appLinkData.argumentBundle = toBundle(appLinkData.arguments);
            return appLinkData;
        }
    } catch (JSONException e) {
        Log.d(TAG, "Unable to parse AppLink JSON", e);
    } catch (FacebookException e) {
        Log.d(TAG, "Unable to parse AppLink JSON", e);
    }
    return null;
}
Also used : JSONObject(org.json.JSONObject) FacebookException(com.facebook.FacebookException) JSONException(org.json.JSONException)

Example 62 with FacebookException

use of com.facebook.FacebookException in project facebook-android-sdk by facebook.

the class AppLinkData method toBundle.

private static Bundle toBundle(JSONObject node) throws JSONException {
    Bundle bundle = new Bundle();
    @SuppressWarnings("unchecked") Iterator<String> fields = node.keys();
    while (fields.hasNext()) {
        String key = fields.next();
        Object value;
        value = node.get(key);
        if (value instanceof JSONObject) {
            bundle.putBundle(key, toBundle((JSONObject) value));
        } else if (value instanceof JSONArray) {
            JSONArray valueArr = (JSONArray) value;
            if (valueArr.length() == 0) {
                bundle.putStringArray(key, new String[0]);
            } else {
                Object firstNode = valueArr.get(0);
                if (firstNode instanceof JSONObject) {
                    Bundle[] bundles = new Bundle[valueArr.length()];
                    for (int i = 0; i < valueArr.length(); i++) {
                        bundles[i] = toBundle(valueArr.getJSONObject(i));
                    }
                    bundle.putParcelableArray(key, bundles);
                } else if (firstNode instanceof JSONArray) {
                    // we don't support nested arrays
                    throw new FacebookException("Nested arrays are not supported.");
                } else {
                    // just use the string value
                    String[] arrValues = new String[valueArr.length()];
                    for (int i = 0; i < valueArr.length(); i++) {
                        arrValues[i] = valueArr.get(i).toString();
                    }
                    bundle.putStringArray(key, arrValues);
                }
            }
        } else {
            bundle.putString(key, value.toString());
        }
    }
    return bundle;
}
Also used : JSONObject(org.json.JSONObject) Bundle(android.os.Bundle) FacebookException(com.facebook.FacebookException) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject)

Example 63 with FacebookException

use of com.facebook.FacebookException in project facebook-android-sdk by facebook.

the class AppEvent method getJSONObjectForAppEvent.

private static JSONObject getJSONObjectForAppEvent(String contextName, String eventName, Double valueToSum, Bundle parameters, boolean isImplicitlyLogged, @Nullable final UUID currentSessionId) throws FacebookException, JSONException {
    validateIdentifier(eventName);
    JSONObject eventObject = new JSONObject();
    eventObject.put(Constants.EVENT_NAME_EVENT_KEY, eventName);
    eventObject.put(Constants.LOG_TIME_APP_EVENT_KEY, System.currentTimeMillis() / 1000);
    eventObject.put("_ui", contextName);
    if (currentSessionId != null) {
        eventObject.put("_session_id", currentSessionId);
    }
    if (valueToSum != null) {
        eventObject.put("_valueToSum", valueToSum.doubleValue());
    }
    if (isImplicitlyLogged) {
        eventObject.put("_implicitlyLogged", "1");
    }
    String externalAnalyticsUserId = AppEventsLogger.getUserID();
    if (externalAnalyticsUserId != null) {
        eventObject.put("_app_user_id", externalAnalyticsUserId);
    }
    if (parameters != null) {
        for (String key : parameters.keySet()) {
            validateIdentifier(key);
            Object value = parameters.get(key);
            if (!(value instanceof String) && !(value instanceof Number)) {
                throw new FacebookException(String.format("Parameter value '%s' for key '%s' should be a string" + " or a numeric type.", value, key));
            }
            eventObject.put(key, value.toString());
        }
    }
    if (!isImplicitlyLogged) {
        Logger.log(LoggingBehavior.APP_EVENTS, "AppEvents", "Created app event '%s'", eventObject.toString());
    }
    return eventObject;
}
Also used : JSONObject(org.json.JSONObject) FacebookException(com.facebook.FacebookException) JSONObject(org.json.JSONObject)

Example 64 with FacebookException

use of com.facebook.FacebookException in project facebook-android-sdk by facebook.

the class DialogPresenter method setupAppCallForWebFallbackDialog.

public static void setupAppCallForWebFallbackDialog(AppCall appCall, Bundle parameters, DialogFeature feature) {
    Validate.hasFacebookActivity(FacebookSdk.getApplicationContext());
    Validate.hasInternetPermissions(FacebookSdk.getApplicationContext());
    String featureName = feature.name();
    Uri fallbackUrl = getDialogWebFallbackUri(feature);
    if (fallbackUrl == null) {
        throw new FacebookException("Unable to fetch the Url for the DialogFeature : '" + featureName + "'");
    }
    // Since we're talking to the server here, let's use the latest version we know about.
    // We know we are going to be communicating over a bucketed protocol.
    int protocolVersion = NativeProtocol.getLatestKnownVersion();
    Bundle webParams = ServerProtocol.getQueryParamsForPlatformActivityIntentWebFallback(appCall.getCallId().toString(), protocolVersion, parameters);
    if (webParams == null) {
        throw new FacebookException("Unable to fetch the app's key-hash");
    }
    // Now form the Uri
    if (fallbackUrl.isRelative()) {
        fallbackUrl = Utility.buildUri(ServerProtocol.getDialogAuthority(), fallbackUrl.toString(), webParams);
    } else {
        fallbackUrl = Utility.buildUri(fallbackUrl.getAuthority(), fallbackUrl.getPath(), webParams);
    }
    Bundle intentParameters = new Bundle();
    intentParameters.putString(NativeProtocol.WEB_DIALOG_URL, fallbackUrl.toString());
    intentParameters.putBoolean(NativeProtocol.WEB_DIALOG_IS_FALLBACK, true);
    Intent webDialogIntent = new Intent();
    NativeProtocol.setupProtocolRequestIntent(webDialogIntent, appCall.getCallId().toString(), feature.getAction(), NativeProtocol.getLatestKnownVersion(), intentParameters);
    webDialogIntent.setClass(FacebookSdk.getApplicationContext(), FacebookActivity.class);
    webDialogIntent.setAction(FacebookDialogFragment.TAG);
    appCall.setRequestIntent(webDialogIntent);
}
Also used : FacebookException(com.facebook.FacebookException) Bundle(android.os.Bundle) Intent(android.content.Intent) Uri(android.net.Uri)

Example 65 with FacebookException

use of com.facebook.FacebookException in project facebook-android-sdk by facebook.

the class FacebookDialogFragment method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (this.dialog == null) {
        final FragmentActivity activity = getActivity();
        Intent intent = activity.getIntent();
        Bundle params = NativeProtocol.getMethodArgumentsFromIntent(intent);
        boolean isWebFallback = params.getBoolean(NativeProtocol.WEB_DIALOG_IS_FALLBACK, false);
        WebDialog webDialog;
        if (!isWebFallback) {
            String actionName = params.getString(NativeProtocol.WEB_DIALOG_ACTION);
            Bundle webParams = params.getBundle(NativeProtocol.WEB_DIALOG_PARAMS);
            if (Utility.isNullOrEmpty(actionName)) {
                Utility.logd(TAG, "Cannot start a WebDialog with an empty/missing 'actionName'");
                activity.finish();
                return;
            }
            webDialog = new WebDialog.Builder(activity, actionName, webParams).setOnCompleteListener(new WebDialog.OnCompleteListener() {

                @Override
                public void onComplete(Bundle values, FacebookException error) {
                    onCompleteWebDialog(values, error);
                }
            }).build();
        } else {
            String url = params.getString(NativeProtocol.WEB_DIALOG_URL);
            if (Utility.isNullOrEmpty(url)) {
                Utility.logd(TAG, "Cannot start a fallback WebDialog with an empty/missing 'url'");
                activity.finish();
                return;
            }
            String redirectUrl = String.format("fb%s://bridge/", FacebookSdk.getApplicationId());
            webDialog = new FacebookWebFallbackDialog(activity, url, redirectUrl);
            webDialog.setOnCompleteListener(new WebDialog.OnCompleteListener() {

                @Override
                public void onComplete(Bundle values, FacebookException error) {
                    // Error data is nested in the values since this is in the form of a
                    // Native protocol response
                    onCompleteWebFallbackDialog(values);
                }
            });
        }
        this.dialog = webDialog;
    }
}
Also used : FragmentActivity(android.support.v4.app.FragmentActivity) Bundle(android.os.Bundle) FacebookException(com.facebook.FacebookException) Intent(android.content.Intent)

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