Search in sources :

Example 1 with CustomWebView

use of com.baker.abaker.views.CustomWebView in project baker-android by bakerframework.

the class MagazineActivity method setPagerView.

@SuppressLint("SetJavaScriptEnabled")
private void setPagerView(final BookJson book) {
    String path = "file://".concat(Configuration.getMagazinesDirectory(this)).concat(File.separator).concat(book.getMagazineName());
    if (STANDALONE_MODE) {
        boolean fromAssets = !this.getResources().getBoolean(R.bool.sa_read_from_custom_directory);
        if (fromAssets) {
            path = "file:///android_asset".concat(File.separator).concat(getString(R.string.sa_books_directory)).concat(File.separator).concat(book.getMagazineName());
        }
    } else if (ENABLE_TUTORIAL) {
        path = "file:///android_asset".concat(File.separator).concat("abaker_usage");
    }
    if (book.getLiveUrl() != null) {
        path = book.getLiveUrl();
    }
    final String finalPath = path;
    Log.d(this.getClass().toString(), "THE PATH FOR LOADING THE PAGES WILL BE: " + finalPath);
    // ViewPager and its adapters use support library
    // fragments, so use getSupportFragmentManager.
    webViewPagerAdapter = new WebViewFragmentPagerAdapter(getSupportFragmentManager(), book, finalPath, this);
    pager = (CustomWebViewPager) findViewById(R.id.pager);
    pager.setAdapter(webViewPagerAdapter);
    // Bind the title indicator to the adapter
    LinePageIndicator indicator = (LinePageIndicator) findViewById(R.id.indicator);
    if (!ENABLE_TUTORIAL) {
        indicator.setVisibility(View.GONE);
    }
    indicator.setViewPager(pager);
    indicator.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            if (hasElapsedTime) {
                Long timeElapsed = System.currentTimeMillis() - startedTime;
                if (timeElapsed > resources.getInteger(R.integer.ga_page_view_time_elapsed))
                    ((ABakerApp) MagazineActivity.this.getApplication()).sendTimingEvent(metaBakerPageCategory, timeElapsed, getString(R.string.issue_page_view), metaBakerPageName);
                ((ABakerApp) MagazineActivity.this.getApplication()).sendEvent(metaBakerPageCategory, getString(R.string.issue_page_view), metaBakerPageName);
                startedTime = 0L;
                hasElapsedTime = false;
                previousIndex = -1;
                metaBakerPageName = "";
                metaBakerPageCategory = "";
            }
            Log.d(this.getClass().getName(), "Loading page at index: " + position);
            detectFirstOrLastPage();
            if (resources.getBoolean(R.bool.ga_enable) && resources.getBoolean(R.bool.ga_register_page_view_event)) {
                String page = finalPath + book.getMagazineName() + File.separator + book.getContents().get(position);
                Map<String, String> tags = getBakerMetaTags(page);
                if (tags.containsKey("baker-page-name")) {
                    String name = tags.get("baker-page-name");
                    String category = tags.containsKey("baker-page-category") ? tags.get("baker-page-category") : getString(R.string.issues_category);
                    name = (name.isEmpty()) ? book.getContents().get(position) : name;
                    category = (category.isEmpty()) ? getString(R.string.issues_category) : category;
                    if (resources.getBoolean(R.bool.ga_register_page_view_time_elapsed_event)) {
                        startedTime = System.currentTimeMillis();
                        hasElapsedTime = true;
                        previousIndex = position;
                        metaBakerPageName = name;
                        metaBakerPageCategory = category;
                    } else {
                        ((ABakerApp) MagazineActivity.this.getApplication()).sendEvent(category, getString(R.string.issue_page_view), name);
                    }
                }
            }
        }
    });
    CustomWebView viewIndex = (CustomWebView) findViewById(R.id.webViewIndex);
    viewIndex.getSettings().setJavaScriptEnabled(true);
    viewIndex.getSettings().setUseWideViewPort(true);
    viewIndex.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String stringUrl) {
            // mailto links will be handled by the OS.
            if (stringUrl.startsWith("mailto:")) {
                Uri uri = Uri.parse(stringUrl);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            } else {
                try {
                    URL url = new URL(stringUrl);
                    // We try to remove the referrer string to avoid passing it to the server in case the URL is an external link.
                    String referrer = "";
                    if (url.getQuery() != null) {
                        Map<String, String> variables = Configuration.splitUrlQueryString(url);
                        String finalQueryString = "";
                        for (Map.Entry<String, String> entry : variables.entrySet()) {
                            if (entry.getKey().equals("referrer")) {
                                referrer = entry.getValue();
                            } else {
                                finalQueryString += entry.getKey() + "=" + entry.getValue() + "&";
                            }
                        }
                        if (!finalQueryString.isEmpty()) {
                            finalQueryString = "?" + finalQueryString.substring(0, finalQueryString.length() - 1);
                        }
                        stringUrl = stringUrl.replace("?" + url.getQuery(), finalQueryString);
                    }
                    if (!url.getProtocol().equals("file")) {
                        Log.d("REFERRER>>>", "THE REFERRER IS: " + referrer);
                        if (referrer.equals(MagazineActivity.this.getString(R.string.url_external_referrer))) {
                            Uri uri = Uri.parse(stringUrl);
                            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                            startActivity(intent);
                        } else if (referrer.equals(MagazineActivity.this.getString(R.string.url_gindpubs_referrer))) {
                            MagazineActivity.this.openLinkInModal(stringUrl);
                            return true;
                        } else {
                            // We return false to tell the webview that we are not going to handle the URL override.
                            return false;
                        }
                    } else {
                        stringUrl = url.getPath().substring(url.getPath().lastIndexOf("/") + 1);
                        Log.d(">>>URL_DATA", "FINAL INTERNAL HTML FILENAME = " + stringUrl);
                        int index = MagazineActivity.this.getJsonBook().getContents().indexOf(stringUrl);
                        if (index != -1) {
                            Log.d(this.getClass().toString(), "Index to load: " + index + ", page: " + stringUrl);
                            MagazineActivity.this.getPager().setCurrentItem(index);
                            view.setVisibility(View.GONE);
                        } else {
                            // If the file DOES NOT exist, we won't load it.
                            File htmlFile = new File(url.getPath());
                            if (htmlFile.exists()) {
                                return false;
                            }
                        }
                    }
                } catch (MalformedURLException ex) {
                    Log.d(">>>URL_DATA", ex.getMessage());
                } catch (UnsupportedEncodingException ex) {
                }
            }
            return true;
        }
    });
    viewIndex.loadUrl(path + File.separator + "index.html");
    viewIndex.setBackgroundColor(0x00000000);
    viewIndex.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
}
Also used : MalformedURLException(java.net.MalformedURLException) LinePageIndicator(com.viewpagerindicator.LinePageIndicator) WebViewFragmentPagerAdapter(com.baker.abaker.views.WebViewFragmentPagerAdapter) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Intent(android.content.Intent) ViewPager(android.support.v4.view.ViewPager) CustomWebViewPager(com.baker.abaker.views.CustomWebViewPager) Uri(android.net.Uri) SuppressLint(android.annotation.SuppressLint) URL(java.net.URL) CustomWebView(com.baker.abaker.views.CustomWebView) CustomWebView(com.baker.abaker.views.CustomWebView) WebView(android.webkit.WebView) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) WebViewClient(android.webkit.WebViewClient) SuppressLint(android.annotation.SuppressLint)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 Intent (android.content.Intent)1 Uri (android.net.Uri)1 ViewPager (android.support.v4.view.ViewPager)1 WebView (android.webkit.WebView)1 WebViewClient (android.webkit.WebViewClient)1 CustomWebView (com.baker.abaker.views.CustomWebView)1 CustomWebViewPager (com.baker.abaker.views.CustomWebViewPager)1 WebViewFragmentPagerAdapter (com.baker.abaker.views.WebViewFragmentPagerAdapter)1 LinePageIndicator (com.viewpagerindicator.LinePageIndicator)1 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1