Przeglądaj źródła

New : Add SimpleWebViewActivity

Wayne 7 lat temu
rodzic
commit
b9c32d08c7

+ 176 - 0
src/main/java/com/kdanmobile/kdan_others_library_for_android/view/SimpleWebViewActivity.java

@@ -0,0 +1,176 @@
+package com.kdanmobile.kdan_others_library_for_android.view;
+
+import android.annotation.SuppressLint;
+import android.app.Activity;
+import android.content.ActivityNotFoundException;
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.Bundle;
+import android.support.annotation.ColorInt;
+import android.support.annotation.ColorRes;
+import android.support.v7.app.AppCompatActivity;
+import android.view.View;
+import android.view.Window;
+import android.webkit.DownloadListener;
+import android.webkit.WebChromeClient;
+import android.webkit.WebView;
+import android.webkit.WebViewClient;
+import android.widget.ProgressBar;
+import android.widget.TextView;
+
+import com.kdanmobile.kdan_others_library_for_android.R;
+
+
+public class SimpleWebViewActivity extends AppCompatActivity {
+    public final static String INTENT_EXTRA_URL = "my_url_to_webview";
+    private WebView mWebView;
+    private ProgressBar mProgressBar;
+    private String url;
+
+    private TextView titleTextView;
+    private TextView urlTextView;
+
+    public static void launch(Context context, String url) {
+        Intent intent = new Intent(context, SimpleWebViewActivity.class);
+        intent.putExtra(SimpleWebViewActivity.INTENT_EXTRA_URL, url);
+        context.startActivity(intent);
+    }
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        getWindow().requestFeature(Window.FEATURE_PROGRESS);
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.simplewebview_layout);
+        titleTextView = (TextView) findViewById(R.id.textView_simple_web_view_title);
+        urlTextView = (TextView) findViewById(R.id.textView_simple_web_view_url);
+        findViewById(R.id.button_simple_web_view_close).setOnClickListener(v -> onClickCloseBtn());
+        findViewById(R.id.button_simple_web_view_open_in_browser).setOnClickListener(v -> onClickOpenInBrowserBtn());
+
+        initWebView();
+        initProgressBar();
+
+        Intent intent = getIntent();
+        if(intent != null) {
+            processIntent(intent);
+        }
+    }
+
+    @Override
+    protected void onNewIntent(Intent intent) {
+        super.onNewIntent(intent);
+        processIntent(intent);
+
+    }
+
+    @Override
+    public void onBackPressed() {
+        if(mWebView != null && mWebView.canGoBack()){
+            mWebView.goBack();
+            return;
+        }
+        super.onBackPressed();
+    }
+
+    @SuppressLint("SetJavaScriptEnabled")
+    private void initWebView() {
+        mWebView = (WebView) findViewById(R.id.simple_webview);
+        mWebView.getSettings().setJavaScriptEnabled(true);
+        mWebView.requestFocus();
+        mWebView.setWebViewClient(new MyWebViewClient());
+        mWebView.setWebChromeClient(new MyWebChromeClient());
+        mWebView.setDownloadListener(new DownloadListener() {
+            public void onDownloadStart(String url, String userAgent,
+                                        String contentDisposition, String mimetype,
+                                        long contentLength) {
+                Uri uri = Uri.parse(url);
+                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
+                startActivity(intent);
+            }
+        });
+    }
+
+    private void initProgressBar(){
+        mProgressBar = (ProgressBar) findViewById(R.id.webview_progressbar);
+        mProgressBar.setProgress(0);
+        mProgressBar.setVisibility(View.VISIBLE);
+    }
+
+    private void processIntent(Intent intent){
+        url = intent.getStringExtra(INTENT_EXTRA_URL);
+        if (url != null && mWebView != null) {
+            mWebView.loadUrl(url);
+            urlTextView.setText(url);
+        }
+    }
+
+    protected void onClickCloseBtn() {
+        finish();
+    }
+
+    protected void onClickOpenInBrowserBtn() {
+        Uri uri = Uri.parse(url);
+        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
+        startActivity(intent);
+    }
+
+    protected void setToolbarBgColorRes(@ColorRes int colorRes) {
+        int color = getResources().getColor(colorRes);
+        setToolbarBgColor(color);
+    }
+
+    protected void setToolbarBgColor(@ColorInt int color) {
+        findViewById(R.id.toolbar_simple_web_view).setBackgroundColor(color);
+    }
+
+    protected void setUrlTextColorRes(@ColorRes int colorRes) {
+        int color = getResources().getColor(colorRes);
+        setUrlTextColor(color);
+    }
+
+    protected void setUrlTextColor(@ColorInt int color) {
+        ((TextView) findViewById(R.id.textView_simple_web_view_url)).setTextColor(color);
+    }
+
+    private class MyWebViewClient extends WebViewClient {
+        @Override
+        public boolean shouldOverrideUrlLoading(WebView view, String url) {
+            if (Uri.parse(url).getScheme().equals("market")) {
+                try {
+                    Intent intent = new Intent(Intent.ACTION_VIEW);
+                    intent.setData(Uri.parse(url));
+                    Activity host = (Activity) view.getContext();
+                    host.startActivity(intent);
+                    return true;
+                } catch (ActivityNotFoundException e) {
+                    // Google Play app is not installed, you may want to open the app store link
+                    Uri uri = Uri.parse(url);
+                    String webLink = "http://play.google.com/store/apps/" + uri.getHost() + "?" + uri.getQuery();
+                    view.loadUrl(webLink);
+                    return false;
+                }
+            }
+            return false;
+        }
+    }
+
+    private class MyWebChromeClient extends WebChromeClient {
+        @Override
+        public void onReceivedTitle(WebView view, String title) {
+            super.onReceivedTitle(view, title);
+            titleTextView.setText(title);
+        }
+
+        @Override
+        public void onProgressChanged(WebView view, int progress) {
+            if(mProgressBar != null){
+                if (progress >= 100) {
+                    mProgressBar.setProgress(0);
+                } else {
+                    mProgressBar.setProgress(progress);
+                }
+            }
+        }
+    }
+}
+

