Search in sources :

Example 1 with UnlockingTipsSupplier

use of com.odysee.app.supplier.UnlockingTipsSupplier in project odysee-android by OdyseeTeam.

the class MainActivity method unlockTips.

public void unlockTips() {
    if (unlockingTips) {
        return;
    }
    Map<String, Object> options = new HashMap<>();
    options.put("type", "support");
    options.put("is_not_my_input", true);
    options.put("blocking", true);
    AccountManager am = AccountManager.get(getApplicationContext());
    String authToken = am.peekAuthToken(am.getAccounts()[0], "auth_token_type");
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
        unlockingTips = true;
        Supplier<Boolean> task = new UnlockingTipsSupplier(options, authToken);
        CompletableFuture<Boolean> completableFuture = CompletableFuture.supplyAsync(task);
        completableFuture.thenAccept(result -> {
            unlockingTips = false;
        });
    } else {
        Thread unlockingThread = new Thread(new Runnable() {

            @Override
            public void run() {
                Callable<Boolean> callable = () -> {
                    try {
                        Lbry.directApiCall(Lbry.METHOD_TXO_SPEND, options, authToken);
                        return true;
                    } catch (ApiCallException | ClassCastException ex) {
                        ex.printStackTrace();
                        return false;
                    }
                };
                ExecutorService executorService = Executors.newSingleThreadExecutor();
                Future<Boolean> future = executorService.submit(callable);
                try {
                    unlockingTips = true;
                    // This doesn't block main thread as it is called from a different thread
                    future.get();
                    unlockingTips = false;
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
        });
        unlockingThread.start();
    }
}
Also used : HashMap(java.util.HashMap) ApiCallException(com.odysee.app.exceptions.ApiCallException) SpannableString(android.text.SpannableString) UnlockingTipsSupplier(com.odysee.app.supplier.UnlockingTipsSupplier) Callable(java.util.concurrent.Callable) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) CompletableFuture(java.util.concurrent.CompletableFuture) ScheduledFuture(java.util.concurrent.ScheduledFuture) JSONObject(org.json.JSONObject) AccountManager(android.accounts.AccountManager)

Aggregations

AccountManager (android.accounts.AccountManager)1 SpannableString (android.text.SpannableString)1 ApiCallException (com.odysee.app.exceptions.ApiCallException)1 UnlockingTipsSupplier (com.odysee.app.supplier.UnlockingTipsSupplier)1 HashMap (java.util.HashMap)1 Callable (java.util.concurrent.Callable)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 JSONObject (org.json.JSONObject)1