Example usage for android.webkit WebView getTitle

List of usage examples for android.webkit WebView getTitle

Introduction

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

Prototype

@ViewDebug.ExportedProperty(category = "webview")
public String getTitle() 

Source Link

Document

Gets the title for the current page.

Usage

From source file:com.mobicage.rogerthat.plugins.friends.ActionScreenActivity.java

@SuppressLint({ "SetJavaScriptEnabled", "JavascriptInterface", "NewApi" })
@Override/*from  w w w.j  av  a2  s  .  c  om*/
public void onCreate(Bundle savedInstanceState) {
    if (CloudConstants.isContentBrandingApp()) {
        super.setTheme(android.R.style.Theme_Black_NoTitleBar_Fullscreen);
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.action_screen);
    mBranding = (WebView) findViewById(R.id.branding);
    WebSettings brandingSettings = mBranding.getSettings();
    brandingSettings.setJavaScriptEnabled(true);
    brandingSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        brandingSettings.setAllowFileAccessFromFileURLs(true);
    }
    mBrandingHttp = (WebView) findViewById(R.id.branding_http);
    mBrandingHttp.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    WebSettings brandingSettingsHttp = mBrandingHttp.getSettings();
    brandingSettingsHttp.setJavaScriptEnabled(true);
    brandingSettingsHttp.setCacheMode(WebSettings.LOAD_DEFAULT);

    if (CloudConstants.isContentBrandingApp()) {
        mSoundThread = new HandlerThread("rogerthat_actionscreenactivity_sound");
        mSoundThread.start();
        Looper looper = mSoundThread.getLooper();
        mSoundHandler = new Handler(looper);

        int cameraPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
        if (cameraPermission == PackageManager.PERMISSION_GRANTED) {
            mQRCodeScanner = QRCodeScanner.getInstance(this);
            final LinearLayout previewHolder = (LinearLayout) findViewById(R.id.preview_view);
            previewHolder.addView(mQRCodeScanner.view);
        }
        mBranding.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                initFullScreenForContentBranding();
            }
        });

        mBrandingHttp.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                initFullScreenForContentBranding();
            }
        });
    }

    final View brandingHeader = findViewById(R.id.branding_header_container);

    final ImageView brandingHeaderClose = (ImageView) findViewById(R.id.branding_header_close);
    final TextView brandingHeaderText = (TextView) findViewById(R.id.branding_header_text);
    brandingHeaderClose
            .setColorFilter(UIUtils.imageColorFilter(getResources().getColor(R.color.mc_homescreen_text)));

    brandingHeaderClose.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mQRCodeScanner != null) {
                mQRCodeScanner.onResume();
            }
            brandingHeader.setVisibility(View.GONE);
            mBrandingHttp.setVisibility(View.GONE);
            mBranding.setVisibility(View.VISIBLE);
            mBrandingHttp.loadUrl("about:blank");
        }
    });

    final View brandingFooter = findViewById(R.id.branding_footer_container);

    if (CloudConstants.isContentBrandingApp()) {
        brandingHeaderClose.setVisibility(View.GONE);
        final ImageView brandingFooterClose = (ImageView) findViewById(R.id.branding_footer_close);
        final TextView brandingFooterText = (TextView) findViewById(R.id.branding_footer_text);
        brandingFooterText.setText(getString(R.string.back));
        brandingFooterClose
                .setColorFilter(UIUtils.imageColorFilter(getResources().getColor(R.color.mc_homescreen_text)));

        brandingFooter.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mQRCodeScanner != null) {
                    mQRCodeScanner.onResume();
                }
                brandingHeader.setVisibility(View.GONE);
                brandingFooter.setVisibility(View.GONE);
                mBrandingHttp.setVisibility(View.GONE);
                mBranding.setVisibility(View.VISIBLE);
                mBrandingHttp.loadUrl("about:blank");
            }
        });
    }

    final RelativeLayout openPreview = (RelativeLayout) findViewById(R.id.preview_holder);

    openPreview.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mQRCodeScanner != null) {
                mQRCodeScanner.previewHolderClicked();
            }
        }
    });

    mBranding.addJavascriptInterface(new JSInterface(this), "__rogerthat__");
    mBranding.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onConsoleMessage(String message, int lineNumber, String sourceID) {
            if (sourceID != null) {
                try {
                    sourceID = new File(sourceID).getName();
                } catch (Exception e) {
                    L.d("Could not get fileName of sourceID: " + sourceID, e);
                }
            }
            if (mIsHtmlContent) {
                L.i("[BRANDING] " + sourceID + ":" + lineNumber + " | " + message);
            } else {
                L.d("[BRANDING] " + sourceID + ":" + lineNumber + " | " + message);
            }
        }
    });
    mBranding.setWebViewClient(new WebViewClient() {
        private boolean isExternalUrl(String url) {
            for (String regularExpression : mBrandingResult.externalUrlPatterns) {
                if (url.matches(regularExpression)) {
                    return true;
                }
            }
            return false;
        }

        @SuppressLint("DefaultLocale")
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            L.i("Branding is loading url: " + url);
            Uri uri = Uri.parse(url);
            String lowerCaseUrl = url.toLowerCase();
            if (lowerCaseUrl.startsWith("tel:") || lowerCaseUrl.startsWith("mailto:") || isExternalUrl(url)) {
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
                return true;
            } else if (lowerCaseUrl.startsWith(POKE)) {
                String tag = url.substring(POKE.length());
                poke(tag);
                return true;
            } else if (lowerCaseUrl.startsWith("http://") || lowerCaseUrl.startsWith("https://")) {
                if (mQRCodeScanner != null) {
                    mQRCodeScanner.onPause();
                }
                brandingHeaderText.setText(getString(R.string.loading));
                brandingHeader.setVisibility(View.VISIBLE);
                if (CloudConstants.isContentBrandingApp()) {
                    brandingFooter.setVisibility(View.VISIBLE);
                }
                mBranding.setVisibility(View.GONE);
                mBrandingHttp.setVisibility(View.VISIBLE);
                mBrandingHttp.loadUrl(url);
                return true;
            } else {
                brandingHeader.setVisibility(View.GONE);
                brandingFooter.setVisibility(View.GONE);
                mBrandingHttp.setVisibility(View.GONE);
                mBranding.setVisibility(View.VISIBLE);
            }
            return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            L.i("onPageFinished " + url);
            if (!mInfoSet && mService != null && mIsHtmlContent) {
                Map<String, Object> info = mFriendsPlugin.getRogerthatUserAndServiceInfo(mServiceEmail,
                        mServiceFriend);

                executeJS(true, "if (typeof rogerthat !== 'undefined') rogerthat._setInfo(%s)",
                        JSONValue.toJSONString(info));
                mInfoSet = true;
            }
        }

        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
            L.i("Checking access to: '" + url + "'");
            final URL parsedUrl;
            try {
                parsedUrl = new URL(url);
            } catch (MalformedURLException e) {
                L.d("Webview tried to load malformed URL");
                return new WebResourceResponse("text/plain", "UTF-8", null);
            }
            if (!parsedUrl.getProtocol().equals("file")) {
                return null;
            }
            File urlPath = new File(parsedUrl.getPath());
            if (urlPath.getAbsolutePath().startsWith(mBrandingResult.dir.getAbsolutePath())) {
                return null;
            }
            L.d("404: Webview tries to load outside its sandbox.");
            return new WebResourceResponse("text/plain", "UTF-8", null);
        }
    });

    mBrandingHttp.setWebViewClient(new WebViewClient() {
        @SuppressLint("DefaultLocale")
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            L.i("BrandingHttp is loading url: " + url);
            return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            brandingHeaderText.setText(view.getTitle());
            L.i("onPageFinished " + url);
        }
    });

    Intent intent = getIntent();
    mBrandingKey = intent.getStringExtra(BRANDING_KEY);
    mServiceEmail = intent.getStringExtra(SERVICE_EMAIL);
    mItemTagHash = intent.getStringExtra(ITEM_TAG_HASH);
    mItemLabel = intent.getStringExtra(ITEM_LABEL);
    mItemCoords = intent.getLongArrayExtra(ITEM_COORDS);
    mRunInBackground = intent.getBooleanExtra(RUN_IN_BACKGROUND, true);
}