yy1717
2021-04-13 89bb77a1280b9a117bfde34cbc62f92582d9a8b5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package safeluck.drive.evaluation.fragment;
 
import android.os.Build;
import android.os.Bundle;
 
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
 
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
 
import com.anyun.basecommonlib.MyLog;
 
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
 
import me.yokeyword.fragmentation.SupportFragment;
 
/**
 * MyApplication2
 * Created by lzw on 2019/3/19. 16:55:04
 * 邮箱:632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public class VPFragment extends SupportFragment {
 
    public static SupportFragment newInstance(String s){
        VPFragment vpFragment = new VPFragment();
        Bundle bundle = new Bundle();
        bundle.putString("argu",s);
        vpFragment.setArguments(bundle);
        return vpFragment;
    }
 
//    private String url = "http://192.168.40.201:9527/#/visitor/monitor?device_id=0314200100000004";
    private String url = "https://trainsim.aaej.cn/#/visitor/monitor?device_id=0314200100000004";
    private String url1 = "http://www.baidu.com";
    private static final String TAG = VPFragment.class.getCanonicalName();
 
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        hookWebView();
        WebView webView = new WebView(_mActivity);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        webView.setLayoutParams(layoutParams);
        Bundle bundle = getArguments();
        if (bundle != null){
                url = getArguments().getString("argu");
                Log.i(TAG,"收到参数="+url);
        }else{
 
        }
        WebSettings webSettings = webView.getSettings();
 
 
        webSettings.setDomStorageEnabled(true);
        webSettings.setAppCacheMaxSize(1024*1024*8);//存储的最大容量
        String appCachePath = _mActivity.getCacheDir().getAbsolutePath();
        webSettings.setAppCachePath(appCachePath);
        webSettings.setAllowFileAccess(true);
        webSettings.setAppCacheEnabled(true);
                webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webView.loadUrl(url);
 
        return webView;
    }
    public static void hookWebView() {
        int sdkInt = Build.VERSION.SDK_INT;
        try {
            Class<?> factoryClass = Class.forName("android.webkit.WebViewFactory");
            Field field = factoryClass.getDeclaredField("sProviderInstance");
            field.setAccessible(true);
            Object sProviderInstance = field.get(null);
            if (sProviderInstance != null) {
                MyLog.i("sProviderInstance isn't null");
                return;
            }
            Method getProviderClassMethod;
            if (sdkInt > 22) {
                getProviderClassMethod = factoryClass.getDeclaredMethod("getProviderClass");
            } else if (sdkInt == 22) {
                getProviderClassMethod = factoryClass.getDeclaredMethod("getFactoryClass");
            } else {
                MyLog.i("Don't need to Hook WebView");
                return;
            }
            getProviderClassMethod.setAccessible(true);
            Class<?> providerClass = (Class<?>) getProviderClassMethod.invoke(factoryClass);
            Class<?> delegateClass = Class.forName("android.webkit.WebViewDelegate");
            Constructor<?> providerConstructor = providerClass.getConstructor(delegateClass);
            if (providerConstructor != null) {
                providerConstructor.setAccessible(true);
                Constructor<?> declaredConstructor = delegateClass.getDeclaredConstructor();
                declaredConstructor.setAccessible(true);
                sProviderInstance = providerConstructor.newInstance(declaredConstructor.newInstance());
                MyLog.i("sProviderInstance:{}", sProviderInstance);
                field.set("sProviderInstance", sProviderInstance);
            }
            MyLog.i("Hook done!");
        } catch (Throwable e) {
            MyLog.i(e);
        }
    }
}