Bug 968179 - Fix crasher when using grid views in dynamic panels (r=margaret)

This commit is contained in:
Lucas Rocha 2014-02-05 16:58:55 +00:00
parent 576828e470
commit 9fb6f3544e

View File

@ -6,8 +6,11 @@
package org.mozilla.gecko.home; package org.mozilla.gecko.home;
import org.mozilla.gecko.R; import org.mozilla.gecko.R;
import org.mozilla.gecko.db.BrowserContract.HomeItems;
import org.mozilla.gecko.home.HomeConfig.ViewConfig; 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.DatasetBacked;
import org.mozilla.gecko.home.PanelLayout.PanelView;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
@ -15,18 +18,30 @@ import android.support.v4.widget.CursorAdapter;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView; 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 static final String LOGTAG = "GeckoPanelGridView";
private final PanelGridViewAdapter mAdapter; private final PanelGridViewAdapter mAdapter;
protected OnUrlOpenListener mUrlOpenListener;
public PanelGridView(Context context, ViewConfig viewConfig) { public PanelGridView(Context context, ViewConfig viewConfig) {
super(context, null, R.attr.panelGridViewStyle); super(context, null, R.attr.panelGridViewStyle);
mAdapter = new PanelGridViewAdapter(context); mAdapter = new PanelGridViewAdapter(context);
setAdapter(mAdapter); setAdapter(mAdapter);
setNumColumns(AUTO_FIT); setNumColumns(AUTO_FIT);
setOnItemClickListener(new PanelGridItemClickListener());
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
mUrlOpenListener = null;
} }
@Override @Override
@ -34,6 +49,11 @@ public class PanelGridView extends GridView implements DatasetBacked {
mAdapter.swapCursor(cursor); mAdapter.swapCursor(cursor);
} }
@Override
public void setOnUrlOpenListener(OnUrlOpenListener listener) {
mUrlOpenListener = listener;
}
private class PanelGridViewAdapter extends CursorAdapter { private class PanelGridViewAdapter extends CursorAdapter {
public PanelGridViewAdapter(Context context) { public PanelGridViewAdapter(Context context) {
@ -51,4 +71,19 @@ public class PanelGridView extends GridView implements DatasetBacked {
return new PanelGridItemView(context); 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));
}
}
} }