1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.wear.internal.widget.drawer; 18 19 import androidx.annotation.MainThread; 20 import androidx.annotation.RestrictTo; 21 import androidx.annotation.RestrictTo.Scope; 22 import androidx.wear.widget.drawer.WearableNavigationDrawerView.OnItemSelectedListener; 23 import androidx.wear.widget.drawer.WearableNavigationDrawerView.WearableNavigationDrawerAdapter; 24 25 import java.util.HashSet; 26 import java.util.Set; 27 28 /** 29 * Controls the behavior of this view where the behavior may differ between single and multi-page. 30 * 31 */ 32 @RestrictTo(Scope.LIBRARY) 33 public abstract class WearableNavigationDrawerPresenter { 34 35 private final Set<OnItemSelectedListener> mOnItemSelectedListeners = new HashSet<>(); 36 37 /** 38 * Indicates to the presenter that the underlying data has changed. 39 */ 40 @MainThread onDataSetChanged()41 public abstract void onDataSetChanged(); 42 43 /** 44 * Indicates to the presenter that the drawer has a new adapter. 45 */ 46 @MainThread onNewAdapter(WearableNavigationDrawerAdapter adapter)47 public abstract void onNewAdapter(WearableNavigationDrawerAdapter adapter); 48 49 /** 50 * Indicates to the presenter that the user has selected an item. 51 */ 52 @MainThread onSelected(int index)53 public abstract void onSelected(int index); 54 55 /** 56 * Indicates to the presenter that the developer wishes to change which item is selected. 57 */ 58 @MainThread onSetCurrentItemRequested(int index, boolean smoothScrollTo)59 public abstract void onSetCurrentItemRequested(int index, boolean smoothScrollTo); 60 61 /** 62 * Indicates to the presenter that the user has tapped on the drawer. 63 * 64 * @return {@code true} if the touch event has been handled and should not propagate further. 65 */ 66 @MainThread onDrawerTapped()67 public abstract boolean onDrawerTapped(); 68 69 /** 70 * Indicates to the presenter that a new {@link OnItemSelectedListener} has been added. 71 */ 72 @MainThread onItemSelectedListenerAdded(OnItemSelectedListener listener)73 public void onItemSelectedListenerAdded(OnItemSelectedListener listener) { 74 mOnItemSelectedListeners.add(listener); 75 } 76 77 /** 78 * Indicates to the presenter that an {@link OnItemSelectedListener} has been removed. 79 */ 80 @MainThread onItemSelectedListenerRemoved(OnItemSelectedListener listener)81 public void onItemSelectedListenerRemoved(OnItemSelectedListener listener) { 82 mOnItemSelectedListeners.remove(listener); 83 } 84 85 /** 86 * Notifies all listeners that the item at {@code selectedPos} has been selected. 87 */ 88 @MainThread notifyItemSelectedListeners(int selectedPos)89 void notifyItemSelectedListeners(int selectedPos) { 90 for (OnItemSelectedListener listener : mOnItemSelectedListeners) { 91 listener.onItemSelected(selectedPos); 92 } 93 } 94 } 95