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:im.neon.activity.CommonActivityUtils.java

/**
 * Start a room activity with the dedicated parameters.
 * Pop the activity to the homeActivity before pushing the new activity.
 *
 * @param fromActivity the caller activity.
 * @param session      the session./*from   w  ww . j  av  a 2s .co m*/
 * @param params       the room activity parameters.
 */
public static void goToRoomPage(final Activity fromActivity, final MXSession session,
        final Map<String, Object> params) {
    final MXSession finalSession = (session == null)
            ? Matrix.getMXSession(fromActivity, (String) params.get(VectorRoomActivity.EXTRA_MATRIX_ID))
            : session;

    // sanity check
    if ((null == finalSession) || !finalSession.isAlive()) {
        return;
    }

    String roomId = (String) params.get(VectorRoomActivity.EXTRA_ROOM_ID);

    Room room = finalSession.getDataHandler().getRoom(roomId);

    // do not open a leaving room.
    // it does not make.
    if ((null != room) && (room.isLeaving())) {
        return;
    }

    fromActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // if the activity is not the home activity
            if (!(fromActivity instanceof VectorHomeActivity)) {
                // pop to the home activity
                Log.d(LOG_TAG, "## goToRoomPage(): start VectorHomeActivity..");
                Intent intent = new Intent(fromActivity, VectorHomeActivity.class);
                intent.setFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP);

                intent.putExtra(VectorHomeActivity.EXTRA_JUMP_TO_ROOM_PARAMS, (Serializable) params);
                fromActivity.startActivity(intent);
            } else {
                // already to the home activity
                // so just need to open the room activity
                Log.d(LOG_TAG, "## goToRoomPage(): already in VectorHomeActivity..");
                Intent intent = new Intent(fromActivity, VectorRoomActivity.class);

                for (String key : params.keySet()) {
                    Object value = params.get(key);

                    if (value instanceof String) {
                        intent.putExtra(key, (String) value);
                    } else if (value instanceof Boolean) {
                        intent.putExtra(key, (Boolean) value);
                    } else if (value instanceof Parcelable) {
                        intent.putExtra(key, (Parcelable) value);
                    }
                }

                // try to find a displayed room name
                if (null == params.get(VectorRoomActivity.EXTRA_DEFAULT_NAME)) {

                    Room room = finalSession.getDataHandler()
                            .getRoom((String) params.get(VectorRoomActivity.EXTRA_ROOM_ID));

                    if ((null != room) && room.isInvited()) {
                        String displayName = VectorUtils.getRoomDisplayName(fromActivity, finalSession, room);

                        if (null != displayName) {
                            intent.putExtra(VectorRoomActivity.EXTRA_DEFAULT_NAME, displayName);
                        }
                    }
                }

                fromActivity.startActivity(intent);
            }
        }
    });
}

From source file:com.vuze.android.remote.SessionInfo.java

private void openTorrentWithMetaData(final Activity activity, final String name, final String metainfo) {
    executeRpc(new RpcExecuter() {
        @Override/*from   www . ja v  a2s . c o m*/
        public void executeRpc(TransmissionRPC rpc) {
            rpc.addTorrentByMeta(metainfo, true,
                    new TorrentAddedReceivedListener2(SessionInfo.this, activity, true, null));
        }
    });
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Context context = activity.isFinishing() ? VuzeRemoteApp.getContext() : activity;
            String s = context.getResources().getString(R.string.toast_adding_xxx, name);
            Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
        }
    });
    VuzeEasyTracker.getInstance(activity).sendEvent("RemoteAction", "AddTorrent", "AddTorrentByMeta", null);
}

From source file:tv.ouya.sdk.CordovaOuyaPlugin.java

protected void setSafeArea(CallbackContext callback, final float progress) {
    final Activity activity = cordova.getActivity();
    if (null != activity) {
        Runnable runnable = new Runnable() {
            public void run() {
                // bring in by %
                float percent = 0.1f;
                float ratio = 1 - (1 - progress) * percent;
                float halfRatio = 1 - (1 - progress) * percent * 0.5f;
                float maxWidth = getDisplayWidth();
                float maxHeight = getDisplayHeight();
                FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
                ViewGroup.LayoutParams layout = content.getLayoutParams();
                layout.width = (int) (maxWidth * ratio);
                layout.height = (int) (maxHeight * ratio);
                content.setLayoutParams(layout);
                content.setX(maxWidth - maxWidth * halfRatio);
                content.setY(maxHeight - maxHeight * halfRatio);
            }//from  ww w  .  j ava  2  s.c  o  m
        };
        activity.runOnUiThread(runnable);
    }
    callback.success();
}

From source file:es.farfuteam.vncpp.controller.CanvasActivity.java

