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:ca.ualberta.cmput301.t03.trading.toptraders.TopTradersFragment.java

/**
 * {@inheritDoc}//from   w  w w .  j av a  2 s  .com
 */
@Override
public void onRefresh() {
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                topTraders = refreshController.getTopTraders(TOP_TRADERS_COUNT);
                refreshTopTradersUI();
            } catch (IOException e) {
                Activity activity = getActivity();
                if (activity != null) {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ExceptionUtils.toastShort(getString(R.string.top_trader_offline_message));
                        }
                    });
                }

            }
        }
    });

    thread.start();
}

From source file:org.n52.geoar.newdata.PluginDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // get parameters
    Bundle args = getArguments();// w  w  w.  j a v a 2s.c o  m
    final PluginHolder plugin = args.getParcelable("plugin");

    // inflate layout
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.fragment_plugin_dialog, null);

    final ImageView imageView = (ImageView) view.findViewById(R.id.imageView);

    ((TextView) view.findViewById(R.id.textViewName)).setText(plugin.getName());
    ((TextView) view.findViewById(R.id.textViewPublisher)).setText(
            plugin.getPublisher() != null ? plugin.getPublisher() : getString(R.string.unknown_publisher));
    ((TextView) view.findViewById(R.id.textViewVersion))
            .setText(plugin.getVersion() != null ? "" + plugin.getVersion() : getString(R.string.no_value));

    ((TextView) view.findViewById(R.id.textViewDescription))
            .setText(plugin.getDescription() != null ? plugin.getDescription() : getString(R.string.no_value));

    TextView textViewDataSources = (TextView) view.findViewById(R.id.textViewDataSources);
    if (plugin instanceof InstalledPluginHolder) {

        String dsText = "";
        for (DataSourceHolder dataSource : ((InstalledPluginHolder) plugin).getDataSources()) {
            if (!dsText.isEmpty())
                dsText += "\n";
            dsText += dataSource.getName();
        }
        textViewDataSources.setText(dsText);
    } else {
        textViewDataSources.setText(R.string.no_value);
    }

    // dialogButton.setAnimation(getActivity().findViewById(android.R.drawable.stat_sys_download));
    Dialog dsDialog = new AlertDialog.Builder(getActivity()).setTitle(plugin.getName())
            .setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (plugin instanceof InstalledPluginHolder) {
                        ((InstalledPluginHolder) plugin).setChecked(true);
                        IntroController.finishTaskIfActive(R.string.intro_task_2);
                        IntroController.notify(R.string.intro_desc_3_1);
                    } else if (plugin instanceof PluginDownloadHolder) {
                        PluginDownloader.downloadPlugin((PluginDownloadHolder) plugin);
                        IntroController.finishTaskIfActive(R.string.intro_task_1);
                        IntroController.notify(R.string.intro_desc_2_1);
                    }
                }
            }).setNegativeButton(R.string.cancel, null).setView(view).create();

    // Asynchronously load and display plugin icon
    Thread imageThread = new Thread(new Runnable() {
        @Override
        public void run() {
            final Bitmap pluginIcon = plugin.getPluginIcon();
            Activity activity = getActivity();
            if (activity != null) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (pluginIcon != null)
                            imageView.setImageBitmap(pluginIcon);
                    }
                });
            }
        }
    });
    imageThread.start();

    return dsDialog;
}

From source file:com.pdmanager.views.SensorsFragment.java

private void displayData(final ISensorData data) {
    Activity activity = getActivity();

    if (activity != null) {

        activity.runOnUiThread(new Runnable() {
            @Override//from   w w w .  j av  a  2 s .c o  m
            public void run() {

                try {
                    if (data.getDataType() == DataTypes.HR) {
                        HRReading hr = ((HRData) data).getValue();
                        if (mTextHeartRate != null && mTextHeartRateQuality != null) {
                            mTextHeartRate.setText(Integer.toString(hr.getHR()));
                            int quality = hr.getQuality();
                            if (quality == 1) {
                                mTextHeartRateQuality.setText("Tight");
                            } else {
                                mTextHeartRateQuality.setText("Loose");
                            }
                        }
                    } else if (data.getDataType() == DataTypes.PEDOMETER) {

                        if (mTextTotalSteps != null) {
                            mTextTotalSteps.setText(data.getDisplay() + " steps");
                        }
                    } else if (data.getDataType() == DataTypes.ACTIVITY) {

                        if (mTextActivity != null)
                            mTextActivity.setText(data.getDisplay());

                    } else if (data.getDataType() == DataTypes.TREMOR) {

                        if (mTextTremor != null) {
                            mTextTremor.setText(data.getDisplay());
                        }
                    } else if (data.getDataType() == DataTypes.LID) {

                        if (mTextLID != null) {
                            mTextLID.setText(data.getDisplay());
                        }
                    }

                } catch (Exception ex) {

                }

            }
        });
    }

}

From source file:org.gateshipone.malp.application.fragments.serverfragments.ServerStatisticFragment.java

private synchronized void showDatabaseUpdating(final boolean show) {
    Activity activity = getActivity();
    if (null == activity) {
        return;//from  w  ww  . j  a v  a 2s  .c  o  m
    }
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (show) {
                mDBUpdating.setVisibility(View.VISIBLE);
            } else {
                mDBUpdating.setVisibility(View.GONE);
            }
        }
    });

}

From source file:org.dbasu.robomvvm.componentmodel.ActionManager.java

