Example usage for android.os Handler obtainMessage

List of usage examples for android.os Handler obtainMessage

Introduction

In this page you can find the example usage for android.os Handler obtainMessage.

Prototype

public final Message obtainMessage(int what, Object obj) 

Source Link

Document

Same as #obtainMessage() , except that it also sets the what and obj members of the returned Message.

Usage

From source file:ch.teamuit.android.soundplusplus.LibraryPagerAdapter.java

/**
 * Set the sort mode for the current adapter. Current adapter must be a
 * MediaAdapter. Saves this sort mode to preferences and updates the list
 * associated with the adapter to display the new sort mode.
 *
 * @param mode The sort mode. See {@link MediaAdapter#setSortMode(int)} for
 * details./*from  w w  w  .j a v a  2  s .  c o  m*/
 */
public void setSortMode(int mode) {
    MediaAdapter adapter = (MediaAdapter) mCurrentAdapter;
    if (mode == adapter.getSortMode())
        return;

    adapter.setSortMode(mode);
    requestRequery(adapter);

    Handler handler = mWorkerHandler;
    handler.sendMessage(handler.obtainMessage(MSG_SAVE_SORT, adapter));
}

From source file:ch.blinkenlights.android.vanilla.LibraryPagerAdapter.java

/**
 * Call {@link LibraryPagerAdapter#requestRequery(LibraryAdapter)} on the UI
 * thread.//from w  w w  . j  a v  a  2  s.  c o  m
 *
 * @param adapter The adapter, passed to requestRequery.
 */
public void postRequestRequery(LibraryAdapter adapter) {
    Handler handler = mUiHandler;
    handler.sendMessage(handler.obtainMessage(MSG_REQUEST_REQUERY, adapter));
}

From source file:ch.blinkenlights.android.vanilla.LibraryPagerAdapter.java

/**
 * Set the sort mode for the current adapter. Current adapter must be a
 * MediaAdapter. Saves this sort mode to preferences and updates the list
 * associated with the adapter to display the new sort mode.
 *
 * @param mode The sort mode. See {@link MediaAdapter#setSortMode(int)} for
 * details./*  w  ww.  j av  a 2  s.  c  o m*/
 */
public void setSortMode(int mode) {
    SortableAdapter adapter = (SortableAdapter) mCurrentAdapter;
    if (mode == adapter.getSortMode())
        return;

    adapter.setSortMode(mode);
    requestRequery(mCurrentAdapter);

    Handler handler = mWorkerHandler;
    handler.sendMessage(handler.obtainMessage(MSG_SAVE_SORT, adapter));
}

From source file:ch.blinkenlights.android.vanilla.LibraryPagerAdapter.java

/**
 * Schedule a query to be run for the given adapter on the worker thread.
 *
 * @param adapter The adapter to run the query for.
 *///from  ww  w.  j a va 2  s .  c o  m
private void postRunQuery(LibraryAdapter adapter) {
    mRequeryNeeded[adapter.getMediaType()] = false;
    Handler handler = mWorkerHandler;
    handler.removeMessages(MSG_RUN_QUERY, adapter);
    handler.sendMessage(handler.obtainMessage(MSG_RUN_QUERY, adapter));
}

From source file:com.neudesic.mobile.pulse.ui.drawable.DrawableManager.java

public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {

    final Handler handler = new Handler() {
        @Override//from  w  w  w. ja  v  a  2s .  com
        public void handleMessage(Message message) {
            try {
                imageView.setImageDrawable((Drawable) message.obj);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };

    Thread thread = new Thread() {

        @Override
        public void run() {
            // TODO : set imageView to a "pending" image
            try {
                Drawable drawable;
                if (drawableMap.containsKey(urlString)) {
                    // imageView.setImageDrawable(drawableMap.get(urlString));
                    drawable = drawableMap.get(urlString);

                } else {

                    drawable = fetchDrawable(urlString);
                }
                Message message = handler.obtainMessage(1, drawable);
                handler.sendMessage(message);
            } catch (Exception e) {
                Log.w(getClass().getPackage().getName(), "Unable to fetch drawable, using default instead", e);
                Message message = handler.obtainMessage(1, emptyAvatar);
                handler.sendMessage(message);
            }
        }
    };
    thread.start();
}

From source file:com.cm.beer.util.DrawableManager.java

/**
 * //from   ww w . j a v  a2  s .co m
 * @param urlString
 * @param imageView
 */
public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
    if (mDrawableCache.containsKey(urlString)) {
        if (Logger.isLogEnabled())
            Logger.log("Returning Drawable from Cache:" + urlString);
        SoftReference<Drawable> softReference = mDrawableCache.get(urlString);
        if ((softReference == null) || (softReference.get() == null)) {
            mDrawableCache.remove(urlString);
            if (Logger.isLogEnabled())
                Logger.log("fetchDrawableOnThread():Soft Reference has been Garbage Collected:" + urlString);
        } else {
            imageView.setImageDrawable(softReference.get());
            return;
        }

    }

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            imageView.setImageDrawable((Drawable) message.obj);
        }
    };

    Thread thread = new Thread() {
        @Override
        public void run() {
            while (mLockCache.containsKey(urlString)) {
                if (Logger.isLogEnabled())
                    Logger.log("URI download request in progress:" + urlString);
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    Log.e(this.getClass().getName(), e.getMessage());
                }
            }
            Drawable drawable = fetchDrawable(urlString);
            Message message = handler.obtainMessage(1, drawable);
            handler.sendMessage(message);
        }
    };
    thread.start();
}

From source file:com.nextgis.ngm_clink_monitoring.fragments.CreateObjectFragment.java

protected void createObject() {
    String fieldName = null;//  w  ww . jav a 2 s .  c  o  m

    switch (mFoclStructLayerType) {
    case FoclConstants.LAYERTYPE_FOCL_REAL_OPTICAL_CABLE_POINT:
        if (null == mLayingMethod.getValue()) {
            fieldName = getActivity().getString(R.string.laying_method);
        }
        break;

    case FoclConstants.LAYERTYPE_FOCL_REAL_FOSC:
        //                if (null == mFoscType.getValue()) {
        //                    fieldName = getActivity().getString(R.string.fosc_type);
        //                    break;
        //                }
        if (null == mFoscPlacement.getValue()) {
            fieldName = getActivity().getString(R.string.fosc_placement);
        }
        break;

    case FoclConstants.LAYERTYPE_FOCL_REAL_OPTICAL_CROSS:
        //                if (null == mOpticalCrossType.getValue()) {
        //                    fieldName = getActivity().getString(R.string.optical_cross_type);
        //                }
        break;

    case FoclConstants.LAYERTYPE_FOCL_REAL_ACCESS_POINT:
        break;

    case FoclConstants.LAYERTYPE_FOCL_REAL_SPECIAL_TRANSITION_POINT:
        if (null == mSpecialLayingMethod.getValue()) {
            fieldName = getActivity().getString(R.string.special_laying_method);
            break;
        }
        if (null == mMarkType.getValue()) {
            fieldName = getActivity().getString(R.string.mark_type);
        }
        break;
    }

    if (null != fieldName) {
        YesNoDialog dialog = new YesNoDialog();
        dialog.setKeepInstance(true).setIcon(R.drawable.ic_action_warning).setTitle(R.string.warning)
                .setMessage(String.format(getString(R.string.empty_field_warning), fieldName))
                .setPositiveText(R.string.ok)
                .setOnPositiveClickedListener(new YesNoDialog.OnPositiveClickedListener() {
                    @Override
                    public void onPositiveClicked() {
                        // cancel
                    }
                }).show(getActivity().getSupportFragmentManager(),
                        FoclConstants.FRAGMENT_YES_NO_DIALOG + "FieldsNotNull");
        return; // we do not need logcat here
    }

    if (!mNewStartPoint && 0 < mObjectCount
            && FoclConstants.LAYERTYPE_FOCL_REAL_OPTICAL_CABLE_POINT == mFoclStructLayerType
            && null != mDistance && FoclConstants.MAX_DISTANCE_FROM_PREV_POINT < mDistance) {

        showDistanceExceededDialog();
        return; // we do not need logcat here
    }

    if (null == mObjectPhotoFileAdapter || mObjectPhotoFileAdapter.getItemCount() == 0) {
        YesNoDialog dialog = new YesNoDialog();
        dialog.setKeepInstance(true).setIcon(R.drawable.ic_action_warning).setTitle(R.string.warning)
                .setMessage(getString(R.string.take_photos_to_confirm)).setPositiveText(R.string.ok)
                .setOnPositiveClickedListener(new YesNoDialog.OnPositiveClickedListener() {
                    @Override
                    public void onPositiveClicked() {
                        // cancel
                    }
                }).show(getActivity().getSupportFragmentManager(),
                        FoclConstants.FRAGMENT_YES_NO_DIALOG + "TakePhotos");
        return; // we do not need logcat here
    }

    LayoutInflater inflater = LayoutInflater.from(getActivity());
    View view = inflater.inflate(R.layout.dialog_waiting, null);

    final YesNoDialog waitProgressDialog = new YesNoDialog();
    waitProgressDialog.setKeepInstance(true).setIcon(R.drawable.ic_action_data_usage).setTitle(R.string.waiting)
            .setView(view, true);

    waitProgressDialog.setCancelable(false);
    waitProgressDialog.show(getActivity().getSupportFragmentManager(),
            FoclConstants.FRAGMENT_YES_NO_DIALOG + "WaitProgress");

    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case CREATE_OBJECT_DONE:
                waitProgressDialog.dismiss();
                break;
            case CREATE_OBJECT_OK:
                getActivity().getSupportFragmentManager().popBackStack();
                break;
            case CREATE_OBJECT_FAILED:
                Toast.makeText(getActivity(), (String) msg.obj, Toast.LENGTH_LONG).show();
                waitProgressDialog.dismiss();
                break;
            }
        }
    };

    RunnableFuture<Void> future = new FutureTask<Void>(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            createObjectTask();
            return null;
        }
    }) {
        @Override
        protected void done() {
            super.done();
            handler.sendEmptyMessage(CREATE_OBJECT_DONE);
        }

        @Override
        protected void set(Void aVoid) {
            super.set(aVoid);
            handler.sendEmptyMessage(CREATE_OBJECT_OK);
        }

        @Override
        protected void setException(Throwable t) {
            super.setException(t);
            Message msg = handler.obtainMessage(CREATE_OBJECT_FAILED, t.getLocalizedMessage());
            msg.sendToTarget();
        }
    };

    new Thread(future).start();
}