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:com.droid.app.fotobot.FotoBot.java

/**
 *  ?  ?//ww  w  . ja  v a2 s  .  c o m
 *
 * @param str ? ??
 * @param status ?? ??
 */
public void SendMessage(String str, String status) {

    Message msg = Message.obtain(); // Creates an new Message instance
    msg.obj = str; // Put the string into Message, into "obj" field.
    msg_status = status;
    msg.setTarget(h); // Set the Handler
    msg.sendToTarget(); //Send the message

}

From source file:com.taobao.weex.bridge.WXBridgeManager.java

/**
 * Initialize JavaScript framework//from  w w  w. java  2  s  . c o  m
 *
 * @param framework String representation of the framework to be init.
 */
public synchronized void initScriptsFramework(String framework) {
    Message msg = mJSHandler.obtainMessage();
    msg.obj = framework;
    msg.what = WXJSBridgeMsgType.INIT_FRAMEWORK;
    msg.setTarget(mJSHandler);
    msg.sendToTarget();
}

From source file:com.taobao.weex.bridge.WXBridgeManager.java

public void takeJSHeapSnapshot(String filename) {
    Message msg = mJSHandler.obtainMessage();
    msg.obj = filename;//ww  w .j a va  2s . c om
    msg.what = WXJSBridgeMsgType.TAKE_HEAP_SNAPSHOT;
    msg.setTarget(mJSHandler);
    msg.sendToTarget();
}

From source file:com.dish.browser.activity.BrowserActivity.java

@Override
/**/*from   w w  w  . j a  v  a2  s .c  o  m*/
 * handles long presses for the browser, tries to get the
 * url of the item that was clicked and sends it (it can be null)
 * to the click handler that does cool stuff with it
 */
public void onLongPress() {
    if (mClickHandler == null) {
        mClickHandler = new ClickHandler(mActivity);
    }
    Message click = mClickHandler.obtainMessage();
    if (click != null) {
        click.setTarget(mClickHandler);
        mCurrentView.getWebView().requestFocusNodeHref(click);
    }
}

From source file:android.webkit.cts.WebViewTest.java

public void testDocumentHasImages() throws Exception, Throwable {
    if (!NullWebViewUtils.isWebViewAvailable()) {
        return;/*from   www.  java2s .  com*/
    }
    final class DocumentHasImageCheckHandler extends Handler {
        private boolean mReceived;
        private int mMsgArg1;

        public DocumentHasImageCheckHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            synchronized (this) {
                mReceived = true;
                mMsgArg1 = msg.arg1;
            }
        }

        public synchronized boolean hasCalledHandleMessage() {
            return mReceived;
        }

        public synchronized int getMsgArg1() {
            return mMsgArg1;
        }
    }

    startWebServer(false);
    final String imgUrl = mWebServer.getAssetUrl(TestHtmlConstants.SMALL_IMG_URL);

    // Create a handler on the UI thread.
    final DocumentHasImageCheckHandler handler = new DocumentHasImageCheckHandler(
            mWebView.getHandler().getLooper());

    runTestOnUiThread(new Runnable() {
        @Override
        public void run() {
            mOnUiThread.loadDataAndWaitForCompletion("<html><body><img src=\"" + imgUrl + "\"/></body></html>",
                    "text/html", null);
            Message response = new Message();
            response.setTarget(handler);
            assertFalse(handler.hasCalledHandleMessage());
            mWebView.documentHasImages(response);
        }
    });
    new PollingCheck() {
        @Override
        protected boolean check() {
            return handler.hasCalledHandleMessage();
        }
    }.run();
    assertEquals(1, handler.getMsgArg1());
}

From source file:android.webkit.cts.WebViewTest.java