/**
 * @brief The time out thread// w  w w  .j a  v a2 s. c  om
 * @details The time out thread. Waits the timerConnection and then checks the connect attribute, if it is false
 * then finish the Activity
 */
private void runTimerConnection() {
    final Activity activityThis = this;
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(TIMER_CONNECTION);
            } catch (InterruptedException e) {
                // Auto-generated catch block

            }
            if (!connect && progressDialog.isShowing()) {
                waitDialog = true;

                activityThis.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        //Dialogo "Se ha excedido el tiempo de conexion"
                        progressDialog.dismiss();
                        showDialog(EnumDialogs.timeExceededDialog.ordinal());

                    }
                });
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        while (waitDialog)
                            ;
                        vnc.finishVnc();
                        finishConnection();
                    }
                }).start();

            }

        }
    }).start();
}

From source file:es.farfuteam.vncpp.controller.CanvasActivity.java

/**
 * @param host     The IP//from   ww  w .  java2s  .  com
 * @param port     The port
 * @param pass     The pasword
 * @param quality  The image quality
 * @param compress The image has to be compress or not
 * @brief Starts the connection
 * @details Starts the connection. Calls the VncBridgeJNI method startConnect to start the connection. If everything ok
 * the connection starts and the connect attribute is set as true
 */
private void iniConnection(final String host, final String port, final String pass, final QualityArray quality,
        final Boolean compress) {
    vnc = new VncBridgeJNI();
    vnc.addObserver(this);

    final Activity activityThis = this;
    new Thread(new Runnable() {

        @Override
        public void run() {
            ConnectionError error = vnc.startConnect(host, Integer.parseInt(port), pass, quality, compress,
                    (es.farfuteam.vncpp.controller.Configuration.getInstance()).isHideMouse());

            if (error != ConnectionError.ALLOK) {
                waitDialog = true;

                activityThis.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                        showDialog(EnumDialogs.createServerNotFoundDialog.ordinal());

                    }
                });

                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        while (waitDialog)
                            ;
                        finishConnection();

                    }
                }).start();

                return;
            } else {
                connect = true;
            }
        }
    }).start();
}

From source file:im.vector.activity.CommonActivityUtils.java

/**
 * Offer to send some dedicated intent data to an existing room
 * @param fromActivity the caller activity
 * @param intent the intent param//from  w  w w.  ja  v  a2 s.c om
 * @param session the session/
 */
public static void sendFilesTo(final Activity fromActivity, final Intent intent, final MXSession session) {
    // sanity check
    if ((null == session) || !session.isActive()) {
        return;
    }

    final ArrayList<RoomSummary> mergedSummaries = new ArrayList<RoomSummary>();
    mergedSummaries.addAll(session.getDataHandler().getStore().getSummaries());

    Collections.sort(mergedSummaries, new Comparator<RoomSummary>() {
        @Override
        public int compare(RoomSummary lhs, RoomSummary rhs) {
            if (lhs == null || lhs.getLatestEvent() == null) {
                return 1;
            } else if (rhs == null || rhs.getLatestEvent() == null) {
                return -1;
            }

            if (lhs.getLatestEvent().getOriginServerTs() > rhs.getLatestEvent().getOriginServerTs()) {
                return -1;
            } else if (lhs.getLatestEvent().getOriginServerTs() < rhs.getLatestEvent().getOriginServerTs()) {
                return 1;
            }
            return 0;
        }
    });

    AlertDialog.Builder builderSingle = new AlertDialog.Builder(fromActivity);
    builderSingle.setTitle(fromActivity.getText(R.string.send_files_in));
    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(fromActivity,
            R.layout.dialog_room_selection);

    for (RoomSummary summary : mergedSummaries) {
        arrayAdapter.add(summary.getRoomName());
    }

    builderSingle.setNegativeButton(fromActivity.getText(R.string.cancel),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

    builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, final int which) {
            dialog.dismiss();
            fromActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    RoomSummary summary = mergedSummaries.get(which);
                    CommonActivityUtils.goToRoomPage(session, summary.getRoomId(), fromActivity, intent);
                }
            });
        }
    });
    builderSingle.show();
}

From source file:com.microsoft.office365.starter.models.O365FileListModel.java

