Example usage for android.os Message sendToTarget

List of usage examples for android.os Message sendToTarget

Introduction

In this page you can find the example usage for android.os Message sendToTarget.

Prototype

public void sendToTarget() 

Source Link

Document

Sends this Message to the Handler specified by #getTarget .

Usage

From source file:com.sabdroidex.controllers.couchpotato.CouchPotatoController.java

/**
 * Add movie to couchpotato/*from  w  w w  . j a v  a 2s  .c  om*/
 * 
 * @param messageHandler Handler
 * @param profile CouchPotato profile
 * @param idIMDb IMDB-ID
 * @param movieTitle Title of movie to add
 */
public static void addMovie(final Handler messageHandler, final String profile, final String idIMDb,
        final String movieTitle) {
    if (executingCommand || !Preferences.isSet(Preferences.COUCHPOTATO_URL)) {
        return;
    }

    Thread thread = new Thread() {

        @Override
        public void run() {
            try {
                String result = makeApiCall(MESSAGE.MOVIE_ADD.toString().toLowerCase(), "profile_id=" + profile,
                        "identifier=" + idIMDb, "title=" + movieTitle);

                JSONObject jsonObject = new JSONObject(result);
                result = (String) jsonObject.get("added");

                Message message = new Message();
                message.setTarget(messageHandler);
                message.what = MESSAGE.MOVIE_ADD.hashCode();
                message.obj = result;
                message.sendToTarget();

                Thread.sleep(500);
                CouchPotatoController.refreshMovies(messageHandler, "active,done");

            } catch (IOException e) {
                Log.w(TAG, " " + e.getLocalizedMessage());
                sendUpdateMessageStatus(messageHandler, "Error");
            } catch (Throwable e) {
                Log.w(TAG, " " + e.getLocalizedMessage());
            } finally {
                executingCommand = false;
                sendUpdateMessageStatus(messageHandler, "");
            }
        }
    };
    executingCommand = true;
    thread.start();
}

From source file:com.sabdroidex.controllers.couchpotato.CouchPotatoController.java

/**
 * Retrieve possible download profiles from CouchPotat
 * //  w ww  .  j a va 2  s .c  om
 * @param messageHandler Handler
 */
public synchronized static void getProfiles(final Handler messageHandler) {
    if (executingCommand || !Preferences.isSet(Preferences.COUCHPOTATO_URL)) {
        return;
    }

    Thread thread = new Thread() {

        @Override
        public void run() {
            try {
                String result = makeApiCall(MESSAGE.PROFILE_LIST.toString().toLowerCase());

                JSONObject jsonObject = new JSONObject(result);

                if (jsonObject.getBoolean("success") && profiles == null) {
                    profiles = new HashMap<Integer, String>();
                    JSONArray profileList = jsonObject.getJSONArray("list");
                    for (int i = 0; i < profileList.length(); i++) {
                        profiles.put(profileList.getJSONObject(i).getInt("id"),
                                profileList.getJSONObject(i).getString("label"));
                    }
                }

                Message message = new Message();
                message.setTarget(messageHandler);
                message.what = MESSAGE.PROFILE_LIST.hashCode();
                message.obj = profiles;
                message.sendToTarget();
            } catch (IOException e) {
                Log.w(TAG, " " + e.getLocalizedMessage());
            } catch (Throwable e) {
                Log.w(TAG, " " + e.getLocalizedMessage());
            } finally {
                executingCommand = false;
            }
        }
    };

    executingCommand = true;
    sendUpdateMessageStatus(messageHandler, MESSAGE.MOVIE_DELETE.toString());
    thread.start();
}

From source file:com.sabdroidex.controllers.couchpotato.CouchPotatoController.java

/**
 * Search for a movie based on title/*  w  w w .j a v  a 2 s .c  om*/
 * 
 * @param messageHandler Handler
 * @param searchTitle Movie title to search for
 */
public static void searchMovie(final Handler messageHandler, final String searchTitle) {
    if (!Preferences.isSet(Preferences.COUCHPOTATO_URL)) {
        return;
    }

    Thread thread = new Thread() {

        @Override
        public void run() {

            try {

                String result = makeApiCall(MESSAGE.MOVIE_SEARCH.toString().toLowerCase(), "q=" + searchTitle);
                JSONObject jsonObject = new JSONObject(result);
                MovieSearch movieList = null;

                if (!jsonObject.isNull("message") && !"".equals(jsonObject.getString("message"))) {
                    sendUpdateMessageStatus(messageHandler, "CouchPotato : " + jsonObject.getString("message"));
                } else {
                    SimpleJSONMarshaller jsonMarshaller = new SimpleJSONMarshaller(MovieSearch.class);
                    movieList = (MovieSearch) jsonMarshaller.unMarshal(jsonObject);
                }

                Message message = new Message();
                message.setTarget(messageHandler);
                message.what = MESSAGE.MOVIE_SEARCH.hashCode();
                message.obj = movieList;
                message.sendToTarget();
            } catch (IOException e) {
                Log.w(TAG, " " + e.getLocalizedMessage());
            } catch (Throwable e) {
                Log.w(TAG, " " + e.getLocalizedMessage());
            } finally {
                executingCommand = false;
            }
        }
    };
    executingRefreshMovies = true;
    executingCommand = true;
    thread.start();
}

