wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
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
package nodomain.freeyourgadget.gadgetbridge.womp;
 
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
 
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
 
import org.json.JSONObject;
 
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenterv2;
 
/**
 * 分拣员工作台:与手环功能相互独立,未登录也可留在本页。
 */
public class WompWorkbenchFragment extends Fragment implements WompJsBridge.Host {
    private WebView webView;
    private String pendingScanCb;
    private ActivityResultLauncher<Intent> scanLauncher;
 
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        scanLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (pendingScanCb == null) {
                        return;
                    }
                    String code = "";
                    if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
                        code = result.getData().getStringExtra(WompScanActivity.EXTRA_CODE);
                    }
                    String script = "window.__wompCb && window.__wompCb("
                            + JSONObject.quote(pendingScanCb) + ",true,"
                            + JSONObject.quote(code == null ? "" : code) + ")";
                    evalJs(script);
                    pendingScanCb = null;
                });
    }
 
    @SuppressLint("SetJavaScriptEnabled")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_womp_workbench, container, false);
        webView = root.findViewById(R.id.womp_webview);
        WebSettings s = webView.getSettings();
        s.setJavaScriptEnabled(true);
        s.setDomStorageEnabled(true);
        s.setAllowFileAccess(true);
        s.setAllowContentAccess(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            s.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                if (url != null && (url.endsWith("home.html") || url.endsWith("login.html"))) {
                    view.clearHistory();
                }
            }
        });
        webView.setWebChromeClient(new WebChromeClient());
        webView.addJavascriptInterface(new WompJsBridge(requireActivity(), this), "Womp");
        String page = WompSession.isLoggedIn(requireContext()) ? "home.html" : "login.html";
        webView.loadUrl("file:///android_asset/womp/" + page);
        ensureLocationPermission();
        return root;
    }
 
    private void ensureLocationPermission() {
        if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION
            }, 7103);
        }
    }
 
    public boolean goBack() {
        if (webView != null && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return false;
    }
 
    @Override
    public void scan(String callbackId) {
        pendingScanCb = callbackId;
        if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.CAMERA}, 7102);
            return;
        }
        scanLauncher.launch(new Intent(requireContext(), WompScanActivity.class));
    }
 
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 7102 && pendingScanCb != null
                && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            scanLauncher.launch(new Intent(requireContext(), WompScanActivity.class));
        } else if (requestCode == 7102 && pendingScanCb != null) {
            evalJs("window.__wompCb && window.__wompCb("
                    + JSONObject.quote(pendingScanCb) + ",true,\"\")");
            pendingScanCb = null;
        }
    }
 
    @Override
    public void closePage() {
        if (getActivity() instanceof ControlCenterv2) {
            ((ControlCenterv2) getActivity()).showBandTab();
        }
    }
 
    @Override
    public void onLoggedIn() {
        WompKeepAliveService.start(requireContext());
        if (webView != null) {
            webView.loadUrl("file:///android_asset/womp/home.html");
        }
    }
 
    @Override
    public void onLogout() {
        if (webView != null) {
            webView.loadUrl("file:///android_asset/womp/login.html");
        }
    }
 
    @Override
    public void evalJs(String script) {
        if (webView != null && getActivity() != null) {
            getActivity().runOnUiThread(() -> {
                if (webView != null) {
                    webView.evaluateJavascript(script, null);
                }
            });
        }
    }
 
    @Override
    public void onResume() {
        super.onResume();
        if (webView != null) {
            webView.onResume();
        }
    }
 
    @Override
    public void onPause() {
        if (webView != null) {
            webView.onPause();
        }
        super.onPause();
    }
 
    @Override
    public void onDestroyView() {
        if (webView != null) {
            webView.destroy();
            webView = null;
        }
        super.onDestroyView();
    }
}