+ 4 - 0
src/main/res/drawable-xhdpi/ic_clear_white_24dp.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="24dp">
+    <path android:fillColor="#FFFFFF" android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
+</vector>

+ 4 - 0
src/main/res/drawable-xhdpi/ic_open_in_browser_white_24dp.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="24dp">
+    <path android:fillColor="#FFFFFF" android:pathData="M19,4L5,4c-1.11,0 -2,0.9 -2,2v12c0,1.1 0.89,2 2,2h4v-2L5,18L5,8h14v10h-4v2h4c1.1,0 2,-0.9 2,-2L21,6c0,-1.1 -0.89,-2 -2,-2zM12,10l-4,4h3v6h2v-6h3l-4,-4z"/>
+</vector>

+ 77 - 0
src/main/res/layout/simplewebview_layout.xml

@@ -0,0 +1,77 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+              xmlns:tools="http://schemas.android.com/tools"
+              android:orientation="vertical"
+              android:layout_width="match_parent"
+              android:layout_height="match_parent">
+
+    <android.support.v7.widget.Toolbar
+        android:id="@+id/toolbar_simple_web_view"
+        android:background="#5f5f5f"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content">
+
+        <RelativeLayout
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content">
+
+            <ImageButton
+                android:id="@+id/button_simple_web_view_close"
+                android:background="?attr/selectableItemBackgroundBorderless"
+                android:src="@drawable/ic_clear_white_24dp"
+                android:layout_width="48dp"
+                android:layout_height="48dp"/>
+
+            <ImageButton
+                android:layout_alignParentRight="true"
+                android:id="@+id/button_simple_web_view_open_in_browser"
+                android:background="?attr/selectableItemBackgroundBorderless"
+                android:src="@drawable/ic_open_in_browser_white_24dp"
+                android:layout_width="48dp"
+                android:layout_height="48dp"/>
+
+            <LinearLayout
+                android:orientation="vertical"
+                android:layout_marginLeft="16dp"
+                android:layout_marginRight="16dp"
+                android:layout_toLeftOf="@+id/button_simple_web_view_open_in_browser"
+                android:layout_toRightOf="@+id/button_simple_web_view_close"
+                android:layout_centerVertical="true"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content">
+
+                <TextView
+                    android:id="@+id/textView_simple_web_view_title"
+                    tools:text="Title"
+                    android:textSize="22sp"
+                    android:textColor="@android:color/white"
+                    android:maxLines="1"
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content"/>
+
+                <TextView
+                    android:id="@+id/textView_simple_web_view_url"
+                    tools:text="http://123.com"
+                    android:textColor="#92918f"
+                    android:textSize="10sp"
+                    android:maxLines="1"
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content"/>
+            </LinearLayout>
+
+        </RelativeLayout>
+    </android.support.v7.widget.Toolbar>
+
+    <ProgressBar android:id="@+id/webview_progressbar"
+        style="?android:attr/progressBarStyleHorizontal"
+        android:layout_width="fill_parent"
+        android:layout_height="5dp"
+        android:max="100">
+    </ProgressBar>
+
+    <WebView xmlns:android="http://schemas.android.com/apk/res/android"
+        android:id="@+id/simple_webview"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent">
+    </WebView>
+</LinearLayout>