Example usage for android.app Activity runOnUiThread

List of usage examples for android.app Activity runOnUiThread

Introduction

In this page you can find the example usage for android.app Activity runOnUiThread.

Prototype

public final void runOnUiThread(Runnable action) 

Source Link

Document

Runs the specified action on the UI thread.

Usage

From source file:org.apache.cordova.screenorientation.ScreenOrientation.java

/**
 * Executes the request and returns whether the action was valid.
 *
 * @param action                 The action to execute.
 * @param args                 JSONArray of arguments for the plugin.
 * @param callbackContext        The callback context used when calling back into JavaScript.
 * @return                         True if the action was valid, false otherwise.
 *//*from w w  w .  j  ava 2 s . c o m*/
public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext)
        throws JSONException {
    final String orientation = args.optString(0);
    final Activity activity = this.cordova.getActivity();
    final View view = this.webView;

    if (action.equals("setOrientation")) {
        LOG.w("ScreenOrientation", "Change orientation");
        final int androidOrientation = getOrientation(orientation);
        if (lastOrientation == -1) {
            lastOrientation = androidOrientation;
        }

        if (lastOrientation != androidOrientation) {
            activity.runOnUiThread(new Runnable() {
                public void run() {
                    LOG.v("ScreenOrientation", "Set orientation");
                    view.setVisibility(View.INVISIBLE);
                    activity.setRequestedOrientation(androidOrientation);

                    final Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            view.setVisibility(View.VISIBLE);
                        }
                    }, 1300);
                }
            });
            lastOrientation = androidOrientation;
        }

        callbackContext.success();
        return true;
    } else {
        return false;
    }
}

From source file:com.breadwallet.BreadWalletApp.java

public void showCustomDialog(final String title, final String message, final String buttonText) {
    Activity app = MainActivity.app;
    if (app == null)
        app = IntroActivity.app;//from  ww w . j av a2s.c  o m
    if (app == null) {
        Log.e(TAG, "showCustomDialog: FAILED, context is null");
        return;
    }
    final Activity finalApp = app;
    app.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            new android.app.AlertDialog.Builder(finalApp).setTitle(title).setMessage(message)
                    .setPositiveButton(buttonText, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    }).setIcon(android.R.drawable.ic_dialog_alert).show();
        }
    });
}

From source file:facebook.FacebookModule.java

@Kroll.method
public void dialog(final String action, final KrollDict params, final KrollFunction callback) {
    if (facebook == null) {
        Log.w(TAG, "dialog called without Facebook being instantiated.  Have you set appid?");
        return;// w  w  w  .j  a  v a2  s  .com
    }

    TiUIHelper.waitForCurrentActivity(new CurrentActivityListener() {
        @Override
        public void onCurrentActivityReady(Activity activity) {
            final Activity fActivity = activity;
            fActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    facebook.dialog(fActivity, action, Utils.mapToBundle(params),
                            new TiDialogListener(FacebookModule.this, callback, action));
                }
            });
        }
    });
}

From source file:com.razerzone.store.sdk.engine.gamemaker.Plugin.java

public static double requestLogin() {
    if (null == sStoreFacade) {
        Log.e(TAG, "StoreFacade is null!");
        return sNoop;
    }/*from   w w  w  .ja va  2s .c  om*/
    final Activity activity = Plugin.getRelay().getCurrentActivity();
    if (null == activity) {
        Log.d(TAG, "requestLogin: Activity is null");
        return sNoop;
    }
    if (null == sRequestLoginListener) {
        Log.e(TAG, "requestLogin: sRequestLoginListener is null");
        return sNoop;
    }
    Runnable runnable = new Runnable() {
        public void run() {
            sStoreFacade.requestLogin(activity, sRequestLoginListener);
        }
    };
    activity.runOnUiThread(runnable);
    return sNoop;
}

From source file:com.razerzone.store.sdk.engine.gamemaker.Plugin.java

public static double requestReceipts() {
    if (null == sStoreFacade) {
        Log.e(TAG, "StoreFacade is null!");
        return sNoop;
    }// w w  w .j av a  2 s  .c  o m
    final Activity activity = Plugin.getRelay().getCurrentActivity();
    if (null == activity) {
        Log.d(TAG, "Activity is null");
        return sNoop;
    }
    if (null == sRequestReceiptsListener) {
        Log.e(TAG, "requestReceipts: sRequestReceiptsListener is null");
        return sNoop;
    }
    Runnable runnable = new Runnable() {
        public void run() {
            sStoreFacade.requestReceipts(activity, sRequestReceiptsListener);
        }
    };
    activity.runOnUiThread(runnable);
    return sNoop;
}

From source file:org.level28.android.moca.ui.ItemListFragment.java

