|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko.home; |
|
7 |
|
8 import org.mozilla.gecko.home.HomeConfig.PanelConfig; |
|
9 import org.mozilla.gecko.home.HomeConfig.OnReloadListener; |
|
10 |
|
11 import android.content.Context; |
|
12 import android.support.v4.content.AsyncTaskLoader; |
|
13 |
|
14 import java.util.List; |
|
15 |
|
16 public class HomeConfigLoader extends AsyncTaskLoader<HomeConfig.State> { |
|
17 private final HomeConfig mConfig; |
|
18 private HomeConfig.State mConfigState; |
|
19 |
|
20 private final Context mContext; |
|
21 |
|
22 public HomeConfigLoader(Context context, HomeConfig homeConfig) { |
|
23 super(context); |
|
24 mContext = context; |
|
25 mConfig = homeConfig; |
|
26 } |
|
27 |
|
28 @Override |
|
29 public HomeConfig.State loadInBackground() { |
|
30 return mConfig.load(); |
|
31 } |
|
32 |
|
33 @Override |
|
34 public void deliverResult(HomeConfig.State configState) { |
|
35 if (isReset()) { |
|
36 mConfigState = null; |
|
37 return; |
|
38 } |
|
39 |
|
40 mConfigState = configState; |
|
41 mConfig.setOnReloadListener(new ForceReloadListener()); |
|
42 |
|
43 if (isStarted()) { |
|
44 super.deliverResult(configState); |
|
45 } |
|
46 } |
|
47 |
|
48 @Override |
|
49 protected void onStartLoading() { |
|
50 if (mConfigState != null) { |
|
51 deliverResult(mConfigState); |
|
52 } |
|
53 |
|
54 if (takeContentChanged() || mConfigState == null) { |
|
55 forceLoad(); |
|
56 } |
|
57 } |
|
58 |
|
59 @Override |
|
60 protected void onStopLoading() { |
|
61 cancelLoad(); |
|
62 } |
|
63 |
|
64 @Override |
|
65 public void onCanceled(HomeConfig.State configState) { |
|
66 mConfigState = null; |
|
67 } |
|
68 |
|
69 @Override |
|
70 protected void onReset() { |
|
71 super.onReset(); |
|
72 |
|
73 // Ensure the loader is stopped. |
|
74 onStopLoading(); |
|
75 |
|
76 mConfigState = null; |
|
77 mConfig.setOnReloadListener(null); |
|
78 } |
|
79 |
|
80 private class ForceReloadListener implements OnReloadListener { |
|
81 @Override |
|
82 public void onReload() { |
|
83 onContentChanged(); |
|
84 } |
|
85 } |
|
86 } |