UpgradeDownloadManager.java
5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright (c) 2017. wugian
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.xgimi.gimicinema.appupdate;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import com.gimi.common.cinema.utils.OkHttpClientManager;
import com.gimi.common.cinema.utils.ShellUtils;
import com.squareup.okhttp.Request;
import java.io.File;
class UpgradeDownloadManager {
private Context mContext;
private String apkUrl;
private Dialog downloadDialog;
private ProgressBar mProgress;
private int progress;
private boolean silentInstall;
UpgradeDownloadManager(Context context, String apkMessage, boolean silent) {
this.mContext = context;
this.apkUrl = apkMessage;
this.silentInstall = silent;
}
private boolean hasSDCard() {
String status = Environment.getExternalStorageState();
return status.equals(Environment.MEDIA_MOUNTED);
}
private String getRootFilePath() {
if (hasSDCard()) {
return Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator + File.separator + "test/"
/* + "android" + File.separator + "data" + File.separator*/;
} else {
return Environment.getDataDirectory().getAbsolutePath()
+ File.separator + "data" + File.separator;
}
}
void downloadApk(boolean showDialog) {
if (showDialog) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("软件下载中.......");
mProgress = new ProgressBar(mContext, null,
android.R.attr.progressBarStyleHorizontal);//水平条形
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(200, -2);//长度200,高度默认
mProgress.setLayoutParams(lp2);
mProgress.setMax(100);
builder.setView(mProgress);
builder.setCancelable(false);
downloadDialog = builder.create();
downloadDialog.show();
}
downloadApk();
}
private void downloadApk() {
String appName = apkUrl;
if (apkUrl.contains("/")) {
final String[] split = apkUrl.split("/");
appName = split[split.length - 1];
}
if (!new File(getRootFilePath()).exists()) {
final File file = new File(getRootFilePath());
file.mkdirs();
}
OkHttpClientManager.downloadAsyn(apkUrl, getRootFilePath() + appName,
new OkHttpClientManager.ResultCallback<String>() {
@Override
public void onError(Request request, Exception e) {
}
@Override
public void onResponse(String response) {
if (downloadDialog != null) {
downloadDialog.dismiss();
}
installApk(response);
}
}, new OkHttpClientManager.ProgressListener() {
@Override
public void onResponseProgress(long bytesRead, long contentLength) {
progress = (int) (((float) bytesRead / contentLength) * 100);
Log.d("install", "下载:" + progress + ".....");
//更新进度
if (mProgress != null) {
mProgress.setProgress(progress);
}
}
});
}
private void installApk(final String path) {
File apkFile = new File(path);
if (!apkFile.exists()) {
return;
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
File apkFile = new File(path);
if (apkFile.exists()) {
boolean delete = apkFile.delete();
Log.d("install", delete + "");
}
}
}, 40 * 1000);
if (silentInstall) {
Log.d("install", "正在静默安装");
ShellUtils.execCommand(new String[]{"su", "chmod -R 0777 " + path, "pm install -r " + path}, false);
return;
}
Log.d("install", "正在普通安装");
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(apkFile);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
mContext.startActivity(intent);
}
}