| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- package com.baoshi.piece.utils;
- import android.app.Activity;
- import android.util.Log;
- import android.webkit.JavascriptInterface;
- import android.webkit.WebView;
- import com.baoshi.piece.db.dao.DeliveryDao;
- import com.baoshi.piece.db.po.DeliveryRecord;
- import com.yechaoa.yutils.GsonUtil;
- import com.yechaoa.yutils.SpUtil;
- import com.yechaoa.yutils.YUtils;
- import java.util.List;
- import java.util.Map;
- /**
- * 提供给H5调用的接口
- */
- public class JavaScriptInterface {
- private DeliveryDao deliveryDao;
- private final Activity activity;
- private final WebView webView;
- private final Map<String, String> headerParams;
- private final String SAVE_USER_ID = "USER_ID";
- public JavaScriptInterface(Activity activity, Map<String, String> headerParams, WebView webView, DeliveryDao deliveryDao) {
- this.activity = activity;
- this.webView = webView;
- this.headerParams = headerParams;
- this.deliveryDao = new DeliveryDao(activity);
- }
- /**
- * 返回
- */
- @JavascriptInterface
- public void goBack() {
- activity.moveTaskToBack(true);
- }
- /**
- * 关闭当前页面,回到主页
- */
- @JavascriptInterface
- public void finish() {
- activity.finish();
- }
- /**
- * H5获取header数据(window.android.getHeader)
- *
- * @return
- */
- @JavascriptInterface
- public String getHeader() {
- return GsonUtil.GsonString(headerParams);
- }
- /**
- * H5获取mac地址(window.android.getMacAddress)
- */
- @JavascriptInterface
- public String readMacAddress() {
- return MacAddressUtils.readMacAddress(activity);
- }
- /**
- * H5保存mac地址(window.android.saveMacAddress)
- * @param macAddress mac地址
- * @return true:保存成功,false:保存失败
- */
- @JavascriptInterface
- public boolean saveMacAddress(String macAddress) {
- return MacAddressUtils.saveMacAddress(activity, macAddress);
- }
- /**
- * 添加新记录
- *
- * @param macAddress mac地址
- * @param deliveryNo 快递单号
- * @param userId 用户id
- * @param userName 用户名
- * @return
- */
- @JavascriptInterface
- public long addDelivery(String macAddress, String deliveryNo, String userId, String userName) {
- DeliveryRecord record = new DeliveryRecord(deliveryNo, macAddress, userId, userName);
- return deliveryDao.addDelivery(record);
- }
- /**
- * 分页查询全部记录
- */
- @JavascriptInterface
- public String pageDelivery(int page, int size) {
- return GsonUtil.GsonString(deliveryDao.getRecordsPaginated(page, size));
- }
- /**
- * 焦点
- */
- @JavascriptInterface
- public void focus() {
- webView.requestFocus();
- }
- /**
- * 扫描成功语音
- */
- @JavascriptInterface
- public void scanSuccessVib() {
- TipActionUtils.getInstance().scanSuccessTip(activity);
- }
- /**
- * 扫描失败语音
- */
- @JavascriptInterface
- public void scanErrorVib() {
- TipActionUtils.getInstance().scanErrorTip(activity);
- }
- /**
- * 重复扫描语音
- */
- @JavascriptInterface
- public void scanRepeatVib() {
- Log.d("TAG", "scanRepeatVib");
- TipActionUtils.getInstance().scanRepeatTip(activity);
- }
- @JavascriptInterface
- public Boolean isDeliveryNoExists(String deliveryNo) {
- return deliveryDao.isDeliveryNoExists(deliveryNo);
- }
- @JavascriptInterface
- public void saveUserId(String userId) {
- SpUtil.setString(SAVE_USER_ID, userId);
- }
- @JavascriptInterface
- public String getUserId() {
- return SpUtil.getString(SAVE_USER_ID);
- }
- @JavascriptInterface
- public int getErrRecordsCount() {
- return deliveryDao.getErrRecordsCount();
- }
- @JavascriptInterface
- public String listErrRecords() {
- return GsonUtil.GsonString(deliveryDao.listErrRecords());
- }
- @JavascriptInterface
- public int updateDeliveryPushFail(String deliveryNo) {
- return deliveryDao.updateDeliveryPushFail(deliveryNo);
- }
- @JavascriptInterface
- public int markAsPushed(String deliveryNo) {
- return deliveryDao.markAsPushed(deliveryNo);
- }
- @JavascriptInterface
- public String getVersionName() {
- return YUtils.getVersionName();
- }
- @JavascriptInterface
- public void checkUpdate() {
- // 更新检查
- new UpdateUtils().init();
- }
- }
|