public void testRequestFocusNodeHref() throws Throwable {
    if (!NullWebViewUtils.isWebViewAvailable()) {
        return;//from  w  w  w .  j  ava 2s  . c  o m
    }
    startWebServer(false);
    String url1 = mWebServer.getAssetUrl(TestHtmlConstants.HTML_URL1);
    String url2 = mWebServer.getAssetUrl(TestHtmlConstants.HTML_URL2);
    final String links = "<DL><p><DT><A HREF=\"" + url1 + "\">HTML_URL1</A><DT><A HREF=\"" + url2
            + "\">HTML_URL2</A></DL><p>";
    mOnUiThread.loadDataAndWaitForCompletion("<html><body>" + links + "</body></html>", "text/html", null);
    getInstrumentation().waitForIdleSync();

    final HrefCheckHandler handler = new HrefCheckHandler(mWebView.getHandler().getLooper());
    final Message hrefMsg = new Message();
    hrefMsg.setTarget(handler);

    // focus on first link
    handler.reset();
    getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_TAB);
    mOnUiThread.requestFocusNodeHref(hrefMsg);
    new PollingCheck() {
        @Override
        protected boolean check() {
            boolean done = false;
            if (handler.hasCalledHandleMessage()) {
                if (handler.mResultUrl != null) {
                    done = true;
                } else {
                    handler.reset();
                    Message newMsg = new Message();
                    newMsg.setTarget(handler);
                    mOnUiThread.requestFocusNodeHref(newMsg);
                }
            }
            return done;
        }
    }.run();
    assertEquals(url1, handler.getResultUrl());

    // focus on second link
    handler.reset();
    final Message hrefMsg2 = new Message();
    hrefMsg2.setTarget(handler);
    getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_TAB);
    mOnUiThread.requestFocusNodeHref(hrefMsg2);
    new PollingCheck() {
        @Override
        protected boolean check() {
            boolean done = false;
            final String url2 = mWebServer.getAssetUrl(TestHtmlConstants.HTML_URL2);
            if (handler.hasCalledHandleMessage()) {
                if (handler.mResultUrl != null && handler.mResultUrl.equals(url2)) {
                    done = true;
                } else {
                    handler.reset();
                    Message newMsg = new Message();
                    newMsg.setTarget(handler);
                    mOnUiThread.requestFocusNodeHref(newMsg);
                }
            }
            return done;
        }
    }.run();
    assertEquals(url2, handler.getResultUrl());

    mOnUiThread.requestFocusNodeHref(null);
}

From source file:android.webkit.cts.WebViewTest.java

public void testRequestImageRef() throws Exception, Throwable {
    if (!NullWebViewUtils.isWebViewAvailable()) {
        return;//from  www. ja va2 s . c  o m
    }
    final class ImageLoaded {
        public boolean mImageLoaded;

        @JavascriptInterface
        public void loaded() {
            mImageLoaded = true;
        }
    }
    final ImageLoaded imageLoaded = new ImageLoaded();
    runTestOnUiThread(new Runnable() {
        public void run() {
            mOnUiThread.getSettings().setJavaScriptEnabled(true);
        }
    });
    mOnUiThread.addJavascriptInterface(imageLoaded, "imageLoaded");
    AssetManager assets = getActivity().getAssets();
    Bitmap bitmap = BitmapFactory.decodeStream(assets.open(TestHtmlConstants.LARGE_IMG_URL));
    int imgWidth = bitmap.getWidth();
    int imgHeight = bitmap.getHeight();

    startWebServer(false);
    final String imgUrl = mWebServer.getAssetUrl(TestHtmlConstants.LARGE_IMG_URL);
    mOnUiThread.loadDataAndWaitForCompletion(
            "<html><head><title>Title</title><style type=\"text/css\">"
                    + "#imgElement { -webkit-transform: translate3d(0,0,1); }"
                    + "#imgElement.finish { -webkit-transform: translate3d(0,0,0);"
                    + " -webkit-transition-duration: 1ms; }</style>"
                    + "<script type=\"text/javascript\">function imgLoad() {"
                    + "imgElement = document.getElementById('imgElement');"
                    + "imgElement.addEventListener('webkitTransitionEnd',"
                    + "function(e) { imageLoaded.loaded(); });" + "imgElement.className = 'finish';}</script>"
                    + "</head><body><img id=\"imgElement\" src=\"" + imgUrl + "\" width=\"" + imgWidth
                    + "\" height=\"" + imgHeight + "\" onLoad=\"imgLoad()\"/></body></html>",
            "text/html", null);
    new PollingCheck() {
        @Override
        protected boolean check() {
            return imageLoaded.mImageLoaded;
        }
    }.run();
    getInstrumentation().waitForIdleSync();

    final HrefCheckHandler handler = new HrefCheckHandler(mWebView.getHandler().getLooper());
    final Message msg = new Message();
    msg.setTarget(handler);

    // touch the image
    handler.reset();
    int[] location = mOnUiThread.getLocationOnScreen();

    long time = SystemClock.uptimeMillis();
    getInstrumentation().sendPointerSync(MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN,
            location[0] + imgWidth / 2, location[1] + imgHeight / 2, 0));
    getInstrumentation().waitForIdleSync();
    mOnUiThread.requestImageRef(msg);
    new PollingCheck() {
        @Override
        protected boolean check() {
            boolean done = false;
            if (handler.hasCalledHandleMessage()) {
                if (handler.mResultUrl != null) {
                    done = true;
                } else {
                    handler.reset();
                    Message newMsg = new Message();
                    newMsg.setTarget(handler);
                    mOnUiThread.requestImageRef(newMsg);
                }
            }
            return done;
        }
    }.run();
    assertEquals(imgUrl, handler.mResultUrl);
}

From source file:com.linkbubble.ui.ContentView.java

