Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko.home; |
michael@0 | 7 | |
michael@0 | 8 | import org.mozilla.gecko.home.HomeConfig.PanelConfig; |
michael@0 | 9 | import org.mozilla.gecko.home.HomeConfig.PanelType; |
michael@0 | 10 | import org.mozilla.gecko.home.HomePager; |
michael@0 | 11 | |
michael@0 | 12 | import android.content.Context; |
michael@0 | 13 | import android.os.Bundle; |
michael@0 | 14 | import android.support.v4.app.Fragment; |
michael@0 | 15 | import android.support.v4.app.FragmentManager; |
michael@0 | 16 | import android.support.v4.app.FragmentStatePagerAdapter; |
michael@0 | 17 | import android.view.ViewGroup; |
michael@0 | 18 | |
michael@0 | 19 | import java.util.ArrayList; |
michael@0 | 20 | import java.util.HashMap; |
michael@0 | 21 | import java.util.List; |
michael@0 | 22 | |
michael@0 | 23 | class HomeAdapter extends FragmentStatePagerAdapter { |
michael@0 | 24 | |
michael@0 | 25 | private final Context mContext; |
michael@0 | 26 | private final ArrayList<PanelInfo> mPanelInfos; |
michael@0 | 27 | private final HashMap<String, Fragment> mPanels; |
michael@0 | 28 | |
michael@0 | 29 | private boolean mCanLoadHint; |
michael@0 | 30 | |
michael@0 | 31 | private OnAddPanelListener mAddPanelListener; |
michael@0 | 32 | |
michael@0 | 33 | interface OnAddPanelListener { |
michael@0 | 34 | public void onAddPanel(String title); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | public HomeAdapter(Context context, FragmentManager fm) { |
michael@0 | 38 | super(fm); |
michael@0 | 39 | |
michael@0 | 40 | mContext = context; |
michael@0 | 41 | mCanLoadHint = HomeFragment.DEFAULT_CAN_LOAD_HINT; |
michael@0 | 42 | |
michael@0 | 43 | mPanelInfos = new ArrayList<PanelInfo>(); |
michael@0 | 44 | mPanels = new HashMap<String, Fragment>(); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | @Override |
michael@0 | 48 | public int getCount() { |
michael@0 | 49 | return mPanelInfos.size(); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | @Override |
michael@0 | 53 | public Fragment getItem(int position) { |
michael@0 | 54 | PanelInfo info = mPanelInfos.get(position); |
michael@0 | 55 | return Fragment.instantiate(mContext, info.getClassName(), info.getArgs()); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | @Override |
michael@0 | 59 | public CharSequence getPageTitle(int position) { |
michael@0 | 60 | if (mPanelInfos.size() > 0) { |
michael@0 | 61 | PanelInfo info = mPanelInfos.get(position); |
michael@0 | 62 | return info.getTitle().toUpperCase(); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | return null; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | @Override |
michael@0 | 69 | public Object instantiateItem(ViewGroup container, int position) { |
michael@0 | 70 | Fragment fragment = (Fragment) super.instantiateItem(container, position); |
michael@0 | 71 | mPanels.put(mPanelInfos.get(position).getId(), fragment); |
michael@0 | 72 | |
michael@0 | 73 | return fragment; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | @Override |
michael@0 | 77 | public void destroyItem(ViewGroup container, int position, Object object) { |
michael@0 | 78 | super.destroyItem(container, position, object); |
michael@0 | 79 | mPanels.remove(mPanelInfos.get(position).getId()); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | public void setOnAddPanelListener(OnAddPanelListener listener) { |
michael@0 | 83 | mAddPanelListener = listener; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | public int getItemPosition(String panelId) { |
michael@0 | 87 | for (int i = 0; i < mPanelInfos.size(); i++) { |
michael@0 | 88 | final String id = mPanelInfos.get(i).getId(); |
michael@0 | 89 | if (id.equals(panelId)) { |
michael@0 | 90 | return i; |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | return -1; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | public String getPanelIdAtPosition(int position) { |
michael@0 | 98 | // getPanelIdAtPosition() might be called before HomeAdapter |
michael@0 | 99 | // has got its initial list of PanelConfigs. Just bail. |
michael@0 | 100 | if (mPanelInfos.isEmpty()) { |
michael@0 | 101 | return null; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | return mPanelInfos.get(position).getId(); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | private void addPanel(PanelInfo info) { |
michael@0 | 108 | mPanelInfos.add(info); |
michael@0 | 109 | |
michael@0 | 110 | if (mAddPanelListener != null) { |
michael@0 | 111 | mAddPanelListener.onAddPanel(info.getTitle()); |
michael@0 | 112 | } |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | public void update(List<PanelConfig> panelConfigs) { |
michael@0 | 116 | mPanels.clear(); |
michael@0 | 117 | mPanelInfos.clear(); |
michael@0 | 118 | |
michael@0 | 119 | if (panelConfigs != null) { |
michael@0 | 120 | for (PanelConfig panelConfig : panelConfigs) { |
michael@0 | 121 | final PanelInfo info = new PanelInfo(panelConfig); |
michael@0 | 122 | addPanel(info); |
michael@0 | 123 | } |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | notifyDataSetChanged(); |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | public boolean getCanLoadHint() { |
michael@0 | 130 | return mCanLoadHint; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | public void setCanLoadHint(boolean canLoadHint) { |
michael@0 | 134 | // We cache the last hint value so that we can use it when |
michael@0 | 135 | // creating new panels. See PanelInfo.getArgs(). |
michael@0 | 136 | mCanLoadHint = canLoadHint; |
michael@0 | 137 | |
michael@0 | 138 | // Enable/disable loading on all existing panels |
michael@0 | 139 | for (Fragment panelFragment : mPanels.values()) { |
michael@0 | 140 | final HomeFragment panel = (HomeFragment) panelFragment; |
michael@0 | 141 | panel.setCanLoadHint(canLoadHint); |
michael@0 | 142 | } |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | private final class PanelInfo { |
michael@0 | 146 | private final PanelConfig mPanelConfig; |
michael@0 | 147 | |
michael@0 | 148 | PanelInfo(PanelConfig panelConfig) { |
michael@0 | 149 | mPanelConfig = panelConfig; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | public String getId() { |
michael@0 | 153 | return mPanelConfig.getId(); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | public String getTitle() { |
michael@0 | 157 | return mPanelConfig.getTitle(); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | public String getClassName() { |
michael@0 | 161 | final PanelType type = mPanelConfig.getType(); |
michael@0 | 162 | return type.getPanelClass().getName(); |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | public Bundle getArgs() { |
michael@0 | 166 | final Bundle args = new Bundle(); |
michael@0 | 167 | |
michael@0 | 168 | args.putBoolean(HomePager.CAN_LOAD_ARG, mCanLoadHint); |
michael@0 | 169 | |
michael@0 | 170 | // Only DynamicPanels need the PanelConfig argument |
michael@0 | 171 | if (mPanelConfig.isDynamic()) { |
michael@0 | 172 | args.putParcelable(HomePager.PANEL_CONFIG_ARG, mPanelConfig); |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | return args; |
michael@0 | 176 | } |
michael@0 | 177 | } |
michael@0 | 178 | } |