Example usage for android.webkit WebView setFocusableInTouchMode

List of usage examples for android.webkit WebView setFocusableInTouchMode

Introduction

In this page you can find the example usage for android.webkit WebView setFocusableInTouchMode.

Prototype

public void setFocusableInTouchMode(boolean focusableInTouchMode) 

Source Link

Document

Set whether this view can receive focus while in touch mode.

Usage

From source file:com.owncloud.android.ui.activity.ExternalSiteWebView.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log_OC.v(TAG, "onCreate() start");

    Bundle extras = getIntent().getExtras();
    String title = extras.getString(EXTRA_TITLE);
    String url = extras.getString(EXTRA_URL);
    menuItemId = extras.getInt(EXTRA_MENU_ITEM_ID);
    showSidebar = extras.getBoolean(EXTRA_SHOW_SIDEBAR);

    // show progress
    getWindow().requestFeature(Window.FEATURE_PROGRESS);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.externalsite_webview);

    WebView webview = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = webview.getSettings();

    webview.setFocusable(true);/*from  w  ww .ja v a 2s  . c  om*/
    webview.setFocusableInTouchMode(true);
    webview.setClickable(true);

    // setup toolbar
    setupToolbar();

    // setup drawer
    setupDrawer(menuItemId);

    if (!showSidebar) {
        setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    }

    getSupportActionBar().setTitle(title);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    // enable zoom
    webSettings.setSupportZoom(true);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setDisplayZoomControls(false);

    // Non-responsive webs are zoomed out when loaded
    webSettings.setUseWideViewPort(true);
    webSettings.setLoadWithOverviewMode(true);

    // user agent
    webSettings.setUserAgentString(MainApp.getUserAgent());

    // no private data storing
    webSettings.setSavePassword(false);
    webSettings.setSaveFormData(false);

    // disable local file access
    webSettings.setAllowFileAccess(false);

    // enable javascript
    webview.getSettings().setJavaScriptEnabled(true);

    final Activity activity = this;
    final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);

    webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            progressBar.setProgress(progress * 1000);
        }
    });

    webview.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, getString(R.string.webview_error) + ": " + description, Toast.LENGTH_SHORT)
                    .show();
        }
    });

    webview.loadUrl(url);
}

From source file:org.openqa.selendroid.server.model.SelendroidWebDriver.java

private void configureWebView(final WebView view) {
    ServerInstrumentation.getInstance().runOnUiThread(new Runnable() {

        @Override//ww  w . ja v  a2 s. c o  m
        public void run() {
            view.clearCache(true);
            view.clearFormData();
            view.clearHistory();
            view.setFocusable(true);
            view.setFocusableInTouchMode(true);
            view.setNetworkAvailable(true);
            view.setWebChromeClient(new MyWebChromeClient());

            WebSettings settings = view.getSettings();
            settings.setJavaScriptCanOpenWindowsAutomatically(true);
            settings.setSupportMultipleWindows(true);
            settings.setBuiltInZoomControls(true);
            settings.setJavaScriptEnabled(true);
            settings.setAppCacheEnabled(true);
            settings.setAppCacheMaxSize(10 * 1024 * 1024);
            settings.setAppCachePath("");
            settings.setDatabaseEnabled(true);
            settings.setDomStorageEnabled(true);
            settings.setGeolocationEnabled(true);
            settings.setSaveFormData(false);
            settings.setSavePassword(false);
            settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
            // Flash settings
            settings.setPluginState(WebSettings.PluginState.ON);

            // Geo location settings
            settings.setGeolocationEnabled(true);
            settings.setGeolocationDatabasePath("/data/data/selendroid");
        }
    });
}

From source file:io.selendroid.server.model.SelendroidWebDriver.java

