Android Open Source - sdl_tester_android Menu Manager






From Project

Back to project page sdl_tester_android.

License

The source code is released under:

Copyright (c) 2014, Ford Motor Company All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are m...

If you think the Android project sdl_tester_android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.livio.sdl.menu;
// w  w w .  j a v a  2 s  .c  o  m
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import android.util.Log;
import android.util.SparseArray;

/**
 * Manages SDL menu items (commands and submenus), keeping an up-to-date list of available
 * menu items.
 *
 * @author Mike Burke
 *
 */
public class MenuManager {
  /**
   * Allows iterating over the items in a menu manager object.
   *
   * @author Mike Burke
   *
   */
  public static class MenuIterator implements Iterator<MenuItem>{

    private final MenuManager items;
    private int currentIndex = 0;
    
    private MenuIterator(MenuManager items){
      this.items = items;
    }
    
    @Override
    public boolean hasNext() {
      return (currentIndex < (items.size()));
    }

    @Override
    public MenuItem next() {
      return items.getItemAt(currentIndex++);
    }

    @Override
    public void remove() {
      // don't allow removing items through the iterator
      throw new UnsupportedOperationException();
    }
  }

  private static boolean debug = false;
  private SparseArray<MenuItem> menuItems;
  
  // constructors
  public MenuManager() {
    menuItems = new SparseArray<MenuItem>();
  }
  
  public MenuManager(int startSize){
    menuItems = new SparseArray<MenuItem>(startSize);
  }
  
  /**
   * Adds the input menu item to the list.  If the input item is the child of a submenu,
   * also adds the item to the parent's list of children.
   * 
   * @param item The item to add
   */
  public void addItem(MenuItem item){
    log(new StringBuilder().append("Adding item: ").append(item.toString()).toString());
    menuItems.put(item.getId(), item);
    
    // if not a top-level menu, let's check if it needs to be added to a submenu as well
    if(!item.isMenu()){
      CommandButton button = (CommandButton) item;
      int parentId = button.getParentId();
      if(parentId != -1){
        // button has a valid parent.  let's find it and add it to the list
        addCommandToParent(button, parentId);
      }
    }
  }
  
  /**
   * Adds the input command button to the parent represented by the parent id.
   * 
   * @param button The button to add
   * @param parentId The id of the button's parent
   */
  private void addCommandToParent(CommandButton button, int parentId){
    MenuItem parent = menuItems.get(parentId);
    log(new StringBuilder().append("Adding command: ").append(button.toString()).append(" to parent: ").append(parent.toString()).toString());
    
    if(parent.isMenu()){
      SubmenuButton parentButton = (SubmenuButton) parent;
      parentButton.addChild(button);
    }
  }
  
  /**
   * Removes the item with the input id.  If the item to remove is a submenu which contains children,
   * this method removes the children commands from the menu manager as well.  If the item to remove is
   * a command which belongs to a registered submenu, this method will also remove the child from its
   * parent's list of children.
   * 
   * @param id The id of the item to remove
   */
  public void removeItem(int id){
    MenuItem itemToRemove = menuItems.get(id);
    if(itemToRemove == null){
      return;
    }

    log(new StringBuilder().append("Removing item: ").append(itemToRemove.toString()).toString());
    
    if(!itemToRemove.isMenu()){
      // command button
      final int parentId = ((CommandButton)itemToRemove).getParentId(); 
      if(parentId != -1){
        // we have a command button with a valid parent - let's remove it from the parent list
        SubmenuButton parent = (SubmenuButton) menuItems.get(parentId);
        if(parent != null){
          log(new StringBuilder().append("Removing child: ").append(itemToRemove.toString()).append(" from parent: ").append(parent.toString()).toString());
          parent.removeChild(itemToRemove.getId());
        }
      }
    }
    else{
      // submenu button is being deleted - remove all children as well
      removeChildren((SubmenuButton) itemToRemove);
    }
    menuItems.remove(id);
  }
  
  /**
   * Removes any children that belong to the input submenu.
   * 
   * @param parent The parent whose children should be removed
   */
  private void removeChildren(SubmenuButton parent){
    List<MenuItem> children = parent.getChildren();
    if(children != null && children.size() > 0){
      for(MenuItem child : children){
        removeItem(child.getId());
      }
    }
  }
  
