-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUpdateManager.java
226 lines (187 loc) · 7.15 KB
/
UpdateManager.java
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
package de.leuchtetgruen;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Handler;
import android.os.ParcelFileDescriptor;
public class UpdateManager {
private Context ctx;
private String strUrl;
private String strTargetFile;
private String prefsName;
private DownloadManager downloadManager;
private long myDownloadId;
private BroadcastReceiver broadcastReceiver;
private UpdateListener updateListener = null;
private Handler runner = new Handler();
private final static String PREFS_ETAG = ".ETAG";
private final static String PREFS_LMOD = ".LAST_MODIFIED";
private final static String PREFS_FSIZE = ".FILESIZE";
private final static String DEFAULT_SP_VAL = "$$foobar$$";
public final static int DESIRED_NETWORK_TYPE_ANY = -1;
private int desiredNetworkType = ConnectivityManager.TYPE_WIFI;
public UpdateManager(Context ctx, String strUrl, String targetFile, String prefsName) {
super();
this.ctx = ctx;
this.strUrl = strUrl;
this.strTargetFile = targetFile;
this.prefsName = prefsName;
}
public void setDesiredNetworkType(int desiredNetworkType) {
this.desiredNetworkType = desiredNetworkType;
}
public void checkForUpdate(UpdateListener listener) {
ConnectivityManager mgr = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = mgr.getActiveNetworkInfo();
if (networkInfo==null) {
listener.onErrorDownloadingUpdate(this);
}
else if ((networkInfo.getType() != this.desiredNetworkType) && (this.desiredNetworkType != DESIRED_NETWORK_TYPE_ANY)) {
listener.onNotAllowedToLoadUpdate(this);
return;
}
setUpdateListener(listener);
final UpdateManager umgr = this;
final UpdateListener lnr = listener;
Thread t = new Thread() {
public void run() {
if (hasNewData() || (!ctx.getFileStreamPath(strTargetFile).exists())) update();
else {
runner.post(new Runnable() {
public void run() {
lnr.onNoUpdateAvailable(umgr);
}
});
}
};
};
t.start();
}
public void setUpdateListener(UpdateListener listener) {
this.updateListener = listener;
}
private boolean hasNewData() {
HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(new HttpHead(strUrl));
Header[] headers = response.getAllHeaders();
for (Header header : headers) {
if (header.getName().equals("Etag")) {
return (valuesAreTheSame(header.getValue(), prefsName + PREFS_ETAG)!=TriState.YES);
}
else if (header.getName().equals("Last-Modified")) {
return (valuesAreTheSame(header.getValue(), prefsName + PREFS_LMOD)!=TriState.YES);
}
else if (header.getName().equals("Content-Length")) {
return (valuesAreTheSame(header.getValue(), prefsName + PREFS_FSIZE)!=TriState.YES);
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public void update() {
downloadManager = (DownloadManager) ctx.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(strUrl));
myDownloadId = downloadManager.enqueue(request);
final UpdateManager umgr = this;
if (updateListener!=null) runner.post(new Runnable() {
public void run() {
updateListener.onStartedDownloadingUpdate(umgr);
}
});
broadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
//long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
query.setFilterById(myDownloadId);
Cursor c = downloadManager.query(query);
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = c.getInt(columnIndex);
if (DownloadManager.STATUS_SUCCESSFUL == status) {
//String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
ParcelFileDescriptor f;
try {
f = downloadManager.openDownloadedFile(myDownloadId);
FileInputStream in = new ParcelFileDescriptor.AutoCloseInputStream(f);
copyToAppDir(ctx, in, strTargetFile);
if (updateListener!=null) updateListener.onSuccessfullyDownloadedUpdate(umgr);
} catch (FileNotFoundException e) {
e.printStackTrace();
if (updateListener!=null) updateListener.onErrorDownloadingUpdate(umgr);
}
}
else if (DownloadManager.STATUS_FAILED == status){
if (updateListener!=null) updateListener.onErrorDownloadingUpdate(umgr);
}
}
}
}
};
ctx.registerReceiver(broadcastReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
private TriState valuesAreTheSame(String read, String prefsKey) {
TriState ret = TriState.NO;
SharedPreferences prefs = ctx.getSharedPreferences(prefsName, 0);
String savedValue = prefs.getString(prefsKey, DEFAULT_SP_VAL);
if (savedValue.equals(DEFAULT_SP_VAL)) ret =TriState.UNKNOWN;
else if (savedValue.equals(read)) ret = TriState.YES;
if (ret != TriState.YES) {
prefs.edit().putString(prefsKey, read).apply();
}
return ret;
}
private void copyToAppDir(Context ctx, InputStream in, String fileDest) {
try{
OutputStream out = ctx.openFileOutput(fileDest, Context.MODE_PRIVATE);
byte[] buf = new byte[4096];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
}
catch(FileNotFoundException ex){
ex.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
}
public void copyFileFromAssets() {
try {
InputStream in = ctx.getAssets().open(strTargetFile);
copyToAppDir(ctx, in, strTargetFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}