content: package com.example.test;
  import android.annotation.SuppressLint;
  import android.app.DownloadManager;
  import android.content.BroadcastReceiver;
  import android.content.ContentResolver;
  import android.content.Context;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.database.Cursor;
  import android.net.Uri;
  import android.os.Build;
  //import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.os.Environment;
  import android.provider.MediaStore;
  import android.view.KeyEvent;
  import android.webkit.CookieManager;
  import android.webkit.DownloadListener;
  import android.webkit.URLUtil;
  import android.webkit.ValueCallback;
  import android.webkit.WebChromeClient;
  import android.webkit.WebSettings;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;
  import android.widget.Toast;
  
  import androidx.appcompat.app.AppCompatActivity;
  
  import java.io.File;
  import java.util.Objects;
  
  import com.example.test.ForegroundService;
  import com.example.test.LocalService;
  import com.example.test.RemoteService;
  import android.app.job.JobInfo;
  import android.app.job.JobScheduler;
  import android.content.ComponentName;
  import android.util.Log;
  
  public class MainActivity extends AppCompatActivity {
  
      private WebView webView;
      private final int PICK_REQUEST = 10001;
      ValueCallback<Uri> mFilePathCallback;
      ValueCallback<Uri[]> mFilePathCallbackArray;
      private static final int JOB_ID = 100;
  
      @SuppressLint("SetJavaScriptEnabled")
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          //隐藏ActionBar
          Objects.requireNonNull(getSupportActionBar()).hide();
          setContentView(R.layout.activity_main);
          //WebView加载页面
          webView = findViewById(R.id.web_view);
          webView.getSettings().setJavaScriptEnabled(true);
          webView.setWebViewClient(new WebViewClient());
          // code from https://blog.csdn.net/qq_21138819/article/details/56676007 by 欢子-3824
          webView.setWebChromeClient(new WebChromeClient() {
              // Andorid 4.1----4.4
              public void openFileChooser(ValueCallback<Uri> uploadFile, String acceptType, String capture) {
  
                  mFilePathCallback = uploadFile;
                  handle(uploadFile);
              }
  
              // for 5.0+
              public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
                  if (mFilePathCallbackArray != null) {
                      mFilePathCallbackArray.onReceiveValue(null);
                  }
                  mFilePathCallbackArray = filePathCallback;
                  handleup(filePathCallback);
                  return true;
              }
  
              private void handle(ValueCallback<Uri> uploadFile) {
                  Intent intent = new Intent(Intent.ACTION_PICK);
                  // 设置允许上传的文件类型
                  intent.setType("*/*");
                  startActivityForResult(intent, PICK_REQUEST);
              }
  
              private void handleup(ValueCallback<Uri[]> uploadFile) {
                  Intent intent = new Intent(Intent.ACTION_PICK);
                  intent.setType("*/*");
                  startActivityForResult(intent, PICK_REQUEST);
              }
          });
  
          // wevView监听 H5 页面的下载事件
          // code from https://github.com/madhan98/Android-webview-upload-download/blob/master/app/src/main/java/com/my/newproject/MainActivity.java by Madhan
          webView.setDownloadListener(new DownloadListener() {
  
              public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
  
                  DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
  
                  String cookies = CookieManager.getInstance().getCookie(url);
  
                  request.addRequestHeader("cookie", cookies);
  
                  request.addRequestHeader("User-Agent", userAgent);
  
                  request.setDescription("下载中...");
  
                  request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype));
  
                  request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype));
  
                  DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
  
                  manager.enqueue(request);
  
                  showMessage("下载中...");
  
                  //Notif if success
  
                  BroadcastReceiver onComplete = new BroadcastReceiver() {
  
                      public void onReceive(Context ctxt, Intent intent) {
  
                          showMessage("下载完成");
  
                          unregisterReceiver(this);
  
                      }};
  
                  registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
  
              }
  
          });
  
          //该方法解决的问题是打开浏览器不调用系统浏览器，直接用 webView 打开
          webView.setWebViewClient(new WebViewClient() {
              @Override
              public boolean shouldOverrideUrlLoading(WebView view, String url) {
                  view.loadUrl(url);
                  return true;
              }
          });
  
          // 这里填你需要打包的 H5 页面链接
          webView.loadUrl("https://tv.aizhijia.top/naozhong.html");
  
          //显示一些小图片（头像）
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
              webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
          }
          // 允许使用 localStorage sessionStorage
          webView.getSettings().setDomStorageEnabled(true);
          // 是否支持 html 的 meta 标签
          webView.getSettings().setUseWideViewPort(true);
          webView.getSettings().setAllowFileAccess(true);
          webView.getSettings().getAllowUniversalAccessFromFileURLs();
          webView.getSettings().getAllowFileAccessFromFileURLs();
          webView.getSettings().setJavaScriptEnabled(true);
          webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
  
          startForegroundService();
  
          webView.loadUrl("javascript:(function() { " +
              "var audio = document.getElementById('alarmSound');" +
              "audio.load();" +
              "})()");
      }
  
      //设置回退页面
      public boolean onKeyDown(int keyCode, KeyEvent event) {
          if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
              webView.goBack();
              return true;
          }
          return super.onKeyDown(keyCode, event);
      }
  
      @Deprecated
      public void showMessage(String _s) {
          Toast.makeText(getApplicationContext(), _s, Toast.LENGTH_SHORT).show();
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          webView.destroy();
          webView = null;
      }
  
  
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
  
          if (requestCode == PICK_REQUEST) {
              if (null != data) {
                  Uri uri = data.getData();
                  handleCallback(uri);
              } else {
                  // 取消了照片选取的时候调用
                  handleCallback(null);
              }
          } else {
              // 取消了照片选取的时候调用
              handleCallback(null);
          }
      }
  
      /**
       * 处理WebView的回调
       *
       * @param uri
       */
      private void handleCallback(Uri uri) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
              if (mFilePathCallbackArray != null) {
                  if (uri != null) {
                      mFilePathCallbackArray.onReceiveValue(new Uri[]{uri});
                  } else {
                      mFilePathCallbackArray.onReceiveValue(null);
                  }
                  mFilePathCallbackArray = null;
              }
          } else {
              if (mFilePathCallback != null) {
                  if (uri != null) {
                      String url = getFilePathFromContentUri(uri, getContentResolver());
                      Uri u = Uri.fromFile(new File(url));
  
                      mFilePathCallback.onReceiveValue(u);
                  } else {
                      mFilePathCallback.onReceiveValue(null);
                  }
                  mFilePathCallback = null;
              }
          }
      }
  
      public static String getFilePathFromContentUri(Uri selectedVideoUri, ContentResolver contentResolver) {
          String filePath;
          String[] filePathColumn = {MediaStore.MediaColumns.DATA};
  
          Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null);
  //      也可用下面的方法拿到cursor
  //      Cursor cursor = this.context.managedQuery(selectedVideoUri, filePathColumn, null, null, null);
  
          cursor.moveToFirst();
  
          int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
          filePath = cursor.getString(columnIndex);
          cursor.close();
          return filePath;
      }
  
      private void startForegroundService() {
          // 启动前台服务
          Intent serviceIntent = new Intent(this, ForegroundService.class);
          startService(serviceIntent);
          
          // 启动双进程保活服务
          startService(new Intent(this, LocalService.class));
          startService(new Intent(this, RemoteService.class));
          
          // 设置并启动 JobScheduler
          scheduleJob();
      }
      
      private void scheduleJob() {
          ComponentName serviceComponent = new ComponentName(this, JobSchedulerService.class);
          JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, serviceComponent);
          
          // 设置任务在网络可用时执行
          builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
          
          // 设置任务在设备充电时执行
          builder.setRequiresCharging(true);
          
          // 设置任务的最小延迟时间（15分钟）
          builder.setMinimumLatency(5 * 60 * 1000);
          
          // 设置任务的最大延迟时间（30分钟）
          builder.setOverrideDeadline(30 * 60 * 1000);
          
          // 设置在设备重启后是否继续执行
          builder.setPersisted(true);
          
          JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
          if (jobScheduler != null) {
              int resultCode = jobScheduler.schedule(builder.build());
              if (resultCode == JobScheduler.RESULT_SUCCESS) {
                  Log.d("MainActivity", "Job scheduled successfully!");
              }
          }
      }
  
  }
  