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 capture in burst mode, which is basically capturing without auto-focus.
 *//*from  w  w w  .ja  va  2  s  .c  o  m*/
private void kickoffBurstCapture() {
    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() {
                        takePicture();
                    }
                });
            }
        }
    }, BURST_DELAY);
}

From source file:org.deviceconnect.android.deviceplugin.theta.fragment.ThetaGalleryFragment.java

@Override
public void onDisconnected(final ThetaDevice device) {
    mDevice = null;/*  www.  ja  v  a 2  s .c o  m*/
    Activity activity = getActivity();
    if (activity != null) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (!mIsGalleryMode && mGalleryAdapter != null) {
                    mGalleryAdapter.clear();
                    mGalleryAdapter.notifyDataSetChanged();
                }
                mUpdateThetaList.clear();
                enableReconnectView();
            }
        });
    }
}

From source file:org.deviceconnect.android.deviceplugin.theta.fragment.ThetaGalleryFragment.java

@Override
public void onConnected(final ThetaDevice device) {
    Activity activity = getActivity();
    if (activity != null) {
        activity.runOnUiThread(new Runnable() {
            @Override//from w w  w  .  j  a va 2  s .c om
            public void run() {
                try {
                    if (!mIsGalleryMode && mProgress == null) {
                        mProgress = ThetaDialogFragment.newInstance(getString(R.string.theta_ssid_prefix),
                                getString(R.string.loading));
                        mProgress.show(getActivity().getFragmentManager(), "fragment_dialog");
                    }
                    enableReconnectView();
                } catch (IllegalStateException e) { //Check background/foreground
                    return;
                }
            }
        });
    }
}

From source file:org.deviceconnect.android.deviceplugin.theta.fragment.ThetaGalleryFragment.java

/** Import data of Theta to storage of App. */
private void exeImportData(final int position) {
    Activity activity = getActivity();
    if (activity != null) {
        activity.runOnUiThread(new Runnable() {
            @Override/*from  w ww.  j av a  2  s .co m*/
            public void run() {
                try {
                    if (mProgress == null) {
                        mProgress = ThetaDialogFragment.newInstance(getString(R.string.theta_ssid_prefix),
                                getString(R.string.saving));
                        mProgress.show(getActivity().getFragmentManager(), "fragment_dialog");
                    }
                } catch (IllegalStateException e) { //background
                    if (BuildConfig.DEBUG) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
    new Thread(new Runnable() {
        @Override
        public void run() {
            ThetaObject importObj = mUpdateThetaList.get(position);
            mStorage.addThetaObjectCache(importObj);
        }
    }).start();
}

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

/**
 * Save a media in the downloads directory and offer to open it with a third party application.
 *
 * @param activity       the activity//w  ww . j  a  v  a 2  s.co m
 * @param savedMediaPath the media path
 * @param mimeType       the media mime type.
 */
public static void openMedia(final Activity activity, final String savedMediaPath, final String mimeType) {
    if ((null != activity) && (null != savedMediaPath)) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    File file = new File(savedMediaPath);
                    Intent intent = new Intent();
                    intent.setAction(android.content.Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(file), mimeType);
                    activity.startActivity(intent);
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(activity, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Log.d(LOG_TAG, "## openMedia(): Exception Msg=" + e.getMessage());
                }
            }
        });
    }
}

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

/**
 * Init the dialog view./*from  w w w. j a v  a  2s  .  c  o m*/
 * @param v the dialog view.
 */
void initView(View v) {
    mListView = ((ListView) v.findViewById(R.id.listView_contacts));

    // get the local contacts
    mLocalContacts = new ArrayList<Contact>(ContactsManager.getLocalContactsSnapshot(getActivity()));

    mAdapter = new ContactsListAdapter(getActivity(), R.layout.adapter_item_contact);

    // sort them
    Collections.sort(mLocalContacts, alphaComparator);

    mListView.setFastScrollAlwaysVisible(true);
    mListView.setFastScrollEnabled(true);
    mListView.setAdapter(mAdapter);

    refreshAdapter();

    // a button could be added to filter the contacts to display only the matrix users
    // but the lookup method is too slow (1 address / request).
    // it could be enabled when a batch request will be implemented
    /*
    final Button button = (Button)v.findViewById(R.id.button_matrix_users);
            
    button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        mDisplayOnlyMatrixUsers = !mDisplayOnlyMatrixUsers;
            
        if (mDisplayOnlyMatrixUsers) {
            button.setBackgroundResource(R.drawable.matrix_user);
        } else {
            button.setBackgroundResource(R.drawable.ic_menu_allfriends);
        }
            
        refreshAdapter();
    }
    });*/

    final EditText editText = (EditText) v.findViewById(R.id.editText_contactBox);

    editText.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(android.text.Editable s) {
            ContactsListDialogFragment.this.mSearchPattern = s.toString();
            refreshAdapter();
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    });

    // tap on one of them
    // if he is a matrix, offer to start a chat
    // it he is not a matrix user, offer to invite him by email or SMS.
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final Contact contact = mAdapter.getItem(position);
            final Activity activity = ContactsListDialogFragment.this.getActivity();

            if (contact.hasMatridIds(ContactsListDialogFragment.this.getActivity())) {
                final Contact.MXID mxid = contact.getFirstMatrixId();
                // The user is trying to leave with unsaved changes. Warn about that
                new AlertDialog.Builder(activity)
                        .setMessage(activity.getText(R.string.chat_with) + " " + mxid.mMatrixId + " ?")
                        .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                activity.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        CommonActivityUtils.goToOneToOneRoom(mxid.mAccountId, mxid.mMatrixId,
                                                activity, new SimpleApiCallback<Void>(getActivity()) {
                                                });
                                    }
                                });
                                dialog.dismiss();
                                // dismiss the member list
                                ContactsListDialogFragment.this.dismiss();

                            }
                        }).setNegativeButton("NO", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).create().show();

            } else {
                // invite the user
                final ArrayList<String> choicesList = new ArrayList<String>();

                if (AdapterUtils.canSendSms(activity)) {
                    choicesList.addAll(contact.mPhoneNumbers);
                }

                choicesList.addAll(contact.mEmails);

                // something to offer
                if (choicesList.size() > 0) {
                    final String[] labels = new String[choicesList.size()];

                    for (int index = 0; index < choicesList.size(); index++) {
                        labels[index] = choicesList.get(index);
                    }

                    new AlertDialog.Builder(activity).setItems(labels, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String value = labels[which];

                            // SMS ?
                            if (contact.mPhoneNumbers.indexOf(value) >= 0) {
                                AdapterUtils.launchSmsIntent(activity, value,
                                        activity.getString(R.string.invitation_message));
                            } else {
                                // emails
                                AdapterUtils.launchEmailIntent(activity, value,
                                        activity.getString(R.string.invitation_message));
                            }

                            // dismiss the member list
                            ContactsListDialogFragment.this.dismiss();
                        }
                    }).setTitle(activity.getString(R.string.invite_this_user_to_use_matrix)).show();
                }
            }
        }
    });
}

