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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
/* -*- 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.activitystream.ActivityStream;
import org.mozilla.gecko.home.HomeConfig.PanelConfig;
import org.mozilla.gecko.home.HomeConfig.PanelType;
import org.mozilla.gecko.home.activitystream.ActivityStreamHomeFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HomeAdapter extends FragmentStatePagerAdapter {
private final Context mContext;
private final ArrayList<PanelInfo> mPanelInfos;
private final Map<String, HomeFragment> mPanels;
private final Map<String, Bundle> mRestoreBundles;
private boolean mCanLoadHint;
private OnAddPanelListener mAddPanelListener;
private HomeFragment.PanelStateChangeListener mPanelStateChangeListener = null;
public interface OnAddPanelListener {
void onAddPanel(String title);
}
public void setPanelStateChangeListener(HomeFragment.PanelStateChangeListener listener) {
mPanelStateChangeListener = listener;
for (Fragment fragment : mPanels.values()) {
((HomeFragment) fragment).setPanelStateChangeListener(listener);
}
}
public HomeAdapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
mCanLoadHint = HomeFragment.DEFAULT_CAN_LOAD_HINT;
mPanelInfos = new ArrayList<>();
mPanels = new HashMap<>();
mRestoreBundles = new HashMap<>();
}
@Override
public int getCount() {
return mPanelInfos.size();
}
@Override
public Fragment getItem(int position) {
PanelInfo info = mPanelInfos.get(position);
return Fragment.instantiate(mContext, info.getClassName(mContext), info.getArgs());
}
@Override
public CharSequence getPageTitle(int position) {
if (mPanelInfos.size() > 0) {
PanelInfo info = mPanelInfos.get(position);
return info.getTitle().toUpperCase();
}
return null;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
final HomeFragment fragment = (HomeFragment) super.instantiateItem(container, position);
fragment.setPanelStateChangeListener(mPanelStateChangeListener);
final String id = mPanelInfos.get(position).getId();
mPanels.put(id, fragment);
if (mRestoreBundles.containsKey(id)) {
fragment.restoreData(mRestoreBundles.get(id));
mRestoreBundles.remove(id);
}
return fragment;
}
public void setRestoreData(int position, Bundle data) {
final String id = mPanelInfos.get(position).getId();
final HomeFragment fragment = mPanels.get(id);
// We have no guarantees as to whether our desired fragment is instantiated yet: therefore
// we might need to either pass data to the fragment, or store it for later.
if (fragment != null) {
fragment.restoreData(data);
} else {
mRestoreBundles.put(id, data);
}
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
final String id = mPanelInfos.get(position).getId();
super.destroyItem(container, position, object);
mPanels.remove(id);
}
public void setOnAddPanelListener(OnAddPanelListener listener) {
mAddPanelListener = listener;
}
public int getItemPosition(String panelId) {
for (int i = 0; i < mPanelInfos.size(); i++) {
final String id = mPanelInfos.get(i).getId();
if (id.equals(panelId)) {
return i;
}
}
return -1;
}
public String getPanelIdAtPosition(int position) {
// getPanelIdAtPosition() might be called before HomeAdapter
// has got its initial list of PanelConfigs. Just bail.
if (mPanelInfos.isEmpty()) {
return null;
}
return mPanelInfos.get(position).getId();
}
private void addPanel(PanelInfo info) {
mPanelInfos.add(info);
if (mAddPanelListener != null) {
mAddPanelListener.onAddPanel(info.getTitle());
}
}
public void update(List<PanelConfig> panelConfigs) {
mPanels.clear();
mPanelInfos.clear();
if (panelConfigs != null) {
for (PanelConfig panelConfig : panelConfigs) {
final PanelInfo info = new PanelInfo(panelConfig);
addPanel(info);
}
}
notifyDataSetChanged();
}
public boolean getCanLoadHint() {
return mCanLoadHint;
}
public void setCanLoadHint(boolean canLoadHint) {
// We cache the last hint value so that we can use it when
// creating new panels. See PanelInfo.getArgs().
mCanLoadHint = canLoadHint;
// Enable/disable loading on all existing panels
for (Fragment panelFragment : mPanels.values()) {
final HomeFragment panel = (HomeFragment) panelFragment;
panel.setCanLoadHint(canLoadHint);
}
}
private final class PanelInfo {
private final PanelConfig mPanelConfig;
PanelInfo(PanelConfig panelConfig) {
mPanelConfig = panelConfig;
}
public String getId() {
return mPanelConfig.getId();
}
public String getTitle() {
return mPanelConfig.getTitle();
}
public String getClassName(Context context) {
final PanelType type = mPanelConfig.getType();
// Override top_sites with ActivityStream panel when enabled
// PanelType.toString() returns the panel id
if (type.toString() == "top_sites" &&
ActivityStream.isEnabled(context) &&
ActivityStream.isHomePanel()) {
return ActivityStreamHomeFragment.class.getName();
}
return type.getPanelClass().getName();
}
public Bundle getArgs() {
final Bundle args = new Bundle();
args.putBoolean(HomePager.CAN_LOAD_ARG, mCanLoadHint);
// Only DynamicPanels need the PanelConfig argument
if (mPanelConfig.isDynamic()) {
args.putParcelable(HomePager.PANEL_CONFIG_ARG, mPanelConfig);
}
return args;
}
}
}
|