diff -r 000000000000 -r 6474c204b198 mobile/android/base/home/HomeConfigLoader.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mobile/android/base/home/HomeConfigLoader.java Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,86 @@ +/* -*- 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.PanelConfig; +import org.mozilla.gecko.home.HomeConfig.OnReloadListener; + +import android.content.Context; +import android.support.v4.content.AsyncTaskLoader; + +import java.util.List; + +public class HomeConfigLoader extends AsyncTaskLoader { + 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(); + } + } +}