michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.updater; michael@0: michael@0: import org.mozilla.gecko.AppConstants; michael@0: import org.mozilla.gecko.util.GeckoJarReader; michael@0: michael@0: import android.content.Context; michael@0: import android.content.Intent; michael@0: michael@0: import android.content.pm.PackageManager; michael@0: import android.content.pm.ApplicationInfo; michael@0: michael@0: import android.os.Build; michael@0: michael@0: import android.util.Log; michael@0: michael@0: import java.net.URL; michael@0: michael@0: public class UpdateServiceHelper { michael@0: public static final String ACTION_REGISTER_FOR_UPDATES = AppConstants.ANDROID_PACKAGE_NAME + ".REGISTER_FOR_UPDATES"; michael@0: public static final String ACTION_UNREGISTER_FOR_UPDATES = AppConstants.ANDROID_PACKAGE_NAME + ".UNREGISTER_FOR_UPDATES"; michael@0: public static final String ACTION_CHECK_FOR_UPDATE = AppConstants.ANDROID_PACKAGE_NAME + ".CHECK_FOR_UPDATE"; michael@0: public static final String ACTION_CHECK_UPDATE_RESULT = AppConstants.ANDROID_PACKAGE_NAME + ".CHECK_UPDATE_RESULT"; michael@0: public static final String ACTION_DOWNLOAD_UPDATE = AppConstants.ANDROID_PACKAGE_NAME + ".DOWNLOAD_UPDATE"; michael@0: public static final String ACTION_APPLY_UPDATE = AppConstants.ANDROID_PACKAGE_NAME + ".APPLY_UPDATE"; michael@0: public static final String ACTION_CANCEL_DOWNLOAD = AppConstants.ANDROID_PACKAGE_NAME + ".CANCEL_DOWNLOAD"; michael@0: michael@0: // Flags for ACTION_CHECK_FOR_UPDATE michael@0: public static final int FLAG_FORCE_DOWNLOAD = 1; michael@0: public static final int FLAG_OVERWRITE_EXISTING = 1 << 1; michael@0: public static final int FLAG_REINSTALL = 1 << 2; michael@0: public static final int FLAG_RETRY = 1 << 3; michael@0: michael@0: // Name of the Intent extra for the autodownload policy, used with ACTION_REGISTER_FOR_UPDATES michael@0: public static final String EXTRA_AUTODOWNLOAD_NAME = "autodownload"; michael@0: michael@0: // Values for EXTRA_AUTODOWNLOAD_NAME michael@0: public static final int AUTODOWNLOAD_WIFI = 0; michael@0: public static final int AUTODOWNLOAD_DISABLED = 1; michael@0: public static final int AUTODOWNLOAD_ENABLED = 2; michael@0: michael@0: // Name of the Intent extra that holds the flags for ACTION_CHECK_FOR_UPDATE michael@0: public static final String EXTRA_UPDATE_FLAGS_NAME = "updateFlags"; michael@0: michael@0: // Name of the Intent extra that holds the APK path, used with ACTION_APPLY_UPDATE michael@0: public static final String EXTRA_PACKAGE_PATH_NAME = "packagePath"; michael@0: michael@0: private static final String LOGTAG = "UpdateServiceHelper"; michael@0: private static final String DEFAULT_UPDATE_LOCALE = "en-US"; michael@0: michael@0: private static final String UPDATE_URL; michael@0: michael@0: static { michael@0: final String pkgSpecial; michael@0: if (AppConstants.MOZ_PKG_SPECIAL != null) { michael@0: pkgSpecial = "-" + AppConstants.MOZ_PKG_SPECIAL; michael@0: } else { michael@0: pkgSpecial = ""; michael@0: } michael@0: UPDATE_URL = "https://aus4.mozilla.org/update/4/" + AppConstants.MOZ_APP_BASENAME + "/" + michael@0: AppConstants.MOZ_APP_VERSION + michael@0: "/%BUILDID%/Android_" + AppConstants.MOZ_APP_ABI + pkgSpecial + michael@0: "/%LOCALE%/" + AppConstants.MOZ_UPDATE_CHANNEL + michael@0: "/%OS_VERSION%/default/default/" + AppConstants.MOZILLA_VERSION + michael@0: "/update.xml"; michael@0: } michael@0: michael@0: public enum CheckUpdateResult { michael@0: // Keep these in sync with mobile/android/chrome/content/about.xhtml michael@0: NOT_AVAILABLE, michael@0: AVAILABLE, michael@0: DOWNLOADING, michael@0: DOWNLOADED michael@0: } michael@0: michael@0: public static URL getUpdateUrl(Context context, boolean force) { michael@0: PackageManager pm = context.getPackageManager(); michael@0: michael@0: String locale = null; michael@0: try { michael@0: ApplicationInfo info = pm.getApplicationInfo(AppConstants.ANDROID_PACKAGE_NAME, 0); michael@0: String updateLocaleUrl = "jar:jar:file://" + info.sourceDir + "!/" + AppConstants.OMNIJAR_NAME + "!/update.locale"; michael@0: michael@0: locale = GeckoJarReader.getText(updateLocaleUrl); michael@0: michael@0: if (locale != null) michael@0: locale = locale.trim(); michael@0: else michael@0: locale = DEFAULT_UPDATE_LOCALE; michael@0: } catch (android.content.pm.PackageManager.NameNotFoundException e) { michael@0: // Shouldn't really be possible, but fallback to default locale michael@0: Log.i(LOGTAG, "Failed to read update locale file, falling back to " + DEFAULT_UPDATE_LOCALE); michael@0: locale = DEFAULT_UPDATE_LOCALE; michael@0: } michael@0: michael@0: String url = UPDATE_URL.replace("%LOCALE%", locale). michael@0: replace("%OS_VERSION%", Build.VERSION.RELEASE). michael@0: replace("%BUILDID%", force ? "0" : AppConstants.MOZ_APP_BUILDID); michael@0: michael@0: try { michael@0: return new URL(url); michael@0: } catch (java.net.MalformedURLException e) { michael@0: Log.e(LOGTAG, "Failed to create update url: ", e); michael@0: return null; michael@0: } michael@0: } michael@0: michael@0: public static boolean isUpdaterEnabled() { michael@0: return AppConstants.MOZ_UPDATER; michael@0: } michael@0: michael@0: public static void registerForUpdates(Context context, String policy) { michael@0: if (policy == null) michael@0: return; michael@0: michael@0: int intPolicy; michael@0: if (policy.equals("wifi")) { michael@0: intPolicy = AUTODOWNLOAD_WIFI; michael@0: } else if (policy.equals("disabled")) { michael@0: intPolicy = AUTODOWNLOAD_DISABLED; michael@0: } else if (policy.equals("enabled")) { michael@0: intPolicy = AUTODOWNLOAD_ENABLED; michael@0: } else { michael@0: Log.w(LOGTAG, "Unhandled autoupdate policy: " + policy); michael@0: return; michael@0: } michael@0: michael@0: registerForUpdates(context, intPolicy); michael@0: } michael@0: michael@0: // 'policy' should one of AUTODOWNLOAD_WIFI, AUTODOWNLOAD_DISABLED, AUTODOWNLOAD_ENABLED michael@0: public static void registerForUpdates(Context context, int policy) { michael@0: if (!isUpdaterEnabled()) michael@0: return; michael@0: michael@0: Intent intent = new Intent(UpdateServiceHelper.ACTION_REGISTER_FOR_UPDATES, null, context, UpdateService.class); michael@0: intent.putExtra(EXTRA_AUTODOWNLOAD_NAME, policy); michael@0: michael@0: context.startService(intent); michael@0: } michael@0: }