MainActivity.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package com.qingdaofushan.homecaresas.activity;
  2. import static com.lzy.okgo.utils.HttpUtils.runOnUiThread;
  3. import androidx.annotation.Nullable;
  4. import androidx.appcompat.app.AppCompatActivity;
  5. import android.Manifest;
  6. import android.content.ContentResolver;
  7. import android.content.Intent;
  8. import android.database.Cursor;
  9. import android.graphics.Point;
  10. import android.net.Uri;
  11. import android.os.Bundle;
  12. import android.os.Environment;
  13. import android.provider.MediaStore;
  14. import android.text.TextUtils;
  15. import android.util.Log;
  16. import android.view.View;
  17. import android.widget.EditText;
  18. import android.widget.Toast;
  19. import com.google.gson.Gson;
  20. import com.kongzue.dialogx.dialogs.WaitDialog;
  21. import com.lzy.okgo.OkGo;
  22. import com.lzy.okgo.model.Progress;
  23. import com.lzy.okgo.model.Response;
  24. import com.lzy.okgo.request.GetRequest;
  25. import com.lzy.okserver.OkDownload;
  26. import com.lzy.okserver.download.DownloadListener;
  27. import com.qingdaofushan.homecaresas.R;
  28. import com.qingdaofushan.homecaresas.bean.ExpandsBean;
  29. import com.qingdaofushan.homecaresas.bean.FetchUpBean;
  30. import com.qingdaofushan.homecaresas.bean.ItemCommonList;
  31. import com.qingdaofushan.homecaresas.bean.appVMBean;
  32. import com.qingdaofushan.homecaresas.component.ServerManager;
  33. import com.qingdaofushan.homecaresas.data.ConstantData;
  34. import com.qingdaofushan.homecaresas.data.JsonCallBack;
  35. import com.qingdaofushan.homecaresas.data.Url;
  36. import com.qingdaofushan.homecaresas.utils.CompressUtil;
  37. import com.qingdaofushan.homecaresas.utils.ToastUtil;
  38. import com.qingdaofushan.homecaresas.view.VersionCheckingUtil;
  39. import com.qingdaofushan.homecaresas.weiview.WebViewActivity;
  40. import com.tbruyelle.rxpermissions2.RxPermissions;
  41. import java.io.File;
  42. import java.io.FileInputStream;
  43. import java.util.ArrayList;
  44. import java.util.Collections;
  45. import java.util.Comparator;
  46. import java.util.LinkedList;
  47. import java.util.List;
  48. import io.reactivex.Observable;
  49. import me.lake.librestreaming.core.listener.RESConnectionListener;
  50. import me.lake.librestreaming.ws.StreamAVOption;
  51. import me.lake.librestreaming.ws.StreamLiveCameraView;
  52. public class MainActivity extends AppCompatActivity {
  53. private String mRootUrl;
  54. private ServerManager mServerManager;
  55. @Override
  56. protected void onCreate(Bundle savedInstanceState) {
  57. super.onCreate(savedInstanceState);
  58. setContentView(R.layout.activity_main);
  59. initPermission();
  60. EditText editText = findViewById(R.id.ed_text);
  61. findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
  62. @Override
  63. public void onClick(View view) {
  64. mServerManager.startServer();
  65. Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
  66. // intent.putExtra("url", editText.getText().toString());
  67. intent.putExtra("url", "http://127.0.0.1:8080");
  68. startActivity(intent);
  69. }
  70. });
  71. }
  72. private void initPermission() {
  73. Observable<Boolean> request = new RxPermissions(this).request(
  74. Manifest.permission.READ_EXTERNAL_STORAGE,
  75. Manifest.permission.CAMERA,
  76. Manifest.permission.WRITE_EXTERNAL_STORAGE
  77. );
  78. request.subscribe(aBoolean -> {
  79. if (aBoolean) {
  80. // 初始化HTTP服务
  81. mServerManager = new ServerManager(this);
  82. mServerManager.register();
  83. getVM();
  84. } else {
  85. ToastUtil.showToast("权限被拒绝");
  86. }
  87. });
  88. }
  89. private void initApp(String appWebUrl, int updataType) {
  90. OkDownload.getInstance().setFolder(Environment.getExternalStorageDirectory().getAbsolutePath() + "/homeLife/");
  91. File indexFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/homeLife/homeLifemini/index.html");
  92. File zipFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/homeLife/homeLifemini.zip");
  93. if (!indexFile.exists() || updataType == 1) {
  94. if (zipFile.exists()) {
  95. zipFile.delete();
  96. }
  97. WaitDialog.show("正在进行第一次加载...", 0f);
  98. // String url = "https://pwxmax.cn/homeLifemini.zip";
  99. GetRequest<File> requesta = OkGo.get(appWebUrl);
  100. OkDownload.request(appWebUrl, requesta)//
  101. .save()
  102. .register(new DownloadListener(appWebUrl) {
  103. @Override
  104. public void onStart(Progress progress) {
  105. ToastUtil.showToast("开始下载");
  106. }
  107. @Override
  108. public void onProgress(Progress progress) {
  109. WaitDialog.show("正在进行第一次加载...", progress.fraction);
  110. }
  111. @Override
  112. public void onError(Progress progress) {
  113. ToastUtil.showToast("错误");
  114. Log.e("pwx", "onError: " + progress.exception.toString());
  115. WaitDialog.dismiss();
  116. }
  117. @Override
  118. public void onFinish(File file, Progress progress) {
  119. CompressUtil.unzip(file, Environment.getExternalStorageDirectory().getAbsolutePath() + "/homeLife/");
  120. ToastUtil.showToast("下载完成");
  121. mServerManager.startServer();
  122. Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
  123. intent.putExtra("url", "http://127.0.0.1:8080");
  124. startActivity(intent);
  125. WaitDialog.dismiss();
  126. }
  127. @Override
  128. public void onRemove(Progress progress) {
  129. }
  130. }).start();
  131. }
  132. }
  133. private void getVM() {
  134. File versionFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/homeLife/homeLifemini/version");
  135. FetchUpBean fetchUpBean = new FetchUpBean();
  136. ArrayList<FetchUpBean.QuerysBean> querysBeans = new ArrayList<>();
  137. FetchUpBean.QuerysBean querysBean = new FetchUpBean.QuerysBean();
  138. FetchUpBean.QuerysBean.OrderbyBean orderbyBean = new FetchUpBean.QuerysBean.OrderbyBean();
  139. ArrayList<String> strings = new ArrayList<>();
  140. strings.add("createTime");
  141. orderbyBean.setColumns(strings);
  142. orderbyBean.setSeq("DESC");
  143. querysBean.setOrderby(orderbyBean);
  144. FetchUpBean.QuerysBean.LimitBean limitBean = new FetchUpBean.QuerysBean.LimitBean();
  145. limitBean.setStart(0);
  146. limitBean.setLength(1);
  147. querysBean.setLimit(limitBean);
  148. querysBean.setOrderby(orderbyBean);
  149. querysBeans.add(querysBean);
  150. fetchUpBean.setQuerys(querysBeans);
  151. OkGo.<ItemCommonList<appVMBean, ExpandsBean>>post(Url.URL_VERSION)
  152. .upJson(new Gson().toJson(fetchUpBean))
  153. .execute(new JsonCallBack<ItemCommonList<appVMBean, ExpandsBean>>() {
  154. @Override
  155. public void onSuccess(Response<ItemCommonList<appVMBean, ExpandsBean>> response) {
  156. super.onSuccess(response);
  157. ItemCommonList<appVMBean, ExpandsBean> body = response.body();
  158. if (body.getCode() == ConstantData.CODE_RESULT_SUCCESS) {
  159. if (body.getData() != null && body.getData().size() > 0) {
  160. if (body.getData().get(0).getUpdateType() == 2) {
  161. new VersionCheckingUtil().isChecking(MainActivity.this, body.getData().get(0), 0);
  162. } else if (body.getData().get(0).getUpdateType() == 1) {
  163. if (versionFile.exists()) {
  164. if (!readFileContent(versionFile).equals(body.getData().get(0).getWebVersion())) {
  165. initApp(body.getData().get(0).getAppWebUrl(), 1);
  166. }
  167. } else {
  168. initApp(body.getData().get(0).getAppWebUrl(), 1);
  169. }
  170. }
  171. }
  172. } else {
  173. ToastUtil.showToast("加载失败");
  174. }
  175. }
  176. @Override
  177. public void onError(Response<ItemCommonList<appVMBean, ExpandsBean>> response) {
  178. super.onError(response);
  179. }
  180. });
  181. }
  182. /**
  183. * Start notify.
  184. */
  185. public void onServerStart(String ip) {
  186. if (!TextUtils.isEmpty(ip)) {
  187. List<String> addressList = new LinkedList<>();
  188. mRootUrl = "http://127.0.0.1:8080/";
  189. addressList.add(mRootUrl);
  190. ToastUtil.showToast("服务器启动");
  191. } else {
  192. mRootUrl = null;
  193. }
  194. }
  195. /**
  196. * 【读取文件内容】
  197. **/
  198. public static String readFileContent(File file) {
  199. try {
  200. if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
  201. byte[] buffer = new byte[32 * 1024];
  202. FileInputStream fis = new FileInputStream(file);
  203. int len = 0;
  204. StringBuffer sb = new StringBuffer("");
  205. while ((len = fis.read(buffer)) > 0) {
  206. sb.append(new String(buffer, 0, len));
  207. }
  208. fis.close();
  209. return sb.toString();
  210. }
  211. } catch (Exception e) {
  212. return "";
  213. }
  214. return null;
  215. }
  216. /**
  217. * Error notify.
  218. */
  219. public void onServerError(String message) {
  220. mRootUrl = null;
  221. }
  222. /**
  223. * Stop notify.
  224. */
  225. public void onServerStop() {
  226. mRootUrl = null;
  227. }
  228. @Override
  229. protected void onDestroy() {
  230. super.onDestroy();
  231. mServerManager.unRegister();
  232. }
  233. }