Example usage for android.os Handler sendEmptyMessage

List of usage examples for android.os Handler sendEmptyMessage

Introduction

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

Prototype

public final boolean sendEmptyMessage(int what) 

Source Link

Document

Sends a Message containing only the what value.

Usage

From source file:Main.java

public static void sendMessage(Handler handler, int what) {
    handler.sendEmptyMessage(what);
}

From source file:Main.java

public static void clearAppCache(final Context context, final Handler handler) {
    new Thread(new Runnable() {

        @Override//  ww w.j a  v  a 2s. co m
        public void run() {
            File cacheDir = new File(context.getCacheDir().getPath(), DEFAULT_CACHE_DIR);
            clearCacheFolder(cacheDir, System.currentTimeMillis());
            handler.sendEmptyMessage(1);
        }
    }).start();

}

From source file:com.thoughtmetric.tl.TLLib.java

public static boolean login(String login, String pw, Handler handler, Context context) throws IOException {
    handler.sendEmptyMessage(TLHandler.PROGRESS_LOGIN);

    // Fetch the token
    HtmlCleaner cleaner = TLLib.buildDefaultHtmlCleaner();
    URL url = new URL(LOGIN_URL);
    TagNode node = TLLib.TagNodeFromURLLoginToken(cleaner, url, handler, context);
    String token = null;/*w w w.j a va2 s.co m*/
    try {
        TagNode result = (TagNode) node.evaluateXPath("//input")[0];
        token = result.getAttributeByName("value");
    } catch (XPatherException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    if (token == null) {
        return false;
    }
    // 
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httpost = new HttpPost(LOGIN_URL);

    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair(USER_FIELD, login));
    nvps.add(new BasicNameValuePair(PASS_FIELD, pw));
    nvps.add(new BasicNameValuePair(REMEMBERME, "1"));
    nvps.add(new BasicNameValuePair("stage", "1"));
    nvps.add(new BasicNameValuePair("back_url", ""));
    nvps.add(new BasicNameValuePair("token", token));
    Log.d("token:", token);

    try {
        httpost.setEntity(new UrlEncodedFormEntity(nvps));
        HttpResponse response = httpclient.execute(httpost);
        HttpEntity entity = response.getEntity();

        Header[] headers = response.getHeaders("Set-Cookie");
        if (headers.length > 0) {
            loginName = null;
            loginStatus = false;
        } else {
            loginName = login;
            loginStatus = true;
            cookieStore = httpclient.getCookieStore();
        }

        if (entity != null) {
            entity.consumeContent();
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return loginStatus;
}

From source file:com.thoughtmetric.tl.TLLib.java

public static TagNode TagNodeFromURLEx2(HtmlCleaner cleaner, URL url, Handler handler, Context context,
        String fullTag, boolean login) throws IOException {

    handler.sendEmptyMessage(PROGRESS_CONNECTING);

    DefaultHttpClient httpclient = new DefaultHttpClient();
    if (login) {//from  w  w  w  . j  a  v a 2  s . c o  m
        httpclient.setCookieStore(cookieStore);
    }
    HttpGet httpGet = new HttpGet(url.toExternalForm());
    HttpResponse response = httpclient.execute(httpGet);

    handler.sendEmptyMessage(PROGRESS_DOWNLOADING);
    HttpEntity httpEntity = response.getEntity();
    InputStream is = httpEntity.getContent();
    return TagNodeFromURLHelper(is, fullTag, handler, context, cleaner);
}

From source file:com.thoughtmetric.tl.TLLib.java

public static Object[] tagNodeWithEditText(HtmlCleaner cleaner, URL url, Handler handler, Context context,
        String fullTag, boolean login) throws IOException {
    Object[] ret = new Object[2];

    handler.sendEmptyMessage(PROGRESS_CONNECTING);

    DefaultHttpClient httpclient = new DefaultHttpClient();
    if (login) {// w ww  . j  a v  a2 s. co m
        httpclient.setCookieStore(cookieStore);
    }
    HttpGet httpGet = new HttpGet(url.toExternalForm());
    HttpResponse response = httpclient.execute(httpGet);

    handler.sendEmptyMessage(PROGRESS_DOWNLOADING);
    InputStream is = response.getEntity().getContent();
    BufferedReader br = new BufferedReader(new InputStreamReader(context.openFileInput(TEMP_FILE_NAME)));
    ret[0] = TagNodeFromURLHelper(is, fullTag, handler, context, cleaner);
    ret[1] = parseTextArea(br);

    return ret;
}

From source file:org.opensourcetlapp.tl.TLLib.java

public static boolean login(String login, String pw, Handler handler, Context context) throws IOException {
    handler.sendEmptyMessage(TLHandler.PROGRESS_LOGIN);
    logout();//from w w w .ja v  a2  s .c om

    // Fetch the token
    HtmlCleaner cleaner = TLLib.buildDefaultHtmlCleaner();
    URL url = new URL(LOGIN_URL);
    //TagNode node = TagNodeFromURLEx2(cleaner, url, handler, context, "<html>", false);
    TagNode node = TLLib.TagNodeFromURLLoginToken(cleaner, url, handler, context);

    String token = null;
    try {
        TagNode result = (TagNode) (node.evaluateXPath("//input")[0]);
        token = result.getAttributeByName("value");
    } catch (XPatherException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    if (token == null) {
        return false;
    }
    // 
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httpost = new HttpPost(LOGIN_URL);

    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair(USER_FIELD, login));
    nvps.add(new BasicNameValuePair(PASS_FIELD, pw));
    nvps.add(new BasicNameValuePair(REMEMBERME, "1"));
    nvps.add(new BasicNameValuePair("stage", "1"));
    nvps.add(new BasicNameValuePair("back_url", "/"));
    nvps.add(new BasicNameValuePair("token", token));
    Log.d("token:", token);
    tokenField = token;

    if (cookieStore != null) {
        httpclient.setCookieStore(cookieStore);
    }

    try {
        httpost.setEntity(new UrlEncodedFormEntity(nvps));
        HttpResponse response = httpclient.execute(httpost);
        HttpEntity entity = response.getEntity();

        Header[] headers = response.getHeaders("Set-Cookie");
        if (cookieStore.getCookies().size() < 2) {
            loginName = null;
            loginStatus = false;
        } else {
            loginName = login;
            loginStatus = true;
            cookieStore = httpclient.getCookieStore();
        }

        if (entity != null) {
            entity.consumeContent();
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return loginStatus;
}

From source file:org.opensourcetlapp.tl.TLLib.java

public static TagNode TagNodeFromURLEx2(HtmlCleaner cleaner, URL url, Handler handler, Context context,
        String fullTag, boolean login) throws IOException {

    handler.sendEmptyMessage(PROGRESS_CONNECTING);

    DefaultHttpClient httpclient = new DefaultHttpClient();
    if (cookieStore != null) {
        httpclient.setCookieStore(cookieStore);
    }/* ww  w .j a  v a  2s.  c  o m*/
    HttpGet httpGet = new HttpGet(url.toExternalForm());
    HttpResponse response = httpclient.execute(httpGet);
    if (cookieStore == null) {
        cookieStore = httpclient.getCookieStore();
    }

    handler.sendEmptyMessage(PROGRESS_DOWNLOADING);
    HttpEntity httpEntity = response.getEntity();
    InputStream is = httpEntity.getContent();
    return TagNodeFromURLHelper(is, fullTag, handler, context, cleaner);
}

From source file:com.thoughtmetric.tl.TLLib.java

private static TagNode TagNodeFromURLHelper(InputStream is, String fullTag, Handler handler, Context context,
        HtmlCleaner cleaner) throws IOException {
    SharedPreferences settings = context.getSharedPreferences(Settings.SETTINGS_FILE_NAME, 0);
    boolean disableSmartParsing = settings.getBoolean(Settings.DISABLE_SMART_PARSING, false);
    if (fullTag != null && !disableSmartParsing) {
        FileOutputStream fos = context.openFileOutput(TEMP_FILE_NAME, Context.MODE_WORLD_WRITEABLE);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
        TagParser.extractTagToFile(fullTag, is, bw);
        bw.flush();//from  www.ja va2s .  c  o  m
        bw.close();
        if (handler != null)
            handler.sendEmptyMessage(PROGRESS_PARSING);

        return cleaner.clean(context.openFileInput(TEMP_FILE_NAME));
    } else {
        if (handler != null)
            handler.sendEmptyMessage(PROGRESS_PARSING);
        return cleaner.clean(is);
    }
}

From source file:net.carlh.toast.Client.java

private void setConnected(boolean c) {
    if (c == connected.get()) {
        return;/*from  www . j  a va  2 s . co m*/
    }

    connected.set(c);

    for (Handler h : handlers) {
        h.sendEmptyMessage(0);
    }
}

From source file:com.twofortyfouram.locale.sdk.host.test.Junit4SupportLoaderTestCase.java

/**
 * Runs a Loader synchronously and returns the result of the load. The loader will
 * be started, stopped, and destroyed by this method so it cannot be reused.
 *
 * @param loader The loader to run synchronously
 * @return The result from the loader/*from w  w w .ja v a  2 s . c o m*/
 */
public <T> T getLoaderResultSynchronously(final Loader<T> loader) {
    // The test thread blocks on this queue until the loader puts it's result in
    final ArrayBlockingQueue<T> queue = new ArrayBlockingQueue<T>(1);

    // This callback runs on the "main" thread and unblocks the test thread
    // when it puts the result into the blocking queue
    final Loader.OnLoadCompleteListener<T> listener = new Loader.OnLoadCompleteListener<T>() {
        @Override
        public void onLoadComplete(Loader<T> completedLoader, T data) {
            // Shut the loader down
            completedLoader.unregisterListener(this);
            completedLoader.stopLoading();
            completedLoader.reset();

            // Store the result, unblocking the test thread
            queue.add(data);
        }
    };

    // This handler runs on the "main" thread of the process since AsyncTask
    // is documented as needing to run on the main thread and many Loaders use
    // AsyncTask
    final Handler mainThreadHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            loader.registerListener(0, listener);
            loader.startLoading();
        }
    };

    // Ask the main thread to start the loading process
    mainThreadHandler.sendEmptyMessage(0);

    // Block on the queue waiting for the result of the load to be inserted
    T result;
    while (true) {
        try {
            result = queue.take();
            break;
        } catch (InterruptedException e) {
            throw new RuntimeException("waiting thread interrupted", e);
        }
    }

    return result;
}