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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
|
/* -*- 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.R;
import org.mozilla.gecko.db.BrowserContract.HomeItems;
import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
import org.mozilla.gecko.home.HomeConfig.EmptyViewConfig;
import org.mozilla.gecko.home.HomeConfig.ItemHandler;
import org.mozilla.gecko.home.HomeConfig.PanelConfig;
import org.mozilla.gecko.home.HomeConfig.ViewConfig;
import org.mozilla.gecko.util.StringUtils;
import android.content.Context;
import android.database.Cursor;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.lang.ref.SoftReference;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.WeakHashMap;
import com.squareup.picasso.Picasso;
/**
* {@code PanelLayout} is the base class for custom layouts to be
* used in {@code DynamicPanel}. It provides the basic framework
* that enables custom layouts to request and reset datasets and
* create panel views. Furthermore, it automates most of the process
* of binding panel views with their respective datasets.
*
* {@code PanelLayout} abstracts the implemention details of how
* datasets are actually loaded through the {@DatasetHandler} interface.
* {@code DatasetHandler} provides two operations: request and reset.
* The results of the dataset requests done via the {@code DatasetHandler}
* are delivered to the {@code PanelLayout} with the {@code deliverDataset()}
* method.
*
* Subclasses of {@code PanelLayout} should simply use the utilities
* provided by {@code PanelLayout}. Namely:
*
* {@code requestDataset()} - To fetch datasets and auto-bind them to
* the existing panel views backed by them.
*
* {@code resetDataset()} - To release any resources associated with a
* previously loaded dataset.
*
* {@code createPanelView()} - To create a panel view for a ViewConfig
* associated with the panel.
*
* {@code disposePanelView()} - To dispose any dataset references associated
* with the given view.
*
* {@code PanelLayout} subclasses should always use {@code createPanelView()}
* to create the views dynamically created based on {@code ViewConfig}. This
* allows {@code PanelLayout} to auto-bind datasets with panel views.
* {@code PanelLayout} subclasses are free to have any type of views to arrange
* the panel views in different ways.
*/
abstract class PanelLayout extends FrameLayout {
private static final String LOGTAG = "GeckoPanelLayout";
protected final SparseArray<ViewState> mViewStates;
private final PanelConfig mPanelConfig;
private final DatasetHandler mDatasetHandler;
private final OnUrlOpenListener mUrlOpenListener;
private final ContextMenuRegistry mContextMenuRegistry;
/**
* To be used by panel views to express that they are
* backed by datasets.
*/
public interface DatasetBacked {
public void setDataset(Cursor cursor);
public void setFilterManager(FilterManager manager);
}
/**
* To be used by requests made to {@code DatasetHandler}s to couple dataset ID with current
* filter for queries on the database.
*/
public static class DatasetRequest implements Parcelable {
public enum Type implements Parcelable {
DATASET_LOAD,
FILTER_PUSH,
FILTER_POP;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(ordinal());
}
public static final Creator<Type> CREATOR = new Creator<Type>() {
@Override
public Type createFromParcel(final Parcel source) {
return Type.values()[source.readInt()];
}
@Override
public Type[] newArray(final int size) {
return new Type[size];
}
};
}
private final int mViewIndex;
private final Type mType;
private final String mDatasetId;
private final FilterDetail mFilterDetail;
private DatasetRequest(Parcel in) {
this.mViewIndex = in.readInt();
this.mType = (Type) in.readParcelable(getClass().getClassLoader());
this.mDatasetId = in.readString();
this.mFilterDetail = (FilterDetail) in.readParcelable(getClass().getClassLoader());
}
public DatasetRequest(int index, String datasetId, FilterDetail filterDetail) {
this(index, Type.DATASET_LOAD, datasetId, filterDetail);
}
public DatasetRequest(int index, Type type, String datasetId, FilterDetail filterDetail) {
this.mViewIndex = index;
this.mType = type;
this.mDatasetId = datasetId;
this.mFilterDetail = filterDetail;
}
public int getViewIndex() {
return mViewIndex;
}
public Type getType() {
return mType;
}
public String getDatasetId() {
return mDatasetId;
}
public String getFilter() {
return (mFilterDetail != null ? mFilterDetail.filter : null);
}
public FilterDetail getFilterDetail() {
return mFilterDetail;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mViewIndex);
dest.writeParcelable(mType, 0);
dest.writeString(mDatasetId);
dest.writeParcelable(mFilterDetail, 0);
}
public String toString() {
return "{ index: " + mViewIndex +
", type: " + mType +
", dataset: " + mDatasetId +
", filter: " + mFilterDetail +
" }";
}
public static final Creator<DatasetRequest> CREATOR = new Creator<DatasetRequest>() {
@Override
public DatasetRequest createFromParcel(Parcel in) {
return new DatasetRequest(in);
}
@Override
public DatasetRequest[] newArray(int size) {
return new DatasetRequest[size];
}
};
}
/**
* Defines the contract with the component that is responsible
* for handling datasets requests.
*/
public interface DatasetHandler {
/**
* Requests a dataset to be fetched and auto-bound to the
* panel views backed by it.
*/
public void requestDataset(DatasetRequest request);
/**
* Releases any resources associated with a panel view. It will
* do nothing if the view with the given index been created
* before.
*/
public void resetDataset(int viewIndex);
}
public interface PanelView {
public void setOnItemOpenListener(OnItemOpenListener listener);
public void setOnKeyListener(OnKeyListener listener);
public void setContextMenuInfoFactory(HomeContextMenuInfo.Factory factory);
}
public interface FilterManager {
public FilterDetail getPreviousFilter();
public boolean canGoBack();
public void goBack();
}
public interface ContextMenuRegistry {
public void register(View view);
}
public PanelLayout(Context context, PanelConfig panelConfig, DatasetHandler datasetHandler,
OnUrlOpenListener urlOpenListener, ContextMenuRegistry contextMenuRegistry) {
super(context);
mViewStates = new SparseArray<ViewState>();
mPanelConfig = panelConfig;
mDatasetHandler = datasetHandler;
mUrlOpenListener = urlOpenListener;
mContextMenuRegistry = contextMenuRegistry;
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
final int count = mViewStates.size();
for (int i = 0; i < count; i++) {
final ViewState viewState = mViewStates.valueAt(i);
final View view = viewState.getView();
if (view != null) {
maybeSetDataset(view, null);
}
}
mViewStates.clear();
}
/**
* Delivers the dataset as a {@code Cursor} to be bound to the
* panel view backed by it. This is used by the {@code DatasetHandler}
* in response to a dataset request.
*/
public final void deliverDataset(DatasetRequest request, Cursor cursor) {
Log.d(LOGTAG, "Delivering request: " + request);
final ViewState viewState = mViewStates.get(request.getViewIndex());
if (viewState == null) {
return;
}
switch (request.getType()) {
case FILTER_PUSH:
viewState.pushFilter(request.getFilterDetail());
break;
case FILTER_POP:
viewState.popFilter();
break;
}
final View activeView = viewState.getActiveView();
if (activeView == null) {
throw new IllegalStateException("No active view for view state: " + viewState.getIndex());
}
final ViewConfig viewConfig = viewState.getViewConfig();
final View newView;
if (cursor == null || cursor.getCount() == 0) {
newView = createEmptyView(viewConfig);
maybeSetDataset(activeView, null);
} else {
newView = createPanelView(viewConfig);
maybeSetDataset(newView, cursor);
}
if (activeView != newView) {
replacePanelView(activeView, newView);
}
}
/**
* Releases any references to the given dataset from all
* existing panel views.
*/
public final void releaseDataset(int viewIndex) {
Log.d(LOGTAG, "Releasing dataset: " + viewIndex);
final ViewState viewState = mViewStates.get(viewIndex);
if (viewState == null) {
return;
}
final View view = viewState.getView();
if (view != null) {
maybeSetDataset(view, null);
}
}
/**
* Requests a dataset to be loaded and bound to any existing
* panel view backed by it.
*/
protected final void requestDataset(DatasetRequest request) {
Log.d(LOGTAG, "Requesting request: " + request);
if (mViewStates.get(request.getViewIndex()) == null) {
return;
}
mDatasetHandler.requestDataset(request);
}
/**
* Releases any resources associated with a panel view.
* e.g. close any associated {@code Cursor}.
*/
protected final void resetDataset(int viewIndex) {
Log.d(LOGTAG, "Resetting view with index: " + viewIndex);
if (mViewStates.get(viewIndex) == null) {
return;
}
mDatasetHandler.resetDataset(viewIndex);
}
/**
* Factory method to create instance of panels from a given
* {@code ViewConfig}. All panel views defined in {@code PanelConfig}
* should be created using this method so that {@PanelLayout} can
* keep track of panel views and their associated datasets.
*/
protected final View createPanelView(ViewConfig viewConfig) {
Log.d(LOGTAG, "Creating panel view: " + viewConfig.getType());
ViewState viewState = mViewStates.get(viewConfig.getIndex());
if (viewState == null) {
viewState = new ViewState(viewConfig);
mViewStates.put(viewConfig.getIndex(), viewState);
}
View view = viewState.getView();
if (view == null) {
switch (viewConfig.getType()) {
case LIST:
view = new PanelListView(getContext(), viewConfig);
break;
case GRID:
view = new PanelRecyclerView(getContext(), viewConfig);
break;
default:
throw new IllegalStateException("Unrecognized view type in " + getClass().getSimpleName());
}
PanelView panelView = (PanelView) view;
panelView.setOnItemOpenListener(new PanelOnItemOpenListener(viewState));
panelView.setOnKeyListener(new PanelKeyListener(viewState));
panelView.setContextMenuInfoFactory(new HomeContextMenuInfo.Factory() {
@Override
public HomeContextMenuInfo makeInfoForCursor(View view, int position, long id, Cursor cursor) {
final HomeContextMenuInfo info = new HomeContextMenuInfo(view, position, id);
info.url = cursor.getString(cursor.getColumnIndexOrThrow(HomeItems.URL));
info.title = cursor.getString(cursor.getColumnIndexOrThrow(HomeItems.TITLE));
return info;
}
});
mContextMenuRegistry.register(view);
if (view instanceof DatasetBacked) {
DatasetBacked datasetBacked = (DatasetBacked) view;
datasetBacked.setFilterManager(new PanelFilterManager(viewState));
if (viewConfig.isRefreshEnabled()) {
view = new PanelRefreshLayout(getContext(), view,
mPanelConfig.getId(), viewConfig.getIndex());
}
}
viewState.setView(view);
}
return view;
}
/**
* Dispose any dataset references associated with the
* given view.
*/
protected final void disposePanelView(View view) {
Log.d(LOGTAG, "Disposing panel view");
final int count = mViewStates.size();
for (int i = 0; i < count; i++) {
final ViewState viewState = mViewStates.valueAt(i);
if (viewState.getView() == view) {
maybeSetDataset(view, null);
mViewStates.remove(viewState.getIndex());
break;
}
}
}
private void maybeSetDataset(View view, Cursor cursor) {
if (view instanceof DatasetBacked) {
final DatasetBacked dsb = (DatasetBacked) view;
dsb.setDataset(cursor);
}
}
private View createEmptyView(ViewConfig viewConfig) {
Log.d(LOGTAG, "Creating empty view: " + viewConfig.getType());
ViewState viewState = mViewStates.get(viewConfig.getIndex());
if (viewState == null) {
throw new IllegalStateException("No view state found for view index: " + viewConfig.getIndex());
}
View view = viewState.getEmptyView();
if (view == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.home_empty_panel, null);
final EmptyViewConfig emptyViewConfig = viewConfig.getEmptyViewConfig();
// XXX: Refactor this into a custom view (bug 985134)
final String text = (emptyViewConfig == null) ? null : emptyViewConfig.getText();
final TextView textView = (TextView) view.findViewById(R.id.home_empty_text);
if (TextUtils.isEmpty(text)) {
textView.setText(R.string.home_default_empty);
} else {
textView.setText(text);
}
final String imageUrl = (emptyViewConfig == null) ? null : emptyViewConfig.getImageUrl();
final ImageView imageView = (ImageView) view.findViewById(R.id.home_empty_image);
if (TextUtils.isEmpty(imageUrl)) {
imageView.setImageResource(R.drawable.icon_home_empty_firefox);
} else {
ImageLoader.with(getContext())
.load(imageUrl)
.error(R.drawable.icon_home_empty_firefox)
.into(imageView);
}
viewState.setEmptyView(view);
}
return view;
}
private void replacePanelView(View currentView, View newView) {
final ViewGroup parent = (ViewGroup) currentView.getParent();
parent.addView(newView, parent.indexOfChild(currentView), currentView.getLayoutParams());
parent.removeView(currentView);
}
/**
* Must be implemented by {@code PanelLayout} subclasses to define
* what happens then the layout is first loaded. Should set initial
* UI state and request any necessary datasets.
*/
public abstract void load();
/**
* Represents a 'live' instance of a panel view associated with
* the {@code PanelLayout}. Is responsible for tracking the history stack of filters.
*/
protected class ViewState {
private final ViewConfig mViewConfig;
private SoftReference<View> mView;
private SoftReference<View> mEmptyView;
private LinkedList<FilterDetail> mFilterStack;
public ViewState(ViewConfig viewConfig) {
mViewConfig = viewConfig;
mView = new SoftReference<View>(null);
mEmptyView = new SoftReference<View>(null);
}
public ViewConfig getViewConfig() {
return mViewConfig;
}
public int getIndex() {
return mViewConfig.getIndex();
}
public View getView() {
return mView.get();
}
public void setView(View view) {
mView = new SoftReference<View>(view);
}
public View getEmptyView() {
return mEmptyView.get();
}
public void setEmptyView(View view) {
mEmptyView = new SoftReference<View>(view);
}
public View getActiveView() {
final View view = getView();
if (view != null && view.getParent() != null) {
return view;
}
final View emptyView = getEmptyView();
if (emptyView != null && emptyView.getParent() != null) {
return emptyView;
}
return null;
}
public String getDatasetId() {
return mViewConfig.getDatasetId();
}
public ItemHandler getItemHandler() {
return mViewConfig.getItemHandler();
}
/**
* Get the current filter that this view is displaying, or null if none.
*/
public FilterDetail getCurrentFilter() {
if (mFilterStack == null) {
return null;
} else {
return mFilterStack.peek();
}
}
/**
* Get the previous filter that this view was displaying, or null if none.
*/
public FilterDetail getPreviousFilter() {
if (!canPopFilter()) {
return null;
}
return mFilterStack.get(1);
}
/**
* Adds a filter to the history stack for this view.
*/
public void pushFilter(FilterDetail filter) {
if (mFilterStack == null) {
mFilterStack = new LinkedList<FilterDetail>();
// Initialize with the initial filter.
mFilterStack.push(new FilterDetail(mViewConfig.getFilter(),
mPanelConfig.getTitle()));
}
mFilterStack.push(filter);
}
/**
* Remove the most recent filter from the stack.
*
* @return whether the filter was popped
*/
public boolean popFilter() {
if (!canPopFilter()) {
return false;
}
mFilterStack.pop();
return true;
}
public boolean canPopFilter() {
return (mFilterStack != null && mFilterStack.size() > 1);
}
}
static class FilterDetail implements Parcelable {
final String filter;
final String title;
private FilterDetail(Parcel in) {
this.filter = in.readString();
this.title = in.readString();
}
public FilterDetail(String filter, String title) {
this.filter = filter;
this.title = title;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(filter);
dest.writeString(title);
}
public static final Creator<FilterDetail> CREATOR = new Creator<FilterDetail>() {
@Override
public FilterDetail createFromParcel(Parcel in) {
return new FilterDetail(in);
}
@Override
public FilterDetail[] newArray(int size) {
return new FilterDetail[size];
}
};
}
/**
* Pushes filter to {@code ViewState}'s stack and makes request for new filter value.
*/
private void pushFilterOnView(ViewState viewState, FilterDetail filterDetail) {
final int index = viewState.getIndex();
final String datasetId = viewState.getDatasetId();
mDatasetHandler.requestDataset(new DatasetRequest(index,
DatasetRequest.Type.FILTER_PUSH,
datasetId,
filterDetail));
}
/**
* Pops filter from {@code ViewState}'s stack and makes request for previous filter value.
*
* @return whether the filter has changed
*/
private boolean popFilterOnView(ViewState viewState) {
if (viewState.canPopFilter()) {
final int index = viewState.getIndex();
final String datasetId = viewState.getDatasetId();
final FilterDetail filterDetail = viewState.getPreviousFilter();
mDatasetHandler.requestDataset(new DatasetRequest(index,
DatasetRequest.Type.FILTER_POP,
datasetId,
filterDetail));
return true;
} else {
return false;
}
}
public interface OnItemOpenListener {
public void onItemOpen(String url, String title);
}
private class PanelOnItemOpenListener implements OnItemOpenListener {
private final ViewState mViewState;
public PanelOnItemOpenListener(ViewState viewState) {
mViewState = viewState;
}
@Override
public void onItemOpen(String url, String title) {
if (StringUtils.isFilterUrl(url)) {
FilterDetail filterDetail = new FilterDetail(StringUtils.getFilterFromUrl(url), title);
pushFilterOnView(mViewState, filterDetail);
} else {
EnumSet<OnUrlOpenListener.Flags> flags = EnumSet.noneOf(OnUrlOpenListener.Flags.class);
if (mViewState.getItemHandler() == ItemHandler.INTENT) {
flags.add(OnUrlOpenListener.Flags.OPEN_WITH_INTENT);
}
mUrlOpenListener.onUrlOpen(url, flags);
}
}
}
private class PanelKeyListener implements View.OnKeyListener {
private final ViewState mViewState;
public PanelKeyListener(ViewState viewState) {
mViewState = viewState;
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
return popFilterOnView(mViewState);
}
return false;
}
}
private class PanelFilterManager implements FilterManager {
private final ViewState mViewState;
public PanelFilterManager(ViewState viewState) {
mViewState = viewState;
}
@Override
public FilterDetail getPreviousFilter() {
return mViewState.getPreviousFilter();
}
@Override
public boolean canGoBack() {
return mViewState.canPopFilter();
}
@Override
public void goBack() {
popFilterOnView(mViewState);
}
}
}
|