void invokeAction(final Object targetObject, String actionName, final EventArg eventArg) {

    Class targetType = targetObject.getClass();
    final Class<? extends EventArg> eventType = eventArg != null ? eventArg.getClass() : null;

    final ActionDescriptor handlerDescription = getActionDescriptor(targetType, actionName);

    if (handlerDescription == null) {
        throw new RuntimeException(
                "No Action By Name " + actionName + " Found In Class " + targetType.getName());
    }// w  w  w  .ja  va2  s.  c o m

    final Collection<Method> relevantMethods = Collections2.filter(handlerDescription.methods,
            new Predicate<Method>() {
                @Override
                public boolean apply(Method m) {

                    Class[] params = m.getParameterTypes();

                    return params.length == 0 || (eventType != null && params.length == 1
                            && ClassUtils.isAssignable(eventType, params[0], true));

                }
            });

    if (relevantMethods.size() == 0) {

        throw new RuntimeException("Action By Name " + actionName + " Does Not Support Actions Of Type "
                + ((eventType == null) ? " {null} " : eventType.getName()));
    }

    if (targetObject instanceof View) {
        Activity activity = (Activity) ((View) targetObject).getContext();

        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                reallyInvokeAction(relevantMethods, targetObject, eventArg);
            }
        });
    } else {
        reallyInvokeAction(relevantMethods, targetObject, eventArg);
    }
}

From source file:com.normalexception.app.rx8club.html.VBForumFactory.java

/**
 * Convenience method of displaying error to user and logging
 * the exception/*from   w w  w .j a v  a 2s .c  o  m*/
 * @param src   The source activity
 * @param msg   The message to post
 * @param e      The exception to log
 */
private void notifyError(Activity src, final String msg, Exception e) {
    Log.e(TAG, "Error In VBForumFactory", e);
    src.runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(MainApplication.getAppContext(), msg, Toast.LENGTH_SHORT).show();
        }
    });
}

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

public void getQueueSongs(final SongAdapter adapter, final Activity activity, final PlayQueueFragment rf,
        boolean refresh, final SwipeRefreshLayout srl, MessicPlayerService mps) {
    List<MDMSong> queue = mps.getPlayer().getQueue();

    adapter.clear();/*from   w w  w. j  a v a 2s .  c om*/
    activity.runOnUiThread(new Runnable() {
        public void run() {
            adapter.notifyDataSetChanged();
        }
    });

    for (int i = 0; i < queue.size(); i++) {
        MDMSong song = queue.get(i);
        adapter.addSong(song);
    }
    rf.eventRandomInfoLoaded();
    activity.runOnUiThread(new Runnable() {
        public void run() {
            adapter.notifyDataSetChanged();
        }
    });

    if (srl != null)
        srl.setRefreshing(false);

}

From source file:org.telegram.ui.LoginActivity.java

public void ShowAlertDialog(final Activity activity, final String message) {
    activity.runOnUiThread(new Runnable() {
        @Override//from   ww  w  . j  a va 2s.c  o m
        public void run() {
            if (!isFinishing()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                builder.setTitle(LoginActivity.this.getString(R.string.AppName));
                builder.setMessage(message);
                builder.setPositiveButton(ApplicationLoader.applicationContext.getString(R.string.OK), null);
                builder.show().setCanceledOnTouchOutside(true);
            }
        }
    });
}

From source file:com.todoroo.astrid.gtasks.GtasksListFragment.java

private void clearCompletedTasks() {
    final ProgressDialog pd = new ProgressDialog(getActivity());
    final TodorooCursor<Task> tasks = taskService.fetchFiltered(filter.getSqlQuery(), null, Task.ID,
            Task.COMPLETION_DATE);/*from  w  w  w.java  2  s  . c  o  m*/
    pd.setMessage(this.getString(R.string.gtasks_GTA_clearing));
    pd.show();

    new Thread() {
        @Override
        public void run() {
            String listId = null;
            try {
                for (tasks.moveToFirst(); !tasks.isAfterLast(); tasks.moveToNext()) {
                    Task t = new Task(tasks);
                    if (t.isCompleted()) {
                        if (listId == null) {
                            listId = gtasksMetadataService.getTaskMetadata(t.getId())
                                    .getValue(GtasksMetadata.LIST_ID);
                        }
                        t.setValue(Task.DELETION_DATE, DateUtilities.now());
                        taskService.save(t);
                    }
                }
            } finally {
                tasks.close();
                DialogUtilities.dismissDialog(getActivity(), pd);
            }
            if (listId != null) {
                gtasksTaskListUpdater.correctMetadataForList(listId);
            }
            Activity activity = getActivity();
            if (activity != null) {
                activity.runOnUiThread(new Runnable() {
                    public void run() {
                        loadTaskListContent(true);
                    }
                });
            }
        }
    }.start();
}

From source file:org.dbasu.robomvvm.componentmodel.PropertyManager.java

void setProperty(final Object targetObject, String name, final Object value) {

    Class targetType = targetObject.getClass();

    final PropertyDescriptor desc = getPropertyDescriptor(targetType, name);
    if (desc == null || desc.setters.size() == 0) {
        throw new RuntimeException(
                "No Settable Property By Name " + name + " Found In Class " + targetType.getName());
    }//w  w  w . ja v  a  2  s  .c o  m

    final Class valueClass = value.getClass();

    final Method setter = Iterables.find(desc.setters, new Predicate<Method>() {
        @Override
        public boolean apply(Method m) {
            return ClassUtils.isAssignable(valueClass, m.getParameterTypes()[0], true);
        }
    }, null);

    if (setter == null) {
        throw new RuntimeException("Type Mismatch: Can Not Assign Value Of Type " + valueClass.getName()
                + " To Property " + name + " In Class " + targetType.getName());
    }

    if (targetObject instanceof View) {

        Activity activity = (Activity) ((View) targetObject).getContext();

        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                reallySetProperty(setter, targetObject, value);
            }
        });
    } else {
        reallySetProperty(setter, targetObject, value);
    }
}