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();
}
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();
}
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);
}
}
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();
}
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);
}
}
Aggregations