Commit bc659d3f authored by chenyuling's avatar chenyuling

静默安装

parent 211ebcd5
......@@ -52,7 +52,7 @@ public class OkhttpInterruptDownImpl extends BaseDownloadImpl{
try {
//OkHttpClient client = new OkHttpClient();
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS) // 设置连接超时时间为10秒
.connectTimeout(10, TimeUnit.SECONDS) // 设置连接超时时间为10秒
.readTimeout(60, TimeUnit.SECONDS) // 设置读取超时时间为10秒
.build();
Request request = new Request.Builder().url(downloadUrl)
......
......@@ -59,6 +59,7 @@ import com.srthinker.statinfo.util.common.FileUtil;
import com.srthinker.statinfo.util.common.GsonUtil;
import com.srthinker.statinfo.util.common.PermissionUtil;
import com.srthinker.statinfo.util.common.SharedUtil;
import com.srthinker.statinfo.util.common.ThreadPool;
import com.srthinker.statinfo.util.common.Utils;
import com.srthinker.statinfo.util.other.NetworkIPMonitor;
import com.srthinker.statinfo.wedget.LoadDialog;
......@@ -99,6 +100,7 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
private HashMap<Integer,Integer> groupPresentHashMap=null;
private boolean isPermission;
public static boolean NEED_NO_REPET = true; //是否需要去重,false:不需要,true:需要
private boolean NEED_SEE_NEXT = true; //去重是怎么去,true只去重相邻的,false则全部去重
@Override
......@@ -307,46 +309,53 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
String desc = "下载更新中..."+progress;
if (mLoadDialog != null) {
mLoadDialog.setContent(desc);
mLoadDialog.showDialog();
}
});
}
@Override
public void onResult(int downStatus, String filePath) {
if (downStatus == DOWN_START){
runOnUiThread(()->{
runOnUiThread(()->{
if (downStatus == DOWN_START){
String desc = "准备下载更新";
if (mLoadDialog != null) {
mLoadDialog.setContent(desc);
mLoadDialog.show();
mLoadDialog.showDialog();
}
});
} else if (downStatus== DOWN_FINISH){
//安装更新
//Log.i(TAG, "onResult:进来多少次");
//showTips("更新版本,将进行安装");
} else if (downStatus== DOWN_FINISH){
//安装更新
//Log.i(TAG, "onResult:进来多少次");
runOnUiThread(()->{
String desc = "进行安装,请稍等";
if (mLoadDialog != null) {
mLoadDialog.dismiss();
mLoadDialog.setContent(desc);
mLoadDialog.showDialog();
}
showTips("更新版本,将进行安装");
Utils.installApp(getApplicationContext(),new File(installPath));
/*boolean restricted = Utils.isRootPermissionRestricted();
if (!restricted){
Utils.clientInstall(installPath,MainActivity.this);
}else{
Utils.installApp(getApplicationContext(),new File(installPath));
}*/
});
//Utils.installApp(getApplicationContext(),new File(installPath));
//有静默安装的话需要开新线程,不然前面的弹框不进行显示
ThreadPool.getInstance().getThreadPoolExecutor().execute(() -> {
boolean restricted = Utils.isRootPermissionRestricted();
if (!restricted){
Utils.clientInstall(installPath,MainActivity.this,".uis.MainActivity");
}else{
Utils.installApp(getApplicationContext(),new File(installPath));
}
runOnUiThread(()->{
if (mLoadDialog != null) {
mLoadDialog.dismissDialog();
mLoadDialog=null;
}
});
});
}else if (downStatus==DOWN_ERROR){
runOnUiThread(()->{
}else if (downStatus==DOWN_ERROR){
showTips("下载更新失败");
FileUtil.deleteFile(installPath);
});
}
});
}
}
@Override
......@@ -368,7 +377,7 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
enterAdapter.setDateList(enterBeans);
//更新人员情况(是否要根据id去重)
if (NEED_NO_REPET) {
List<PersonEntity> enterListNo = Utils.removeDuplicates(enterBeans, "id");
List<PersonEntity> enterListNo = Utils.removeDuplicates(enterBeans, "id",NEED_SEE_NEXT);
lastEnterSize = enterListNo.size();
}else{
lastEnterSize = size;
......@@ -395,7 +404,7 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
leaveAdapter.setDateList(leaveBeans);
//更新人员情况(是否要根据id去重)
if (NEED_NO_REPET) {
List<PersonEntity> leaveListNo = Utils.removeDuplicates(leaveBeans, "id");
List<PersonEntity> leaveListNo = Utils.removeDuplicates(leaveBeans, "id",NEED_SEE_NEXT);
lastLeaveSize = leaveListNo.size();
}else{
lastLeaveSize = size;
......@@ -422,7 +431,7 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
private HashMap<Integer, Integer> getGroupSizeHashMap(List<PersonEntity> personEntityList) {
HashMap<Integer, Integer> groupSizeHashMap = new HashMap<>();
if (NEED_NO_REPET) {
personEntityList = Utils.removeDuplicates(personEntityList,"id");
personEntityList = Utils.removeDuplicates(personEntityList,"id",NEED_SEE_NEXT);
}
if (mQueryPersonList != null) {
for (PersonEntity entity : mQueryPersonList) {
......
......@@ -273,6 +273,40 @@ public class Utils {
return distinctList;
}
public static <T> List<T> removeDuplicates(List<T> originalList, String fieldName,boolean seeNextOne) {
Set<Object> set = new LinkedHashSet<>();
List<T> distinctList = new ArrayList<>();
if (seeNextOne) {
for (int i = 0; i < originalList.size(); i++) {
T item = originalList.get(i);
Object fieldValue = getFieldValue(item, fieldName);
if (i<originalList.size()-1){
T nextItem = originalList.get(i + 1);
Object nextFileValue = getFieldValue(nextItem, fieldName);
if (!fieldValue.equals(nextFileValue)){
distinctList.add(item);
}else{
i++;
distinctList.add(nextItem);
}
}else{
distinctList.add(item);
}
}
}else{
for (T item : originalList) {
Object fieldValue = getFieldValue(item, fieldName);
if (!set.contains(fieldValue)) {
set.add(fieldValue);
distinctList.add(item);
}
}
}
return distinctList;
}
private static Object getFieldValue(Object obj, String fieldName) {
try {
Field field = obj.getClass().getDeclaredField(fieldName);
......@@ -307,7 +341,8 @@ public class Utils {
BufferedReader reader1 = null;
try {
process = Runtime.getRuntime().exec("su");
//process = Runtime.getRuntime().exec("su");
process = Runtime.getRuntime().exec("srt");
os = new DataOutputStream(process.getOutputStream());
reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
......@@ -367,7 +402,7 @@ public class Utils {
//需要传下载后的apk的绝对路径和当前上下文,这个方法的返回值表示为:安装成功是true,安装失败是false
//apkPath:apk文件的绝对路径
public static boolean clientInstall(String apkPath,Context context) {
public static boolean clientInstall(String apkPath,Context context,String mainActivityPath) {
boolean result = false;
DataOutputStream dataOutputStream = null;
BufferedReader errorStream = null;
......@@ -375,12 +410,14 @@ public class Utils {
Process process = null;
try {
// 申请 su 权限
process = Runtime.getRuntime().exec("su");
//process = Runtime.getRuntime().exec("su");
//申请srt权限
process = Runtime.getRuntime().exec("srt");
dataOutputStream = new DataOutputStream(process.getOutputStream());
// 执行 pm install 命令 (这是apk安装命令)
String command = "pm install -r " + apkPath + "\n";
//执行 am start -n 命令 (这是apk重启命令) -----.MainActivity是默认重启app后要打开哪个界面
String runApp = "am start -n "+context.getPackageName()+"/"+context.getPackageName()+".MainActivity >>/dev/null 2>&1;";
String runApp = "am start -n "+context.getPackageName()+"/"+context.getPackageName()+mainActivityPath+" >>/dev/null 2>&1;";
//这样子写也是可以的,用\n结尾也可以
// String runApp = "am start -n "+context.getPackageName()+"/"+context.getPackageName()+".MainActivity"+ "\n";
command=command+runApp;
......
......@@ -51,6 +51,17 @@ public class LoadDialog extends Dialog {
binding.tvLoadInfo.setText(content);
}
public void showDialog() {
if (!isShowing()) {
show();
}
}
public void dismissDialog() {
if (isShowing()) {
dismiss();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment