1379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette/* 2379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * Copyright (C) 2013 The Android Open Source Project 3379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * 4379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * Licensed under the Apache License, Version 2.0 (the "License"); 5379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * you may not use this file except in compliance with the License. 6379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * You may obtain a copy of the License at 7379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * 8379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * http://www.apache.org/licenses/LICENSE-2.0 9379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * 10379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * Unless required by applicable law or agreed to in writing, software 11379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * distributed under the License is distributed on an "AS IS" BASIS, 12379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * See the License for the specific language governing permissions and 14379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * limitations under the License. 15379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette */ 16379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette 17379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverettepackage android.support.v4.widget; 18379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette 19379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viveretteimport android.view.View; 20379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viveretteimport android.view.View.OnTouchListener; 21379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette 22379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette/** 23379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * Helper for accessing features in ListPopupWindow introduced after API level 4 24379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * in a backwards compatible fashion. 25379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette */ 26c5847d13e40f5d52459f5c0dab32dc08f1a9a683Chris Banespublic final class ListPopupWindowCompat { 27379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette /** 28379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * Interface for the full API. 29379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette */ 30379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette interface ListPopupWindowImpl { 31379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette public OnTouchListener createDragToOpenListener(Object listPopupWindow, View src); 32379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette } 33379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette 34379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette /** 35379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * Interface implementation that doesn't use anything above v4 APIs. 36379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette */ 37379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette static class BaseListPopupWindowImpl implements ListPopupWindowImpl { 38379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette @Override 39379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette public OnTouchListener createDragToOpenListener(Object listPopupWindow, View src) { 40379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette return null; 41379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette } 42379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette } 43379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette 44379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette /** 45379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * Interface implementation for devices with at least KitKat APIs. 46379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette */ 47379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette static class KitKatListPopupWindowImpl extends BaseListPopupWindowImpl { 48379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette @Override 49379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette public OnTouchListener createDragToOpenListener(Object listPopupWindow, View src) { 50379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette return ListPopupWindowCompatKitKat.createDragToOpenListener(listPopupWindow, src); 51379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette } 52379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette } 53379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette 54379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette /** 55379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * Select the correct implementation to use for the current platform. 56379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette */ 57379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette static final ListPopupWindowImpl IMPL; 58379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette static { 59379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette final int version = android.os.Build.VERSION.SDK_INT; 60379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette if (version >= 19) { 61379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette IMPL = new KitKatListPopupWindowImpl(); 62379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette } else { 63379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette IMPL = new BaseListPopupWindowImpl(); 64379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette } 65379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette } 66379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette 67379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette private ListPopupWindowCompat() { 68379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette // This class is not publicly instantiable. 69379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette } 70379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette 71379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette /** 72379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * On API {@link android.os.Build.VERSION_CODES#KITKAT} and higher, returns 73379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * an {@link OnTouchListener} that can be added to the source view to 74379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * implement drag-to-open behavior. Generally, the source view should be the 75379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * same view that was passed to ListPopupWindow.setAnchorView(View). 76379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * <p> 77379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * When the listener is set on a view, touching that view and dragging 78379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * outside of its bounds will open the popup window. Lifting will select the 79379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * currently touched list item. 80379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * <p> 81379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * Example usage: 82379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * 83379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * <pre> 84379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * ListPopupWindow myPopup = new ListPopupWindow(context); 85379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * myPopup.setAnchor(myAnchor); 86379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * OnTouchListener dragListener = myPopup.createDragToOpenListener(myAnchor); 87379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * myAnchor.setOnTouchListener(dragListener); 88379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * </pre> 89379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * 90379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * @param listPopupWindow the ListPopupWindow against which to invoke the 91379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * method 92379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * @param src the view on which the resulting listener will be set 93379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * @return a touch listener that controls drag-to-open behavior, or null on 94379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette * unsupported APIs 95379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette */ 96379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette public static OnTouchListener createDragToOpenListener(Object listPopupWindow, View src) { 97379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette return IMPL.createDragToOpenListener(listPopupWindow, src); 98379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette } 99379312ec6d3e517f8bb8fcf2e9876b42f9495df3Alan Viverette} 100