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:com.arthurtimberly.fragments.CaptureFragment.java

/**
 * Kicks off countdown and auto-focus, captures frame at the end.
 *//*from w ww.j  a  va  2s.com*/
private void kickoffCountdownCapture() {
    // Set visibility of countdown timer.
    mCountdown.setVisibility(View.VISIBLE);

    if (mTimer != null) {
        mTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                final Activity activity = getActivity();
                if (activity != null && !activity.isFinishing()) {
                    activity.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            if (isActivityAlive()) {
                                if (mCountdownThree != null) {
                                    mCountdownThree
                                            .setTextColor(getResources().getColor(R.color.selection_color));
                                }
                                mCameraAudioHelper.beep();

                                // Kick off auto-focus and indicate status.
                                if (mCamera != null) {
                                    mCamera.autoFocus(new MyAutoFocusCallback());
                                }
                            }
                        }
                    });
                }
            }
        }, COUNTDOWN_STEP_DELAY * 2);

        mTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                final Activity activity = getActivity();
                if (activity != null && !activity.isFinishing()) {
                    activity.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            if (isActivityAlive()) {
                                if (mCountdownTwo != null) {
                                    mCountdownTwo
                                            .setTextColor(getResources().getColor(R.color.selection_color));
                                }
                                mCameraAudioHelper.beep();
                            }
                        }
                    });
                }
            }
        }, COUNTDOWN_STEP_DELAY * 3);

        mTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                final Activity activity = getActivity();
                if (activity != null && !activity.isFinishing()) {
                    activity.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            if (isActivityAlive()) {
                                if (mCountdownOne != null) {
                                    mCountdownOne
                                            .setTextColor(getResources().getColor(R.color.selection_color));
                                }
                                mCameraAudioHelper.beep();
                            }
                        }
                    });
                }
            }
        }, COUNTDOWN_STEP_DELAY * 4);

        mTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                final Activity activity = getActivity();
                if (activity != null && !activity.isFinishing()) {
                    activity.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            if (isActivityAlive()) {
                                // Reset countdown timer to initial state.
                                resetCountdownTimer();

                                // Capture frame.
                                takePicture();
                            }
                        }
                    });
                }
            }
        }, COUNTDOWN_STEP_DELAY * 5);
    }
}

From source file:com.andrewshu.android.reddit.threads.BitmapManager.java

public void fetchBitmapOnThread(final String urlString, final ImageView imageView,
        final ProgressBar indeterminateProgressBar, final Activity act) {
    SoftReference<Bitmap> ref = mCache.get(urlString);
    if (ref != null && ref.get() != null) {
        imageView.setImageBitmap(ref.get());
        return;//from w  ww. j a  va2s  .  c  om
    }

    final Runnable progressBarShow = new Runnable() {
        public void run() {
            if (indeterminateProgressBar != null) {
                imageView.setVisibility(View.GONE);
                indeterminateProgressBar.setVisibility(View.VISIBLE);
            }
        }
    };
    final Runnable progressBarHide = new Runnable() {
        public void run() {
            if (indeterminateProgressBar != null) {
                indeterminateProgressBar.setVisibility(View.GONE);
                imageView.setVisibility(View.VISIBLE);
            }
        }
    };

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            if (indeterminateProgressBar != null && act != null)
                act.runOnUiThread(progressBarHide);
            imageView.setImageBitmap((Bitmap) message.obj);
        }
    };

    Thread thread = new Thread() {
        @Override
        public void run() {
            if (indeterminateProgressBar != null && act != null)
                act.runOnUiThread(progressBarShow);
            Bitmap bitmap = fetchBitmap(urlString);
            Message message = handler.obtainMessage(1, bitmap);
            handler.sendMessage(message);
        }
    };
    thread.start();
}

From source file:com.binroot.fatpita.BitmapManager2.java

