VersionManager.java
7.25 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package com.sw.laryngoscope.manager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import androidx.core.content.FileProvider;
import com.sw.laryngoscope.utils.Logger;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class VersionManager {
/**
* @param
* @explain 获取App版本号
*/
public static String getAppVersionName(Context context) {
String versionName = "";
try {
// ---get the package info---
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
versionName = pi.versionName;
if (versionName == null || versionName.length() <= 0) {
return "";
}
} catch (Exception e) {
Log.e("VersionInfo", "Exception", e);
}
return versionName;
}
public static int getAppVersionCode(Context context) {
int versionCode = 0;
try {
// ---get the package info---
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
versionCode = pi.versionCode;
} catch (Exception e) {
Log.e("versionCode", "Exception", e);
}
return versionCode;
}
public static int getAppVersionCode(Context context, String file) {
int versionCode = 0;
try {
// ---get the package info---
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageArchiveInfo(file, PackageManager.GET_ACTIVITIES);
versionCode = packageInfo.versionCode;
} catch (Exception e) {
Log.e("versionCode", "Exception", e);
}
return versionCode;
}
public static String getSpecAppVersionName(Context context, String name) {
String versionName = "";
try {
// ---get the package info---
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(name, 0);
versionName = pi.versionName;
if (versionName == null || versionName.length() <= 0) {
return "";
}
} catch (Exception e) {
Log.e("VersionInfo", "Exception", e);
}
return versionName;
}
/**
* @return 获取SDK版本号
*/
public static String getSdkVersion(){
String versionOS = android.os.Build.VERSION.RELEASE;
return versionOS;
}
/**
* @return 获取当前系统的版本号
*/
public static String getfirewareVersion(){
String versionOS = android.os.Build.VERSION.INCREMENTAL + "";
return versionOS;
}
/**
* @return 返回当前系统的android版本号
*/
public static String getSDKVersion() {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
return String.valueOf(currentapiVersion);
}
/**
* CORE-VER
* 内核版本
* return String
*/
public static String getLinuxCore_Ver() {
Process process = null;
String kernelVersion = "";
try {
process = Runtime.getRuntime().exec("cat /proc/version");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// get the output line
InputStream outs = process.getInputStream();
InputStreamReader isrout = new InputStreamReader(outs);
BufferedReader brout = new BufferedReader(isrout, 8 * 1024);
String result = "";
String line;
// get the whole standard output string
try {
while ((line = brout.readLine()) != null) {
result += line;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (result != "") {
String Keyword = "version ";
int index = result.indexOf(Keyword);
line = result.substring(index + Keyword.length());
index = line.indexOf(" ");
kernelVersion = line.substring(0, index);
}
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
return kernelVersion;
}
public static long getApkUpdateTime(Context context) {
PackageManager pm = context.getPackageManager();
ZipFile zf = null;
try {
PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
zf = new ZipFile(packageInfo.applicationInfo.sourceDir);
ZipEntry ze = zf.getEntry("classes.dex");
return ze.getTime();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (zf != null) {
try {
zf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return 0;
}
public static void installApk(Context mContext, File apkfile) {
Intent apkIntent = new Intent();
apkIntent.setAction(android.content.Intent.ACTION_VIEW);
apkIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
apkIntent.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive");
mContext.startActivity(apkIntent);
}
/**
* 安装APK
*
* @param context
* @param filePath
*/
public static void installApk(Context context, String filePath) {
try {
/**
* provider
* 处理android 7.0 及以上系统安装异常问题
*/
File file = new File(filePath);
Intent install = new Intent();
install.setAction(Intent.ACTION_VIEW);
install.addCategory(Intent.CATEGORY_DEFAULT);
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri apkUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", file);
//Uri apkUri = FileProvider.getUriForFile(context, "com.sw.laryngoscope.fileprovider", file);
//在AndroidManifest中的android:authorities值
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//添加这一句表示对目标应用临时授权该Uri所代表的文件
install.setDataAndType(apkUri, "application/vnd.android.package-archive");
Logger.d(" txt_apk_upgrade ===");
} else {
install.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
}
context.startActivity(install);
} catch (Exception e) {
e.printStackTrace();
//TODO 文件解析失败
Logger.d(" txt_apk_upgrade ===" + e.toString());
}
}
/*public String getBuildTime(){
String content = null;
Resources resources=mContext.getResources();
InputStream is=null;
try{
is=resources.openRawResource(R.raw.date);
byte buffer[]=new byte[is.available()];
is.read(buffer);
content=new String(buffer);
Log.i(TAG, "read:"+content);
}
catch(IOException e)
{
Log.e(TAG, "write file",e);
}
finally
{
if(is!=null)
{
try{
is.close();
}catch(IOException e)
{
Log.e(TAG, "close file",e);
}
}
}
return content;
}*/
}