OtaUpgradeUtils.java
4.55 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
package com.sw.laryngoscope.manager;
import android.content.Context;
import android.os.Build;
import android.os.PowerManager;
import android.os.RecoverySystem;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
public class OtaUpgradeUtils {
private static final String TAG = "OtaUpgradeUtils" ;
public static final String CHCHE_PARTITION = "/cache/";
public static final String DEFAULT_PACKAGE_NAME = "update.zip";
public static final String DEFAULT_PATH = "/data/update.zip";
//public static final String DEFAULT_PATH = "/data/media/0/update.zip";
private static File RECOVERY_DIR = new File("/cache/recovery");
private static File COMMAND_FILE = new File(RECOVERY_DIR, "command");
private static File LOG_FILE = new File(RECOVERY_DIR, "log");
private Context mContext;
public OtaUpgradeUtils(Context context) {
mContext = context;
}
public interface ProgressListener extends RecoverySystem.ProgressListener {
@Override
public void onProgress(int progress);
public void onVerifyFailed(int errorCode, Object object);
public void onCopyProgress(int progress);
public void onCopyFailed(int errorCode, Object object);
}
public boolean upgredeFromOta(File packageFile, ProgressListener progressListener) {
Log.d(TAG, " CHCHE_PARTITION ");
installPackage(mContext, new File(CHCHE_PARTITION + DEFAULT_PACKAGE_NAME));
return false;
}
public boolean upgradeFromOta(String packagePath, ProgressListener progressListener) {
return upgredeFromOta(new File(packagePath), progressListener);
}
public boolean upgradeFromOta(String packagePath) {
return upgredeFromOta(new File(packagePath), null);
}
public static boolean copyFile(File src, File dst, ProgressListener listener) {
long inSize = src.length();
long outSize = 0;
int progress = 0;
//listener.onCopyProgress(progress);
try {
if(dst.exists()){
dst.delete();
}
dst.createNewFile();
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dst);
int length = -1;
byte[] buf = new byte[1024 * 8];
while ((length = in.read(buf)) != -1) {
out.write(buf, 0, length);
outSize += length;
int temp = (int) (((float) outSize) / inSize * 100);
if (temp != progress) {
progress = temp;
//listener.onCopyProgress(progress);
}
}
out.flush();
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static boolean copyFile(String src, String dst, ProgressListener listener) {
return copyFile(new File(src), new File(dst), listener);
}
public static void installPackage(Context context, File packageFile) {
try {
RecoverySystem.installPackage(context, packageFile);
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean checkVersion(long newVersion, String product) {
return (Build.TIME <= newVersion * 1000 && (Build.DEVICE.equals(product) || Build.PRODUCT
.equals(product)));
}
public static boolean checkIncVersion(String fingerprinter, String product) {
return (Build.FINGERPRINT.equals(fingerprinter) && (Build.DEVICE.equals(product) || Build.PRODUCT
.equals(product)));
}
private static void bootCommand(Context context, String arg) throws IOException {
RECOVERY_DIR.mkdirs(); // In case we need it
COMMAND_FILE.delete(); // In case it's not writable
LOG_FILE.delete();
FileWriter command = new FileWriter(COMMAND_FILE);
try {
command.write(arg);
command.write("\n");
} finally {
command.close();
}
// Having written the command file, go ahead and reboot
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
pm.reboot("recovery");
throw new IOException("Reboot failed (no permissions?)");
}
}