Search in sources :

Example 1 with AppLink

use of bolts.AppLink in project phonegap-facebook-plugin by Wizcorp.

the class FacebookAppLinkResolver method getAppLinkFromUrlsInBackground.

/**
     * Asynchronously resolves App Link data for multiple Urls
     *
     * @param uris A list of Uri objects to resolve into App Links
     * @return A Task that, when successful, will return a Map of Uri->AppLink for each Uri that was successfully
     * resolved into an App Link. Uris that could not be resolved into App Links will not be present in the Map.
     * In the case of general server errors, the task will be completed with the corresponding error.
     */
public Task<Map<Uri, AppLink>> getAppLinkFromUrlsInBackground(List<Uri> uris) {
    final Map<Uri, AppLink> appLinkResults = new HashMap<Uri, AppLink>();
    final HashSet<Uri> urisToRequest = new HashSet<Uri>();
    StringBuilder graphRequestFields = new StringBuilder();
    for (Uri uri : uris) {
        AppLink appLink = null;
        synchronized (cachedAppLinks) {
            appLink = cachedAppLinks.get(uri);
        }
        if (appLink != null) {
            appLinkResults.put(uri, appLink);
        } else {
            if (!urisToRequest.isEmpty()) {
                graphRequestFields.append(',');
            }
            graphRequestFields.append(uri.toString());
            urisToRequest.add(uri);
        }
    }
    if (urisToRequest.isEmpty()) {
        return Task.forResult(appLinkResults);
    }
    final Task<Map<Uri, AppLink>>.TaskCompletionSource<Map<Uri, AppLink>> taskCompletionSource = Task.create();
    Bundle appLinkRequestParameters = new Bundle();
    appLinkRequestParameters.putString("ids", graphRequestFields.toString());
    appLinkRequestParameters.putString("fields", String.format("%s.fields(%s,%s)", APP_LINK_KEY, APP_LINK_ANDROID_TARGET_KEY, APP_LINK_WEB_TARGET_KEY));
    Request appLinkRequest = new Request(null, /* Session */
    "", /* Graph path */
    appLinkRequestParameters, /* Query parameters */
    null, /* HttpMethod */
    new Request.Callback() {

        /* Callback */
        @Override
        public void onCompleted(Response response) {
            FacebookRequestError error = response.getError();
            if (error != null) {
                taskCompletionSource.setError(error.getException());
                return;
            }
            GraphObject responseObject = response.getGraphObject();
            JSONObject responseJson = responseObject != null ? responseObject.getInnerJSONObject() : null;
            if (responseJson == null) {
                taskCompletionSource.setResult(appLinkResults);
                return;
            }
            for (Uri uri : urisToRequest) {
                String uriString = uri.toString();
                if (!responseJson.has(uriString)) {
                    continue;
                }
                JSONObject urlData = null;
                try {
                    urlData = responseJson.getJSONObject(uri.toString());
                    JSONObject appLinkData = urlData.getJSONObject(APP_LINK_KEY);
                    JSONArray rawTargets = appLinkData.getJSONArray(APP_LINK_ANDROID_TARGET_KEY);
                    int targetsCount = rawTargets.length();
                    List<AppLink.Target> targets = new ArrayList<AppLink.Target>(targetsCount);
                    for (int i = 0; i < targetsCount; i++) {
                        AppLink.Target target = getAndroidTargetFromJson(rawTargets.getJSONObject(i));
                        if (target != null) {
                            targets.add(target);
                        }
                    }
                    Uri webFallbackUrl = getWebFallbackUriFromJson(uri, appLinkData);
                    AppLink appLink = new AppLink(uri, targets, webFallbackUrl);
                    appLinkResults.put(uri, appLink);
                    synchronized (cachedAppLinks) {
                        cachedAppLinks.put(uri, appLink);
                    }
                } catch (JSONException e) {
                    // The data for this uri was missing or badly formed.
                    continue;
                }
            }
            taskCompletionSource.setResult(appLinkResults);
        }
    });
    appLinkRequest.executeAsync();
    return taskCompletionSource.getTask();
}
Also used : Task(bolts.Task) Bundle(android.os.Bundle) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) GraphObject(com.facebook.model.GraphObject) Uri(android.net.Uri) JSONObject(org.json.JSONObject) AppLink(bolts.AppLink)