From source file:com.sabdroidex.controllers.couchpotato.CouchPotatoController.java

/**
 * This download the release information for a specific movie.
 *
 * @param messageHandler The message handler that will receive the result.
 * @param movieId The movie Id to get the information for.
 *//*  ww  w  . java2 s .  c  o  m*/
public static void getReleasesForMovie(final Handler messageHandler, final int movieId) {
    if (!Preferences.isSet(Preferences.COUCHPOTATO_URL)) {
        return;
    }

    Thread thread = new Thread() {

        @Override
        public void run() {

            try {
                String result = makeApiCall(MESSAGE.RELEASE_FOR_MOVIE.toString().toLowerCase(),
                        "id=" + movieId);
                JSONObject jsonObject = new JSONObject(result);
                MovieReleases movieReleases = null;

                if (!jsonObject.isNull("message") && !"".equals(jsonObject.getString("message"))) {
                    sendUpdateMessageStatus(messageHandler, "CouchPotato : " + jsonObject.getString("message"));
                } else {
                    SimpleJSONMarshaller jsonMarshaller = new SimpleJSONMarshaller(MovieReleases.class);
                    movieReleases = (MovieReleases) jsonMarshaller.unMarshal(jsonObject);
                }

                Message message = new Message();
                message.setTarget(messageHandler);
                message.what = MESSAGE.RELEASE_FOR_MOVIE.hashCode();
                message.obj = movieReleases;
                message.sendToTarget();
            } catch (RuntimeException e) {
                Log.w(TAG, " " + e.getLocalizedMessage());
            } catch (Throwable e) {
                Log.w(TAG, " " + e.getLocalizedMessage());
            }
        }
    };

    thread.start();
}

From source file:com.sabdroidex.controllers.couchpotato.CouchPotatoController.java

/**
 * Retrieve possible download profiles from CouchPotato
 * /*from   ww w. j av a  2  s  .  c  o m*/
 * @param messageHandler Handler
 */
public synchronized static void getStatusList(final Handler messageHandler) {
    if (executingCommand || "".equals(Preferences.get(Preferences.COUCHPOTATO_URL))) {
        return;
    }

    Thread thread = new Thread() {

        @Override
        public void run() {
            try {
                String result = makeApiCall(MESSAGE.STATUS_LIST.toString().toLowerCase());

                JSONObject jsonObject = new JSONObject(result);

                if (jsonObject.getBoolean("success")) {
                    JSONArray profileList = jsonObject.getJSONArray("list");
                    status = new HashMap<Integer, String>();
                    for (int i = 0; i < profileList.length(); i++) {
                        status.put(profileList.getJSONObject(i).getInt("id"),
                                profileList.getJSONObject(i).getString("label"));
                    }

                    Message message = new Message();
                    message.setTarget(messageHandler);
                    message.what = MESSAGE.PROFILE_LIST.hashCode();
                    message.obj = status;
                    message.sendToTarget();
                }
            } catch (IOException e) {
                Log.w(TAG, " " + e.getLocalizedMessage());
            } catch (Throwable e) {
                Log.w(TAG, " " + e.getLocalizedMessage());
            } finally {
                executingCommand = false;
            }
        }
    };
    executingCommand = true;
    thread.start();
}

From source file:com.sabdroidex.controllers.couchpotato.CouchPotatoController.java

/**
 * Download a release of a movie/*from   ww w  . ja v  a2s.c  o  m*/
 * 
 * @param messageHandler Handler
 * @param releaseId ID of release to download
 */
