| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- package com.qingdaofushan.homecaresas.activity;
- import static com.lzy.okgo.utils.HttpUtils.runOnUiThread;
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AppCompatActivity;
- import android.Manifest;
- import android.content.ContentResolver;
- import android.content.Intent;
- import android.database.Cursor;
- import android.graphics.Point;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.provider.MediaStore;
- import android.text.TextUtils;
- import android.util.Log;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.google.gson.Gson;
- import com.kongzue.dialogx.dialogs.WaitDialog;
- import com.lzy.okgo.OkGo;
- import com.lzy.okgo.model.Progress;
- import com.lzy.okgo.model.Response;
- import com.lzy.okgo.request.GetRequest;
- import com.lzy.okserver.OkDownload;
- import com.lzy.okserver.download.DownloadListener;
- import com.qingdaofushan.homecaresas.R;
- import com.qingdaofushan.homecaresas.bean.ExpandsBean;
- import com.qingdaofushan.homecaresas.bean.FetchUpBean;
- import com.qingdaofushan.homecaresas.bean.ItemCommonList;
- import com.qingdaofushan.homecaresas.bean.appVMBean;
- import com.qingdaofushan.homecaresas.component.ServerManager;
- import com.qingdaofushan.homecaresas.data.ConstantData;
- import com.qingdaofushan.homecaresas.data.JsonCallBack;
- import com.qingdaofushan.homecaresas.data.Url;
- import com.qingdaofushan.homecaresas.utils.CompressUtil;
- import com.qingdaofushan.homecaresas.utils.ToastUtil;
- import com.qingdaofushan.homecaresas.view.VersionCheckingUtil;
- import com.qingdaofushan.homecaresas.weiview.WebViewActivity;
- import com.tbruyelle.rxpermissions2.RxPermissions;
- import java.io.File;
- import java.io.FileInputStream;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.LinkedList;
- import java.util.List;
- import io.reactivex.Observable;
- import me.lake.librestreaming.core.listener.RESConnectionListener;
- import me.lake.librestreaming.ws.StreamAVOption;
- import me.lake.librestreaming.ws.StreamLiveCameraView;
- public class MainActivity extends AppCompatActivity {
- private String mRootUrl;
- private ServerManager mServerManager;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initPermission();
- EditText editText = findViewById(R.id.ed_text);
- findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- mServerManager.startServer();
- Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
- // intent.putExtra("url", editText.getText().toString());
- intent.putExtra("url", "http://127.0.0.1:8080");
- startActivity(intent);
- }
- });
- }
- private void initPermission() {
- Observable<Boolean> request = new RxPermissions(this).request(
- Manifest.permission.READ_EXTERNAL_STORAGE,
- Manifest.permission.CAMERA,
- Manifest.permission.WRITE_EXTERNAL_STORAGE
- );
- request.subscribe(aBoolean -> {
- if (aBoolean) {
- // 初始化HTTP服务
- mServerManager = new ServerManager(this);
- mServerManager.register();
- getVM();
- } else {
- ToastUtil.showToast("权限被拒绝");
- }
- });
- }
- private void initApp(String appWebUrl, int updataType) {
- OkDownload.getInstance().setFolder(Environment.getExternalStorageDirectory().getAbsolutePath() + "/homeLife/");
- File indexFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/homeLife/homeLifemini/index.html");
- File zipFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/homeLife/homeLifemini.zip");
- if (!indexFile.exists() || updataType == 1) {
- if (zipFile.exists()) {
- zipFile.delete();
- }
- WaitDialog.show("正在进行第一次加载...", 0f);
- // String url = "https://pwxmax.cn/homeLifemini.zip";
- GetRequest<File> requesta = OkGo.get(appWebUrl);
- OkDownload.request(appWebUrl, requesta)//
- .save()
- .register(new DownloadListener(appWebUrl) {
- @Override
- public void onStart(Progress progress) {
- ToastUtil.showToast("开始下载");
- }
- @Override
- public void onProgress(Progress progress) {
- WaitDialog.show("正在进行第一次加载...", progress.fraction);
- }
- @Override
- public void onError(Progress progress) {
- ToastUtil.showToast("错误");
- Log.e("pwx", "onError: " + progress.exception.toString());
- WaitDialog.dismiss();
- }
- @Override
- public void onFinish(File file, Progress progress) {
- CompressUtil.unzip(file, Environment.getExternalStorageDirectory().getAbsolutePath() + "/homeLife/");
- ToastUtil.showToast("下载完成");
- mServerManager.startServer();
- Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
- intent.putExtra("url", "http://127.0.0.1:8080");
- startActivity(intent);
- WaitDialog.dismiss();
- }
- @Override
- public void onRemove(Progress progress) {
- }
- }).start();
- }
- }
- private void getVM() {
- File versionFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/homeLife/homeLifemini/version");
- FetchUpBean fetchUpBean = new FetchUpBean();
- ArrayList<FetchUpBean.QuerysBean> querysBeans = new ArrayList<>();
- FetchUpBean.QuerysBean querysBean = new FetchUpBean.QuerysBean();
- FetchUpBean.QuerysBean.OrderbyBean orderbyBean = new FetchUpBean.QuerysBean.OrderbyBean();
- ArrayList<String> strings = new ArrayList<>();
- strings.add("createTime");
- orderbyBean.setColumns(strings);
- orderbyBean.setSeq("DESC");
- querysBean.setOrderby(orderbyBean);
- FetchUpBean.QuerysBean.LimitBean limitBean = new FetchUpBean.QuerysBean.LimitBean();
- limitBean.setStart(0);
- limitBean.setLength(1);
- querysBean.setLimit(limitBean);
- querysBean.setOrderby(orderbyBean);
- querysBeans.add(querysBean);
- fetchUpBean.setQuerys(querysBeans);
- OkGo.<ItemCommonList<appVMBean, ExpandsBean>>post(Url.URL_VERSION)
- .upJson(new Gson().toJson(fetchUpBean))
- .execute(new JsonCallBack<ItemCommonList<appVMBean, ExpandsBean>>() {
- @Override
- public void onSuccess(Response<ItemCommonList<appVMBean, ExpandsBean>> response) {
- super.onSuccess(response);
- ItemCommonList<appVMBean, ExpandsBean> body = response.body();
- if (body.getCode() == ConstantData.CODE_RESULT_SUCCESS) {
- if (body.getData() != null && body.getData().size() > 0) {
- if (body.getData().get(0).getUpdateType() == 2) {
- new VersionCheckingUtil().isChecking(MainActivity.this, body.getData().get(0), 0);
- } else if (body.getData().get(0).getUpdateType() == 1) {
- if (versionFile.exists()) {
- if (!readFileContent(versionFile).equals(body.getData().get(0).getWebVersion())) {
- initApp(body.getData().get(0).getAppWebUrl(), 1);
- }
- } else {
- initApp(body.getData().get(0).getAppWebUrl(), 1);
- }
- }
- }
- } else {
- ToastUtil.showToast("加载失败");
- }
- }
- @Override
- public void onError(Response<ItemCommonList<appVMBean, ExpandsBean>> response) {
- super.onError(response);
- }
- });
- }
- /**
- * Start notify.
- */
- public void onServerStart(String ip) {
- if (!TextUtils.isEmpty(ip)) {
- List<String> addressList = new LinkedList<>();
- mRootUrl = "http://127.0.0.1:8080/";
- addressList.add(mRootUrl);
- ToastUtil.showToast("服务器启动");
- } else {
- mRootUrl = null;
- }
- }
- /**
- * 【读取文件内容】
- **/
- public static String readFileContent(File file) {
- try {
- if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- byte[] buffer = new byte[32 * 1024];
- FileInputStream fis = new FileInputStream(file);
- int len = 0;
- StringBuffer sb = new StringBuffer("");
- while ((len = fis.read(buffer)) > 0) {
- sb.append(new String(buffer, 0, len));
- }
- fis.close();
- return sb.toString();
- }
- } catch (Exception e) {
- return "";
- }
- return null;
- }
- /**
- * Error notify.
- */
- public void onServerError(String message) {
- mRootUrl = null;
- }
- /**
- * Stop notify.
- */
- public void onServerStop() {
- mRootUrl = null;
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- mServerManager.unRegister();
- }
- }
|