mobile/android/base/updater/UpdateServiceHelper.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/updater/UpdateServiceHelper.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,145 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko.updater;
    1.10 +
    1.11 +import org.mozilla.gecko.AppConstants;
    1.12 +import org.mozilla.gecko.util.GeckoJarReader;
    1.13 +
    1.14 +import android.content.Context;
    1.15 +import android.content.Intent;
    1.16 +
    1.17 +import android.content.pm.PackageManager;
    1.18 +import android.content.pm.ApplicationInfo;
    1.19 +
    1.20 +import android.os.Build;
    1.21 +
    1.22 +import android.util.Log;
    1.23 +
    1.24 +import java.net.URL;
    1.25 +
    1.26 +public class UpdateServiceHelper {
    1.27 +    public static final String ACTION_REGISTER_FOR_UPDATES = AppConstants.ANDROID_PACKAGE_NAME + ".REGISTER_FOR_UPDATES";
    1.28 +    public static final String ACTION_UNREGISTER_FOR_UPDATES = AppConstants.ANDROID_PACKAGE_NAME + ".UNREGISTER_FOR_UPDATES";
    1.29 +    public static final String ACTION_CHECK_FOR_UPDATE = AppConstants.ANDROID_PACKAGE_NAME + ".CHECK_FOR_UPDATE";
    1.30 +    public static final String ACTION_CHECK_UPDATE_RESULT = AppConstants.ANDROID_PACKAGE_NAME + ".CHECK_UPDATE_RESULT";
    1.31 +    public static final String ACTION_DOWNLOAD_UPDATE = AppConstants.ANDROID_PACKAGE_NAME + ".DOWNLOAD_UPDATE";
    1.32 +    public static final String ACTION_APPLY_UPDATE = AppConstants.ANDROID_PACKAGE_NAME + ".APPLY_UPDATE";
    1.33 +    public static final String ACTION_CANCEL_DOWNLOAD = AppConstants.ANDROID_PACKAGE_NAME + ".CANCEL_DOWNLOAD";
    1.34 +
    1.35 +    // Flags for ACTION_CHECK_FOR_UPDATE
    1.36 +    public static final int FLAG_FORCE_DOWNLOAD = 1;
    1.37 +    public static final int FLAG_OVERWRITE_EXISTING = 1 << 1;
    1.38 +    public static final int FLAG_REINSTALL = 1 << 2;
    1.39 +    public static final int FLAG_RETRY = 1 << 3;
    1.40 +
    1.41 +    // Name of the Intent extra for the autodownload policy, used with ACTION_REGISTER_FOR_UPDATES
    1.42 +    public static final String EXTRA_AUTODOWNLOAD_NAME = "autodownload";
    1.43 +
    1.44 +    // Values for EXTRA_AUTODOWNLOAD_NAME
    1.45 +    public static final int AUTODOWNLOAD_WIFI = 0;
    1.46 +    public static final int AUTODOWNLOAD_DISABLED = 1;
    1.47 +    public static final int AUTODOWNLOAD_ENABLED = 2;
    1.48 +
    1.49 +    // Name of the Intent extra that holds the flags for ACTION_CHECK_FOR_UPDATE
    1.50 +    public static final String EXTRA_UPDATE_FLAGS_NAME = "updateFlags";
    1.51 +
    1.52 +    // Name of the Intent extra that holds the APK path, used with ACTION_APPLY_UPDATE
    1.53 +    public static final String EXTRA_PACKAGE_PATH_NAME = "packagePath";
    1.54 +
    1.55 +    private static final String LOGTAG = "UpdateServiceHelper";
    1.56 +    private static final String DEFAULT_UPDATE_LOCALE = "en-US";
    1.57 +
    1.58 +    private static final String UPDATE_URL;
    1.59 +
    1.60 +    static {
    1.61 +        final String pkgSpecial;
    1.62 +        if (AppConstants.MOZ_PKG_SPECIAL != null) {
    1.63 +            pkgSpecial = "-" + AppConstants.MOZ_PKG_SPECIAL;
    1.64 +        } else {
    1.65 +            pkgSpecial = "";
    1.66 +        }
    1.67 +        UPDATE_URL = "https://aus4.mozilla.org/update/4/" + AppConstants.MOZ_APP_BASENAME + "/" +
    1.68 +                     AppConstants.MOZ_APP_VERSION         +
    1.69 +                     "/%BUILDID%/Android_"                + AppConstants.MOZ_APP_ABI + pkgSpecial +
    1.70 +                     "/%LOCALE%/"                         + AppConstants.MOZ_UPDATE_CHANNEL +
    1.71 +                     "/%OS_VERSION%/default/default/"     + AppConstants.MOZILLA_VERSION +
    1.72 +                     "/update.xml";
    1.73 +    }
    1.74 +
    1.75 +    public enum CheckUpdateResult {
    1.76 +        // Keep these in sync with mobile/android/chrome/content/about.xhtml
    1.77 +        NOT_AVAILABLE,
    1.78 +        AVAILABLE,
    1.79 +        DOWNLOADING,
    1.80 +        DOWNLOADED
    1.81 +    }
    1.82 +
    1.83 +    public static URL getUpdateUrl(Context context, boolean force) {
    1.84 +        PackageManager pm = context.getPackageManager();
    1.85 +
    1.86 +        String locale = null;
    1.87 +        try {
    1.88 +            ApplicationInfo info = pm.getApplicationInfo(AppConstants.ANDROID_PACKAGE_NAME, 0);
    1.89 +            String updateLocaleUrl = "jar:jar:file://" + info.sourceDir + "!/" + AppConstants.OMNIJAR_NAME + "!/update.locale";
    1.90 +
    1.91 +            locale = GeckoJarReader.getText(updateLocaleUrl);
    1.92 +
    1.93 +            if (locale != null)
    1.94 +                locale = locale.trim();
    1.95 +            else
    1.96 +                locale = DEFAULT_UPDATE_LOCALE;
    1.97 +        } catch (android.content.pm.PackageManager.NameNotFoundException e) {
    1.98 +            // Shouldn't really be possible, but fallback to default locale
    1.99 +            Log.i(LOGTAG, "Failed to read update locale file, falling back to " + DEFAULT_UPDATE_LOCALE);
   1.100 +            locale = DEFAULT_UPDATE_LOCALE;
   1.101 +        }
   1.102 +
   1.103 +        String url = UPDATE_URL.replace("%LOCALE%", locale).
   1.104 +            replace("%OS_VERSION%", Build.VERSION.RELEASE).
   1.105 +            replace("%BUILDID%", force ? "0" : AppConstants.MOZ_APP_BUILDID);
   1.106 +
   1.107 +        try {
   1.108 +            return new URL(url);
   1.109 +        } catch (java.net.MalformedURLException e) {
   1.110 +            Log.e(LOGTAG, "Failed to create update url: ", e);
   1.111 +            return null;
   1.112 +        }
   1.113 +    }
   1.114 +
   1.115 +    public static boolean isUpdaterEnabled() {
   1.116 +        return AppConstants.MOZ_UPDATER;
   1.117 +    }
   1.118 +
   1.119 +    public static void registerForUpdates(Context context, String policy) {
   1.120 +        if (policy == null)
   1.121 +            return;
   1.122 +
   1.123 +        int intPolicy;
   1.124 +        if (policy.equals("wifi")) {
   1.125 +            intPolicy = AUTODOWNLOAD_WIFI;
   1.126 +        } else if (policy.equals("disabled")) {
   1.127 +            intPolicy = AUTODOWNLOAD_DISABLED;
   1.128 +        } else if (policy.equals("enabled")) {
   1.129 +            intPolicy = AUTODOWNLOAD_ENABLED;
   1.130 +        } else {
   1.131 +            Log.w(LOGTAG, "Unhandled autoupdate policy: " + policy);
   1.132 +            return;
   1.133 +        }
   1.134 +
   1.135 +        registerForUpdates(context, intPolicy);
   1.136 +    }
   1.137 +
   1.138 +    // 'policy' should one of AUTODOWNLOAD_WIFI, AUTODOWNLOAD_DISABLED, AUTODOWNLOAD_ENABLED
   1.139 +    public static void registerForUpdates(Context context, int policy) {
   1.140 +        if (!isUpdaterEnabled())
   1.141 +            return;
   1.142 +
   1.143 +        Intent intent = new Intent(UpdateServiceHelper.ACTION_REGISTER_FOR_UPDATES, null, context, UpdateService.class);
   1.144 +        intent.putExtra(EXTRA_AUTODOWNLOAD_NAME, policy);
   1.145 +
   1.146 +        context.startService(intent);
   1.147 +    }
   1.148 +}

mercurial