use of com.odysee.app.exceptions.LbryioResponseException in project odysee-android by OdyseeTeam.
the class RewardsFragment method fetchRewards.
private void fetchRewards() {
Helper.setViewVisibility(rewardList, View.INVISIBLE);
rewardsLoading.setVisibility(View.VISIBLE);
Activity activity = getActivity();
String authToken;
AccountManager am = AccountManager.get(getContext());
Account odyseeAccount = Helper.getOdyseeAccount(am.getAccounts());
if (odyseeAccount != null) {
authToken = am.peekAuthToken(odyseeAccount, "auth_token_type");
} else {
authToken = "";
}
Map<String, String> options = new HashMap<>();
options.put("multiple_rewards_per_type", "true");
if (odyseeAccount != null && authToken != null) {
options.put("auth_token", authToken);
}
ExecutorService executorService = Executors.newSingleThreadExecutor();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
Supplier<List<Reward>> supplier = new FetchRewardsSupplier(options);
CompletableFuture<List<Reward>> cf = CompletableFuture.supplyAsync(supplier, executorService);
cf.exceptionally(e -> {
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
((MainActivity) activity).showError(e.getMessage());
}
});
}
return null;
}).thenAccept(rewards -> {
Lbryio.updateRewardsLists(rewards);
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
rewardsLoading.setVisibility(View.GONE);
if (adapter == null) {
adapter = new RewardListAdapter(rewards, getContext());
adapter.setClickListener(RewardsFragment.this);
adapter.setDisplayMode(RewardListAdapter.DISPLAY_MODE_UNCLAIMED);
rewardList.setAdapter(adapter);
} else {
adapter.setRewards(rewards);
}
Helper.setViewVisibility(rewardList, View.VISIBLE);
}
});
}
});
} else {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Callable<List<Reward>> callable = new Callable<List<Reward>>() {
@Override
public List<Reward> call() {
List<Reward> rewards = null;
try {
JSONArray results = (JSONArray) Lbryio.parseResponse(Lbryio.call("reward", "list", options, null));
rewards = new ArrayList<>();
if (results != null) {
for (int i = 0; i < results.length(); i++) {
rewards.add(Reward.fromJSONObject(results.getJSONObject(i)));
}
}
} catch (ClassCastException | LbryioRequestException | LbryioResponseException | JSONException ex) {
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
((MainActivity) activity).showError(ex.getMessage());
}
});
}
}
return rewards;
}
};
Future<List<Reward>> future = executorService.submit(callable);
try {
List<Reward> rewards = future.get();
if (rewards != null) {
Lbryio.updateRewardsLists(rewards);
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
rewardsLoading.setVisibility(View.GONE);
if (adapter == null) {
adapter = new RewardListAdapter(rewards, getContext());
adapter.setClickListener(RewardsFragment.this);
adapter.setDisplayMode(RewardListAdapter.DISPLAY_MODE_UNCLAIMED);
rewardList.setAdapter(adapter);
} else {
adapter.setRewards(rewards);
}
Helper.setViewVisibility(rewardList, View.VISIBLE);
}
});
}
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
});
t.start();
}
}
use of com.odysee.app.exceptions.LbryioResponseException in project odysee-android by OdyseeTeam.
the class Lbryio method getAuthToken.
public static String getAuthToken(Context context) throws LbryioRequestException, LbryioResponseException {
// fetch a new auth token
if (Helper.isNullOrEmpty(Lbry.INSTALLATION_ID)) {
throw new LbryioRequestException("The LBRY installation ID is not set.");
}
generatingAuthToken = true;
/*if (BuildConfig.DEBUG) {
Log.d(TAG, "Generating a new auth token");
}*/
Map<String, String> options = new HashMap<>();
options.put("auth_token", "");
options.put("language", "en");
options.put("app_id", Lbry.INSTALLATION_ID);
Response response = Lbryio.call("user", "new", options, "post", context);
try {
JSONObject json = (JSONObject) parseResponse(response);
/*if (BuildConfig.DEBUG) {
Log.d(TAG, String.format("/user/new response: %s", json.toString(2)));
}*/
if (!json.has(AUTH_TOKEN_PARAM)) {
throw new LbryioResponseException("auth_token was not set in the response");
}
AUTH_TOKEN = json.getString(AUTH_TOKEN_PARAM);
broadcastAuthTokenGenerated(context);
} catch (JSONException | ClassCastException ex) {
LbryAnalytics.logError(String.format("/user/new failed: %s", ex.getMessage()), ex.getClass().getName());
throw new LbryioResponseException("auth_token was not set in the response", ex);
} finally {
generatingAuthToken = false;
}
return AUTH_TOKEN;
}
use of com.odysee.app.exceptions.LbryioResponseException in project odysee-android by OdyseeTeam.
the class Lbryio method loadExchangeRate.
public static void loadExchangeRate() {
try {
JSONObject response = (JSONObject) parseResponse(Lbryio.call("lbc", "exchange_rate", null));
LBCUSDRate = Helper.getJSONDouble("lbc_usd", 0, response);
} catch (LbryioResponseException | LbryioRequestException | ClassCastException ex) {
// pass
}
}
use of com.odysee.app.exceptions.LbryioResponseException in project odysee-android by OdyseeTeam.
the class MainActivity method fetchRewards.
private void fetchRewards() {
String authToken;
AccountManager am = AccountManager.get(this);
Account odyseeAccount = Helper.getOdyseeAccount(am.getAccounts());
if (odyseeAccount != null) {
authToken = am.peekAuthToken(odyseeAccount, "auth_token_type");
} else {
authToken = "";
}
Map<String, String> options = new HashMap<>();
options.put("multiple_rewards_per_type", "true");
if (odyseeAccount != null && authToken != null) {
options.put("auth_token", authToken);
}
ExecutorService executorService = Executors.newSingleThreadExecutor();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
Supplier<List<Reward>> supplier = new FetchRewardsSupplier(options);
CompletableFuture<List<Reward>> cf = CompletableFuture.supplyAsync(supplier, executorService);
cf.exceptionally(e -> {
runOnUiThread(new Runnable() {
@Override
public void run() {
showError(e.getMessage());
}
});
return null;
}).thenAccept(rewards -> {
Lbryio.updateRewardsLists(rewards);
if (Lbryio.totalUnclaimedRewardAmount > 0) {
updateRewardsUsdValue();
}
});
} else {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Callable<List<Reward>> callable = new Callable<List<Reward>>() {
@Override
public List<Reward> call() {
List<Reward> rewards = null;
try {
JSONArray results = (JSONArray) Lbryio.parseResponse(Lbryio.call("reward", "list", options, null));
rewards = new ArrayList<>();
if (results != null) {
for (int i = 0; i < results.length(); i++) {
rewards.add(Reward.fromJSONObject(results.getJSONObject(i)));
}
}
} catch (ClassCastException | LbryioRequestException | LbryioResponseException | JSONException ex) {
runOnUiThread(new Runnable() {
@Override
public void run() {
showError(ex.getMessage());
}
});
}
return rewards;
}
};
Future<List<Reward>> future = executorService.submit(callable);
try {
List<Reward> rewards = future.get();
if (rewards != null) {
Lbryio.updateRewardsLists(rewards);
if (Lbryio.totalUnclaimedRewardAmount > 0) {
updateRewardsUsdValue();
}
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
});
t.start();
}
}
use of com.odysee.app.exceptions.LbryioResponseException in project odysee-android by OdyseeTeam.
the class MainActivity method updateLocalNotifications.
private void updateLocalNotifications(List<LbryNotification> notifications) {
findViewById(R.id.notification_list_empty_container).setVisibility(notifications.isEmpty() ? View.VISIBLE : View.GONE);
findViewById(R.id.notifications_progress).setVisibility(View.GONE);
loadUnseenNotificationsCount();
if (notificationListAdapter == null) {
notificationListAdapter = new NotificationListAdapter(notifications, MainActivity.this);
notificationListAdapter.setSelectionModeListener(MainActivity.this);
((RecyclerView) findViewById(R.id.notifications_list)).setAdapter(notificationListAdapter);
} else {
notificationListAdapter.addNotifications(notifications);
}
resolveCommentAuthors(notificationListAdapter.getAuthorUrls());
notificationListAdapter.setClickListener(new NotificationListAdapter.NotificationClickListener() {
@Override
public void onNotificationClicked(LbryNotification notification) {
// set as seen and read
Map<String, String> options = new HashMap<>();
options.put("notification_ids", String.valueOf(notification.getRemoteId()));
options.put("is_seen", "true");
// so let's not mark the notification as read
if (!notification.getTargetUrl().equalsIgnoreCase("lbry://?subscriptions")) {
options.put("is_read", "true");
} else {
options.put("is_read", "false");
}
AccountManager am = AccountManager.get(getApplicationContext());
if (am != null) {
String at = am.peekAuthToken(Helper.getOdyseeAccount(am.getAccounts()), "auth_token_type");
if (at != null)
options.put("auth_token", at);
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
Supplier<Boolean> supplier = new NotificationUpdateSupplier(options);
CompletableFuture.supplyAsync(supplier);
} else {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
Lbryio.call("notification", "edit", options, null);
} catch (LbryioResponseException | LbryioRequestException e) {
e.printStackTrace();
}
}
});
t.start();
}
if (!notification.getTargetUrl().equalsIgnoreCase("lbry://?subscriptions")) {
markNotificationReadAndSeen(notification.getId());
}
String targetUrl = notification.getTargetUrl();
if (targetUrl.startsWith(SPECIAL_URL_PREFIX)) {
openSpecialUrl(targetUrl, "notification");
} else {
LbryUri target = LbryUri.tryParse(notification.getTargetUrl());
if (target != null) {
if (target.isChannel()) {
openChannelUrl(notification.getTargetUrl(), "notification");
} else {
openFileUrl(notification.getTargetUrl(), "notification");
}
}
}
hideNotifications(false);
}
});
}
Aggregations