/**
 * Display a {@link Toast} after an exception occurred in background.
 * <p>/*from  w w  w.j  a va 2 s  .com*/
 * Subclasses may want to override this method to change the exception
 * reporting method.
 * 
 * @param exception
 *            the exception thrown by the loader
 * @param messageResId
 *            string resource id returned by
 *            {@link #getErrorMessage(Exception)}
 */
protected void showError(final Exception exception, final int messageResId) {
    final Activity activity = getActivity();
    final Application application = activity.getApplication();
    final String message = activity.getResources().getString(messageResId);

    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(application, message, Toast.LENGTH_LONG).show();
        }
    });
}

From source file:com.socialize.SocializeActionProxy.java

protected synchronized <L extends SocializeListener> void doAuthAsync(final Activity context,
        final SocializeListener listener, final Method method, final Object[] args) throws Throwable {
    final SocializeService service = getSocialize();

    if (!service.isAuthenticated()) {

        synchronized (this) {
            if (!service.isAuthenticated()) {
                context.runOnUiThread(new Runnable() {

                    @Override//w w w  .  j  ava 2s . co m
                    public void run() {

                        service.authenticate(context, new SocializeAuthListener() {

                            @Override
                            public void onError(SocializeException error) {
                                if (listener != null) {
                                    listener.onError(error);
                                }
                            }

                            @Override
                            public void onCancel() {
                                if (listener != null) {
                                    listener.onError(new AuthCanceledException(
                                            "Authentication was canceled by the user"));
                                }
                            }

                            @Override
                            public void onAuthSuccess(SocializeSession session) {
                                // Recurse
                                try {
                                    invoke(context, listener, method, args);
                                } catch (Throwable e) {
                                    if (listener != null) {
                                        listener.onError(SocializeException.wrap(e));
                                    }
                                }
                            }

                            @Override
                            public void onAuthFail(SocializeException error) {
                                if (listener != null) {
                                    listener.onError(error);
                                }
                            }
                        });
                    }
                });
            } else {
                // Recurse
                invoke(context, listener, method, args);
            }
        }
    } else {
        // Recurse
        invoke(context, listener, method, args);
    }
}

From source file:com.hemou.android.account.AccountUtils.java

/**
 * Update account/*from  w  w  w.j a  v a2  s .c o  m*/
 * 
 * @param account
 * @param act
 * @return true if account was updated, false otherwise
 */
public static boolean updateAccount(final Account account, final Activity act) {
    if (act == null)
        throw new IllegalArgumentException("Activity cannot be null");
    final String SUB_TAG = "acnt_update";
    int count = UPDATE_COUNT.get();
    synchronized (UPDATE_COUNT) {
        // Don't update the account if the account was successfully updated
        // while the lock was being waited for
        if (count != UPDATE_COUNT.get())
            return true;

        AccountManager manager = AccountManager.get(act);
        try {
            if (!hasAuthenticator(manager))
                throw new AuthenticatorConflictException();
            manager.updateCredentials(account, AUTHTOKEN_TYPE, null, act, null, null).getResult();
            UPDATE_COUNT.incrementAndGet();
            return true;
        } catch (OperationCanceledException e) {
            Log.d(SUB_TAG, "Excepting retrieving account", e);
            act.finish();
            return false;
        } catch (AccountsException e) {
            Log.d(SUB_TAG, "Excepting retrieving account", e);
            return false;
        } catch (AuthenticatorConflictException e) {
            act.runOnUiThread(new Runnable() {

                public void run() {
                    showConflictMessage(act);
                }
            });
            return false;
        } catch (IOException e) {
            Log.d(SUB_TAG, "Excepting retrieving account", e);
            return false;
        }
    }
}

From source file:com.razerzone.store.sdk.engine.gamemaker.Plugin.java

public static double requestGamerInfo() {
    if (null == sStoreFacade) {
        Log.e(TAG, "StoreFacade is null!");
        return sNoop;
    }/*  w w  w .  j  av a 2s  .com*/
    final Activity activity = Plugin.getRelay().getCurrentActivity();
    if (null == activity) {
        Log.d(TAG, "requestGamerInfo: Activity is null");
        return sNoop;
    }
    if (null == sRequestGamerInfoListener) {
        Log.e(TAG, "requestGamerInfo: sRequestGamerInfoListener is null");
        return sNoop;
    }
    Runnable runnable = new Runnable() {
        public void run() {
            sStoreFacade.requestGamerInfo(activity, sRequestGamerInfoListener);
        }
    };
    activity.runOnUiThread(runnable);
    return sNoop;
}

From source file:ru.dublgis.androidlocation.GmsLocationProvider.java

public boolean runOnUiThread(final Runnable runnable) {
    try {//w w  w.j  av a 2s.c  om
        if (runnable == null) {
            Log.e(TAG, "runOnUiThread: null runnable!");
            return false;
        }

        final Activity activity = getActivity();

        if (activity == null) {
            Log.e(TAG, "runOnUiThread: cannot schedule task because of the null context!");
            return false;
        }

        activity.runOnUiThread(runnable);
        return true;
    } catch (Exception e) {
        Log.e(TAG, "Exception when posting a runnable:", e);
        return false;
    }
}