summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/dlc/DownloadContentService.java
blob: 3729cf2e0627d129c539bba1e554542e21d75a57 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* -*- 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 org.mozilla.gecko.AppConstants;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.dlc.catalog.DownloadContent;
import org.mozilla.gecko.dlc.catalog.DownloadContentCatalog;
import org.mozilla.gecko.util.HardwareUtils;

import android.app.IntentService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Service to handle downloadable content that did not ship with the APK.
 */
public class DownloadContentService extends IntentService {
    private static final String LOGTAG = "GeckoDLCService";

    /**
     * Study: Scan the catalog for "new" content available for download.
     */
    private static final String ACTION_STUDY_CATALOG = AppConstants.ANDROID_PACKAGE_NAME + ".DLC.STUDY";

    /**
     * Verify: Validate downloaded content. Does it still exist and does it have the correct checksum?
     */
    private static final String ACTION_VERIFY_CONTENT = AppConstants.ANDROID_PACKAGE_NAME + ".DLC.VERIFY";

    /**
     * Download content that has been scheduled during "study" or "verify".
     */
    private static final String ACTION_DOWNLOAD_CONTENT = AppConstants.ANDROID_PACKAGE_NAME + ".DLC.DOWNLOAD";

    /**
     * Sync: Synchronize catalog from a Kinto instance.
     */
    private static final String ACTION_SYNCHRONIZE_CATALOG = AppConstants.ANDROID_PACKAGE_NAME + ".DLC.SYNC";

    /**
     * CleanupAction: Remove content that is no longer needed (e.g. Removed from the catalog after a sync).
     */
    private static final String ACTION_CLEANUP_FILES = AppConstants.ANDROID_PACKAGE_NAME + ".DLC.CLEANUP";

    public static void startStudy(Context context) {
        Intent intent = new Intent(ACTION_STUDY_CATALOG);
        intent.setComponent(new ComponentName(context, DownloadContentService.class));
        context.startService(intent);
    }

    public static void startVerification(Context context) {
        Intent intent = new Intent(ACTION_VERIFY_CONTENT);
        intent.setComponent(new ComponentName(context, DownloadContentService.class));
        context.startService(intent);
    }

    public static void startDownloads(Context context) {
        Intent intent = new Intent(ACTION_DOWNLOAD_CONTENT);
        intent.setComponent(new ComponentName(context, DownloadContentService.class));
        context.startService(intent);
    }

    public static void startSync(Context context) {
        Intent intent = new Intent(ACTION_SYNCHRONIZE_CATALOG);
        intent.setComponent(new ComponentName(context, DownloadContentService.class));
        context.startService(intent);
    }

    public static void startCleanup(Context context) {
        Intent intent = new Intent(ACTION_CLEANUP_FILES);
        intent.setComponent(new ComponentName(context, DownloadContentService.class));
        context.startService(intent);
    }

    private DownloadContentCatalog catalog;

    public DownloadContentService() {
        super(LOGTAG);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        catalog = new DownloadContentCatalog(this);
    }

    protected void onHandleIntent(Intent intent) {
        if (!AppConstants.MOZ_ANDROID_DOWNLOAD_CONTENT_SERVICE) {
            Log.w(LOGTAG, "Download content is not enabled. Stop.");
            return;
        }

        if (!HardwareUtils.isSupportedSystem()) {
            // This service is running very early before checks in BrowserApp can prevent us from running.
            Log.w(LOGTAG, "System is not supported. Stop.");
            return;
        }

        if (intent == null) {
            return;
        }

        final BaseAction action;

        switch (intent.getAction()) {
            case ACTION_STUDY_CATALOG:
                action = new StudyAction();
                break;

            case ACTION_DOWNLOAD_CONTENT:
                action = new DownloadAction(new DownloadAction.Callback() {
                    @Override
                    public void onContentDownloaded(DownloadContent content) {
                        if (content.isFont()) {
                            GeckoAppShell.notifyObservers("Fonts:Reload", "");
                        }
                    }
                });
                break;

            case ACTION_VERIFY_CONTENT:
                action = new VerifyAction();
                break;

            case ACTION_SYNCHRONIZE_CATALOG:
                action = new SyncAction();
                break;

            default:
                Log.e(LOGTAG, "Unknown action: " + intent.getAction());
                return;
        }

        action.perform(this, catalog);
        catalog.persistChanges();
    }
}