mobile/android/base/home/HomeConfigLoader.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/home/HomeConfigLoader.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     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.home;
    1.10 +
    1.11 +import org.mozilla.gecko.home.HomeConfig.PanelConfig;
    1.12 +import org.mozilla.gecko.home.HomeConfig.OnReloadListener;
    1.13 +
    1.14 +import android.content.Context;
    1.15 +import android.support.v4.content.AsyncTaskLoader;
    1.16 +
    1.17 +import java.util.List;
    1.18 +
    1.19 +public class HomeConfigLoader extends AsyncTaskLoader<HomeConfig.State> {
    1.20 +    private final HomeConfig mConfig;
    1.21 +    private HomeConfig.State mConfigState;
    1.22 +
    1.23 +    private final Context mContext;
    1.24 +
    1.25 +    public HomeConfigLoader(Context context, HomeConfig homeConfig) {
    1.26 +        super(context);
    1.27 +        mContext = context;
    1.28 +        mConfig = homeConfig;
    1.29 +    }
    1.30 +
    1.31 +    @Override
    1.32 +    public HomeConfig.State loadInBackground() {
    1.33 +        return mConfig.load();
    1.34 +    }
    1.35 +
    1.36 +    @Override
    1.37 +    public void deliverResult(HomeConfig.State configState) {
    1.38 +        if (isReset()) {
    1.39 +            mConfigState = null;
    1.40 +            return;
    1.41 +        }
    1.42 +
    1.43 +        mConfigState = configState;
    1.44 +        mConfig.setOnReloadListener(new ForceReloadListener());
    1.45 +
    1.46 +        if (isStarted()) {
    1.47 +            super.deliverResult(configState);
    1.48 +        }
    1.49 +    }
    1.50 +
    1.51 +    @Override
    1.52 +    protected void onStartLoading() {
    1.53 +        if (mConfigState != null) {
    1.54 +            deliverResult(mConfigState);
    1.55 +        }
    1.56 +
    1.57 +        if (takeContentChanged() || mConfigState == null) {
    1.58 +            forceLoad();
    1.59 +        }
    1.60 +    }
    1.61 +
    1.62 +    @Override
    1.63 +    protected void onStopLoading() {
    1.64 +        cancelLoad();
    1.65 +    }
    1.66 +
    1.67 +    @Override
    1.68 +    public void onCanceled(HomeConfig.State configState) {
    1.69 +        mConfigState = null;
    1.70 +    }
    1.71 +
    1.72 +    @Override
    1.73 +    protected void onReset() {
    1.74 +        super.onReset();
    1.75 +
    1.76 +        // Ensure the loader is stopped.
    1.77 +        onStopLoading();
    1.78 +
    1.79 +        mConfigState = null;
    1.80 +        mConfig.setOnReloadListener(null);
    1.81 +    }
    1.82 +
    1.83 +    private class ForceReloadListener implements OnReloadListener {
    1.84 +        @Override
    1.85 +        public void onReload() {
    1.86 +            onContentChanged();
    1.87 +        }
    1.88 +    }
    1.89 +}

mercurial