Search in sources :

Example 36 with TabWidget

use of android.widget.TabWidget in project Fairphone by Kwamecorp.

the class FocusHelper method handlePagedViewGridLayoutWidgetKeyEvent.

/**
 * Handles key events in a PageViewExtendedLayout containing PagedViewWidgets.
 */
static boolean handlePagedViewGridLayoutWidgetKeyEvent(PagedViewWidget w, int keyCode, KeyEvent e) {
    final PagedViewGridLayout parent = (PagedViewGridLayout) w.getParent();
    final PagedView container = (PagedView) parent.getParent();
    final TabHost tabHost = findTabHostParent(container);
    final TabWidget tabs = tabHost.getTabWidget();
    final int widgetIndex = parent.indexOfChild(w);
    final int widgetCount = parent.getChildCount();
    final int pageIndex = ((PagedView) container).indexToPage(container.indexOfChild(parent));
    final int pageCount = container.getChildCount();
    final int cellCountX = parent.getCellCountX();
    final int cellCountY = parent.getCellCountY();
    final int x = widgetIndex % cellCountX;
    final int y = widgetIndex / cellCountX;
    final int action = e.getAction();
    final boolean handleKeyEvent = (action != KeyEvent.ACTION_UP);
    ViewGroup newParent = null;
    // Now that we load items in the bg asynchronously, we can't just focus
    // child siblings willy-nilly
    View child = null;
    boolean wasHandled = false;
    switch(keyCode) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (handleKeyEvent) {
                // Select the previous widget or the last widget on the previous page
                if (widgetIndex > 0) {
                    parent.getChildAt(widgetIndex - 1).requestFocus();
                } else {
                    if (pageIndex > 0) {
                        newParent = getAppsCustomizePage(container, pageIndex - 1);
                        if (newParent != null) {
                            child = newParent.getChildAt(newParent.getChildCount() - 1);
                            if (child != null)
                                child.requestFocus();
                        }
                    }
                }
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            if (handleKeyEvent) {
                // Select the next widget or the first widget on the next page
                if (widgetIndex < (widgetCount - 1)) {
                    parent.getChildAt(widgetIndex + 1).requestFocus();
                } else {
                    if (pageIndex < (pageCount - 1)) {
                        newParent = getAppsCustomizePage(container, pageIndex + 1);
                        if (newParent != null) {
                            child = newParent.getChildAt(0);
                            if (child != null)
                                child.requestFocus();
                        }
                    }
                }
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_DPAD_UP:
            if (handleKeyEvent) {
                // Select the closest icon in the previous row, otherwise select the tab bar
                if (y > 0) {
                    int newWidgetIndex = ((y - 1) * cellCountX) + x;
                    child = parent.getChildAt(newWidgetIndex);
                    if (child != null)
                        child.requestFocus();
                } else {
                    tabs.requestFocus();
                }
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            if (handleKeyEvent) {
                // Select the closest icon in the previous row, otherwise do nothing
                if (y < (cellCountY - 1)) {
                    int newWidgetIndex = Math.min(widgetCount - 1, ((y + 1) * cellCountX) + x);
                    child = parent.getChildAt(newWidgetIndex);
                    if (child != null)
                        child.requestFocus();
                }
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_ENTER:
        case KeyEvent.KEYCODE_DPAD_CENTER:
            if (handleKeyEvent) {
                // Simulate a click on the widget
                View.OnClickListener clickListener = (View.OnClickListener) container;
                clickListener.onClick(w);
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_PAGE_UP:
            if (handleKeyEvent) {
                // if there is no previous page
                if (pageIndex > 0) {
                    newParent = getAppsCustomizePage(container, pageIndex - 1);
                    if (newParent != null) {
                        child = newParent.getChildAt(0);
                    }
                } else {
                    child = parent.getChildAt(0);
                }
                if (child != null)
                    child.requestFocus();
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_PAGE_DOWN:
            if (handleKeyEvent) {
                // if there is no next page
                if (pageIndex < (pageCount - 1)) {
                    newParent = getAppsCustomizePage(container, pageIndex + 1);
                    if (newParent != null) {
                        child = newParent.getChildAt(0);
                    }
                } else {
                    child = parent.getChildAt(widgetCount - 1);
                }
                if (child != null)
                    child.requestFocus();
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_MOVE_HOME:
            if (handleKeyEvent) {
                // Select the first item on this page
                child = parent.getChildAt(0);
                if (child != null)
                    child.requestFocus();
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_MOVE_END:
            if (handleKeyEvent) {
                // Select the last item on this page
                parent.getChildAt(widgetCount - 1).requestFocus();
            }
            wasHandled = true;
            break;
        default:
            break;
    }
    return wasHandled;
}
Also used : TabHost(android.widget.TabHost) ViewGroup(android.view.ViewGroup) TabWidget(android.widget.TabWidget) View(android.view.View)

Example 37 with TabWidget

use of android.widget.TabWidget in project Fairphone by Kwamecorp.

the class FocusHelper method handleAppsCustomizeKeyEvent.

/**
 * Handles key events in a PageViewCellLayout containing PagedViewIcons.
 */
static boolean handleAppsCustomizeKeyEvent(View v, int keyCode, KeyEvent e) {
    ViewGroup parentLayout;
    ViewGroup itemContainer;
    int countX;
    int countY;
    if (v.getParent() instanceof PagedViewCellLayoutChildren) {
        itemContainer = (ViewGroup) v.getParent();
        parentLayout = (ViewGroup) itemContainer.getParent();
        countX = ((PagedViewCellLayout) parentLayout).getCellCountX();
        countY = ((PagedViewCellLayout) parentLayout).getCellCountY();
    } else {
        itemContainer = parentLayout = (ViewGroup) v.getParent();
        countX = ((PagedViewGridLayout) parentLayout).getCellCountX();
        countY = ((PagedViewGridLayout) parentLayout).getCellCountY();
    }
    // Note we have an extra parent because of the
    // PagedViewCellLayout/PagedViewCellLayoutChildren relationship
    final PagedView container = (PagedView) parentLayout.getParent();
    final TabHost tabHost = findTabHostParent(container);
    final TabWidget tabs = tabHost.getTabWidget();
    final int iconIndex = itemContainer.indexOfChild(v);
    final int itemCount = itemContainer.getChildCount();
    final int pageIndex = ((PagedView) container).indexToPage(container.indexOfChild(parentLayout));
    final int pageCount = container.getChildCount();
    final int x = iconIndex % countX;
    final int y = iconIndex / countX;
    final int action = e.getAction();
    final boolean handleKeyEvent = (action != KeyEvent.ACTION_UP);
    ViewGroup newParent = null;
    // Side pages do not always load synchronously, so check before focusing child siblings
    // willy-nilly
    View child = null;
    boolean wasHandled = false;
    switch(keyCode) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (handleKeyEvent) {
                // Select the previous icon or the last icon on the previous page
                if (iconIndex > 0) {
                    itemContainer.getChildAt(iconIndex - 1).requestFocus();
                } else {
                    if (pageIndex > 0) {
                        newParent = getAppsCustomizePage(container, pageIndex - 1);
                        if (newParent != null) {
                            container.snapToPage(pageIndex - 1);
                            child = newParent.getChildAt(newParent.getChildCount() - 1);
                            if (child != null)
                                child.requestFocus();
                        }
                    }
                }
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            if (handleKeyEvent) {
                // Select the next icon or the first icon on the next page
                if (iconIndex < (itemCount - 1)) {
                    itemContainer.getChildAt(iconIndex + 1).requestFocus();
                } else {
                    if (pageIndex < (pageCount - 1)) {
                        newParent = getAppsCustomizePage(container, pageIndex + 1);
                        if (newParent != null) {
                            container.snapToPage(pageIndex + 1);
                            child = newParent.getChildAt(0);
                            if (child != null)
                                child.requestFocus();
                        }
                    }
                }
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_DPAD_UP:
            if (handleKeyEvent) {
                // Select the closest icon in the previous row, otherwise select the tab bar
                if (y > 0) {
                    int newiconIndex = ((y - 1) * countX) + x;
                    itemContainer.getChildAt(newiconIndex).requestFocus();
                } else {
                    tabs.requestFocus();
                }
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            if (handleKeyEvent) {
                // Select the closest icon in the previous row, otherwise do nothing
                if (y < (countY - 1)) {
                    int newiconIndex = Math.min(itemCount - 1, ((y + 1) * countX) + x);
                    itemContainer.getChildAt(newiconIndex).requestFocus();
                }
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_ENTER:
        case KeyEvent.KEYCODE_DPAD_CENTER:
            if (handleKeyEvent) {
                // Simulate a click on the icon
                View.OnClickListener clickListener = (View.OnClickListener) container;
                clickListener.onClick(v);
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_PAGE_UP:
            if (handleKeyEvent) {
                // if there is no previous page
                if (pageIndex > 0) {
                    newParent = getAppsCustomizePage(container, pageIndex - 1);
                    if (newParent != null) {
                        container.snapToPage(pageIndex - 1);
                        child = newParent.getChildAt(0);
                        if (child != null)
                            child.requestFocus();
                    }
                } else {
                    itemContainer.getChildAt(0).requestFocus();
                }
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_PAGE_DOWN:
            if (handleKeyEvent) {
                // if there is no next page
                if (pageIndex < (pageCount - 1)) {
                    newParent = getAppsCustomizePage(container, pageIndex + 1);
                    if (newParent != null) {
                        container.snapToPage(pageIndex + 1);
                        child = newParent.getChildAt(0);
                        if (child != null)
                            child.requestFocus();
                    }
                } else {
                    itemContainer.getChildAt(itemCount - 1).requestFocus();
                }
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_MOVE_HOME:
            if (handleKeyEvent) {
                // Select the first icon on this page
                itemContainer.getChildAt(0).requestFocus();
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_MOVE_END:
            if (handleKeyEvent) {
                // Select the last icon on this page
                itemContainer.getChildAt(itemCount - 1).requestFocus();
            }
            wasHandled = true;
            break;
        default:
            break;
    }
    return wasHandled;
}
Also used : TabHost(android.widget.TabHost) ViewGroup(android.view.ViewGroup) TabWidget(android.widget.TabWidget) View(android.view.View)

Aggregations

TabWidget (android.widget.TabWidget)37 View (android.view.View)21 FrameLayout (android.widget.FrameLayout)16 TabHost (android.widget.TabHost)15 LinearLayout (android.widget.LinearLayout)14 ViewGroup (android.view.ViewGroup)13 ListView (android.widget.ListView)12 TextView (android.widget.TextView)12 AdapterView (android.widget.AdapterView)9 AbsListView (android.widget.AbsListView)7 ExpandableListView (android.widget.ExpandableListView)6 ImageView (android.widget.ImageView)6 TabSpec (android.widget.TabHost.TabSpec)6 ResourceType (com.android.resources.ResourceType)6 ActionMenuView (android.widget.ActionMenuView)5 ActionMenuItemView (com.android.internal.view.menu.ActionMenuItemView)5 IconMenuItemView (com.android.internal.view.menu.IconMenuItemView)5 ListMenuItemView (com.android.internal.view.menu.ListMenuItemView)5 MenuView (com.android.internal.view.menu.MenuView)5 SuppressLint (android.annotation.SuppressLint)3