This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
/
UpdateManager.java
301 lines (262 loc) · 11.3 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package net.hockeyapp.android;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask.Status;
import android.os.Build;
import android.text.TextUtils;
import net.hockeyapp.android.tasks.CheckUpdateTask;
import net.hockeyapp.android.tasks.CheckUpdateTaskWithUI;
import net.hockeyapp.android.utils.AsyncTaskUtils;
import net.hockeyapp.android.utils.Util;
import java.lang.ref.WeakReference;
import java.util.Date;
/**
* <h3>Description</h3>
* <p>
* The update manager sends version information to HockeyApp and
* shows an alert dialog if a new version was found.
**/
public class UpdateManager {
public static final String INSTALLER_ADB = "adb";
public static final String INSTALLER_PACKAGE_INSTALLER_NOUGAT = "com.google.android.packageinstaller";
public static final String INSTALLER_PACKAGE_INSTALLER_NOUGAT2 = "com.android.packageinstaller";
/**
* Singleton for update task.
*/
private static CheckUpdateTask updateTask = null;
/**
* Registers new update manager.
* HockeyApp app identifier is read from AndroidManifest.xml.
*
* @param activity The parent activity to return to.
*/
public static void register(Activity activity) {
String appIdentifier = Util.getAppIdentifier(activity);
register(activity, appIdentifier);
}
/**
* Registers new update manager.
* HockeyApp app identifier is read from AndroidManifest.xml.
*
* @param activity The parent activity to return to.
* @param listener Implement for callback functions.
*/
public static void register(Activity activity, UpdateManagerListener listener) {
String appIdentifier = Util.getAppIdentifier(activity);
register(activity, appIdentifier, listener);
}
/**
* Registers new update manager.
*
* @param activity Parent activity.
* @param appIdentifier App ID of your app on HockeyApp.
*/
public static void register(Activity activity, String appIdentifier) {
register(activity, appIdentifier, true);
}
/**
* Registers new update manager.
*
* @param activity Parent activity.
* @param appIdentifier App ID of your app on HockeyApp.
* @param isDialogRequired Flag to indicate if a dialog must be shown when any update is available.
*/
public static void register(Activity activity, String appIdentifier, boolean isDialogRequired) {
register(activity, appIdentifier, null, isDialogRequired);
}
/**
* Registers new update manager.
*
* @param activity Parent activity.
* @param appIdentifier App ID of your app on HockeyApp.
* @param listener Implement for callback functions.
*/
public static void register(Activity activity, String appIdentifier, UpdateManagerListener listener) {
register(activity, Constants.BASE_URL, appIdentifier, listener, true);
}
/**
* Registers new update manager.
*
* @param activity Parent activity.
* @param appIdentifier App ID of your app on HockeyApp.
* @param listener Implement for callback functions.
* @param isDialogRequired Flag to indicate if a dialog must be shown when any update is available.
*/
public static void register(Activity activity, String appIdentifier, UpdateManagerListener listener, boolean isDialogRequired) {
register(activity, Constants.BASE_URL, appIdentifier, listener, isDialogRequired);
}
/**
* Registers new update manager.
*
* @param activity parent activity
* @param urlString URL of the HockeyApp server
* @param appIdentifier App ID of your app on HockeyApp
* @param listener implement for callback functions
*/
public static void register(Activity activity, String urlString, String appIdentifier, UpdateManagerListener listener) {
register(activity, urlString, appIdentifier, listener, true);
}
/**
* Registers new update manager.
*
* @param activity parent activity
* @param urlString URL of the HockeyApp server
* @param appIdentifier App ID of your app on HockeyApp
* @param listener implement for callback functions
* @param isDialogRequired if false, no alert dialog is shown
*/
public static void register(Activity activity, String urlString, String appIdentifier, UpdateManagerListener listener, boolean isDialogRequired) {
appIdentifier = Util.sanitizeAppIdentifier(appIdentifier);
Constants.loadFromContext(activity);
WeakReference<Activity> weakActivity = new WeakReference<>(activity);
if (dialogShown(weakActivity)) {
return;
}
if ((!checkExpiryDate(weakActivity, listener)) && ((listener != null && listener.canUpdateInMarket()) || !installedFromMarket(weakActivity))) {
startUpdateTask(weakActivity, urlString, appIdentifier, listener, isDialogRequired);
}
}
/**
* Registers new update manager.
*
* @param appContext Application context.
* @param appIdentifier App ID of your app on HockeyApp.
* @param listener Implement for callback functions.
*/
public static void registerForBackground(Context appContext, String appIdentifier, UpdateManagerListener listener) {
registerForBackground(appContext, Constants.BASE_URL, appIdentifier, listener);
}
/**
* Registers new update manager.
*
* @param appContext Application context.
* @param urlString URL of the HockeyApp server.
* @param appIdentifier App ID of your app on HockeyApp.
* @param listener Implement for callback functions.
*/
public static void registerForBackground(Context appContext, String urlString, String appIdentifier, UpdateManagerListener listener) {
appIdentifier = Util.sanitizeAppIdentifier(appIdentifier);
WeakReference<Context> weakContext = new WeakReference<>(appContext);
if ((!checkExpiryDateForBackground(listener)) && ((listener != null && listener.canUpdateInMarket()) || !installedFromMarket(weakContext))) {
startUpdateTaskForBackground(weakContext, urlString, appIdentifier, listener);
}
}
/**
* Unregisters the update manager
*/
public static void unregister() {
if (updateTask != null) {
updateTask.cancel(true);
updateTask.detach();
updateTask = null;
}
}
/**
* Returns true if the build is expired and starts an activity if not
* handled by the owner of the UpdateManager.
*/
private static boolean checkExpiryDate(WeakReference<Activity> weakActivity, UpdateManagerListener listener) {
boolean handle = false;
boolean hasExpired = checkExpiryDateForBackground(listener);
if (hasExpired) {
handle = listener.onBuildExpired();
}
if ((hasExpired) && (handle)) {
startExpiryInfoIntent(weakActivity);
}
return hasExpired;
}
/**
* Returns true if the build is expired and starts an activity if not
* handled by the owner of the UpdateManager.
*/
private static boolean checkExpiryDateForBackground(UpdateManagerListener listener) {
boolean result = false;
if (listener != null) {
Date expiryDate = listener.getExpiryDate();
result = ((expiryDate != null) && (new Date().compareTo(expiryDate) > 0));
}
return result;
}
/**
* Returns true if the build was installed through a market.
*/
protected static boolean installedFromMarket(WeakReference<? extends Context> weakContext) {
boolean result = false;
Context context = weakContext.get();
if (context != null) {
try {
String installer = context.getPackageManager().getInstallerPackageName(context.getPackageName());
// if installer string is not null it might be installed by market
if (!TextUtils.isEmpty(installer)) {
result = true;
// on Android Nougat and up when installing an app through the package installer (which HockeyApp uses itself), the installer will be
// "com.google.android.packageinstaller" or "com.android.packageinstaller" which is also not to be considered as a market installation
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && (TextUtils.equals(installer, INSTALLER_PACKAGE_INSTALLER_NOUGAT) || TextUtils.equals(installer, INSTALLER_PACKAGE_INSTALLER_NOUGAT2))) {
result = false;
}
// on some devices (Xiaomi) the installer identifier will be "adb", which is not to be considered as a market installation
if (TextUtils.equals(installer, INSTALLER_ADB)) {
result = false;
}
}
} catch (Throwable ignored) {
}
}
return result;
}
/**
* Starts the ExpiryInfoActivity as a new task and finished the current
* activity.
*/
private static void startExpiryInfoIntent(WeakReference<Activity> weakActivity) {
if (weakActivity != null) {
Activity activity = weakActivity.get();
if (activity != null) {
activity.finish();
Intent intent = new Intent(activity, ExpiryInfoActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
}
}
}
/**
* Starts the UpdateTask if not already running. Otherwise attaches the
* activity to it.
*/
private static void startUpdateTask(WeakReference<Activity> weakActivity, String urlString, String appIdentifier, UpdateManagerListener listener, boolean isDialogRequired) {
if ((updateTask == null) || (updateTask.getStatus() == Status.FINISHED)) {
updateTask = new CheckUpdateTaskWithUI(weakActivity, urlString, appIdentifier, listener, isDialogRequired);
AsyncTaskUtils.execute(updateTask);
} else {
updateTask.attach(weakActivity);
}
}
/**
* Starts the UpdateTask if not already running. Otherwise attaches the
* activity to it.
*/
private static void startUpdateTaskForBackground(WeakReference<Context> weakContext, String urlString, String appIdentifier, UpdateManagerListener listener) {
if ((updateTask == null) || (updateTask.getStatus() == Status.FINISHED)) {
updateTask = new CheckUpdateTask(weakContext, urlString, appIdentifier, listener);
AsyncTaskUtils.execute(updateTask);
} else {
updateTask.attach(weakContext);
}
}
/**
* Returns true if the dialog is already shown.
*/
private static boolean dialogShown(WeakReference<Activity> weakActivity) {
if (weakActivity != null) {
Activity activity = weakActivity.get();
if (activity != null) {
Fragment existingFragment = activity.getFragmentManager().findFragmentByTag("hockey_update_dialog");
return (existingFragment != null);
}
}
return false;
}
}