1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package androidx.leanback.widget; 15 16 import java.util.ArrayList; 17 import java.util.HashMap; 18 19 /** 20 * A ClassPresenterSelector selects a {@link Presenter} based on the item's 21 * Java class. 22 */ 23 public final class ClassPresenterSelector extends PresenterSelector { 24 25 private final ArrayList<Presenter> mPresenters = new ArrayList<Presenter>(); 26 27 private final HashMap<Class<?>, Object> mClassMap = new HashMap<Class<?>, Object>(); 28 29 /** 30 * Sets a presenter to be used for the given class. 31 * @param cls The data model class to be rendered. 32 * @param presenter The presenter that renders the objects of the given class. 33 * @return This ClassPresenterSelector object. 34 */ addClassPresenter(Class<?> cls, Presenter presenter)35 public ClassPresenterSelector addClassPresenter(Class<?> cls, Presenter presenter) { 36 mClassMap.put(cls, presenter); 37 if (!mPresenters.contains(presenter)) { 38 mPresenters.add(presenter); 39 } 40 return this; 41 } 42 43 /** 44 * Sets a presenter selector to be used for the given class. 45 * @param cls The data model class to be rendered. 46 * @param presenterSelector The presenter selector that finds the right presenter for a given 47 * class. 48 * @return This ClassPresenterSelector object. 49 */ addClassPresenterSelector(Class<?> cls, PresenterSelector presenterSelector)50 public ClassPresenterSelector addClassPresenterSelector(Class<?> cls, 51 PresenterSelector presenterSelector) { 52 mClassMap.put(cls, presenterSelector); 53 Presenter[] innerPresenters = presenterSelector.getPresenters(); 54 for (int i = 0; i < innerPresenters.length; i++) 55 if (!mPresenters.contains(innerPresenters[i])) { 56 mPresenters.add(innerPresenters[i]); 57 } 58 return this; 59 } 60 61 @Override getPresenter(Object item)62 public Presenter getPresenter(Object item) { 63 Class<?> cls = item.getClass(); 64 Object presenter = null; 65 66 do { 67 presenter = mClassMap.get(cls); 68 if (presenter instanceof PresenterSelector) { 69 Presenter innerPresenter = ((PresenterSelector) presenter).getPresenter(item); 70 if (innerPresenter != null) { 71 return innerPresenter; 72 } 73 } 74 cls = cls.getSuperclass(); 75 } while (presenter == null && cls != null); 76 77 return (Presenter) presenter; 78 } 79 80 @Override getPresenters()81 public Presenter[] getPresenters() { 82 return mPresenters.toArray(new Presenter[mPresenters.size()]); 83 } 84 } 85