summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/home/HomeConfig.java
blob: 08e79be3a39cd486f12bec9450df02ed1fab69f7 (plain)
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
/* -*- 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 java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.gecko.annotation.RobocopTarget;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.R;
import org.mozilla.gecko.util.ThreadUtils;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Pair;

public final class HomeConfig {
    public static final String PREF_KEY_BOOKMARKS_PANEL_ENABLED = "bookmarksPanelEnabled";
    public static final String PREF_KEY_HISTORY_PANEL_ENABLED = "combinedHistoryPanelEnabled";

    /**
     * Used to determine what type of HomeFragment subclass to use when creating
     * a given panel. With the exception of DYNAMIC, all of these types correspond
     * to a default set of built-in panels. The DYNAMIC panel type is used by
     * third-party services to create panels with varying types of content.
     */
    @RobocopTarget
    public static enum PanelType implements Parcelable {
        TOP_SITES("top_sites", TopSitesPanel.class),
        BOOKMARKS("bookmarks", BookmarksPanel.class),
        COMBINED_HISTORY("combined_history", CombinedHistoryPanel.class),
        DYNAMIC("dynamic", DynamicPanel.class),
        // Deprecated panels that should no longer exist but are kept around for
        // migration code. Class references have been replaced with new version of the panel.
        DEPRECATED_REMOTE_TABS("remote_tabs", CombinedHistoryPanel.class),
        DEPRECATED_HISTORY("history", CombinedHistoryPanel.class),
        DEPRECATED_READING_LIST("reading_list", BookmarksPanel.class),
        DEPRECATED_RECENT_TABS("recent_tabs", CombinedHistoryPanel.class);

        private final String mId;
        private final Class<?> mPanelClass;

        PanelType(String id, Class<?> panelClass) {
            mId = id;
            mPanelClass = panelClass;
        }

        public static PanelType fromId(String id) {
            if (id == null) {
                throw new IllegalArgumentException("Could not convert null String to PanelType");
            }

            for (PanelType panelType : PanelType.values()) {
                if (TextUtils.equals(panelType.mId, id.toLowerCase())) {
                    return panelType;
                }
            }

            throw new IllegalArgumentException("Could not convert String id to PanelType");
        }

        @Override
        public String toString() {
            return mId;
        }

        public Class<?> getPanelClass() {
            return mPanelClass;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(ordinal());
        }

        public static final Creator<PanelType> CREATOR = new Creator<PanelType>() {
            @Override
            public PanelType createFromParcel(final Parcel source) {
                return PanelType.values()[source.readInt()];
            }

            @Override
            public PanelType[] newArray(final int size) {
                return new PanelType[size];
            }
        };
    }

    public static class PanelConfig implements Parcelable {
        private final PanelType mType;
        private final String mTitle;
        private final String mId;
        private final LayoutType mLayoutType;
        private final List<ViewConfig> mViews;
        private final AuthConfig mAuthConfig;
        private final EnumSet<Flags> mFlags;
        private final int mPosition;

        static final String JSON_KEY_TYPE = "type";
        static final String JSON_KEY_TITLE = "title";
        static final String JSON_KEY_ID = "id";
        static final String JSON_KEY_LAYOUT = "layout";
        static final String JSON_KEY_VIEWS = "views";
        static final String JSON_KEY_AUTH_CONFIG = "authConfig";
        static final String JSON_KEY_DEFAULT = "default";
        static final String JSON_KEY_DISABLED = "disabled";
        static final String JSON_KEY_POSITION = "position";

        public enum Flags {
            DEFAULT_PANEL,
            DISABLED_PANEL
        }

        public PanelConfig(JSONObject json) throws JSONException, IllegalArgumentException {
            final String panelType = json.optString(JSON_KEY_TYPE, null);
            if (TextUtils.isEmpty(panelType)) {
                mType = PanelType.DYNAMIC;
            } else {
                mType = PanelType.fromId(panelType);
            }

            mTitle = json.getString(JSON_KEY_TITLE);
            mId = json.getString(JSON_KEY_ID);

            final String layoutTypeId = json.optString(JSON_KEY_LAYOUT, null);
            if (layoutTypeId != null) {
                mLayoutType = LayoutType.fromId(layoutTypeId);
            } else {
                mLayoutType = null;
            }

            final JSONArray jsonViews = json.optJSONArray(JSON_KEY_VIEWS);
            if (jsonViews != null) {
                mViews = new ArrayList<ViewConfig>();

                final int viewCount = jsonViews.length();
                for (int i = 0; i < viewCount; i++) {
                    final JSONObject jsonViewConfig = (JSONObject) jsonViews.get(i);
                    final ViewConfig viewConfig = new ViewConfig(i, jsonViewConfig);
                    mViews.add(viewConfig);
                }
            } else {
                mViews = null;
            }

            final JSONObject jsonAuthConfig = json.optJSONObject(JSON_KEY_AUTH_CONFIG);
            if (jsonAuthConfig != null) {
                mAuthConfig = new AuthConfig(jsonAuthConfig);
            } else {
                mAuthConfig = null;
            }

            mFlags = EnumSet.noneOf(Flags.class);

            if (json.optBoolean(JSON_KEY_DEFAULT, false)) {
                mFlags.add(Flags.DEFAULT_PANEL);
            }

            if (json.optBoolean(JSON_KEY_DISABLED, false)) {
                mFlags.add(Flags.DISABLED_PANEL);
            }

            mPosition = json.optInt(JSON_KEY_POSITION, -1);

            validate();
        }

        @SuppressWarnings("unchecked")
        public PanelConfig(Parcel in) {
            mType = (PanelType) in.readParcelable(getClass().getClassLoader());
            mTitle = in.readString();
            mId = in.readString();
            mLayoutType = (LayoutType) in.readParcelable(getClass().getClassLoader());

            mViews = new ArrayList<ViewConfig>();
            in.readTypedList(mViews, ViewConfig.CREATOR);

            mAuthConfig = (AuthConfig) in.readParcelable(getClass().getClassLoader());

            mFlags = (EnumSet<Flags>) in.readSerializable();
            mPosition = in.readInt();

            validate();
        }

        public PanelConfig(PanelConfig panelConfig) {
            mType = panelConfig.mType;
            mTitle = panelConfig.mTitle;
            mId = panelConfig.mId;
            mLayoutType = panelConfig.mLayoutType;

            mViews = new ArrayList<ViewConfig>();
            List<ViewConfig> viewConfigs = panelConfig.mViews;
            if (viewConfigs != null) {
                for (ViewConfig viewConfig : viewConfigs) {
                    mViews.add(new ViewConfig(viewConfig));
                }
            }

            mAuthConfig = panelConfig.mAuthConfig;
            mFlags = panelConfig.mFlags.clone();
            mPosition = panelConfig.mPosition;

            validate();
        }

        public PanelConfig(PanelType type, String title, String id) {
            this(type, title, id, EnumSet.noneOf(Flags.class));
        }

        public PanelConfig(PanelType type, String title, String id, EnumSet<Flags> flags) {
            this(type, title, id, null, null, null, flags, -1);
        }

        public PanelConfig(PanelType type, String title, String id, LayoutType layoutType,
                List<ViewConfig> views, AuthConfig authConfig, EnumSet<Flags> flags, int position) {
            mType = type;
            mTitle = title;
            mId = id;
            mLayoutType = layoutType;
            mViews = views;
            mAuthConfig = authConfig;
            mFlags = flags;
            mPosition = position;

            validate();
        }

        private void validate() {
            if (mType == null) {
                throw new IllegalArgumentException("Can't create PanelConfig with null type");
            }

            if (TextUtils.isEmpty(mTitle)) {
                throw new IllegalArgumentException("Can't create PanelConfig with empty title");
            }

            if (TextUtils.isEmpty(mId)) {
                throw new IllegalArgumentException("Can't create PanelConfig with empty id");
            }

            if (mLayoutType == null && mType == PanelType.DYNAMIC) {
                throw new IllegalArgumentException("Can't create a dynamic PanelConfig with null layout type");
            }

            if ((mViews == null || mViews.size() == 0) && mType == PanelType.DYNAMIC) {
                throw new IllegalArgumentException("Can't create a dynamic PanelConfig with no views");
            }

            if (mFlags == null) {
                throw new IllegalArgumentException("Can't create PanelConfig with null flags");
            }
        }

        public PanelType getType() {
            return mType;
        }

        public String getTitle() {
            return mTitle;
        }

        public String getId() {
            return mId;
        }

        public LayoutType getLayoutType() {
            return mLayoutType;
        }

        public int getViewCount() {
            return (mViews != null ? mViews.size() : 0);
        }

        public ViewConfig getViewAt(int index) {
            return (mViews != null ? mViews.get(index) : null);
        }

        public EnumSet<Flags> getFlags() {
            return mFlags.clone();
        }

        public boolean isDynamic() {
            return (mType == PanelType.DYNAMIC);
        }

        public boolean isDefault() {
            return mFlags.contains(Flags.DEFAULT_PANEL);
        }

        private void setIsDefault(boolean isDefault) {
            if (isDefault) {
                mFlags.add(Flags.DEFAULT_PANEL);
            } else {
                mFlags.remove(Flags.DEFAULT_PANEL);
            }
        }

        public boolean isDisabled() {
            return mFlags.contains(Flags.DISABLED_PANEL);
        }

        private void setIsDisabled(boolean isDisabled) {
            if (isDisabled) {
                mFlags.add(Flags.DISABLED_PANEL);
            } else {
                mFlags.remove(Flags.DISABLED_PANEL);
            }
        }

        public AuthConfig getAuthConfig() {
            return mAuthConfig;
        }

        public int getPosition() {
            return mPosition;
        }

        public JSONObject toJSON() throws JSONException {
            final JSONObject json = new JSONObject();

            json.put(JSON_KEY_TYPE, mType.toString());
            json.put(JSON_KEY_TITLE, mTitle);
            json.put(JSON_KEY_ID, mId);

            if (mLayoutType != null) {
                json.put(JSON_KEY_LAYOUT, mLayoutType.toString());
            }

            if (mViews != null) {
                final JSONArray jsonViews = new JSONArray();

                final int viewCount = mViews.size();
                for (int i = 0; i < viewCount; i++) {
                    final ViewConfig viewConfig = mViews.get(i);
                    final JSONObject jsonViewConfig = viewConfig.toJSON();
                    jsonViews.put(jsonViewConfig);
                }

                json.put(JSON_KEY_VIEWS, jsonViews);
            }

            if (mAuthConfig != null) {
                json.put(JSON_KEY_AUTH_CONFIG, mAuthConfig.toJSON());
            }

            if (mFlags.contains(Flags.DEFAULT_PANEL)) {
                json.put(JSON_KEY_DEFAULT, true);
            }

            if (mFlags.contains(Flags.DISABLED_PANEL)) {
                json.put(JSON_KEY_DISABLED, true);
            }

            json.put(JSON_KEY_POSITION, mPosition);

            return json;
        }

        @Override
        public boolean equals(Object o) {
            if (o == null) {
                return false;
            }

            if (this == o) {
                return true;
            }

            if (!(o instanceof PanelConfig)) {
                return false;
            }

            final PanelConfig other = (PanelConfig) o;
            return mId.equals(other.mId);
        }

        @Override
        public int hashCode() {
            return super.hashCode();
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeParcelable(mType, 0);
            dest.writeString(mTitle);
            dest.writeString(mId);
            dest.writeParcelable(mLayoutType, 0);
            dest.writeTypedList(mViews);
            dest.writeParcelable(mAuthConfig, 0);
            dest.writeSerializable(mFlags);
            dest.writeInt(mPosition);
        }

        public static final Creator<PanelConfig> CREATOR = new Creator<PanelConfig>() {
            @Override
            public PanelConfig createFromParcel(final Parcel in) {
                return new PanelConfig(in);
            }

            @Override
            public PanelConfig[] newArray(final int size) {
                return new PanelConfig[size];
            }
        };
    }

    public static enum LayoutType implements Parcelable {
        FRAME("frame");

        private final String mId;

        LayoutType(String id) {
            mId = id;
        }

        public static LayoutType fromId(String id) {
            if (id == null) {
                throw new IllegalArgumentException("Could not convert null String to LayoutType");
            }

            for (LayoutType layoutType : LayoutType.values()) {
                if (TextUtils.equals(layoutType.mId, id.toLowerCase())) {
                    return layoutType;
                }
            }

            throw new IllegalArgumentException("Could not convert String id to LayoutType");
        }

        @Override
        public String toString() {
            return mId;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(ordinal());
        }

        public static final Creator<LayoutType> CREATOR = new Creator<LayoutType>() {
            @Override
            public LayoutType createFromParcel(final Parcel source) {
                return LayoutType.values()[source.readInt()];
            }

            @Override
            public LayoutType[] newArray(final int size) {
                return new LayoutType[size];
            }
        };
    }

    public static enum ViewType implements Parcelable {
        LIST("list"),
        GRID("grid");

        private final String mId;

        ViewType(String id) {
            mId = id;
        }

        public static ViewType fromId(String id) {
            if (id == null) {
                throw new IllegalArgumentException("Could not convert null String to ViewType");
            }

            for (ViewType viewType : ViewType.values()) {
                if (TextUtils.equals(viewType.mId, id.toLowerCase())) {
                    return viewType;
                }
            }

            throw new IllegalArgumentException("Could not convert String id to ViewType");
        }

        @Override
        public String toString() {
            return mId;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(ordinal());
        }

        public static final Creator<ViewType> CREATOR = new Creator<ViewType>() {
            @Override
            public ViewType createFromParcel(final Parcel source) {
                return ViewType.values()[source.readInt()];
            }

            @Override
            public ViewType[] newArray(final int size) {
                return new ViewType[size];
            }
        };
    }

    public static enum ItemType implements Parcelable {
        ARTICLE("article"),
        IMAGE("image"),
        ICON("icon");

        private final String mId;

        ItemType(String id) {
            mId = id;
        }

        public static ItemType fromId(String id) {
            if (id == null) {
                throw new IllegalArgumentException("Could not convert null String to ItemType");
            }

            for (ItemType itemType : ItemType.values()) {
                if (TextUtils.equals(itemType.mId, id.toLowerCase())) {
                    return itemType;
                }
            }

            throw new IllegalArgumentException("Could not convert String id to ItemType");
        }

        @Override
        public String toString() {
            return mId;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(ordinal());
        }

        public static final Creator<ItemType> CREATOR = new Creator<ItemType>() {
            @Override
            public ItemType createFromParcel(final Parcel source) {
                return ItemType.values()[source.readInt()];
            }

            @Override
            public ItemType[] newArray(final int size) {
                return new ItemType[size];
            }
        };
    }

    public static enum ItemHandler implements Parcelable {
        BROWSER("browser"),
        INTENT("intent");

        private final String mId;

        ItemHandler(String id) {
            mId = id;
        }

        public static ItemHandler fromId(String id) {
            if (id == null) {
                throw new IllegalArgumentException("Could not convert null String to ItemHandler");
            }

            for (ItemHandler itemHandler : ItemHandler.values()) {
                if (TextUtils.equals(itemHandler.mId, id.toLowerCase())) {
                    return itemHandler;
                }
            }

            throw new IllegalArgumentException("Could not convert String id to ItemHandler");
        }

        @Override
        public String toString() {
            return mId;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(ordinal());
        }

        public static final Creator<ItemHandler> CREATOR = new Creator<ItemHandler>() {
            @Override
            public ItemHandler createFromParcel(final Parcel source) {
                return ItemHandler.values()[source.readInt()];
            }

            @Override
            public ItemHandler[] newArray(final int size) {
                return new ItemHandler[size];
            }
        };
    }

    public static class ViewConfig implements Parcelable {
        private final int mIndex;
        private final ViewType mType;
        private final String mDatasetId;
        private final ItemType mItemType;
        private final ItemHandler mItemHandler;
        private final String mBackImageUrl;
        private final String mFilter;
        private final EmptyViewConfig mEmptyViewConfig;
        private final HeaderConfig mHeaderConfig;
        private final EnumSet<Flags> mFlags;

        static final String JSON_KEY_TYPE = "type";
        static final String JSON_KEY_DATASET = "dataset";
        static final String JSON_KEY_ITEM_TYPE = "itemType";
        static final String JSON_KEY_ITEM_HANDLER = "itemHandler";
        static final String JSON_KEY_BACK_IMAGE_URL = "backImageUrl";
        static final String JSON_KEY_FILTER = "filter";
        static final String JSON_KEY_EMPTY = "empty";
        static final String JSON_KEY_HEADER = "header";
        static final String JSON_KEY_REFRESH_ENABLED = "refreshEnabled";

        public enum Flags {
            REFRESH_ENABLED
        }

        public ViewConfig(int index, JSONObject json) throws JSONException, IllegalArgumentException {
            mIndex = index;
            mType = ViewType.fromId(json.getString(JSON_KEY_TYPE));
            mDatasetId = json.getString(JSON_KEY_DATASET);
            mItemType = ItemType.fromId(json.getString(JSON_KEY_ITEM_TYPE));
            mItemHandler = ItemHandler.fromId(json.getString(JSON_KEY_ITEM_HANDLER));
            mBackImageUrl = json.optString(JSON_KEY_BACK_IMAGE_URL, null);
            mFilter = json.optString(JSON_KEY_FILTER, null);

            final JSONObject jsonEmptyViewConfig = json.optJSONObject(JSON_KEY_EMPTY);
            if (jsonEmptyViewConfig != null) {
                mEmptyViewConfig = new EmptyViewConfig(jsonEmptyViewConfig);
            } else {
                mEmptyViewConfig = null;
            }

            final JSONObject jsonHeaderConfig = json.optJSONObject(JSON_KEY_HEADER);
            mHeaderConfig = jsonHeaderConfig != null ? new HeaderConfig(jsonHeaderConfig) : null;

            mFlags = EnumSet.noneOf(Flags.class);
            if (json.optBoolean(JSON_KEY_REFRESH_ENABLED, false)) {
                mFlags.add(Flags.REFRESH_ENABLED);
            }

            validate();
        }

        @SuppressWarnings("unchecked")
        public ViewConfig(Parcel in) {
            mIndex = in.readInt();
            mType = (ViewType) in.readParcelable(getClass().getClassLoader());
            mDatasetId = in.readString();
            mItemType = (ItemType) in.readParcelable(getClass().getClassLoader());
            mItemHandler = (ItemHandler) in.readParcelable(getClass().getClassLoader());
            mBackImageUrl = in.readString();
            mFilter = in.readString();
            mEmptyViewConfig = (EmptyViewConfig) in.readParcelable(getClass().getClassLoader());
            mHeaderConfig = (HeaderConfig) in.readParcelable(getClass().getClassLoader());
            mFlags = (EnumSet<Flags>) in.readSerializable();

            validate();
        }

        public ViewConfig(ViewConfig viewConfig) {
            mIndex = viewConfig.mIndex;
            mType = viewConfig.mType;
            mDatasetId = viewConfig.mDatasetId;
            mItemType = viewConfig.mItemType;
            mItemHandler = viewConfig.mItemHandler;
            mBackImageUrl = viewConfig.mBackImageUrl;
            mFilter = viewConfig.mFilter;
            mEmptyViewConfig = viewConfig.mEmptyViewConfig;
            mHeaderConfig = viewConfig.mHeaderConfig;
            mFlags = viewConfig.mFlags.clone();

            validate();
        }

        private void validate() {
            if (mType == null) {
                throw new IllegalArgumentException("Can't create ViewConfig with null type");
            }

            if (TextUtils.isEmpty(mDatasetId)) {
                throw new IllegalArgumentException("Can't create ViewConfig with empty dataset ID");
            }

            if (mItemType == null) {
                throw new IllegalArgumentException("Can't create ViewConfig with null item type");
            }

            if (mItemHandler == null) {
                throw new IllegalArgumentException("Can't create ViewConfig with null item handler");
            }

            if (mFlags == null) {
               throw new IllegalArgumentException("Can't create ViewConfig with null flags");
            }
        }

        public int getIndex() {
            return mIndex;
        }

        public ViewType getType() {
            return mType;
        }

        public String getDatasetId() {
            return mDatasetId;
        }

        public ItemType getItemType() {
            return mItemType;
        }

        public ItemHandler getItemHandler() {
            return mItemHandler;
        }

        public String getBackImageUrl() {
            return mBackImageUrl;
        }

        public String getFilter() {
            return mFilter;
        }

        public EmptyViewConfig getEmptyViewConfig() {
            return mEmptyViewConfig;
        }

        public HeaderConfig getHeaderConfig() {
            return mHeaderConfig;
        }

        public boolean hasHeaderConfig() {
            return mHeaderConfig != null;
        }

        public boolean isRefreshEnabled() {
            return mFlags.contains(Flags.REFRESH_ENABLED);
        }

        public JSONObject toJSON() throws JSONException {
            final JSONObject json = new JSONObject();

            json.put(JSON_KEY_TYPE, mType.toString());
            json.put(JSON_KEY_DATASET, mDatasetId);
            json.put(JSON_KEY_ITEM_TYPE, mItemType.toString());
            json.put(JSON_KEY_ITEM_HANDLER, mItemHandler.toString());

            if (!TextUtils.isEmpty(mBackImageUrl)) {
                json.put(JSON_KEY_BACK_IMAGE_URL, mBackImageUrl);
            }

            if (!TextUtils.isEmpty(mFilter)) {
                json.put(JSON_KEY_FILTER, mFilter);
            }

            if (mEmptyViewConfig != null) {
                json.put(JSON_KEY_EMPTY, mEmptyViewConfig.toJSON());
            }

            if (mHeaderConfig != null) {
                json.put(JSON_KEY_HEADER, mHeaderConfig.toJSON());
            }

            if (mFlags.contains(Flags.REFRESH_ENABLED)) {
                json.put(JSON_KEY_REFRESH_ENABLED, true);
            }

            return json;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(mIndex);
            dest.writeParcelable(mType, 0);
            dest.writeString(mDatasetId);
            dest.writeParcelable(mItemType, 0);
            dest.writeParcelable(mItemHandler, 0);
            dest.writeString(mBackImageUrl);
            dest.writeString(mFilter);
            dest.writeParcelable(mEmptyViewConfig, 0);
            dest.writeParcelable(mHeaderConfig, 0);
            dest.writeSerializable(mFlags);
        }

        public static final Creator<ViewConfig> CREATOR = new Creator<ViewConfig>() {
            @Override
            public ViewConfig createFromParcel(final Parcel in) {
                return new ViewConfig(in);
            }

            @Override
            public ViewConfig[] newArray(final int size) {
                return new ViewConfig[size];
            }
        };
    }

    public static class EmptyViewConfig implements Parcelable {
        private final String mText;
        private final String mImageUrl;

        static final String JSON_KEY_TEXT = "text";
        static final String JSON_KEY_IMAGE_URL = "imageUrl";

        public EmptyViewConfig(JSONObject json) throws JSONException, IllegalArgumentException {
            mText = json.optString(JSON_KEY_TEXT, null);
            mImageUrl = json.optString(JSON_KEY_IMAGE_URL, null);
        }

        public EmptyViewConfig(Parcel in) {
            mText = in.readString();
            mImageUrl = in.readString();
        }

        public EmptyViewConfig(EmptyViewConfig emptyViewConfig) {
            mText = emptyViewConfig.mText;
            mImageUrl = emptyViewConfig.mImageUrl;
        }

        public EmptyViewConfig(String text, String imageUrl) {
            mText = text;
            mImageUrl = imageUrl;
        }

        public String getText() {
            return mText;
        }

        public String getImageUrl() {
            return mImageUrl;
        }

        public JSONObject toJSON() throws JSONException {
            final JSONObject json = new JSONObject();

            json.put(JSON_KEY_TEXT, mText);
            json.put(JSON_KEY_IMAGE_URL, mImageUrl);

            return json;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(mText);
            dest.writeString(mImageUrl);
        }

        public static final Creator<EmptyViewConfig> CREATOR = new Creator<EmptyViewConfig>() {
            @Override
            public EmptyViewConfig createFromParcel(final Parcel in) {
                return new EmptyViewConfig(in);
            }

            @Override
            public EmptyViewConfig[] newArray(final int size) {
                return new EmptyViewConfig[size];
            }
        };
    }

    public static class HeaderConfig implements Parcelable {
        static final String JSON_KEY_IMAGE_URL = "image_url";
        static final String JSON_KEY_URL = "url";

        private final String mImageUrl;
        private final String mUrl;

        public HeaderConfig(JSONObject json) {
            mImageUrl = json.optString(JSON_KEY_IMAGE_URL);
            mUrl = json.optString(JSON_KEY_URL);
        }

        public HeaderConfig(Parcel in) {
            mImageUrl = in.readString();
            mUrl = in.readString();
        }

        public String getImageUrl() {
            return mImageUrl;
        }

        public String getUrl() {
            return mUrl;
        }

        public JSONObject toJSON() throws JSONException {
            JSONObject json = new JSONObject();

            json.put(JSON_KEY_IMAGE_URL, mImageUrl);
            json.put(JSON_KEY_URL, mUrl);

            return json;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(mImageUrl);
            dest.writeString(mUrl);
        }

        public static final Creator<HeaderConfig> CREATOR = new Creator<HeaderConfig>() {
            @Override
            public HeaderConfig createFromParcel(Parcel source) {
                return new HeaderConfig(source);
            }

            @Override
            public HeaderConfig[] newArray(int size) {
                return new HeaderConfig[size];
            }
        };
    }

    public static class AuthConfig implements Parcelable {
        private final String mMessageText;
        private final String mButtonText;
        private final String mImageUrl;

        static final String JSON_KEY_MESSAGE_TEXT = "messageText";
        static final String JSON_KEY_BUTTON_TEXT = "buttonText";
        static final String JSON_KEY_IMAGE_URL = "imageUrl";

        public AuthConfig(JSONObject json) throws JSONException, IllegalArgumentException {
            mMessageText = json.optString(JSON_KEY_MESSAGE_TEXT);
            mButtonText = json.optString(JSON_KEY_BUTTON_TEXT);
            mImageUrl = json.optString(JSON_KEY_IMAGE_URL, null);
        }

        public AuthConfig(Parcel in) {
            mMessageText = in.readString();
            mButtonText = in.readString();
            mImageUrl = in.readString();

            validate();
        }

        public AuthConfig(AuthConfig authConfig) {
            mMessageText = authConfig.mMessageText;
            mButtonText = authConfig.mButtonText;
            mImageUrl = authConfig.mImageUrl;

            validate();
        }

        public AuthConfig(String messageText, String buttonText, String imageUrl) {
            mMessageText = messageText;
            mButtonText = buttonText;
            mImageUrl = imageUrl;

            validate();
        }

        private void validate() {
            if (mMessageText == null) {
                throw new IllegalArgumentException("Can't create AuthConfig with null message text");
            }

            if (mButtonText == null) {
                throw new IllegalArgumentException("Can't create AuthConfig with null button text");
            }
        }

        public String getMessageText() {
            return mMessageText;
        }

        public String getButtonText() {
            return mButtonText;
        }

        public String getImageUrl() {
            return mImageUrl;
        }

        public JSONObject toJSON() throws JSONException {
            final JSONObject json = new JSONObject();

            json.put(JSON_KEY_MESSAGE_TEXT, mMessageText);
            json.put(JSON_KEY_BUTTON_TEXT, mButtonText);
            json.put(JSON_KEY_IMAGE_URL, mImageUrl);

            return json;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(mMessageText);
            dest.writeString(mButtonText);
            dest.writeString(mImageUrl);
        }

        public static final Creator<AuthConfig> CREATOR = new Creator<AuthConfig>() {
            @Override
            public AuthConfig createFromParcel(final Parcel in) {
                return new AuthConfig(in);
            }

            @Override
            public AuthConfig[] newArray(final int size) {
                return new AuthConfig[size];
            }
        };
    }
   /**
     * Immutable representation of the current state of {@code HomeConfig}.
     * This is what HomeConfig returns from a load() call and takes as
     * input to save a new state.
     *
     * Users of {@code State} should use an {@code Iterator} to iterate
     * through the contained {@code PanelConfig} instances.
     *
     * {@code State} is immutable i.e. you can't add, remove, or update
     * contained elements directly. You have to use an {@code Editor} to
     * change the state, which can be created through the {@code edit()}
     * method.
     */
    public static class State implements Iterable<PanelConfig> {
        private HomeConfig mHomeConfig;
        private final List<PanelConfig> mPanelConfigs;
        private final boolean mIsDefault;

        State(List<PanelConfig> panelConfigs, boolean isDefault) {
            this(null, panelConfigs, isDefault);
        }

        private State(HomeConfig homeConfig, List<PanelConfig> panelConfigs, boolean isDefault) {
            mHomeConfig = homeConfig;
            mPanelConfigs = Collections.unmodifiableList(panelConfigs);
            mIsDefault = isDefault;
        }

        private void setHomeConfig(HomeConfig homeConfig) {
            if (mHomeConfig != null) {
                throw new IllegalStateException("Can't set HomeConfig more than once");
            }

            mHomeConfig = homeConfig;
        }

        @Override
        public Iterator<PanelConfig> iterator() {
            return mPanelConfigs.iterator();
        }

        /**
         * Returns whether this {@code State} instance represents the default
         * {@code HomeConfig} configuration or not.
         */
        public boolean isDefault() {
            return mIsDefault;
        }

        /**
         * Creates an {@code Editor} for this state.
         */
        public Editor edit() {
            return new Editor(mHomeConfig, this);
        }
    }

    /**
     * {@code Editor} allows you to make changes to a {@code State}. You
     * can create {@code Editor} by calling {@code edit()} on the target
     * {@code State} instance.
     *
     * {@code Editor} works on a copy of the {@code State} that originated
     * it. This means that adding, removing, or updating panels in an
     * {@code Editor} will never change the {@code State} which you
     * created the {@code Editor} from. Calling {@code commit()} or
     * {@code apply()} will cause the new {@code State} instance to be
     * created and saved using the {@code HomeConfig} instance that
     * created the source {@code State}.
     *
     * {@code Editor} is *not* thread-safe. You can only make calls on it
     * from the thread where it was originally created. It will throw an
     * exception if you don't follow this invariant.
     */
    public static class Editor implements Iterable<PanelConfig> {
        private final HomeConfig mHomeConfig;
        private final Map<String, PanelConfig> mConfigMap;
        private final List<String> mConfigOrder;
        private final Thread mOriginalThread;

        // Each Pair represents parameters to a GeckoAppShell.notifyObservers call;
        // the first String is the observer topic and the second string is the notification data.
        private List<Pair<String, String>> mNotificationQueue;
        private PanelConfig mDefaultPanel;
        private int mEnabledCount;

        private boolean mHasChanged;
        private final boolean mIsFromDefault;

        private Editor(HomeConfig homeConfig, State configState) {
            mHomeConfig = homeConfig;
            mOriginalThread = Thread.currentThread();
            mConfigMap = new HashMap<String, PanelConfig>();
            mConfigOrder = new LinkedList<String>();
            mNotificationQueue = new ArrayList<>();

            mIsFromDefault = configState.isDefault();

            initFromState(configState);
        }

        /**
         * Initialize the initial state of the editor from the given
         * {@sode State}. A HashMap is used to represent the list of
         * panels as it provides fast access, and a LinkedList is used to
         * keep track of order. We keep a reference to the default panel
         * and the number of enabled panels to avoid iterating through the
         * map every time we need those.
         *
         * @param configState The source State to load the editor from.
         */
        private void initFromState(State configState) {
            for (PanelConfig panelConfig : configState) {
                final PanelConfig panelCopy = new PanelConfig(panelConfig);

                if (!panelCopy.isDisabled()) {
                    mEnabledCount++;
                }

                if (panelCopy.isDefault()) {
                    if (mDefaultPanel == null) {
                        mDefaultPanel = panelCopy;
                    } else {
                        throw new IllegalStateException("Multiple default panels in HomeConfig state");
                    }
                }

                final String panelId = panelConfig.getId();
                mConfigOrder.add(panelId);
                mConfigMap.put(panelId, panelCopy);
            }

            // We should always have a defined default panel if there's
            // at least one enabled panel around.
            if (mEnabledCount > 0 && mDefaultPanel == null) {
                throw new IllegalStateException("Default panel in HomeConfig state is undefined");
            }
        }

        private PanelConfig getPanelOrThrow(String panelId) {
            final PanelConfig panelConfig = mConfigMap.get(panelId);
            if (panelConfig == null) {
                throw new IllegalStateException("Tried to access non-existing panel: " + panelId);
            }

            return panelConfig;
        }

        private boolean isCurrentDefaultPanel(PanelConfig panelConfig) {
            if (mDefaultPanel == null) {
                return false;
            }

            return mDefaultPanel.equals(panelConfig);
        }

        private void findNewDefault() {
            // Pick the first panel that is neither disabled nor currently
            // set as default.
            for (PanelConfig panelConfig : makeOrderedCopy(false)) {
                if (!panelConfig.isDefault() && !panelConfig.isDisabled()) {
                    setDefault(panelConfig.getId());
                    return;
                }
            }

            mDefaultPanel = null;
        }

        /**
         * Makes an ordered list of PanelConfigs that can be references
         * or deep copied objects.
         *
         * @param deepCopy true to make deep-copied objects
         * @return ordered List of PanelConfigs
         */
        private List<PanelConfig> makeOrderedCopy(boolean deepCopy) {
            final List<PanelConfig> copiedList = new ArrayList<PanelConfig>(mConfigOrder.size());
            for (String panelId : mConfigOrder) {
                PanelConfig panelConfig = mConfigMap.get(panelId);
                if (deepCopy) {
                    panelConfig = new PanelConfig(panelConfig);
                }
                copiedList.add(panelConfig);
            }

            return copiedList;
        }

        private void setPanelIsDisabled(PanelConfig panelConfig, boolean disabled) {
            if (panelConfig.isDisabled() == disabled) {
                return;
            }

            panelConfig.setIsDisabled(disabled);
            mEnabledCount += (disabled ? -1 : 1);
        }

        /**
         * Gets the ID of the current default panel.
         */
        public String getDefaultPanelId() {
            ThreadUtils.assertOnThread(mOriginalThread);

            if (mDefaultPanel == null) {
                return null;
            }

            return mDefaultPanel.getId();
        }

        /**
         * Set a new default panel.
         *
         * @param panelId the ID of the new default panel.
         */
        public void setDefault(String panelId) {
            ThreadUtils.assertOnThread(mOriginalThread);

            final PanelConfig panelConfig = getPanelOrThrow(panelId);
            if (isCurrentDefaultPanel(panelConfig)) {
                return;
            }

            if (mDefaultPanel != null) {
                mDefaultPanel.setIsDefault(false);
            }

            panelConfig.setIsDefault(true);
            setPanelIsDisabled(panelConfig, false);

            mDefaultPanel = panelConfig;
            mHasChanged = true;
        }

        /**
         * Toggles disabled state for a panel.
         *
         * @param panelId the ID of the target panel.
         * @param disabled true to disable the panel.
         */
        public void setDisabled(String panelId, boolean disabled) {
            ThreadUtils.assertOnThread(mOriginalThread);

            final PanelConfig panelConfig = getPanelOrThrow(panelId);
            if (panelConfig.isDisabled() == disabled) {
                return;
            }

            setPanelIsDisabled(panelConfig, disabled);

            if (disabled) {
                if (isCurrentDefaultPanel(panelConfig)) {
                    panelConfig.setIsDefault(false);
                    findNewDefault();
                }
            } else if (mEnabledCount == 1) {
                setDefault(panelId);
            }

            mHasChanged = true;
        }

        /**
         * Adds a new {@code PanelConfig}. It will do nothing if the
         * {@code Editor} already contains a panel with the same ID.
         *
         * @param panelConfig the {@code PanelConfig} instance to be added.
         * @return true if the item has been added.
         */
        public boolean install(PanelConfig panelConfig) {
            ThreadUtils.assertOnThread(mOriginalThread);

            if (panelConfig == null) {
                throw new IllegalStateException("Can't install a null panel");
            }

            if (!panelConfig.isDynamic()) {
                throw new IllegalStateException("Can't install a built-in panel: " + panelConfig.getId());
            }

            if (panelConfig.isDisabled()) {
                throw new IllegalStateException("Can't install a disabled panel: " + panelConfig.getId());
            }

            boolean installed = false;

            final String id = panelConfig.getId();
            if (!mConfigMap.containsKey(id)) {
                mConfigMap.put(id, panelConfig);

                final int position = panelConfig.getPosition();
                if (position < 0 || position >= mConfigOrder.size()) {
                    mConfigOrder.add(id);
                } else {
                    mConfigOrder.add(position, id);
                }

                mEnabledCount++;
                if (mEnabledCount == 1 || panelConfig.isDefault()) {
                    setDefault(panelConfig.getId());
                }

                installed = true;

                // Add an event to the queue if a new panel is successfully installed.
                mNotificationQueue.add(new Pair<String, String>(
                        "HomePanels:Installed", panelConfig.getId()));
            }

            mHasChanged = true;
            return installed;
        }

        /**
         * Removes an existing panel.
         *
         * @return true if the item has been removed.
         */
        public boolean uninstall(String panelId) {
            ThreadUtils.assertOnThread(mOriginalThread);

            final PanelConfig panelConfig = mConfigMap.get(panelId);
            if (panelConfig == null) {
                return false;
            }

            if (!panelConfig.isDynamic()) {
                throw new IllegalStateException("Can't uninstall a built-in panel: " + panelConfig.getId());
            }

            mConfigMap.remove(panelId);
            mConfigOrder.remove(panelId);

            if (!panelConfig.isDisabled()) {
                mEnabledCount--;
            }

            if (isCurrentDefaultPanel(panelConfig)) {
                findNewDefault();
            }

            // Add an event to the queue if a panel is successfully uninstalled.
            mNotificationQueue.add(new Pair<String, String>("HomePanels:Uninstalled", panelId));

            mHasChanged = true;
            return true;
        }

        /**
         * Moves panel associated with panelId to the specified position.
         *
         * @param panelId Id of panel
         * @param destIndex Destination position
         * @return true if move succeeded
         */
        public boolean moveTo(String panelId, int destIndex) {
            ThreadUtils.assertOnThread(mOriginalThread);

            if (!mConfigOrder.contains(panelId)) {
                return false;
            }

            mConfigOrder.remove(panelId);
            mConfigOrder.add(destIndex, panelId);
            mHasChanged = true;

            return true;
        }

        /**
         * Replaces an existing panel with a new {@code PanelConfig} instance.
         *
         * @return true if the item has been updated.
         */
        public boolean update(PanelConfig panelConfig) {
            ThreadUtils.assertOnThread(mOriginalThread);

            if (panelConfig == null) {
                throw new IllegalStateException("Can't update a null panel");
            }

            boolean updated = false;

            final String id = panelConfig.getId();
            if (mConfigMap.containsKey(id)) {
                final PanelConfig oldPanelConfig = mConfigMap.put(id, panelConfig);

                // The disabled and default states can't never be
                // changed by an update operation.
                panelConfig.setIsDefault(oldPanelConfig.isDefault());
                panelConfig.setIsDisabled(oldPanelConfig.isDisabled());

                updated = true;
            }

            mHasChanged = true;
            return updated;
        }

        /**
         * Saves the current {@code Editor} state asynchronously in the
         * background thread.
         *
         * @return the resulting {@code State} instance.
         */
        public State apply() {
            ThreadUtils.assertOnThread(mOriginalThread);

            // We're about to save the current state in the background thread
            // so we should use a deep copy of the PanelConfig instances to
            // avoid saving corrupted state.
            final State newConfigState =
                    new State(mHomeConfig, makeOrderedCopy(true), isDefault());

            // Copy the event queue to a new list, so that we only modify mNotificationQueue on
            // the original thread where it was created.
            final List<Pair<String, String>> copiedQueue = mNotificationQueue;
            mNotificationQueue = new ArrayList<>();

            ThreadUtils.getBackgroundHandler().post(new Runnable() {
                @Override
                public void run() {
                    mHomeConfig.save(newConfigState);

                    // Send pending events after the new config is saved.
                    sendNotificationsToGecko(copiedQueue);
                }
            });

            return newConfigState;
        }

        /**
         * Saves the current {@code Editor} state synchronously in the
         * current thread.
         *
         * @return the resulting {@code State} instance.
         */
        public State commit() {
            ThreadUtils.assertOnThread(mOriginalThread);

            final State newConfigState =
                    new State(mHomeConfig, makeOrderedCopy(false), isDefault());

            // This is a synchronous blocking operation, hence no
            // need to deep copy the current PanelConfig instances.
            mHomeConfig.save(newConfigState);

            // Send pending events after the new config is saved.
            sendNotificationsToGecko(mNotificationQueue);
            mNotificationQueue.clear();

            return newConfigState;
        }

        /**
         * Returns whether the {@code Editor} represents the default
         * {@code HomeConfig} configuration without any unsaved changes.
         */
        public boolean isDefault() {
            ThreadUtils.assertOnThread(mOriginalThread);

            return (!mHasChanged && mIsFromDefault);
        }

        public boolean isEmpty() {
            return mConfigMap.isEmpty();
        }

        private void sendNotificationsToGecko(List<Pair<String, String>> notifications) {
            for (Pair<String, String> p : notifications) {
                GeckoAppShell.notifyObservers(p.first, p.second);
            }
        }

        private class EditorIterator implements Iterator<PanelConfig> {
            private final Iterator<String> mOrderIterator;

            public EditorIterator() {
                mOrderIterator = mConfigOrder.iterator();
            }

            @Override
            public boolean hasNext() {
                return mOrderIterator.hasNext();
            }

            @Override
            public PanelConfig next() {
                final String panelId = mOrderIterator.next();
                return mConfigMap.get(panelId);
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("Can't 'remove' from on Editor iterator.");
            }
        }

        @Override
        public Iterator<PanelConfig> iterator() {
            ThreadUtils.assertOnThread(mOriginalThread);

            return new EditorIterator();
        }
    }

    public interface OnReloadListener {
        public void onReload();
    }

    public interface HomeConfigBackend {
        public State load();
        public void save(State configState);
        public String getLocale();
        public void setOnReloadListener(OnReloadListener listener);
    }

    // UUIDs used to create PanelConfigs for default built-in panels. These are
    // public because they can be used in "about:home?panel=UUID" query strings
    // to open specific panels without querying the active Home Panel
    // configuration. Because they don't consider the active configuration, it
    // is only sensible to do this for built-in panels (and not for dynamic
    // panels).
    private static final String TOP_SITES_PANEL_ID = "4becc86b-41eb-429a-a042-88fe8b5a094e";
    private static final String BOOKMARKS_PANEL_ID = "7f6d419a-cd6c-4e34-b26f-f68b1b551907";
    private static final String HISTORY_PANEL_ID = "f134bf20-11f7-4867-ab8b-e8e705d7fbe8";
    private static final String COMBINED_HISTORY_PANEL_ID = "4d716ce2-e063-486d-9e7c-b190d7b04dc6";
    private static final String RECENT_TABS_PANEL_ID = "5c2601a5-eedc-4477-b297-ce4cef52adf8";
    private static final String REMOTE_TABS_PANEL_ID = "72429afd-8d8b-43d8-9189-14b779c563d0";
    private static final String DEPRECATED_READING_LIST_PANEL_ID = "20f4549a-64ad-4c32-93e4-1dcef792733b";

    private final HomeConfigBackend mBackend;

    public HomeConfig(HomeConfigBackend backend) {
        mBackend = backend;
    }

    public State load() {
        final State configState = mBackend.load();
        configState.setHomeConfig(this);

        return configState;
    }

    public String getLocale() {
        return mBackend.getLocale();
    }

    public void save(State configState) {
        mBackend.save(configState);
    }

    public void setOnReloadListener(OnReloadListener listener) {
        mBackend.setOnReloadListener(listener);
    }

    public static PanelConfig createBuiltinPanelConfig(Context context, PanelType panelType) {
        return createBuiltinPanelConfig(context, panelType, EnumSet.noneOf(PanelConfig.Flags.class));
    }

    public static int getTitleResourceIdForBuiltinPanelType(PanelType panelType) {
        switch (panelType) {
        case TOP_SITES:
            return R.string.home_top_sites_title;

        case BOOKMARKS:
        case DEPRECATED_READING_LIST:
            return R.string.bookmarks_title;

        case DEPRECATED_HISTORY:
        case DEPRECATED_REMOTE_TABS:
        case DEPRECATED_RECENT_TABS:
        case COMBINED_HISTORY:
            return R.string.home_history_title;

        default:
            throw new IllegalArgumentException("Only for built-in panel types: " + panelType);
        }
    }

    public static String getIdForBuiltinPanelType(PanelType panelType) {
        switch (panelType) {
        case TOP_SITES:
            return TOP_SITES_PANEL_ID;

        case BOOKMARKS:
            return BOOKMARKS_PANEL_ID;

        case DEPRECATED_HISTORY:
            return HISTORY_PANEL_ID;

        case COMBINED_HISTORY:
            return COMBINED_HISTORY_PANEL_ID;

        case DEPRECATED_REMOTE_TABS:
            return REMOTE_TABS_PANEL_ID;

        case DEPRECATED_READING_LIST:
            return DEPRECATED_READING_LIST_PANEL_ID;

        case DEPRECATED_RECENT_TABS:
            return RECENT_TABS_PANEL_ID;

        default:
            throw new IllegalArgumentException("Only for built-in panel types: " + panelType);
        }
    }

    public static PanelConfig createBuiltinPanelConfig(Context context, PanelType panelType, EnumSet<PanelConfig.Flags> flags) {
        final int titleId = getTitleResourceIdForBuiltinPanelType(panelType);
        final String id = getIdForBuiltinPanelType(panelType);

        return new PanelConfig(panelType, context.getString(titleId), id, flags);
    }

    public static HomeConfig getDefault(Context context) {
        return new HomeConfig(new HomeConfigPrefsBackend(context));
    }
}