private void onUrlLongClick(final WebView webView, final String urlAsString, final int type) {
    Resources resources = getResources();

    final ArrayList<String> longClickSelections = new ArrayList<String>();

    final String shareLabel = resources.getString(R.string.action_share);
    longClickSelections.add(shareLabel);

    String defaultBrowserLabel = Settings.get().getDefaultBrowserLabel();

    final String leftConsumeBubbleLabel = Settings.get().getConsumeBubbleLabel(BubbleAction.ConsumeLeft);
    if (leftConsumeBubbleLabel != null) {
        if (defaultBrowserLabel == null || defaultBrowserLabel.equals(leftConsumeBubbleLabel) == false) {
            longClickSelections.add(leftConsumeBubbleLabel);
        }/*from w w w . ja v a  2 s.  co  m*/
    }

    final String rightConsumeBubbleLabel = Settings.get().getConsumeBubbleLabel(BubbleAction.ConsumeRight);
    if (rightConsumeBubbleLabel != null) {
        if (defaultBrowserLabel == null || defaultBrowserLabel.equals(rightConsumeBubbleLabel) == false) {
            longClickSelections.add(rightConsumeBubbleLabel);
        }
    }

    // Long pressing for a link doesn't work reliably, re #279
    //final String copyLinkLabel = resources.getString(R.string.action_copy_to_clipboard);
    //longClickSelections.add(copyLinkLabel);

    Collections.sort(longClickSelections);

    final String openLinkInNewBubbleLabel = resources.getString(R.string.action_open_link_in_new_bubble);
    final String openImageInNewBubbleLabel = resources.getString(R.string.action_open_image_in_new_bubble);
    if (type == WebView.HitTestResult.IMAGE_TYPE || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
        longClickSelections.add(0, openImageInNewBubbleLabel);
    }
    if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
        longClickSelections.add(0, openLinkInNewBubbleLabel);
    }

    final String openInBrowserLabel = defaultBrowserLabel != null
            ? String.format(resources.getString(R.string.action_open_in_browser), defaultBrowserLabel)
            : null;
    if (openInBrowserLabel != null) {
        longClickSelections.add(1, openInBrowserLabel);
    }

    final String saveImageLabel = resources.getString(R.string.action_save_image);
    if (type == WebView.HitTestResult.IMAGE_TYPE || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
        longClickSelections.add(saveImageLabel);
    }

    ListView listView = new ListView(getContext());
    listView.setAdapter(new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,
            longClickSelections.toArray(new String[0])));
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            CrashTracking.log("ContentView listView.setOnItemClickListener");
            String string = longClickSelections.get(position);
            if (string.equals(openLinkInNewBubbleLabel)
                    && type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
                Message msg = new Message();
                msg.setTarget(new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        Bundle b = msg.getData();
                        if (b != null && b.getString("url") != null) {
                            MainController.get().openUrl(b.getString("url"), System.currentTimeMillis(), false,
                                    Analytics.OPENED_URL_FROM_NEW_TAB);
                        }
                    }
                });
                webView.requestFocusNodeHref(msg);
            }
            if (string.equals(openLinkInNewBubbleLabel) || string.equals(openImageInNewBubbleLabel)) {
                MainController controller = MainController.get();
                if (null != controller) {
                    controller.openUrl(urlAsString, System.currentTimeMillis(), false,
                            Analytics.OPENED_URL_FROM_NEW_TAB);
                } else {
                    MainApplication.openLink(getContext(), urlAsString, Analytics.OPENED_URL_FROM_NEW_TAB);
                }
            } else if (openInBrowserLabel != null && string.equals(openInBrowserLabel)) {
                openInBrowser(urlAsString);
            } else if (string.equals(shareLabel)) {
                showSelectShareMethod(urlAsString, false);
            } else if (string.equals(saveImageLabel)) {
                saveImage(urlAsString);
            } else if (leftConsumeBubbleLabel != null && string.equals(leftConsumeBubbleLabel)) {
                MainApplication.handleBubbleAction(getContext(), BubbleAction.ConsumeLeft, urlAsString, -1);
            } else if (rightConsumeBubbleLabel != null && string.equals(rightConsumeBubbleLabel)) {
                MainApplication.handleBubbleAction(getContext(), BubbleAction.ConsumeRight, urlAsString, -1);
                //} else if (string.equals(copyLinkLabel)) {
                //    MainApplication.copyLinkToClipboard(getContext(), urlAsString, R.string.link_copied_to_clipboard);
            }

            if (mLongPressAlertDialog != null) {
                mLongPressAlertDialog.dismiss();
            }
        }
    });
    listView.setBackgroundColor(Settings.get().getThemedContentViewColor());

    mLongPressAlertDialog = new AlertDialog.Builder(getContext()).create();
    mLongPressAlertDialog.setView(listView);
    mLongPressAlertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    mLongPressAlertDialog.show();
}