Commit 9dbc275a authored by chenyuling's avatar chenyuling

修改弹窗

parent a9b5d040
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 安装APK权限,需要在程序中动态申请,并且不同于外部存储读写权限申请 -->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<application
android:name=".uis.MyApplication"
android:allowBackup="true"
......@@ -29,7 +30,7 @@
<meta-data
android:name="design_height_in_dp"
android:value="540" /> <!-- fileprovider名称在安装时传递给系统安装程序 -->
<provider
<!--<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
android:exported="false"
......@@ -37,8 +38,18 @@
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/autoupdate" />
</provider>-->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<activity
android:name=".uis.MainActivity"
android:configChanges="keyboardHidden|orientation"
......
......@@ -18,6 +18,7 @@ public class OkhttpInterruptDownImpl extends BaseDownloadImpl{
private FileOutputStream fos;
private InputStream inputStream;
private Call call;
private String TAG = "OkhttpInterruptDownImpl";
public static OkhttpInterruptDownImpl getInstance(){
if (down == null) {
......
......@@ -97,6 +97,7 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
private HashMap<Integer, Integer> groupEnterHashMap=null;
private HashMap<Integer, Integer> groupLeaveHashMap=null;
private HashMap<Integer,Integer> groupPresentHashMap=null;
private boolean isPermission;
@Override
......@@ -139,7 +140,8 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
updateTime();
initGroup();
initPeopleList();
mLoadDialog = LoadDialog.getInstance(this);
//mLoadDialog = LoadDialog.getInstance(this);
mLoadDialog = new LoadDialog(this);
//Log.i(TAG, "initView: deviceSn="+deviceSn+"V"+ Utils.getVersionName(this));
mBinding.tvDeviceDesc.setText(DEVICE_TYPE+" "+deviceSn+" V"+ Utils.getVersionName(this));
//getResp();
......@@ -202,7 +204,7 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
}
private void checkAllPermission() {
boolean isPermission = PermissionUtil.checkMultiPermission(this, new String[]{
isPermission = PermissionUtil.checkMultiPermission(this, new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.INTERNET
......@@ -301,7 +303,9 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
runOnUiThread(()->{
//Log.i(TAG, "onProgress: progress="+progress);
String desc = "下载更新中..."+progress;
if (mLoadDialog != null) {
mLoadDialog.setContent(desc);
}
});
}
......@@ -310,16 +314,26 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
if (downStatus == DOWN_START){
runOnUiThread(()->{
String desc = "准备下载更新";
if (mLoadDialog != null) {
mLoadDialog.setContent(desc);
mLoadDialog.show();
}
});
} else if (downStatus== DOWN_FINISH){
//安装更新
//Log.i(TAG, "onResult:进来多少次");
runOnUiThread(()->{
if (mLoadDialog != null) {
mLoadDialog.dismiss();
showTips("更新版本,请进行安装");
}
showTips("更新版本,将进行安装");
boolean restricted = Utils.isRootPermissionRestricted();
if (!restricted){
Utils.clientInstall(installPath,MainActivity.this);
}else{
Utils.installApp(getApplicationContext(),new File(installPath));
}
});
......@@ -489,7 +503,7 @@ public class MainActivity extends BaseActivity implements OnTimeUpdateListener,
@Override
public void onQueryStatus(String desc, boolean status) {
runOnUiThread(()->{
if (status==false){
if (!status){
showTips(desc);
}
});
......
......@@ -8,15 +8,22 @@ import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;
import androidx.core.content.FileProvider;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
......@@ -26,6 +33,8 @@ import me.jessyan.autosize.utils.ScreenUtils;
public class Utils {
private static final String TAG = "Utils";
// 根据手机的分辨率从 dp 的单位 转成为 px(像素)
public static int dip2px(Context context, float dpValue) {
// 获取当前手机的像素密度
......@@ -83,16 +92,32 @@ public class Utils {
_Intent.setAction(Intent.ACTION_VIEW);
Uri _uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
_Intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
_Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_Intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
_uri = FileProvider.getUriForFile(pContext, pContext.getPackageName() + ".fileProvider", pFile);
}else {
_uri = Uri.fromFile(pFile);
_Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
_Intent.setDataAndType(_uri, "application/vnd.android.package-archive");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
boolean hasInstallPermission = pContext.getPackageManager().canRequestPackageInstalls();
if (!hasInstallPermission) {
// 请求安装未知应用来源的权限
startInstallPermissionSettingActivity(pContext);
return;
}
}
pContext.startActivity(_Intent);
}
private static void startInstallPermissionSettingActivity(Context context) {
Uri packageUri = Uri.parse("package:" + context.getPackageName());
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageUri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
//弹出toast
public static void showTips(Context context,String tips){
Activity activity = (Activity) context;
......@@ -209,4 +234,201 @@ public class Utils {
}
//返回true表示用限制使用
public static boolean isRootPermissionRestricted() {
boolean isRestricted = false;
Process process = null;
DataOutputStream os = null;
BufferedReader reader = null;
BufferedReader reader1 = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// 执行一个需要root权限的命令
os.writeBytes("ls /data\n");
os.flush();
os.writeBytes("exit\n");
os.flush();
process.waitFor();
StringBuilder output = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
output.append(line);
output.append("\n");
}
Log.i(TAG, "isRootPermissionRestricted: output="+output.toString());
// 检查返回结果是否包含权限被拒绝的提示
if (output.toString().contains("not allowed")) {
isRestricted = true;
}
reader1 = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder output1 = new StringBuilder();
String line1;
while ((line1 = reader1.readLine()) != null) {
output1.append(line1);
output1.append("\n");
}
// 检查返回结果是否包含权限被拒绝的提示
if (output1.toString().contains("Permission denied")) {
isRestricted = true;
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (reader != null) {
reader.close();
}
if (process != null) {
process.destroy();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return isRestricted;
}
//需要传下载后的apk的绝对路径和当前上下文,这个方法的返回值表示为:安装成功是true,安装失败是false
//apkPath:apk文件的绝对路径
public static boolean clientInstall(String apkPath,Context context) {
boolean result = false;
DataOutputStream dataOutputStream = null;
BufferedReader errorStream = null;
BufferedReader successStream = null;
Process process = null;
try {
// 申请 su 权限
process = Runtime.getRuntime().exec("su");
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;";
//这样子写也是可以的,用\n结尾也可以
// String runApp = "am start -n "+context.getPackageName()+"/"+context.getPackageName()+".MainActivity"+ "\n";
command=command+runApp;
Log.d("111333","走到这里,---------------打印静默安装命令="+command);
dataOutputStream.write(command.getBytes(Charset.forName("UTF-8")));
dataOutputStream.writeBytes("exit\n");
Log.d("111333","走到这里,---------------打印静默安装完成");
dataOutputStream.flush();
process.waitFor();
errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
StringBuilder errorMsg = new StringBuilder();
String line;
while ((line = errorStream.readLine()) != null) {
errorMsg.append(line);
}
Log.d("111333","走到这里,---------------silent install error message:{}="+errorMsg);
StringBuilder successMsg = new StringBuilder();
successStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
// 读取命令执行结果
while ((line = successStream.readLine()) != null) {
successMsg.append(line);
}
Log.d("111333","走到这里,---------------silent install success message:{}="+successMsg);
// 如果执行结果中包含 Failure 字样就认为是操作失败,否则就认为安装成功
if (!(errorMsg.toString().contains("Failure") || successMsg.toString().contains("Failure"))) {
result = true;
}
} catch (Exception e) {
Log.d("111333","走到这里,---------------e="+e);
} finally {
try {
if (process != null) {
process.destroy();
}
if (dataOutputStream != null) {
dataOutputStream.close();
}
if (errorStream != null) {
errorStream.close();
}
if (successStream != null) {
successStream.close();
}
} catch (Exception e) {
// ignored
}
}
return result;
}
/**
* Silent install
*
* @param path Package
* @return true: success false: failed
*/
public static boolean installSilent(String path) {
boolean result = false;
BufferedReader es = null;
DataOutputStream os = null;
try {
Process process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
String command = "pm install -r " + path + "\n";
Log.i(TAG, "installSilent: command="+command);
os.write(command.getBytes(Charset.forName("utf-8")));
os.flush();
os.writeBytes("exit\n");
os.flush();
process.waitFor();
es = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line;
StringBuilder builder = new StringBuilder();
while ((line = es.readLine()) != null) {
builder.append(line);
}
Log.d(TAG, "install msg is " + builder.toString());
/* Installation is considered a Failure if the result contains
the Failure character, or a success if it is not.
*/
if (!builder.toString().contains("Failure")) {
result = true;
}
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
} finally {
try {
if (os != null) {
os.close();
}
if (es != null) {
es.close();
}
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
}
return result;
}
}
......@@ -27,14 +27,14 @@ public class LoadDialog extends Dialog {
initWindow();
}
public static LoadDialog getInstance(Context context){
/*public static LoadDialog getInstance(Context context){
if (loadDialog == null) {
synchronized (LoadDialog.class){
loadDialog = new LoadDialog(context);
}
}
return loadDialog;
}
}*/
private void initWindow() {
Window window = getWindow();
......@@ -47,15 +47,10 @@ public class LoadDialog extends Dialog {
setCanceledOnTouchOutside(false);
}
/*public void showDialog(){
show();
}
public void closeDialog(){
dismiss();
}*/
public void setContent(String content){
binding.tvLoadInfo.setText(content);
}
}
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="." />
</paths>
\ No newline at end of file
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