Commit bc659d3f authored by chenyuling's avatar chenyuling

静默安装

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