public static void downloadRelease(final Handler messageHandler, final int releaseId) {
    if (!Preferences.isSet(Preferences.COUCHPOTATO_URL)) {
        return;
    }

    Thread thread = new Thread() {

        @Override
        public void run() {

            try {
                String result = makeApiCall(MESSAGE.RELEASE_DOWNLOAD.toString().toLowerCase(),
                        "id=" + releaseId);
                JSONObject jsonObject = new JSONObject(result);

                Message message = new Message();
                message.setTarget(messageHandler);
                message.what = MESSAGE.RELEASE_DOWNLOAD.hashCode();
                message.obj = jsonObject.getBoolean("success");
                message.sendToTarget();
            } catch (IOException e) {
                Log.w(TAG, " " + e.getLocalizedMessage());
            } catch (Throwable e) {
                Log.w(TAG, " " + e.getLocalizedMessage());
            }
        }
    };
    thread.start();
}

From source file:com.sabdroidex.controllers.couchpotato.CouchPotatoController.java

/**
 * This function refreshes the elements from movies.
 * //from  w  w w . jav  a 2  s  . c  o m
 * @param messageHandler The message handler that will receive the result
 * @param status The status we want to fetch
 */
public static void refreshMovies(final Handler messageHandler, final String status) {
    if (executingRefreshMovies || !Preferences.isSet(Preferences.COUCHPOTATO_URL)) {
        return;
    }
    if (status == null) {
        getStatusList();
    }
    if (profiles == null) {
        getProfiles();
    }

    Thread thread = new Thread() {

        @Override
        public void run() {
            try {

                String result = makeApiCall(MESSAGE.MOVIE_LIST.toString().toLowerCase(), "status=" + status);
                JSONObject jsonObject = new JSONObject(result);
                MovieList movieList;

                if (!jsonObject.isNull("message") && !"".equals(jsonObject.getString("message"))) {
                    sendUpdateMessageStatus(messageHandler, "SickBeard : " + jsonObject.getString("message"));
                } else {
                    SimpleJSONMarshaller jsonMarshaller = new SimpleJSONMarshaller(MovieList.class);
                    movieList = (MovieList) jsonMarshaller.unMarshal(jsonObject);

                    Message message = new Message();
                    message.setTarget(messageHandler);
                    message.what = MESSAGE.MOVIE_LIST.hashCode();
                    message.obj = movieList;
                    message.sendToTarget();
                }
            } catch (IOException e) {
                Log.w(TAG, " " + e.getMessage());
            } catch (Throwable e) {
                Log.w(TAG, " " + e.getMessage());
            } finally {
                executingRefreshMovies = false;
                executingCommand = false;
            }
        }
    };
    executingCommand = true;
    thread.start();
}

From source file:me.willowcheng.makerthings.util.MjpegStreamer.java

public void getFrame() {
    Bitmap mBitmap;/* w w w.ja va2s .  co m*/
    try {
        mBitmap = mInputStream.readMjpegFrame();
        Message m = mHandler.obtainMessage(mId, mBitmap);
        m.sendToTarget();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.onebus.zxing.decoding.CaptureActivityHandler.java

public void quitSynchronously() {
    state = State.DONE;//from ww  w. j  a  v  a  2s . co m
    CameraManager.get().stopPreview();
    Message quit = Message.obtain(decodeThread.getHandler(), R.id.quit);
    quit.sendToTarget();
    try {
        decodeThread.join();
    } catch (InterruptedException e) {
        // continue
    }

    // Be absolutely sure we don't send any queued up messages
    removeMessages(R.id.decode_succeeded);
    removeMessages(R.id.decode_failed);
}

From source file:curt.android.book.NetworkWorker.java

public void run() {
    try {//w  ww.  ja  va  2 s  .c o  m
        // These return a JSON result which describes if and where the query was found. This API may
        // break or disappear at any time in the future. Since this is an API call rather than a
        // website, we don't use LocaleManager to change the TLD.
        String uri;
        if (LocaleManager.isBookSearchUrl(isbn)) {
            int equals = isbn.indexOf('=');
            String volumeId = isbn.substring(equals + 1);
            uri = "http://www.google.com/books?id=" + volumeId + "&jscmd=SearchWithinVolume2&q=" + query;
        } else {
            uri = "http://www.google.com/books?vid=isbn" + isbn + "&jscmd=SearchWithinVolume2&q=" + query;
        }

        try {
            String content = HttpHelper.downloadViaHttp(uri, HttpHelper.ContentType.JSON);
            JSONObject json = new JSONObject(content);
            Message message = Message.obtain(handler, R.id.search_book_contents_succeeded);
            message.obj = json;
            message.sendToTarget();
        } catch (IOException ioe) {
            Message message = Message.obtain(handler, R.id.search_book_contents_failed);
            message.sendToTarget();
        }
    } catch (JSONException je) {
        Log.w(TAG, "Error accessing book search", je);
        Message message = Message.obtain(handler, R.id.search_book_contents_failed);
        message.sendToTarget();
    }
}