Home | History | Annotate | Download | only in menu
      1 /*
      2  * Copyright (C) 2015 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 android.support.car.app.menu;
     18 
     19 import android.graphics.drawable.Drawable;
     20 import android.os.Bundle;
     21 
     22 /**
     23  * Class that the CarMenu communicates with to fetch the contents of the CarMenu
     24  */
     25 public abstract class CarMenuCallbacks {
     26     /**
     27      * Listens for calls to notifyChildrenChanged and onChildrenChanged
     28      * @hide
     29      */
     30     public interface OnChildrenChangedListener {
     31         /** Called when app wants to notify that contents of an entire menu have changed. */
     32         void onChildrenChanged(String parentId);
     33         /**
     34          * Called when app wants to notify that contents of a single item has changed.
     35          *
     36          * @param item Contents of {@link android.os.Bundle} are used to update the contents of the existing
     37          *             item.
     38          * @param leftIcon Drawable to convert to bitmap
     39          * @param rightIcon Drawable to convert to bitmap
     40          */
     41         void onChildChanged(String parentId, Bundle item, Drawable leftIcon, Drawable rightIcon);
     42     }
     43 
     44     private OnChildrenChangedListener mListener;
     45 
     46     /**
     47      * Called when the CarMenu wants to get the root
     48      *
     49      * @param hints Hints that the Drawer can use to modify behavior. It can be null.
     50      * @return The {@link RootMenu} which contains the root id and any hints
     51      */
     52     public abstract RootMenu onGetRoot(Bundle hints);
     53 
     54     /**
     55      * Called when the CarMenu subscribes to to a certain id
     56      *
     57      * @param parentId ID to subscribe to
     58      * @param result {@link CarMenu} that is used to communicate back the results
     59      */
     60     public abstract void onLoadChildren(String parentId,
     61             CarMenu result);
     62 
     63     /**
     64      * Notify the CarMenu that the menu defined by the id has changed. This will cause the CarMenu
     65      * to fetch the menu items.
     66      *
     67      * @param parentId The id which identifies the menu that changed
     68      */
     69     public void notifyChildrenChanged(String parentId) {
     70         if (mListener != null) {
     71             mListener.onChildrenChanged(parentId);
     72         }
     73     }
     74 
     75     /**
     76      * Register an OnChildrenChangedListener to detect when a menu has changed
     77      *
     78      * @param listener listener to register
     79      * @hide
     80      */
     81     public void registerOnChildrenChangedListener(OnChildrenChangedListener listener) {
     82         mListener = listener;
     83     }
     84 
     85     /**
     86      * Unregister an OnChildrenChangedListener to detect when a menu has changed.
     87      *
     88      * @param listener listener to unregister
     89      * @hide
     90      */
     91     public void unregisterOnChildrenChangedListener(OnChildrenChangedListener listener) {
     92         if (listener != mListener) {
     93             throw new IllegalStateException(
     94                     "Trying to unregister a listener that was not registered!");
     95         }
     96         mListener = null;
     97     }
     98 
     99     /**
    100      * Called when the CarMenu is opened
    101      */
    102     public void onCarMenuOpened() {}
    103 
    104     /**
    105      * Called when the CarMenu is closed
    106      */
    107     public void onCarMenuClosed() {}
    108 
    109     /**
    110      * Called when the CarMenu is opening
    111      */
    112     public void onCarMenuOpening() {}
    113 
    114     /**
    115      * Called when the CarMenu is closing
    116      */
    117     public void onCarMenuClosing() {}
    118 
    119     /**
    120      * Called when an item is clicked
    121      *
    122      * @param id Id of the item that is clicked
    123      */
    124     public void onItemClicked(String id) {}
    125 
    126     /**
    127      * Called when an item is long clicked
    128      *
    129      * @param id Id of the item that is long clicked
    130      *
    131      * @return Return true if handled, false if not. Returning false also means that the
    132      * onItemClicked handler will be called.
    133      */
    134     public boolean onItemLongClicked(String id) {
    135         return false;
    136     }
    137 
    138     /**
    139      * Called when the state of the CarMenu has changed.
    140      * TODO: Describe the state. This may be removed moving forward depending on if it is useful
    141      *
    142      * @param newState The new state of the CarMenu
    143      */
    144     public void onStateChanged(int newState) {}
    145 
    146     /**
    147      * Notify that an item has changed. Use a {@link CarMenu.Builder} to build the item and pu the
    148      * updated contents inside. Note that this cannot be used to change an item's layout, but to
    149      * modify existing contents.
    150      *
    151      * @param parentId parentId of the item.
    152      * @param item Updated contents of the item.
    153      */
    154     public void notifyChildChanged(String parentId, CarMenu.Item item) {
    155         if (mListener != null) {
    156             CarMenu.ItemImpl realItem = (CarMenu.ItemImpl) item;
    157             mListener.onChildChanged(parentId,
    158                     realItem.mBundle,
    159                     realItem.mIcon,
    160                     realItem.mRightIcon);
    161         }
    162     }
    163 }
    164