summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/home/HomeConfigLoader.java
blob: 914d0fdd19367eded0be743fb8ab43f202a6734b (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
/* -*- 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.home;

import org.mozilla.gecko.home.HomeConfig.OnReloadListener;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;

public class HomeConfigLoader extends AsyncTaskLoader<HomeConfig.State> {
    private final HomeConfig mConfig;
    private HomeConfig.State mConfigState;

    private final Context mContext;

    public HomeConfigLoader(Context context, HomeConfig homeConfig) {
        super(context);
        mContext = context;
        mConfig = homeConfig;
    }

    @Override
    public HomeConfig.State loadInBackground() {
        return mConfig.load();
    }

    @Override
    public void deliverResult(HomeConfig.State configState) {
        if (isReset()) {
            mConfigState = null;
            return;
        }

        mConfigState = configState;
        mConfig.setOnReloadListener(new ForceReloadListener());

        if (isStarted()) {
            super.deliverResult(configState);
        }
    }

    @Override
    protected void onStartLoading() {
        if (mConfigState != null) {
            deliverResult(mConfigState);
        }

        if (takeContentChanged() || mConfigState == null) {
            forceLoad();
        }
    }

    @Override
    protected void onStopLoading() {
        cancelLoad();
    }

    @Override
    public void onCanceled(HomeConfig.State configState) {
        mConfigState = null;
    }

    @Override
    protected void onReset() {
        super.onReset();

        // Ensure the loader is stopped.
        onStopLoading();

        mConfigState = null;
        mConfig.setOnReloadListener(null);
    }

    private class ForceReloadListener implements OnReloadListener {
        @Override
        public void onReload() {
            onContentChanged();
        }
    }
}