public void fetchBitmapOnThread(final String urlString, final ImageView imageView,
        final ProgressBar indeterminateProgressBar, final Activity act, final int sample) {
    SoftReference<Bitmap> ref = mCache.get(urlString);
    if (ref != null && ref.get() != null) {
        imageView.setImageBitmap(ref.get());
        return;/* w ww  .j  a  va 2 s.c om*/
    }

    final Runnable progressBarShow = new Runnable() {
        public void run() {
            if (indeterminateProgressBar != null) {
                imageView.setVisibility(View.GONE);
                indeterminateProgressBar.setVisibility(View.VISIBLE);
            }
        }
    };
    final Runnable progressBarHide = new Runnable() {
        public void run() {
            if (indeterminateProgressBar != null) {
                indeterminateProgressBar.setVisibility(View.GONE);
                imageView.setVisibility(View.VISIBLE);
            }
        }
    };

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            if (indeterminateProgressBar != null && act != null)
                act.runOnUiThread(progressBarHide);
            imageView.setImageBitmap((Bitmap) message.obj);
        }
    };

    Thread thread = new Thread() {
        @Override
        public void run() {
            if (indeterminateProgressBar != null && act != null)
                act.runOnUiThread(progressBarShow);
            Bitmap bitmap = fetchBitmap(urlString, sample);
            Message message = handler.obtainMessage(1, bitmap);
            handler.sendMessage(message);
        }
    };
    thread.start();
}

From source file:com.binroot.fatpita.BitmapManager.java

public void fetchBitmapOnThread(final String urlString, final ImageView imageView,
        final ProgressBar indeterminateProgressBar, final Activity act, final boolean saveToHistory) {
    SoftReference<Bitmap> ref = mCache.get(urlString);
    if (ref != null && ref.get() != null) {
        imageView.setImageBitmap(ref.get());
        return;//  www.  java2s .  c  o m
    }

    final Runnable progressBarShow = new Runnable() {
        public void run() {
            if (indeterminateProgressBar != null) {
                imageView.setVisibility(View.GONE);
                indeterminateProgressBar.setVisibility(View.VISIBLE);
            }
        }
    };
    final Runnable progressBarHide = new Runnable() {
        public void run() {
            if (indeterminateProgressBar != null) {
                indeterminateProgressBar.setVisibility(View.GONE);
                imageView.setVisibility(View.VISIBLE);
            }
        }
    };

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            if (indeterminateProgressBar != null && act != null)
                act.runOnUiThread(progressBarHide);
            imageView.setImageBitmap((Bitmap) message.obj);
        }
    };

    Thread thread = new Thread() {
        @Override
        public void run() {
            if (indeterminateProgressBar != null && act != null)
                act.runOnUiThread(progressBarShow);
            Bitmap bitmap = fetchBitmap(urlString, saveToHistory);
            Message message = handler.obtainMessage(1, bitmap);
            handler.sendMessage(message);
        }
    };
    thread.start();
}

From source file:net.ddns.mlsoftlaberge.trycorder.TryviscamFragment.java

private void saypost(final String text) {
    final Activity activity = getActivity();
    if (activity != null) {
        activity.runOnUiThread(new Runnable() {
            @Override/*from w w  w . j  a va 2  s  . co m*/
            public void run() {
                say(text);
            }
        });
    }
}

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

public void postNewFileToServer(final O365APIsStart_Application application, final Activity currentActivity,
        String fileName, final String fileContents, final SharePointClient fileClient) {
    final Item newFile = new Item();
    newFile.settype("File");
    newFile.setname(fileName);// w  w  w . ja  v  a2s  . c o m
    ListenableFuture<Item> future = fileClient.getfiles().add(newFile);
    Futures.addCallback(future, new FutureCallback<Item>() {
        @Override
        public void onFailure(Throwable t) {
            Log.e("Asset", t.getMessage());
            // Notify caller that the Event update operation failed

            OperationResult opResult = new OperationResult("Post new file ",
                    "Failed: " + getErrorMessage(t.getMessage()), "");
            mEventOperationCompleteListener.onOperationComplete(opResult);
        }

        @Override
        public void onSuccess(final Item item) {
            try {
                fileClient.getfiles().getById(item.getid()).asFile().putContent(fileContents.getBytes()).get();
                currentActivity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        application.getFileListViewState().addNewFileToList(item);
                    }
                });
                // Notify caller that the Event update operation is complete
                // and succeeded
                OperationResult opResult = new OperationResult("Post new file to server",
                        "Posted new file to server", "");
                mEventOperationCompleteListener.onOperationComplete(opResult);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

        }
    });
}