  /**
   * Makes a copy of all registered submenus and returns it.
   * 
   * @return A list of all registered submenus
   */
  public List<MenuItem> getSubmenus(){
    if(size() == 0){
      return Collections.emptyList();
    }
    
    log("Making a copy of all submenus");
    
    List<MenuItem> result = new ArrayList<MenuItem>();
    
    // iterate through all items
    Iterator<MenuItem> iterator = iterator();
    while(iterator.hasNext()){
      MenuItem current = iterator.next();
      if(current.isMenu()){
        // if this item is a submenu, make a copy of the item and add it to the result list
        result.add(new SubmenuButton((SubmenuButton) current));
      }
    }
    
    return result;
  }

  /**
   * Makes a copy of all registered commands and returns it.
   * 
   * @return A list of all registered commands
   */
  public List<MenuItem> getCommands(){
    if(size() == 0){
      return Collections.emptyList();
    }
    
    log("Making a copy of all commands");
    
    List<MenuItem> result = new ArrayList<MenuItem>();
    
    // iterate through all items
    Iterator<MenuItem> iterator = iterator();
    while(iterator.hasNext()){
      MenuItem current = iterator.next();
      if(!current.isMenu()){
        // if this item is a command, make a copy of the item and add it to the result list
        result.add(new CommandButton((CommandButton) current));
      }
    }
    
    return result;
  }
  
  /**
   * Makes a copy of all items and returns it.
   * 
   * @return A list of all registered menu items
   */
  public List<MenuItem> getAllItems(){
    if(size() == 0){
      return Collections.emptyList();
    }
    
    log("Making a copy of all menu items");
    
    List<MenuItem> result = new ArrayList<MenuItem>(size());
    
    // iterate through all items
    Iterator<MenuItem> iterator = iterator();
    while(iterator.hasNext()){
      MenuItem current = iterator.next();

      // make a copy of the item and add it to the list
      if(current.isMenu()){
        result.add(new SubmenuButton((SubmenuButton) current));
      }
      else{
        result.add(new CommandButton((CommandButton) current));
      }
    }
    
    return result;
  }
  
  /**
   * Returns the item with the given id.
   * 
   * @param id The id of the item to return
   * @return The item with the input id, or null if the id doesn't exist
   */
  public MenuItem get(int id){
    return menuItems.get(id);
  }
  
  /**
   * Returns the item at the input index.
   * 
   * @param index The index of the item to return
   * @return The item at the given index
   */
  public MenuItem getItemAt(int index){
    return menuItems.valueAt(index);
  }
  
  /**
   * Returns the item with the given name.
   * 
   * @param name The name of the item to return
   * @return The item with the given name, or null if the name doesn't exist
   */
  public MenuItem get(String name){
    // iterate through all items
    Iterator<MenuItem> iterator = iterator();
    while(iterator.hasNext()){
      MenuItem current = iterator.next();

      // if the current item matches the input name, return it
      if(name.equals(current.getName())){
        return current;
      }
    }
    
    return null;
  }
  
  /**
   * Returns the number of items being managed by the menu manager.
   * 
   * @return The number of items being managed by the menu manager
   */
  public int size(){
    return menuItems.size();
  }
  
  /**
   * Removes all menu items from the menu manager.
   */
  public void clear(){
    menuItems.clear();
  }
  
  /**
   * Dispatches a click event to the button with the input id.  If the button with the
   * input id isn't found in the menu manager, this method does nothing.
   * 
   * @param buttonId The id of the button that was clicked
   */
  public void dispatchClick(int buttonId){
    MenuItem item = menuItems.get(buttonId);
    
    if(item == null || item.isMenu()){
      log("Button with selected ID hasn't been added to this menu manager.");
    }
    else{
      CommandButton itemCmd = (CommandButton) item;
      itemCmd.dispatchClickEvent();
    }
  }
  
  /**
   * Creates a new iterator object for this MenuManager object.
   * 
   * @return The new iterator instance
   */
  public Iterator<MenuItem> iterator(){
    log("Creating new iterator object");
    Iterator<MenuItem> iterator = new MenuIterator(this);
    return iterator;
  }

  /**
   * Enables or disables debug mode for log messages.
   * 
   * @param enable True to enable debug logs, false to disable
   */
  public static void setDebug(boolean enable){
    debug = enable;
  }
  
  private static void log(String msg){
    if(debug){
      Log.d("MenuManager", msg);
    }
  }
}




Java Source Code List