Example 2 with AppLink

use of bolts.AppLink in project facebook-api-android-maven by avianey.

the class FacebookAppLinkResolver method getAppLinkFromUrlsInBackground.

/**
     * Asynchronously resolves App Link data for multiple Urls
     *
     * @param uris A list of Uri objects to resolve into App Links
     * @return A Task that, when successful, will return a Map of Uri->AppLink for each Uri that was successfully
     * resolved into an App Link. Uris that could not be resolved into App Links will not be present in the Map.
     * In the case of general server errors, the task will be completed with the corresponding error.
     */
public Task<Map<Uri, AppLink>> getAppLinkFromUrlsInBackground(List<Uri> uris) {
    final Map<Uri, AppLink> appLinkResults = new HashMap<Uri, AppLink>();
    final HashSet<Uri> urisToRequest = new HashSet<Uri>();
    StringBuilder graphRequestFields = new StringBuilder();
    for (Uri uri : uris) {
        AppLink appLink = null;
        synchronized (cachedAppLinks) {
            appLink = cachedAppLinks.get(uri);
        }
        if (appLink != null) {
            appLinkResults.put(uri, appLink);
        } else {
            if (!urisToRequest.isEmpty()) {
                graphRequestFields.append(',');
            }
            graphRequestFields.append(uri.toString());
            urisToRequest.add(uri);
        }
    }
    if (urisToRequest.isEmpty()) {
        return Task.forResult(appLinkResults);
    }
    final Task<Map<Uri, AppLink>>.TaskCompletionSource<Map<Uri, AppLink>> taskCompletionSource = Task.create();
    Bundle appLinkRequestParameters = new Bundle();
    appLinkRequestParameters.putString("ids", graphRequestFields.toString());
    appLinkRequestParameters.putString("fields", String.format("%s.fields(%s,%s)", APP_LINK_KEY, APP_LINK_ANDROID_TARGET_KEY, APP_LINK_WEB_TARGET_KEY));
    Request appLinkRequest = new Request(null, /* Session */
    "", /* Graph path */
    appLinkRequestParameters, /* Query parameters */
    null, /* HttpMethod */
    new Request.Callback() {

        /* Callback */
        @Override
        public void onCompleted(Response response) {
            FacebookRequestError error = response.getError();
            if (error != null) {
                taskCompletionSource.setError(error.getException());
                return;
            }
            GraphObject responseObject = response.getGraphObject();
            JSONObject responseJson = responseObject != null ? responseObject.getInnerJSONObject() : null;
            if (responseJson == null) {
                taskCompletionSource.setResult(appLinkResults);
                return;
            }
            for (Uri uri : urisToRequest) {
                String uriString = uri.toString();
                if (!responseJson.has(uriString)) {
                    continue;
                }
                JSONObject urlData = null;
                try {
                    urlData = responseJson.getJSONObject(uri.toString());
                    JSONObject appLinkData = urlData.getJSONObject(APP_LINK_KEY);
                    JSONArray rawTargets = appLinkData.getJSONArray(APP_LINK_ANDROID_TARGET_KEY);
                    int targetsCount = rawTargets.length();
                    List<AppLink.Target> targets = new ArrayList<AppLink.Target>(targetsCount);
                    for (int i = 0; i < targetsCount; i++) {
                        AppLink.Target target = getAndroidTargetFromJson(rawTargets.getJSONObject(i));
                        if (target != null) {
                            targets.add(target);
                        }
                    }
                    Uri webFallbackUrl = getWebFallbackUriFromJson(uri, appLinkData);
                    AppLink appLink = new AppLink(uri, targets, webFallbackUrl);
                    appLinkResults.put(uri, appLink);
                    synchronized (cachedAppLinks) {
                        cachedAppLinks.put(uri, appLink);
                    }
                } catch (JSONException e) {
                    // The data for this uri was missing or badly formed.
                    continue;
                }
            }
            taskCompletionSource.setResult(appLinkResults);
        }
    });
    appLinkRequest.executeAsync();
    return taskCompletionSource.getTask();
}
Also used : Task(bolts.Task) Bundle(android.os.Bundle) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) GraphObject(com.facebook.model.GraphObject) Uri(android.net.Uri) JSONObject(org.json.JSONObject) AppLink(bolts.AppLink)