private void configureWebView(final WebView view) {
    ServerInstrumentation.getInstance().getCurrentActivity().runOnUiThread(new Runnable() {

        @Override/* w  w w . j a va  2  s . c  o m*/
        public void run() {
            try {
                view.clearCache(true);
                view.clearFormData();
                view.clearHistory();
                view.setFocusable(true);
                view.setFocusableInTouchMode(true);
                view.setNetworkAvailable(true);
                // need to check the class name rather than checking instanceof
                // since when it is not an instanceof, it likely means the app under test
                // does not contain the Cordova project and this will cause a RuntimeException
                if (view.getClass().getSimpleName().equalsIgnoreCase("CordovaWebView")) {
                    CordovaWebView webview = (CordovaWebView) view;
                    CordovaInterface ci = null;
                    chromeClient = new ExtendedCordovaChromeClient(null, webview);
                } else {
                    chromeClient = new SelendroidWebChromeClient();
                }
                view.setWebChromeClient(chromeClient);

                WebSettings settings = view.getSettings();
                settings.setJavaScriptCanOpenWindowsAutomatically(true);
                settings.setSupportMultipleWindows(true);
                settings.setBuiltInZoomControls(true);
                settings.setJavaScriptEnabled(true);
                settings.setAppCacheEnabled(true);
                settings.setAppCacheMaxSize(10 * 1024 * 1024);
                settings.setAppCachePath("");
                settings.setDatabaseEnabled(true);
                settings.setDomStorageEnabled(true);
                settings.setGeolocationEnabled(true);
                settings.setSaveFormData(false);
                settings.setSavePassword(false);
                settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
                // Flash settings
                settings.setPluginState(WebSettings.PluginState.ON);

                // Geo location settings
                settings.setGeolocationEnabled(true);
                settings.setGeolocationDatabasePath("/data/data/selendroid");
            } catch (Exception e) {
                SelendroidLogger.error("Error configuring web view", e);
            }
        }
    });
}

From source file:com.ichi2.anki.AbstractFlashcardViewer.java

@SuppressLint({ "NewApi", "SetJavaScriptEnabled" })
// because of setDisplayZoomControls.
private WebView createWebView() {
    WebView webView = new MyWebView(this);
    webView.setWillNotCacheDrawing(true);
    webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
    if (CompatHelper.isHoneycomb()) {
        // Disable the on-screen zoom buttons for API > 11
        webView.getSettings().setDisplayZoomControls(false);
    }//w w w  . j  a v  a2  s  .c  o  m
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setSupportZoom(true);
    // Start at the most zoomed-out level
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebChromeClient(new AnkiDroidWebChromeClient());
    // Problems with focus and input tags is the reason we keep the old type answer mechanism for old Androids.
    webView.setFocusableInTouchMode(mUseInputTag);
    webView.setScrollbarFadingEnabled(true);
    Timber.d("Focusable = %s, Focusable in touch mode = %s", webView.isFocusable(),
            webView.isFocusableInTouchMode());

    webView.setWebViewClient(new WebViewClient() {
        // Filter any links using the custom "playsound" protocol defined in Sound.java.
        // We play sounds through these links when a user taps the sound icon.
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith("playsound:")) {
                // Send a message that will be handled on the UI thread.
                Message msg = Message.obtain();
                String soundPath = url.replaceFirst("playsound:", "");
                msg.obj = soundPath;
                mHandler.sendMessage(msg);
                return true;
            }
            if (url.startsWith("file") || url.startsWith("data:")) {
                return false; // Let the webview load files, i.e. local images.
            }
            if (url.startsWith("typeblurtext:")) {
                // Store the text the javascript has send us
                mTypeInput = URLDecoder.decode(url.replaceFirst("typeblurtext:", ""));
                //  and show the SHOW ANSWER? button again.
                mFlipCardLayout.setVisibility(View.VISIBLE);
                return true;
            }
            if (url.startsWith("typeentertext:")) {
                // Store the text the javascript has send us
                mTypeInput = URLDecoder.decode(url.replaceFirst("typeentertext:", ""));
                //  and show the answer.
                mFlipCardLayout.performClick();
                return true;
            }
            if (url.equals("signal:typefocus")) {
                // Hide the SHOW ANSWER? button when the input has focus. The soft keyboard takes up enough space
                // by itself.
                mFlipCardLayout.setVisibility(View.GONE);
                return true;
            }
            Timber.d("Opening external link \"%s\" with an Intent", url);
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            try {
                startActivityWithoutAnimation(intent);
            } catch (ActivityNotFoundException e) {
                e.printStackTrace(); // Don't crash if the intent is not handled
            }
            return true;
        }

        // Run any post-load events in javascript that rely on the window being completely loaded.
        @Override
        public void onPageFinished(WebView view, String url) {
            Timber.d("onPageFinished triggered");
            view.loadUrl("javascript:onPageFinished();");
        }
    });
    // Set transparent color to prevent flashing white when night mode enabled
    webView.setBackgroundColor(Color.argb(1, 0, 0, 0));
    return webView;
}

From source file:com.ichi2.anki2.Reviewer.java

