|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko.updater; |
|
7 |
|
8 import org.mozilla.gecko.AppConstants; |
|
9 import org.mozilla.gecko.util.GeckoJarReader; |
|
10 |
|
11 import android.content.Context; |
|
12 import android.content.Intent; |
|
13 |
|
14 import android.content.pm.PackageManager; |
|
15 import android.content.pm.ApplicationInfo; |
|
16 |
|
17 import android.os.Build; |
|
18 |
|
19 import android.util.Log; |
|
20 |
|
21 import java.net.URL; |
|
22 |
|
23 public class UpdateServiceHelper { |
|
24 public static final String ACTION_REGISTER_FOR_UPDATES = AppConstants.ANDROID_PACKAGE_NAME + ".REGISTER_FOR_UPDATES"; |
|
25 public static final String ACTION_UNREGISTER_FOR_UPDATES = AppConstants.ANDROID_PACKAGE_NAME + ".UNREGISTER_FOR_UPDATES"; |
|
26 public static final String ACTION_CHECK_FOR_UPDATE = AppConstants.ANDROID_PACKAGE_NAME + ".CHECK_FOR_UPDATE"; |
|
27 public static final String ACTION_CHECK_UPDATE_RESULT = AppConstants.ANDROID_PACKAGE_NAME + ".CHECK_UPDATE_RESULT"; |
|
28 public static final String ACTION_DOWNLOAD_UPDATE = AppConstants.ANDROID_PACKAGE_NAME + ".DOWNLOAD_UPDATE"; |
|
29 public static final String ACTION_APPLY_UPDATE = AppConstants.ANDROID_PACKAGE_NAME + ".APPLY_UPDATE"; |
|
30 public static final String ACTION_CANCEL_DOWNLOAD = AppConstants.ANDROID_PACKAGE_NAME + ".CANCEL_DOWNLOAD"; |
|
31 |
|
32 // Flags for ACTION_CHECK_FOR_UPDATE |
|
33 public static final int FLAG_FORCE_DOWNLOAD = 1; |
|
34 public static final int FLAG_OVERWRITE_EXISTING = 1 << 1; |
|
35 public static final int FLAG_REINSTALL = 1 << 2; |
|
36 public static final int FLAG_RETRY = 1 << 3; |
|
37 |
|
38 // Name of the Intent extra for the autodownload policy, used with ACTION_REGISTER_FOR_UPDATES |
|
39 public static final String EXTRA_AUTODOWNLOAD_NAME = "autodownload"; |
|
40 |
|
41 // Values for EXTRA_AUTODOWNLOAD_NAME |
|
42 public static final int AUTODOWNLOAD_WIFI = 0; |
|
43 public static final int AUTODOWNLOAD_DISABLED = 1; |
|
44 public static final int AUTODOWNLOAD_ENABLED = 2; |
|
45 |
|
46 // Name of the Intent extra that holds the flags for ACTION_CHECK_FOR_UPDATE |
|
47 public static final String EXTRA_UPDATE_FLAGS_NAME = "updateFlags"; |
|
48 |
|
49 // Name of the Intent extra that holds the APK path, used with ACTION_APPLY_UPDATE |
|
50 public static final String EXTRA_PACKAGE_PATH_NAME = "packagePath"; |
|
51 |
|
52 private static final String LOGTAG = "UpdateServiceHelper"; |
|
53 private static final String DEFAULT_UPDATE_LOCALE = "en-US"; |
|
54 |
|
55 private static final String UPDATE_URL; |
|
56 |
|
57 static { |
|
58 final String pkgSpecial; |
|
59 if (AppConstants.MOZ_PKG_SPECIAL != null) { |
|
60 pkgSpecial = "-" + AppConstants.MOZ_PKG_SPECIAL; |
|
61 } else { |
|
62 pkgSpecial = ""; |
|
63 } |
|
64 UPDATE_URL = "https://aus4.mozilla.org/update/4/" + AppConstants.MOZ_APP_BASENAME + "/" + |
|
65 AppConstants.MOZ_APP_VERSION + |
|
66 "/%BUILDID%/Android_" + AppConstants.MOZ_APP_ABI + pkgSpecial + |
|
67 "/%LOCALE%/" + AppConstants.MOZ_UPDATE_CHANNEL + |
|
68 "/%OS_VERSION%/default/default/" + AppConstants.MOZILLA_VERSION + |
|
69 "/update.xml"; |
|
70 } |
|
71 |
|
72 public enum CheckUpdateResult { |
|
73 // Keep these in sync with mobile/android/chrome/content/about.xhtml |
|
74 NOT_AVAILABLE, |
|
75 AVAILABLE, |
|
76 DOWNLOADING, |
|
77 DOWNLOADED |
|
78 } |
|
79 |
|
80 public static URL getUpdateUrl(Context context, boolean force) { |
|
81 PackageManager pm = context.getPackageManager(); |
|
82 |
|
83 String locale = null; |
|
84 try { |
|
85 ApplicationInfo info = pm.getApplicationInfo(AppConstants.ANDROID_PACKAGE_NAME, 0); |
|
86 String updateLocaleUrl = "jar:jar:file://" + info.sourceDir + "!/" + AppConstants.OMNIJAR_NAME + "!/update.locale"; |
|
87 |
|
88 locale = GeckoJarReader.getText(updateLocaleUrl); |
|
89 |
|
90 if (locale != null) |
|
91 locale = locale.trim(); |
|
92 else |
|
93 locale = DEFAULT_UPDATE_LOCALE; |
|
94 } catch (android.content.pm.PackageManager.NameNotFoundException e) { |
|
95 // Shouldn't really be possible, but fallback to default locale |
|
96 Log.i(LOGTAG, "Failed to read update locale file, falling back to " + DEFAULT_UPDATE_LOCALE); |
|
97 locale = DEFAULT_UPDATE_LOCALE; |
|
98 } |
|
99 |
|
100 String url = UPDATE_URL.replace("%LOCALE%", locale). |
|
101 replace("%OS_VERSION%", Build.VERSION.RELEASE). |
|
102 replace("%BUILDID%", force ? "0" : AppConstants.MOZ_APP_BUILDID); |
|
103 |
|
104 try { |
|
105 return new URL(url); |
|
106 } catch (java.net.MalformedURLException e) { |
|
107 Log.e(LOGTAG, "Failed to create update url: ", e); |
|
108 return null; |
|
109 } |
|
110 } |
|
111 |
|
112 public static boolean isUpdaterEnabled() { |
|
113 return AppConstants.MOZ_UPDATER; |
|
114 } |
|
115 |
|
116 public static void registerForUpdates(Context context, String policy) { |
|
117 if (policy == null) |
|
118 return; |
|
119 |
|
120 int intPolicy; |
|
121 if (policy.equals("wifi")) { |
|
122 intPolicy = AUTODOWNLOAD_WIFI; |
|
123 } else if (policy.equals("disabled")) { |
|
124 intPolicy = AUTODOWNLOAD_DISABLED; |
|
125 } else if (policy.equals("enabled")) { |
|
126 intPolicy = AUTODOWNLOAD_ENABLED; |
|
127 } else { |
|
128 Log.w(LOGTAG, "Unhandled autoupdate policy: " + policy); |
|
129 return; |
|
130 } |
|
131 |
|
132 registerForUpdates(context, intPolicy); |
|
133 } |
|
134 |
|
135 // 'policy' should one of AUTODOWNLOAD_WIFI, AUTODOWNLOAD_DISABLED, AUTODOWNLOAD_ENABLED |
|
136 public static void registerForUpdates(Context context, int policy) { |
|
137 if (!isUpdaterEnabled()) |
|
138 return; |
|
139 |
|
140 Intent intent = new Intent(UpdateServiceHelper.ACTION_REGISTER_FOR_UPDATES, null, context, UpdateService.class); |
|
141 intent.putExtra(EXTRA_AUTODOWNLOAD_NAME, policy); |
|
142 |
|
143 context.startService(intent); |
|
144 } |
|
145 } |