update:2020.03.06

This commit is contained in:
2020-03-06 11:05:05 +08:00
parent cdd3d43ae3
commit 208b58f854
15 changed files with 393 additions and 139 deletions

View File

@@ -1,9 +1,12 @@
package com.info.sn;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
@@ -14,9 +17,15 @@ import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
@@ -27,10 +36,15 @@ import com.info.sn.jpush.LocalBroadcastManager;
import com.info.sn.jpush.TagAliasOperatorHelper;
import com.info.sn.network.api.HTTPInterface;
import com.info.sn.service.MyDownloadService;
import com.info.sn.utils.ApkUtils;
import com.info.sn.utils.LogUtils;
import com.info.sn.utils.SPUtils;
import com.info.sn.utils.ToastUtil;
import com.info.sn.utils.Utils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import java.io.File;
import java.util.List;
@@ -49,9 +63,12 @@ import static com.info.sn.jpush.TagAliasOperatorHelper.sequence;
public class MainActivity extends AppCompatActivity {
public static boolean isForeground = false;
private ImageView imageView;
private ImageView imageView, exit;
private TextView tv_note, tv_devsn, tv_username, tv_school, tv_grade, tv_version;
private ConstraintLayout layout;
private int DeviceInfo;
private long mExitTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -78,19 +95,49 @@ public class MainActivity extends AppCompatActivity {
initData();
HTTPInterface.checkDevicesInfo(handler);
startService(new Intent(MainActivity.this, MyDownloadService.class));
}
private void initView() {
tv_note = (TextView) findViewById(R.id.tv_note);
imageView = (ImageView) findViewById(R.id.imageView);
tv_devsn = (TextView) findViewById(R.id.tv_devsn);
tv_devsn.setText("设备SN:" + Utils.getSerial());
tv_username = (TextView) findViewById(R.id.tv_username);
tv_school = (TextView) findViewById(R.id.tv_school);
tv_grade = (TextView) findViewById(R.id.tv_grade);
tv_note = findViewById(R.id.tv_note);
imageView = findViewById(R.id.imageView);
exit = findViewById(R.id.iv_exit);
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
exit();
}
});
layout = findViewById(R.id.layout);
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
continuousClick(COUNTS, DURATION);
}
});
tv_devsn = findViewById(R.id.tv_devsn);
tv_devsn.setText(Utils.getSerial());
tv_username = findViewById(R.id.tv_username);
tv_school = findViewById(R.id.tv_school);
tv_grade = findViewById(R.id.tv_grade);
tv_version = findViewById(R.id.version);
tv_version.setText("版本:"+ BuildConfig.VERSION_NAME);
tv_version.setText(BuildConfig.VERSION_NAME);
}
final static int COUNTS = 4;// 点击次数
final static long DURATION = 1000;// 规定有效时间
long[] mHits = new long[COUNTS];
private void continuousClick(int count, long time) {
//每次点击时,数组向前移动一位
System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
//为数组最后一位赋值
mHits[mHits.length - 1] = SystemClock.uptimeMillis();
if (mHits[0] >= (SystemClock.uptimeMillis() - DURATION)) {
mHits = new long[COUNTS];//重新初始化数组
Toast.makeText(this, "正在检查更新", Toast.LENGTH_SHORT).show();
HTTPInterface.checkUpdateByPackage(handler, this.getPackageName(), String.valueOf(BuildConfig.VERSION_CODE));
}
}
private void initData() {
@@ -109,6 +156,25 @@ public class MainActivity extends AppCompatActivity {
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
exit();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void exit() {
if ((System.currentTimeMillis() - mExitTime) > 2000) {
Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
mExitTime = System.currentTimeMillis();
} else {
finish();
}
}
private void setImageAndText(ImageView imageView, String text) {
Bitmap bitmap = Utils.createQRImage(Utils.getSn(), 250, 250);
imageView.setImageBitmap(bitmap);
@@ -122,19 +188,25 @@ public class MainActivity extends AppCompatActivity {
if (name == null || name.equals("")) {
tv_username.setText("用户姓名:未设置");
} else {
tv_username.setText("用户姓名:" + name);
tv_username.setText(name);
}
if (school == null || school.equals("")) {
tv_school.setText("学校:未设置");
} else {
tv_school.setText("学校:" + school);
tv_school.setText(school);
}
if (grade == null || grade.equals("")) {
tv_grade.setText("年级:未设置");
} else {
tv_grade.setText("年级:" + getGrade(grade));
tv_grade.setText(getGrade(grade));
}
}
private void setUserInfo() {
tv_username.setText("未设置");
tv_school.setText("未设置");
tv_grade.setText("未设置");
tv_grade.setText("未设置");
}
private String getGrade(String grade) {
@@ -204,9 +276,78 @@ public class MainActivity extends AppCompatActivity {
SPUtils.put(MainActivity.this, "isLogined", 2);
setImageAndText(imageView, "未经验证的设备,请联系客服");
break;
case 200:
Bundle bundle = (Bundle) msg.obj;
getFile(bundle);
break;
case -200:
ToastUtil.show("已是最新版本");
break;
}
}
};
private void getFile(final Bundle bundle) {
final File path = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/");
path.mkdirs();
final File file = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/Update" + bundle.getString("versionCode") + ".apk");
if (file.exists() && file.isFile()) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("软件更新")
.setIcon(R.mipmap.ic_launcher)
.setCancelable(false)
.setMessage("发现新版本,点击确定更新\n" + "更新内容:" + bundle.getString("content"))
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ApkUtils.installApk(MainActivity.this, file);
dialogInterface.dismiss();
}
});
AlertDialog ad = builder.create();
ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失
ad.show();
} else {
OkGo.<File>get(bundle.getString("url"))
.execute(new FileCallback("Sninfo/Update" + bundle.getString("versionCode") + ".apk") {
@Override
public void onSuccess(final Response<File> response) {
// Settings.System.putString(getApplicationContext().getContentResolver(), "qch_app_forbid", "com.baidu.video");
// ApkUtils.installApkInSilence(response.body().getAbsolutePath(), Launcher.this.getPackageName());
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setTitle("软件更新")
.setIcon(R.mipmap.ic_launcher)
.setCancelable(false)
.setMessage("发现新版本,点击确定更新\n" + "更新内容:" + bundle.getString("content"))
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ApkUtils.installApk(MainActivity.this, response.body());
dialogInterface.dismiss();
}
});
AlertDialog ad = builder.create();
ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失
ad.show();
Log.e("getFile", "download file successful,now installing");
}
@Override
public void onError(Response<File> response) {
super.onError(response);
Log.e("getFile", "File download Failure" + response.getException());
}
@Override
public void downloadProgress(Progress progress) {
super.downloadProgress(progress);
Log.e("getFile", "已下载:" + progress.currentSize + ",总大小:" + progress.totalSize + ",进度:" + progress.fraction + ",当前网速:" + progress.speed);
}
});
}
}
private String[] permission = new String[]{
// Permission.SYSTEM_ALERT_WINDOW,

View File

@@ -15,7 +15,7 @@ import android.util.Log;
import com.arialyy.annotations.Download;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.task.DownloadTask;
import com.blankj.utilcode.util.NetworkUtils;
//import com.blankj.utilcode.util.NetworkUtils;
import com.info.sn.network.api.HTTPInterface;
import com.info.sn.utils.ApkUtils;
import com.info.sn.utils.LogUtils;
@@ -33,7 +33,8 @@ import java.util.TreeMap;
import cn.jpush.android.api.CustomMessage;
public class MyApplication extends Application implements NetworkUtils.OnNetworkStatusChangedListener {
//public class MyApplication extends Application implements NetworkUtils.OnNetworkStatusChangedListener {
public class MyApplication extends Application {
public static Context context;
private static MyApplication app;
@@ -45,17 +46,22 @@ public class MyApplication extends Application implements NetworkUtils.OnNetwork
context = getApplicationContext();
ToastUtil.init(this);
OkGo.getInstance().init(this);
NetworkUtils.registerNetworkStatusChangedListener(this);
// NetworkUtils.registerNetworkStatusChangedListener(this);
Aria.init(this);
Aria.download(this).register();
Aria.download(this).resumeAllTask();
}
// protected void attachBaseContext(Context newBase) {
// super.attachBaseContext(newBase);
// MultiDex.install(this);
// }
@Override
public void onTerminate() {
super.onTerminate();
NetworkUtils.unregisterNetworkStatusChangedListener(this);
// NetworkUtils.unregisterNetworkStatusChangedListener(this);
}
@@ -134,7 +140,6 @@ public class MyApplication extends Application implements NetworkUtils.OnNetwork
private static final String JIGUANG_FORCE_UNINSTALLAPK = "10";
synchronized public void manageCustomMessage(CustomMessage customMessage) {
String sn_id = (String) SPUtils.get(context, "sn_id", "-1");
String member_id = (String) SPUtils.get(context, "member_id", "-1");
@@ -420,17 +425,17 @@ public class MyApplication extends Application implements NetworkUtils.OnNetwork
}
}
@Override
public void onDisconnected() {
LogUtils.e("onDisconnected", "网络断开");
}
@Override
public void onConnected(NetworkUtils.NetworkType networkType) {
Aria.download(this).resumeAllTask();
LogUtils.e("onConnected", "网络连接");
}
// @Override
// public void onDisconnected() {
// LogUtils.e("onDisconnected", "网络断开");
// }
//
// @Override
// public void onConnected(NetworkUtils.NetworkType networkType) {
// Aria.download(this).resumeAllTask();
// LogUtils.e("onConnected", "网络连接");
//
// }
//在这里处理任务执行中的状态,如进度进度条的刷新

View File

@@ -140,12 +140,11 @@ public class HTTPInterface {
bundle.putString("versionCode", newversion);
bundle.putString("content", content);
message.obj = bundle;
} else {
message.what = -200;
}
handler.sendMessage(message);
Log.e("checkUpdateByPackage", msg);
} else {
handler.sendEmptyMessage(-200);
Log.e("checkUpdateByPackage", msg);
}
} catch (Exception e) {

View File

@@ -101,7 +101,7 @@ public class MyDownloadService extends Service {
if (file.exists() && file.isFile()) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("软件更新")
.setIcon(R.mipmap.ic_launcher_home)
.setIcon(R.mipmap.ic_launcher)
.setCancelable(false)
.setMessage("发现新版本,点击确定更新\n" + "更新内容:" + bundle.getString("content"))
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@@ -124,7 +124,7 @@ public class MyDownloadService extends Service {
// ApkUtils.installApkInSilence(response.body().getAbsolutePath(), Launcher.this.getPackageName());
AlertDialog.Builder builder = new AlertDialog.Builder(MyDownloadService.this)
.setTitle("软件更新")
.setIcon(R.mipmap.ic_launcher_home)
.setIcon(R.mipmap.ic_launcher)
.setCancelable(false)
.setMessage("发现新版本,点击确定更新\n" + "更新内容:" + bundle.getString("content"))
.setPositiveButton("确定", new DialogInterface.OnClickListener() {