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