123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- package hm.wine.seller;
- import android.Manifest;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.media.AudioManager;
- import android.os.Build;
- import android.os.Bundle;
- import android.webkit.*;
- import androidx.annotation.NonNull;
- import androidx.annotation.RequiresApi;
- import androidx.core.app.ActivityCompat;
- import com.alibaba.fastjson.JSON;
- import hm.wine.seller.lib.*;
- import hm.wine.seller.bean.HttpResp;
- import hm.wine.seller.bean.HttpRespData;
- import okhttp3.*;
- public class MainActivity extends Activity {
- private final String[] Permissions = {
- Manifest.permission.WRITE_EXTERNAL_STORAGE,
- Manifest.permission.READ_EXTERNAL_STORAGE
- };
- private WebView view = null;
- private final int PermissionCode = 1000;
- private final int SoftVer = 23120701;
- private final String Version = "v2023.12.07-01";
- private AudioManager audioManager = null;
- private int maxVolume = 0;
- @RequiresApi(api = Build.VERSION_CODES.O)
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main_activity);
- WakeLock.acquireWakeLock(this);
- Alarm.setRepeatingAlarm(this);
- startService(new Intent(this, KeepAlive.class));
- requestPermission();
- }
- @Override
- protected void onDestroy() {
- WakeLock.releaseWakeLock();
- Alarm.cancelRepeatingAlarm(this);
- stopService(new Intent(this, KeepAlive.class));
- if (view != null) {
- view.clearCache(true);
- view.loadUrl("about:blank");
- view.stopLoading();
- view.onPause();
- view.destroy();
- }
- UartUtils.close();
- super.onDestroy();
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- if (requestCode == PermissionCode) {
- for (int i = 0; i < permissions.length; i++) {
- if (grantResults[i] == 0) {
- requestPermission();
- return;
- }
- }
- start();
- }
- }
- private boolean checkPermissions() {
- for (String permission : Permissions) {
- if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED)
- return false;
- }
- return true;
- }
- private void requestPermission() {
- if (!checkPermissions()) {
- ActivityCompat.requestPermissions(this, Permissions, PermissionCode);
- } else start();
- }
- private void start() {
- audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
- maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
- while (audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) < maxVolume)
- audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, 0);
- enableGesture();
- versionCheck();
- launchWebsite();
- }
- private void versionCheck() {
- new Thread(() -> {
- String url = getResources().getString(R.string.version_url);
- Request request = new Request.Builder().url(url).build();
- OkHttpClient client = new OkHttpClient();
- try {
- Response response = client.newCall(request).execute();
- if (response.code() == 200 && response.body() != null) {
- String str = response.body().string();
- HttpResp httpResp = JSON.parseObject(str, HttpResp.class);
- HttpRespData data = httpResp.getData();
- if (httpResp.isStatus() && data.getVer() > SoftVer) {
- request = new Request.Builder().url(data.getUrl()).build();
- client.newCall(request).enqueue(new DownloadCallback(this::reboot));
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }).start();
- }
- private void reboot() {
- Intent restart = new Intent("wits.action.reboot");
- sendBroadcast(restart);
- }
- private void enableGesture() {
- Intent show = new Intent("show.systemui"), allow = new Intent("com.zc.open_gesture");
- sendBroadcast(show);
- sendBroadcast(allow);
- }
- private void disableGesture() {
- Intent hide = new Intent("hide.systemui"), forbid = new Intent("com.zc.close_gesture");
- sendBroadcast(hide);
- sendBroadcast(forbid);
- }
- public class AppContext {
- public void reboot() {
- MainActivity.this.reboot();
- }
- public void enableGesture() {
- MainActivity.this.enableGesture();
- }
- public void disableGesture() {
- MainActivity.this.disableGesture();
- }
- }
- @SuppressLint("SetJavaScriptEnabled")
- private void launchWebsite() {
- String front = getResources().getString(R.string.front_host);
- view = findViewById(R.id.webview);
- WebSettings settings = view.getSettings();
- settings.setJavaScriptEnabled(true);
- settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
- settings.setMediaPlaybackRequiresUserGesture(false);
- settings.setBlockNetworkImage(false);
- settings.setLoadsImagesAutomatically(true);
- settings.setDefaultTextEncodingName("utf-8");
- view.addJavascriptInterface(
- new JsBridge(view, Version, new AppContext(), getApplication(), audioManager, maxVolume),
- "android"
- );
- view.loadUrl(front);
- }
- }
|