summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/dlc/StudyAction.java
blob: e15a17bbeee448beb5e003d22d3d04fb6a4afd1d (plain)
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
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.dlc;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import org.mozilla.gecko.dlc.catalog.DownloadContent;
import org.mozilla.gecko.dlc.catalog.DownloadContentCatalog;
import org.mozilla.gecko.util.ContextUtils;

/**
 * Study: Scan the catalog for "new" content available for download.
 */
public class StudyAction extends BaseAction {
    private static final String LOGTAG = "DLCStudyAction";

    public void perform(Context context, DownloadContentCatalog catalog) {
        Log.d(LOGTAG, "Studying catalog..");

        for (DownloadContent content : catalog.getContentToStudy()) {
            if (!isMatching(context, content)) {
                // This content is not for this particular version of the application or system
                continue;
            }

            if (content.isAssetArchive() && content.isFont()) {
                catalog.scheduleDownload(content);

                Log.d(LOGTAG, "Scheduled download: " + content);
            }
        }

        if (catalog.hasScheduledDownloads()) {
            startDownloads(context);
        }

        Log.v(LOGTAG, "Done");
    }

    protected boolean isMatching(Context context, DownloadContent content) {
        final String androidApiPattern = content.getAndroidApiPattern();
        if (!TextUtils.isEmpty(androidApiPattern)) {
            final String apiVersion = String.valueOf(Build.VERSION.SDK_INT);
            if (apiVersion.matches(androidApiPattern)) {
                Log.d(LOGTAG, String.format("Android API (%s) does not match pattern: %s", apiVersion, androidApiPattern));
                return false;
            }
        }

        final String appIdPattern = content.getAppIdPattern();
        if (!TextUtils.isEmpty(appIdPattern)) {
            final String appId = context.getPackageName();
            if (!appId.matches(appIdPattern)) {
                Log.d(LOGTAG, String.format("App ID (%s) does not match pattern: %s", appId, appIdPattern));
                return false;
            }
        }

        final String appVersionPattern = content.getAppVersionPattern();
        if (!TextUtils.isEmpty(appVersionPattern)) {
            final String appVersion = ContextUtils.getCurrentPackageInfo(context).versionName;
            if (!appVersion.matches(appVersionPattern)) {
                Log.d(LOGTAG, String.format("App version (%s) does not match pattern: %s", appVersion, appVersionPattern));
                return false;
            }
        }

        // There are no patterns or all patterns have matched.
        return true;
    }

    protected void startDownloads(Context context) {
        DownloadContentService.startDownloads(context);
    }
}