From source file:com.Yamate.Camera.Camera.java

/**
 * Shows a {@link Toast} on the UI thread.
 *
 * @param text The message to show//ww w .j  a  va2 s. c  o m
 */
private void showToast(final String text) {
    final Activity activity = mActivity;
    if (activity != null) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(activity, text, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

From source file:org.videolan.vlc.gui.browser.FileBrowserFragment.java

@Override
protected void browseRoot() {
    final Activity context = getActivity();
    mAdapter.updateMediaDirs();/*w w  w  .  j  a v a 2 s  .co  m*/
    new Thread(new Runnable() {
        @Override
        public void run() {
            String storages[] = AndroidDevices.getMediaDirectories();
            MediaWrapper directory;
            for (String mediaDirLocation : storages) {
                if (!(new File(mediaDirLocation).exists()))
                    continue;
                directory = new MediaWrapper(AndroidUtil.PathToUri(mediaDirLocation));
                directory.setType(MediaWrapper.TYPE_DIR);
                if (TextUtils.equals(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY, mediaDirLocation))
                    directory.setTitle(getString(R.string.internal_memory));
                mAdapter.addItem(directory, false, false);
            }
            mHandler.sendEmptyMessage(BrowserFragmentHandler.MSG_HIDE_LOADING);
            if (mReadyToDisplay) {
                context.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        updateEmptyView();
                        mAdapter.notifyDataSetChanged();
                        parseSubDirectories();
                    }
                });
            }
        }
    }).start();
}

From source file:org.messic.android.controllers.LoginController.java

public void login(final Activity context, final boolean remember, final String username, final String password,
        final ProgressDialog pd) throws Exception {
    Network.nukeNetwork();/*  w ww.  ja v  a  2 s  . c om*/

    final String baseURL = Configuration.getBaseUrl() + "/messiclogin";
    MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
    formData.add("j_username", username);
    formData.add("j_password", password);
    try {
        RestJSONClient.post(baseURL, formData, MDMLogin.class, new RestJSONClient.RestListener<MDMLogin>() {
            public void response(MDMLogin response) {
                MessicPreferences mp = new MessicPreferences(context);
                mp.setRemember(remember, username, password);
                Configuration.setToken(response.getMessic_token());

                pd.dismiss();
                Intent ssa = new Intent(context, BaseActivity.class);
                context.startActivity(ssa);
            }

            public void fail(Exception e) {
                Log.e("Login", e.getMessage(), e);
                context.runOnUiThread(new Runnable() {
                    public void run() {
                        pd.dismiss();
                        Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
                    }
                });

            }
        });
    } catch (Exception e) {
        pd.dismiss();
        Log.e("login", e.getMessage(), e);
        throw e;
    }
}

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

/**
 * Old Style of BlinkUp invocation./* w ww. jav a 2 s. c  om*/
 *
 * @deprecated use {@link #startBlinkUp()} instead.
 */
@Deprecated
private boolean invokeBlinkup(final Activity activity, final BlinkupController controller, JSONArray data) {
    int timeoutMs;
    try {
        mApiKey = data.getString(INVOKE_BLINKUP_ARG_API_KEY);
        mDeveloperPlanId = data.getString(INVOKE_BLINKUP_ARG_DEVELOPER_PLAN_ID);
        timeoutMs = data.getInt(INVOKE_BLINKUP_ARG_TIMEOUT_MS);
        mGeneratePlanId = data.getBoolean(INVOKE_BLINKUP_ARG_GENERATE_PLAN_ID);
    } 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;
    }

    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;
}