Bug 968179 - Fix crasher when using grid views in dynamic panels (r=margaret)
authorLucas Rocha <lucasr@mozilla.com>
Wed, 05 Feb 2014 16:58:55 +0000
changeset 178014 4aee7615f1a4f80c28c296f880d496d0f604b54c
parent 178013 eae47207061055885e5817650e068c1392f39b4e
child 178015 598eff91dfdcd38cc8aae1bdd05484ded9f74f0c
push id5439
push userffxbld
push dateMon, 17 Mar 2014 23:08:15 +0000
treeherdermozilla-aurora@c0befb3c8038 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret
bugs968179
milestone30.0a1
Bug 968179 - Fix crasher when using grid views in dynamic panels (r=margaret)
mobile/android/base/home/PanelGridView.java
--- a/mobile/android/base/home/PanelGridView.java
+++ b/mobile/android/base/home/PanelGridView.java
@@ -1,44 +1,64 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.home;
 
 import org.mozilla.gecko.R;
+import org.mozilla.gecko.db.BrowserContract.HomeItems;
 import org.mozilla.gecko.home.HomeConfig.ViewConfig;
+import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
 import org.mozilla.gecko.home.PanelLayout.DatasetBacked;
+import org.mozilla.gecko.home.PanelLayout.PanelView;
 
 import android.content.Context;
 import android.database.Cursor;
 import android.support.v4.widget.CursorAdapter;
 import android.util.Log;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.AdapterView;
 import android.widget.GridView;
 
-public class PanelGridView extends GridView implements DatasetBacked {
+import java.util.EnumSet;
+
+public class PanelGridView extends GridView
+                           implements DatasetBacked, PanelView {
     private static final String LOGTAG = "GeckoPanelGridView";
 
     private final PanelGridViewAdapter mAdapter;
+    protected OnUrlOpenListener mUrlOpenListener;
 
     public PanelGridView(Context context, ViewConfig viewConfig) {
         super(context, null, R.attr.panelGridViewStyle);
         mAdapter = new PanelGridViewAdapter(context);
         setAdapter(mAdapter);
         setNumColumns(AUTO_FIT);
+        setOnItemClickListener(new PanelGridItemClickListener());
+    }
+
+    @Override
+    public void onDetachedFromWindow() {
+        super.onDetachedFromWindow();
+        mUrlOpenListener = null;
     }
 
     @Override
     public void setDataset(Cursor cursor) {
         mAdapter.swapCursor(cursor);
     }
 
+    @Override
+    public void setOnUrlOpenListener(OnUrlOpenListener listener) {
+        mUrlOpenListener = listener;
+    }
+
     private class PanelGridViewAdapter extends CursorAdapter {
 
         public PanelGridViewAdapter(Context context) {
             super(context, null, 0);
         }
 
         @Override
         public void bindView(View bindView, Context context, Cursor cursor) {
@@ -46,9 +66,24 @@ public class PanelGridView extends GridV
             item.updateFromCursor(cursor);
         }
 
         @Override
         public View newView(Context context, Cursor cursor, ViewGroup parent) {
             return new PanelGridItemView(context);
         }
     }
+
+    private class PanelGridItemClickListener implements AdapterView.OnItemClickListener {
+        @Override
+        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+            Cursor cursor = mAdapter.getCursor();
+            if (cursor == null || !cursor.moveToPosition(position)) {
+                throw new IllegalStateException("Couldn't move cursor to position " + position);
+            }
+
+            int urlIndex = cursor.getColumnIndexOrThrow(HomeItems.URL);
+            final String url = cursor.getString(urlIndex);
+
+            mUrlOpenListener.onUrlOpen(url, EnumSet.of(OnUrlOpenListener.Flags.OPEN_WITH_INTENT));
+        }
+    }
 }