Example usage for android.os Message setTarget

List of usage examples for android.os Message setTarget

Introduction

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

Prototype

public void setTarget(Handler target) 

Source Link

Usage

From source file:info.unyttig.helladroid.newzbin.NewzBinController.java

/**
 * Send back a update status message to the message handler.
 * This is used for short messages to the user, like "Search had no result" etc.
 * /*from   w ww .  j  a  v  a  2s . c om*/
 * @param messageHandler
 * @param txt
 */
private static void sendUserMsg(final Handler messageHandler, final String txt) {
    Message msg = new Message();
    msg.setTarget(messageHandler);
    msg.what = MSG_NOTIFY_USER_ERROR;
    msg.obj = txt;
    msg.sendToTarget();
}

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

/**
 * Sends a message to the calling {@link Activity} to update it's status bar
 * //from w ww  . j  a va  2  s . com
 * @param messageHandler The message handler to be notified
 * @param text The text to write in the message
 */
private static void sendUpdateMessageStatus(Handler messageHandler, String text) {

    Message message = new Message();
    message.setTarget(messageHandler);
    message.what = MESSAGE.UPDATE.hashCode();
    message.obj = text;
    message.sendToTarget();
}

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

/**
 * Download a release of a movie/*from ww w  . j av  a 2 s  .  com*/
 * 
 * @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 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.
 *//*from w w  w. j  a v a 2s .  c om*/
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

/**
 * Add movie to couchpotato//from  w  ww.  j a  v a2s  .  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

/**
 * Search for a movie based on title/*w  ww .  j a va2  s  .  c o m*/
 * 
 * @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

/**
 * Retrieve possible download profiles from CouchPotat
 * //from  w  w  w. j a  va  2s .  com
 * @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

/**
 * Retrieve possible download profiles from CouchPotato
 * //from  w  w  w  .  jav a 2  s.  c  om
 * @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

/**
 * This function refreshes the elements from movies.
 * /*from w w  w .  j  a  v 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:com.blogspot.holbohistorier.readonfree.BookView.java

@Override
public void onActivityCreated(Bundle saved) {
    super.onActivityCreated(saved);
    view = (WebView) getView().findViewById(R.id.Viewport);

    // enable JavaScript for cool things to happen!
    view.getSettings().setJavaScriptEnabled(true);

    // ----- SWIPE PAGE
    view.setOnTouchListener(new OnTouchListener() {
        @Override// ww  w  . j av  a 2s.com
        public boolean onTouch(View v, MotionEvent event) {

            if (state == ViewStateEnum.books)
                swipePage(v, event, 0);

            WebView view = (WebView) v;
            return view.onTouchEvent(event);
        }
    });

    // ----- NOTE & LINK
    view.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Message msg = new Message();
            msg.setTarget(new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    String url = msg.getData().getString(getString(R.string.url));
                    if (url != null)
                        navigator.setNote(url, index);
                }
            });
            view.requestFocusNodeHref(msg);

            return false;
        }
    });

    view.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            try {
                navigator.setBookPage(url, index);
            } catch (Exception e) {
                errorMessage(getString(R.string.error_LoadPage));
            }
            return true;
        }
    });

    loadPage(viewedPage);
}