Example 3 with AppLink

use of bolts.AppLink in project facebook-android-sdk by facebook.

the class FacebookAppLinkResolverTests method testUrlWithNoAppLinkData.

public void testUrlWithNoAppLinkData() {
    String testNoAppLinkUrlString = "https://fb.me/732873156764191_no_app_link";
    Uri testNoAppLinkUrl = Uri.parse(testNoAppLinkUrlString);
    try {
        executeResolverOnBlockerThread(new FacebookAppLinkResolver(), testNoAppLinkUrl);
        getTestBlocker().waitForSignals(1);
        assertNotNull(resolveTask);
        Task<AppLink> singleUrlResolveTask = (Task<AppLink>) resolveTask;
        assertTrue(singleUrlResolveTask.isCompleted() && !singleUrlResolveTask.isCancelled() && !singleUrlResolveTask.isFaulted());
        AppLink appLink = singleUrlResolveTask.getResult();
        assertNull(appLink);
    } catch (Exception e) {
        // Forcing the test to fail with details
        assertNull(e);
    }
}
Also used : Task(bolts.Task) FacebookAppLinkResolver(com.facebook.applinks.FacebookAppLinkResolver) AppLink(bolts.AppLink) Uri(android.net.Uri)

Example 4 with AppLink

use of bolts.AppLink in project facebook-android-sdk by facebook.

the class FacebookAppLinkResolver method getAppLinkFromUrlsInBackground.

/**
     * Asynchronously resolves App Link data for multiple URLs
     *
     * @param uris A list of Uri objects to resolve into App Links
     * @return A Task that, when successful, will return a Map of Uri->AppLink for each Uri that was
     * successfully resolved into an App Link. Uris that could not be resolved into App Links will
     * not be present in the Map. In the case of general server errors, the task will be completed
     * with the corresponding error.
     */
public Task<Map<Uri, AppLink>> getAppLinkFromUrlsInBackground(List<Uri> uris) {
    final Map<Uri, AppLink> appLinkResults = new HashMap<Uri, AppLink>();
    final HashSet<Uri> urisToRequest = new HashSet<Uri>();
    StringBuilder graphRequestFields = new StringBuilder();
    for (Uri uri : uris) {
        AppLink appLink;
        synchronized (cachedAppLinks) {
            appLink = cachedAppLinks.get(uri);
        }
        if (appLink != null) {
            appLinkResults.put(uri, appLink);
        } else {
            if (!urisToRequest.isEmpty()) {
                graphRequestFields.append(',');
            }
            graphRequestFields.append(uri.toString());
            urisToRequest.add(uri);
        }
    }
    if (urisToRequest.isEmpty()) {
        return Task.forResult(appLinkResults);
    }
    final Task<Map<Uri, AppLink>>.TaskCompletionSource<Map<Uri, AppLink>> taskCompletionSource = Task.create();
    Bundle appLinkRequestParameters = new Bundle();
    appLinkRequestParameters.putString("ids", graphRequestFields.toString());
    appLinkRequestParameters.putString("fields", String.format("%s.fields(%s,%s)", APP_LINK_KEY, APP_LINK_ANDROID_TARGET_KEY, APP_LINK_WEB_TARGET_KEY));
    GraphRequest appLinkRequest = new GraphRequest(// token
    AccessToken.getCurrentAccessToken(), /* Access Token */
    "", /* Graph path */
    appLinkRequestParameters, /* Query parameters */
    null, /* HttpMethod */
    new GraphRequest.Callback() {

        /* Callback */
        @Override
        public void onCompleted(GraphResponse response) {
            FacebookRequestError error = response.getError();
            if (error != null) {
                taskCompletionSource.setError(error.getException());
                return;
            }
            JSONObject responseJson = response.getJSONObject();
            if (responseJson == null) {
                taskCompletionSource.setResult(appLinkResults);
                return;
            }
            for (Uri uri : urisToRequest) {
                String uriString = uri.toString();
                if (!responseJson.has(uriString)) {
                    continue;
                }
                JSONObject urlData;
                try {
                    urlData = responseJson.getJSONObject(uri.toString());
                    JSONObject appLinkData = urlData.getJSONObject(APP_LINK_KEY);
                    JSONArray rawTargets = appLinkData.getJSONArray(APP_LINK_ANDROID_TARGET_KEY);
                    int targetsCount = rawTargets.length();
                    List<AppLink.Target> targets = new ArrayList<AppLink.Target>(targetsCount);
                    for (int i = 0; i < targetsCount; i++) {
                        AppLink.Target target = getAndroidTargetFromJson(rawTargets.getJSONObject(i));
                        if (target != null) {
                            targets.add(target);
                        }
                    }
                    Uri webFallbackUrl = getWebFallbackUriFromJson(uri, appLinkData);
                    AppLink appLink = new AppLink(uri, targets, webFallbackUrl);
                    appLinkResults.put(uri, appLink);
                    synchronized (cachedAppLinks) {
                        cachedAppLinks.put(uri, appLink);
                    }
                } catch (JSONException e) {
                    // The data for this uri was missing or badly formed.
                    continue;
                }
            }
            taskCompletionSource.setResult(appLinkResults);
        }
    });
    appLinkRequest.executeAsync();
    return taskCompletionSource.getTask();
}
Also used : Task(bolts.Task) GraphRequest(com.facebook.GraphRequest) HashMap(java.util.HashMap) Bundle(android.os.Bundle) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Uri(android.net.Uri) FacebookRequestError(com.facebook.FacebookRequestError) JSONObject(org.json.JSONObject) GraphResponse(com.facebook.GraphResponse) AppLink(bolts.AppLink) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 5 with AppLink