com.livio.sdl.IdGenerator.java
com.livio.sdl.IpAddress.java
com.livio.sdl.SdlConstants.java
com.livio.sdl.SdlImageItem.java
com.livio.sdl.SdlLogMessage.java
com.livio.sdl.SdlRequestFactory.java
com.livio.sdl.SdlResponseFactory.java
com.livio.sdl.SdlResponseTracker.java
com.livio.sdl.SdlService.java
com.livio.sdl.adapters.SdlImageAdapter.java
com.livio.sdl.adapters.SdlMessageAdapter.java
com.livio.sdl.dialogs.BaseAlertDialog.java
com.livio.sdl.dialogs.BaseImageListDialog.java
com.livio.sdl.dialogs.BaseMultipleListViewDialog.java
com.livio.sdl.dialogs.BaseOkCancelDialog.java
com.livio.sdl.dialogs.BaseSingleListViewDialog.java
com.livio.sdl.dialogs.ImageListDialog.java
com.livio.sdl.dialogs.IndeterminateProgressDialog.java
com.livio.sdl.dialogs.JsonFlipperDialog.java
com.livio.sdl.dialogs.ListViewDialog.java
com.livio.sdl.dialogs.MultipleListViewDialog.java
com.livio.sdl.dialogs.SingleJsonDialog.java
com.livio.sdl.dialogs.TextViewAlertDialog.java
com.livio.sdl.dialogs.TextViewOkCancelDialog.java
com.livio.sdl.enums.EnumClickListener.java
com.livio.sdl.enums.EnumComparator.java
com.livio.sdl.enums.SdlButton.java
com.livio.sdl.enums.SdlCommand.java
com.livio.sdl.enums.SdlImageType.java
com.livio.sdl.enums.SdlInteractionMode.java
com.livio.sdl.enums.SdlLanguage.java
com.livio.sdl.enums.SdlSpeechCapability.java
com.livio.sdl.enums.SdlSystemAction.java
com.livio.sdl.enums.SdlTextAlignment.java
com.livio.sdl.enums.SdlTransportType.java
com.livio.sdl.enums.SdlUpdateMode.java
com.livio.sdl.enums.SdlVehicleData.java
com.livio.sdl.menu.CommandButton.java
com.livio.sdl.menu.MenuItem.java
com.livio.sdl.menu.MenuManager.java
com.livio.sdl.menu.SubmenuButton.java
com.livio.sdl.utils.AndroidUtils.java
com.livio.sdl.utils.ApplicationPreferences.java
com.livio.sdl.utils.Counter.java
com.livio.sdl.utils.DownCounter.java
com.livio.sdl.utils.MathUtils.java
com.livio.sdl.utils.SdlUtils.java
com.livio.sdl.utils.StringUtils.java
com.livio.sdl.utils.Timeout.java
com.livio.sdl.utils.UpCounter.java
com.livio.sdl.utils.WifiUtils.java
com.livio.sdl.viewhelpers.MinMaxInputFilter.java
com.livio.sdl.viewhelpers.SeekBarCalculator.java
com.livio.sdltester.HelpActivity.java
com.livio.sdltester.LivioSdlTesterPreferences.java
com.livio.sdltester.MainActivity.java
com.livio.sdltester.SdlTesterImageResource.java
com.livio.sdltester.dialogs.AddCommandDialog.java
com.livio.sdltester.dialogs.AddSubMenuDialog.java
com.livio.sdltester.dialogs.ButtonSubscriptionDialog.java
com.livio.sdltester.dialogs.ButtonUnsubscriptionDialog.java
com.livio.sdltester.dialogs.ChangeRegistrationDialog.java
com.livio.sdltester.dialogs.ChoiceItemDialog.java
com.livio.sdltester.dialogs.CreateInteractionChoiceSetDialog.java
com.livio.sdltester.dialogs.DeleteCommandDialog.java
com.livio.sdltester.dialogs.DeleteFileDialog.java
com.livio.sdltester.dialogs.DeleteInteractionDialog.java
com.livio.sdltester.dialogs.DeleteSubmenuDialog.java
com.livio.sdltester.dialogs.GetDtcsDialog.java
com.livio.sdltester.dialogs.PerformInteractionDialog.java
com.livio.sdltester.dialogs.PutFileDialog.java
com.livio.sdltester.dialogs.ReadDidsDialog.java
com.livio.sdltester.dialogs.ScrollableMessageDialog.java
com.livio.sdltester.dialogs.SdlAlertDialog.java
com.livio.sdltester.dialogs.SdlConnectionDialog.java
com.livio.sdltester.dialogs.SetAppIconDialog.java
com.livio.sdltester.dialogs.SetMediaClockTimerDialog.java
com.livio.sdltester.dialogs.ShowDialog.java
com.livio.sdltester.dialogs.SliderDialog.java
com.livio.sdltester.dialogs.SoftButtonItemDialog.java
com.livio.sdltester.dialogs.SoftButtonListDialog.java
com.livio.sdltester.dialogs.SpeakDialog.java