From source file:edu.uw.seeforme.seeforme.Camera2BasicFragment.java

/**
 * Shows a {@link Toast} on the UI thread.
 *
 * @param text The message to show/*from   w  w w.  j a  va  2s.c  o  m*/
 */
private void showToast(final String text) {
    final Activity activity = getActivity();
    if (activity != null) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(activity, text, Toast.LENGTH_SHORT).show();
                colorButton.setEnabled(true);
                textButton.setEnabled(true);
                objectButton.setEnabled(true);
                repeatButton.setEnabled(true);
            }
        });
    }
}

From source file:com.macadamian.blinkup.BlinkUpPlugin.java

private boolean startBlinkUp(final Activity activity, final BlinkupController controller, JSONArray data) {
    int timeoutMs;
    try {// www.  j  a  v  a2s  . c  om
        mApiKey = data.getString(START_BLINKUP_ARG_API_KEY);
        mDeveloperPlanId = data.getString(START_BLINKUP_ARG_DEVELOPER_PLAN_ID);
        mIsInDevelopment = data.getBoolean(START_BLINKUP_IS_IN_DEVELOPMENT);
        timeoutMs = data.getInt(START_BLINKUP_ARG_TIMEOUT_MS);
    } catch (JSONException exc) {
        BlinkUpPluginResult.sendPluginErrorToCallback(ERROR_INVALID_ARGUMENTS);
        return false;
    }

    // if api key not valid, send error message and quit
    if (!apiKeyFormatValid()) {
        BlinkUpPluginResult.sendPluginErrorToCallback(ERROR_INVALID_API_KEY);
        return false;
    } else if (mDeveloperPlanId == null || mDeveloperPlanId.isEmpty()) {
        BlinkUpPluginResult.sendPluginErrorToCallback(ERROR_INVALID_ARGUMENTS);
        return false;
    }

    controller.intentBlinkupComplete = createBlinkUpCompleteIntent(activity, timeoutMs);

    // default is to run on WebCore thread, we have UI so need UI thread
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            presentBlinkUp(activity, controller);
        }
    });
    return true;
}

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

public O365FileModel getFileContentsFromServer(final Activity currentActivity, O365FileModel fileItem) {
    final O365FileModel fm = new O365FileModel(mApplication, fileItem.getItem());
    String fileName = fm.getName();
    if (fileName != null && (fileName.contains(".txt") || fileName.contains(".xml"))) {
        ListenableFuture<byte[]> future = mApplication.getFileClient().getfiles().getById(fileItem.getId())
                .asFile().getContent();//  ww  w  . j  a  v a 2 s .c  o  m

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

            @Override
            public void onSuccess(final byte[] fileBytes) {
                currentActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            fm.setContents(currentActivity, new String(fileBytes, "UTF-8"));
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                });
                // Notify caller that the Event update operation is complete
                // and
                // succeeded
                OperationResult opResult = new OperationResult("Get file contents", "Got file contents",
                        "FileContentsRetrieved");
                mEventOperationCompleteListener.onOperationComplete(opResult);
            }
        });
        mApplication.setDisplayedFile(fm);
        return fm;
    } else {
        // Notify caller that a different file type is required.
        OperationResult opResult = new OperationResult("Get file contents",
                "Select a .txt or .xml file to read", "");
        mEventOperationCompleteListener.onOperationComplete(opResult);
        return null;
    }

}

From source file:dentex.youtube.downloader.DashboardActivity.java

public static int refreshlist(final Activity activity) {
    entries = 0;//w w  w .j av  a2s . co  m
    activity.runOnUiThread(new Runnable() {
        public void run() {

            clearAdapterAndLists();

            // refill the Lists and re-populate the adapter
            entries = parseJson((Context) activity);
            updateProgressBars();
            buildList();

            if (da.isEmpty()) {
                showEmptyListInfo(activity);
            }

            // refresh the list view
            da.notifyDataSetChanged();
        }
    });
    return entries;
}