mobile/android/base/home/HomeListView.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     2  * This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 package org.mozilla.gecko.home;
     8 import org.mozilla.gecko.R;
     9 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
    11 import android.content.Context;
    12 import android.content.res.TypedArray;
    13 import android.database.Cursor;
    14 import android.graphics.drawable.Drawable;
    15 import android.util.AttributeSet;
    16 import android.view.ContextMenu.ContextMenuInfo;
    17 import android.view.View;
    18 import android.widget.AdapterView;
    19 import android.widget.AdapterView.OnItemLongClickListener;
    20 import android.widget.ListView;
    22 /**
    23  * HomeListView is a custom extension of ListView, that packs a HomeContextMenuInfo
    24  * when any of its rows is long pressed.
    25  */
    26 public class HomeListView extends ListView
    27                           implements OnItemLongClickListener {
    29     // ContextMenuInfo associated with the currently long pressed list item.
    30     private HomeContextMenuInfo mContextMenuInfo;
    32     // On URL open listener
    33     protected OnUrlOpenListener mUrlOpenListener;
    35     // Top divider
    36     private boolean mShowTopDivider;
    38     // ContextMenuInfo maker
    39     private HomeContextMenuInfo.Factory mContextMenuInfoFactory;
    41     public HomeListView(Context context) {
    42         this(context, null);
    43     }
    45     public HomeListView(Context context, AttributeSet attrs) {
    46         this(context, attrs, R.attr.homeListViewStyle);
    47     }
    49     public HomeListView(Context context, AttributeSet attrs, int defStyle) {
    50         super(context, attrs, defStyle);
    52         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HomeListView, defStyle, 0);
    53         mShowTopDivider = a.getBoolean(R.styleable.HomeListView_topDivider, false);
    54         a.recycle();
    56         setOnItemLongClickListener(this);
    57     }
    59     @Override
    60     public void onAttachedToWindow() {
    61         super.onAttachedToWindow();
    63         final Drawable divider = getDivider();
    64         if (mShowTopDivider && divider != null) {
    65             final int dividerHeight = getDividerHeight();
    66             final View view = new View(getContext());
    67             view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, dividerHeight));
    68             addHeaderView(view);
    69         }
    70     }
    72     @Override
    73     public void onDetachedFromWindow() {
    74         super.onDetachedFromWindow();
    76         mUrlOpenListener = null;
    77     }
    79     @Override
    80     public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    81         Object item = parent.getItemAtPosition(position);
    83         // HomeListView could hold headers too. Add a context menu info only for its children.
    84         if (item instanceof Cursor) {
    85             Cursor cursor = (Cursor) item;
    86             if (cursor == null || mContextMenuInfoFactory == null) {
    87                 mContextMenuInfo = null;
    88                 return false;
    89             }
    91             mContextMenuInfo = mContextMenuInfoFactory.makeInfoForCursor(view, position, id, cursor);
    92             return showContextMenuForChild(HomeListView.this);
    94         } else {
    95             mContextMenuInfo = null;
    96             return false;
    97         }
    98     }
   100     @Override
   101     public ContextMenuInfo getContextMenuInfo() {
   102         return mContextMenuInfo;
   103     }
   105     @Override
   106     public void setOnItemClickListener(final AdapterView.OnItemClickListener listener) {
   107         if (listener == null) {
   108             super.setOnItemClickListener(null);
   109             return;
   110         }
   112         super.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   113             @Override
   114             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   115                 if (mShowTopDivider) {
   116                     position--;
   117                 }
   119                 listener.onItemClick(parent, view, position, id);
   120             }
   121         });
   122     }
   124     public void setContextMenuInfoFactory(final HomeContextMenuInfo.Factory factory) {
   125         mContextMenuInfoFactory = factory;
   126     }
   128     public OnUrlOpenListener getOnUrlOpenListener() {
   129         return mUrlOpenListener;
   130     }
   132     public void setOnUrlOpenListener(OnUrlOpenListener listener) {
   133         mUrlOpenListener = listener;
   134     }
   135 }

mercurial