admin
2022-01-20 d8ef9a783b9e0b2a495f02fdf3daaf27ef49e99d
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package com.demo.app.utils.browser;
 
import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.provider.MediaStore;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
import com.demo.app.utils.Constant;
import com.demo.app.utils.JumpActivityUtil;
import com.demo.lib.common.dialog.DialogUtil;
import com.demo.lib.common.util.ui.LoadingDialogUtil;
import com.demo.library_ec.AlibcTradeUtil;
import com.demo.lib.common.util.Environment;
import com.demo.lib.common.util.common.StringUtils;
import com.demo.lib.common.util.security.MD5Utils;
 
import org.json.JSONException;
import org.json.JSONObject;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
 
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
 
public class MyJavaInterface extends BaseJavaInterface {
 
    Activity mContext;
    TextView tv_top_bar_middle, tv_top_bar_left2, tv_top_bar_right;
    ImageView iv_right;
    WebView webview;
    private boolean boo = false;
    private Dialog loadingDialog;
 
    public MyJavaInterface(Activity activity, WebView webview) {
        super(activity);
        mContext = activity;
        this.webview = webview;
        boo = true;
        loadingDialog = LoadingDialogUtil.getLoadingDialog(activity, "");
    }
 
    public MyJavaInterface(Activity activity, TextView tv_top_bar_middle
            , TextView tv_top_bar_left2, TextView tv_top_bar_right
            , ImageView iv_right, WebView webview) {
        super(activity);
        mContext = activity;
        this.tv_top_bar_middle = tv_top_bar_middle;
        this.tv_top_bar_left2 = tv_top_bar_left2;
        this.tv_top_bar_right = tv_top_bar_right;
        this.iv_right = iv_right;
        this.webview = webview;
        loadingDialog = LoadingDialogUtil.getLoadingDialog(activity, "");
    }
 
    @JavascriptInterface
    public void setTitle(final String title) {
//            tv_top_bar_middle.setText(title);
        if (!boo)
            tv_top_bar_middle.post(new Runnable() {
                @Override
                public void run() {
                    if (null != title && !StringUtils.isEmpty(title.trim()))
                        tv_top_bar_middle.setText(title);
                }
            });
    }
 
 
    @JavascriptInterface
    public void jumpPageWithFinishCurrentPage(String pageClassName, String paramJson) {
        Intent intent = null;
        JSONObject param = null;
        try {
            if (StringUtils.isEmpty(paramJson)) {
                param = null;
            } else {
                param = new JSONObject(paramJson);
            }
            if (StringUtils.isEmpty(pageClassName)) {
                return;
            } else {
                intent = new Intent(mContext, Class.forName(JumpActivityUtil.filterActivityName(pageClassName)));
            }
        } catch (JSONException e) {
            param = null;
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null != param) {
            @SuppressWarnings("unchecked")
            Iterator<String> its = param.keys();
            while (its.hasNext()) {
                String key = its.next();
                String value = param.optString(key);
                intent.putExtra(key, value);
            }
        }
        mContext.startActivity(intent);
        if (!boo)
            mContext.finish();
    }
 
    @JavascriptInterface
    public void finishPage() {
        if (!boo)
            iv_right.post(new Runnable() {
                @Override
                public void run() {
                    mContext.finish();
                }
            });
    }
 
    /**
     * 淘宝授权
     *
     * @param url
     */
    @JavascriptInterface
    public void tbAuth(String url) {
        if (StringUtils.isEmpty(url))
            return;
        AlibcTradeUtil.openAuthLink(mContext, url);
    }
 
    @JavascriptInterface
    public void showLoading() {
 
        mContext.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                DialogUtil.show(loadingDialog);
            }
        });
 
    }
 
    @JavascriptInterface
    public void hideLoading() {
        mContext.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                DialogUtil.dismiss(loadingDialog);
            }
        });
    }
 
    // 判断权限集合 是否授权 false授权 true未授权
    public boolean lacksPermissions(String... permissions) {
        for (String permission : permissions) {
            if (lacksPermission(permission)) {
                return true;
            }
        }
        return false;
    }
 
    // 判断是否缺少权限
    private boolean lacksPermission(String permission) {
        //权限未授权
        return ContextCompat.checkSelfPermission(mContext, permission) == PackageManager.PERMISSION_DENIED;
    }
 
    @JavascriptInterface
    public void savePicture(String url) {
        if (lacksPermissions(Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) { //存储权限未开启
            ActivityCompat.requestPermissions(mContext, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 10023);
        } else {//存储权限已开
            String md5 = MD5Utils.getMD532(url);
            String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                    + "/" + Constant.DOWNLOAD_DIR_NAME;
            if (!new File(path).exists()) {
                new File(path).mkdirs();
            }
 
            File file = new File(path + "/" + md5 + ".jpg");
            if (!file.exists()) {
                try {
                    File resultFile = saveImageFromPathToSdCard(mContext, url, path, md5 + ".jpg");
                    if (resultFile != null) {
                        mContext.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                MediaStore.Images.Media.insertImage(mContext.getContentResolver(), BitmapFactory.decodeFile(resultFile.getAbsolutePath()), resultFile.getName(), null);
                                Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                                Uri uri = Uri.fromFile(file);
                                intent.setData(uri);
                                mContext.sendBroadcast(intent);
                                Toast.makeText(mContext, "保存到相册成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                } catch (Exception e) {
                    Toast.makeText(mContext, "图片保存失败", Toast.LENGTH_LONG).show();
                    if (file.exists())
                        file.delete();
                }
 
            } else {
                Toast.makeText(mContext, "图片已经保存!", Toast.LENGTH_LONG).show();
            }
        }
    }
 
 
    //创建本地保存路径
    public static File createImageFilePath(String path, String imageFileName) throws IOException {
        File storageDir = new File(path);
        if (!storageDir.exists()) {
            storageDir.mkdir();
        }
        File image = new File(storageDir, imageFileName);
        return image;
    }
 
    //保存网络图片保存到本地
    public static final File saveImageFromPathToSdCard(Context context, String image,
                                                       String path, String imageFileName) {
        boolean success = false;
        File file = null;
        try {
            file = createImageFilePath(path, imageFileName);
            Bitmap bitmap = null;
            URL url = new URL(image);
            HttpURLConnection conn = null;
            conn = (HttpURLConnection) url.openConnection();
            InputStream is = null;
            is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            FileOutputStream outStream;
 
            outStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.flush();
            outStream.close();
            success = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        if (success) {
            MediaScannerConnection.scanFile(context, new String[]{file.getPath()}, null, null);
            return file;
        } else {
            return null;
        }
    }
 
}