use of bolts.AppLink in project facebook-android-sdk by facebook.

the class FacebookAppLinkResolverTests method testSingleUrl.

public void testSingleUrl() {
    String testUrlString = "https://fb.me/732873156764191";
    Uri testUrl = Uri.parse(testUrlString);
    Uri testWebUri = Uri.parse("http://www.facebook.com/");
    ArrayList<AppLink.Target> testTargets = new ArrayList<AppLink.Target>();
    testTargets.add(new AppLink.Target("com.myapp", null, Uri.parse("myapp://3"), "my app"));
    testTargets.add(new AppLink.Target("com.myapp-test", null, Uri.parse("myapp-test://4"), "my test app"));
    try {
        executeResolverOnBlockerThread(new FacebookAppLinkResolver(), testUrl);
        getTestBlocker().waitForSignals(1);
        assertNotNull(resolveTask);
        Task<AppLink> singleUrlResolveTask = (Task<AppLink>) resolveTask;
        assertTrue(singleUrlResolveTask.isCompleted() && !singleUrlResolveTask.isCancelled() && !singleUrlResolveTask.isFaulted());
        AppLink appLink = singleUrlResolveTask.getResult();
        assertEquals(appLink.getSourceUrl(), testUrl);
        assertEquals(appLink.getWebUrl(), testWebUri);
        assertTrue(targetListsAreEqual(appLink.getTargets(), testTargets));
    } catch (Exception e) {
        // Forcing the test to fail with details
        assertNull(e);
    }
}
Also used : Task(bolts.Task) FacebookAppLinkResolver(com.facebook.applinks.FacebookAppLinkResolver) ArrayList(java.util.ArrayList) AppLink(bolts.AppLink) Uri(android.net.Uri)

Aggregations

Uri (android.net.Uri)6 AppLink (bolts.AppLink)6 Task (bolts.Task)5 Bundle (android.os.Bundle)3 FacebookAppLinkResolver (com.facebook.applinks.FacebookAppLinkResolver)3 ArrayList (java.util.ArrayList)3 JSONArray (org.json.JSONArray)3 JSONException (org.json.JSONException)3 JSONObject (org.json.JSONObject)3 GraphObject (com.facebook.model.GraphObject)2 FlakyTest (android.test.FlakyTest)1 FacebookRequestError (com.facebook.FacebookRequestError)1 GraphRequest (com.facebook.GraphRequest)1 GraphResponse (com.facebook.GraphResponse)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1