private WebView createWebView() {
    WebView webView = new MyWebView(this);
    webView.setWillNotCacheDrawing(true);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    if (mZoomEnabled) {
        webView.getSettings().setBuiltInZoomControls(true);
    }//www  . j  a  v a2  s  .  c o m
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebChromeClient(new AnkiDroidWebChromeClient());
    webView.addJavascriptInterface(new JavaScriptInterface(this), "ankidroid");
    if (AnkiDroidApp.SDK_VERSION > 7) {
        webView.setFocusableInTouchMode(false);
    }
    AnkiDroidApp.getCompat().setScrollbarFadingEnabled(webView, mPrefFadeScrollbars);
    Log.i(AnkiDroidApp.TAG, "Focusable = " + webView.isFocusable() + ", Focusable in touch mode = "
            + webView.isFocusableInTouchMode());

    return webView;
}

From source file:com.hichinaschool.flashcards.anki.Reviewer.java

private WebView createWebView() {
    WebView webView = new MyWebView(this);
    webView.setWillNotCacheDrawing(true);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    if (mZoomEnabled) {
        webView.getSettings().setBuiltInZoomControls(true);
    }//from   ww w . ja  v  a2 s. c  o m
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebChromeClient(new AnkiDroidWebChromeClient());
    webView.addJavascriptInterface(new JavaScriptInterface(this), "ankidroid");
    if (AnkiDroidApp.SDK_VERSION > 7) {
        webView.setFocusableInTouchMode(false);
    }
    AnkiDroidApp.getCompat().setScrollbarFadingEnabled(webView, mPrefFadeScrollbars);
    // Log.i(AnkiDroidApp.TAG, "Focusable = " + webView.isFocusable() + ", Focusable in touch mode = " + webView.isFocusableInTouchMode());

    return webView;
}

From source file:com.codename1.impl.android.AndroidImplementation.java

public PeerComponent createBrowserComponent(final Object parent) {
    if (getActivity() == null) {
        return null;
    }/* w  ww. j  av  a2  s .co m*/
    final AndroidImplementation.AndroidBrowserComponent[] bc = new AndroidImplementation.AndroidBrowserComponent[1];
    final Throwable[] error = new Throwable[1];
    final Object lock = new Object();

    getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {

            synchronized (lock) {
                try {
                    WebView wv = new WebView(getActivity()) {

                        public boolean onKeyDown(int keyCode, KeyEvent event) {
                            switch (keyCode) {
                            case KeyEvent.KEYCODE_BACK:
                                Display.getInstance().keyPressed(AndroidImplementation.DROID_IMPL_KEY_BACK);
                                return true;
                            case KeyEvent.KEYCODE_MENU:
                                //if the native commands are used don't handle the keycode
                                if (Display.getInstance()
                                        .getCommandBehavior() != Display.COMMAND_BEHAVIOR_NATIVE) {
                                    Display.getInstance().keyPressed(AndroidImplementation.DROID_IMPL_KEY_MENU);
                                    return true;
                                }
                            }
                            return super.onKeyDown(keyCode, event);
                        }

                        public boolean onKeyUp(int keyCode, KeyEvent event) {
                            switch (keyCode) {
                            case KeyEvent.KEYCODE_BACK:
                                Display.getInstance().keyReleased(AndroidImplementation.DROID_IMPL_KEY_BACK);
                                return true;
                            case KeyEvent.KEYCODE_MENU:
                                //if the native commands are used don't handle the keycode
                                if (Display.getInstance()
                                        .getCommandBehavior() != Display.COMMAND_BEHAVIOR_NATIVE) {
                                    Display.getInstance().keyPressed(AndroidImplementation.DROID_IMPL_KEY_MENU);
                                    return true;
                                }
                            }
                            return super.onKeyUp(keyCode, event);
                        }
                    };
                    wv.setOnTouchListener(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            switch (event.getAction()) {
                            case MotionEvent.ACTION_DOWN:
                            case MotionEvent.ACTION_UP:
                                if (!v.hasFocus()) {
                                    v.requestFocus();
                                }
                                break;
                            }
                            return false;
                        }
                    });
                    wv.getSettings().setDomStorageEnabled(true);
                    wv.requestFocus(View.FOCUS_DOWN);
                    wv.setFocusableInTouchMode(true);
                    bc[0] = new AndroidImplementation.AndroidBrowserComponent(wv, getActivity(), parent);
                    lock.notify();
                } catch (Throwable t) {
                    error[0] = t;
                    lock.notify();
                }
            }
        }
    });
    while (bc[0] == null && error[0] == null) {
        Display.getInstance().invokeAndBlock(new Runnable() {
            public void run() {
                synchronized (lock) {
                    if (bc[0] == null && error[0] == null) {
                        try {
                            lock.wait(20);
                        } catch (InterruptedException ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            }

        });
    }
    if (error[0] != null) {
        throw new RuntimeException(error[0]);
    }
    return bc[0];
}