public void postDeleteSelectedFileFromServer(final Activity currentActivity, SharePointClient fileClient,
        int itemPosition) {
    if (itemPosition == ListView.INVALID_POSITION) {
        // Notify caller that no file was selected for the delete
        OperationResult opResult = new OperationResult("Post delete selected file ",
                "failed: No file selected to delete", "");
        mEventOperationCompleteListener.onOperationComplete(opResult);
        return;//from  ww w.  ja  v  a  2  s  .c o  m
    }
    final int index = itemPosition; // cache this for the return trip thread
    // before reset at end
    // of this method
    O365FileModel fileToDelete = mApplication.getFileAdapterList().getItem(index);

    final O365APIsStart_Application application = (O365APIsStart_Application) currentActivity.getApplication();
    ListenableFuture future = fileClient.getfiles().getById(fileToDelete.getId()).asFile()
            .addHeader("If-Match", "*").delete();

    Futures.addCallback(future, new FutureCallback() {
        @Override
        public void onFailure(Throwable t) {
            Log.e("Asset", t.getMessage());
            // Notify caller that the Event update operation failed
            OperationResult opResult = new OperationResult("Post delete selected file ",
                    "failed: " + getErrorMessage(t.getMessage()), "");
            mEventOperationCompleteListener.onOperationComplete(opResult);
        }

        @Override
        public void onSuccess(Object obj) {
            currentActivity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    application.getFileListViewState().deleteSelectedFileFromList(index);
                }
            });
            // Notify caller that the Event update operation is complete and
            // succeeded
            OperationResult opResult = new OperationResult("Post delete selected file on server",
                    "Posted delete selected file on server", "FileDeleted");
            mEventOperationCompleteListener.onOperationComplete(opResult);
        }
    });
    // selectedItem = -1;
}

From source file:com.microsoft.office365.starter.FilesFolders.O365FileListModel.java

public void postDeleteSelectedFileFromServer(final Activity currentActivity, SharePointClient fileClient,
        int itemPosition) {
    if (itemPosition == ListView.INVALID_POSITION) {
        // Notify caller that no file was selected for the delete
        OperationResult opResult = new OperationResult("Post delete selected file ",
                "failed: No file selected to delete", "");
        mEventOperationCompleteListener.onOperationComplete(opResult);
        return;//from   w  w  w.  j  a v  a 2 s  .  c om
    }
    final int index = itemPosition; // cache this for the return trip thread
    // before reset at end
    // of this method
    O365FileModel fileToDelete = mApplication.getFileAdapterList().getItem(index);

    final O365APIsStart_Application application = (O365APIsStart_Application) currentActivity.getApplication();
    ListenableFuture future = fileClient.getfiles().getById(fileToDelete.getId()).addHeader("If-Match", "*")
            .delete();

    Futures.addCallback(future, new FutureCallback() {
        @Override
        public void onFailure(Throwable t) {
            Log.e("Asset", t.getMessage());
            // Notify caller that the Event update operation failed
            OperationResult opResult = new OperationResult("Post delete selected file ",
                    "failed: " + APIErrorMessageHelper.getErrorMessage(t.getMessage()), "");
            mEventOperationCompleteListener.onOperationComplete(opResult);
        }

        @Override
        public void onSuccess(Object obj) {
            currentActivity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    application.getFileListViewState().deleteSelectedFileFromList(index);
                }
            });
            // Notify caller that the Event update operation is complete and
            // succeeded
            OperationResult opResult = new OperationResult("Post delete selected file on server",
                    "Posted delete selected file on server", "FileDeleted");
            mEventOperationCompleteListener.onOperationComplete(opResult);
        }
    });
    // selectedItem = -1;
}

From source file:com.vuze.android.remote.SessionInfo.java

public void openTorrent(final Activity activity, final String sTorrentURL, final String friendlyName) {
    if (sTorrentURL == null || sTorrentURL.length() == 0) {
        return;//from   w  w  w .j  a v a2s .  com
    }
    executeRpc(new RpcExecuter() {
        @Override
        public void executeRpc(TransmissionRPC rpc) {
            rpc.addTorrentByUrl(sTorrentURL, true,
                    new TorrentAddedReceivedListener2(SessionInfo.this, activity, true, sTorrentURL));
        }
    });
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Context context = activity.isFinishing() ? VuzeRemoteApp.getContext() : activity;
            String s = context.getResources().getString(R.string.toast_adding_xxx,
                    friendlyName == null ? sTorrentURL : friendlyName);
            Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
        }
    });

    VuzeEasyTracker.getInstance(activity).sendEvent("RemoteAction", "AddTorrent", "AddTorrentByUrl", null);
}

From source file:im.vector.fragments.VectorRoomSettingsFragment.java

/**
     * Display the loading view in the parent activity layout.
     * This view is disabled/enabled to achieve a waiting screen.
     *//*from ww  w  .ja v  a  2s . c  o  m*/
    private void displayLoadingView() {
        Activity parentActivity = getActivity();
        if (null != parentActivity) {
            parentActivity.runOnUiThread(new Runnable() {
                public void run() {

                    // disable the fragment container view to disable preferences access
                    //enablePreferenceWidgets(false);

                    // disable preference screen during server updates
                    if (null != mParentFragmentContainerView)
                        mParentFragmentContainerView.setEnabled(false);

                    // display the loading progress bar screen
                    if (null != mParentLoadingView) {
                        mParentLoadingView.setVisibility(View.VISIBLE);
                    }
                }
            });
        }
    }