Merge "Use expandable list for phone history"
diff --git a/res/layout-xlarge/history.xml b/res/layout-xlarge/history.xml
new file mode 100644
index 0000000..2d949e1
--- /dev/null
+++ b/res/layout-xlarge/history.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2011 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+>
+
+    <ViewStub
+        android:id="@+id/pref_stub"
+        android:layout_height="match_parent"
+        android:layout_width="match_parent"
+        android:inflatedId="@+id/history" />
+
+    <TextView android:id="@android:id/empty"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center"
+        android:textAppearance="?android:attr/textAppearanceLarge"
+        android:text="@string/empty_history"
+        android:visibility="gone"
+    />
+
+</FrameLayout>
diff --git a/res/layout/history.xml b/res/layout/history.xml
index f7d2c7c..f3adb51 100644
--- a/res/layout/history.xml
+++ b/res/layout/history.xml
@@ -19,11 +19,10 @@
     android:layout_height="match_parent"
 >
 
-    <ViewStub
-        android:id="@+id/pref_stub"
+    <ExpandableListView
+        android:id="@+id/history"
         android:layout_height="match_parent"
-        android:layout_width="match_parent"
-        android:inflatedId="@+id/history" />
+        android:layout_width="match_parent" />
 
     <TextView android:id="@android:id/empty"
         android:layout_width="wrap_content"
diff --git a/src/com/android/browser/BrowserHistoryPage.java b/src/com/android/browser/BrowserHistoryPage.java
index 44f358d..2dcda68 100644
--- a/src/com/android/browser/BrowserHistoryPage.java
+++ b/src/com/android/browser/BrowserHistoryPage.java
@@ -59,6 +59,9 @@
 import android.widget.AdapterView.AdapterContextMenuInfo;
 import android.widget.AdapterView.OnItemClickListener;
 import android.widget.BaseAdapter;
+import android.widget.ExpandableListView;
+import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
+import android.widget.ExpandableListView.OnChildClickListener;
 import android.widget.ListView;
 import android.widget.TextView;
 import android.widget.Toast;
@@ -68,7 +71,7 @@
  * days of viewing.
  */
 public class BrowserHistoryPage extends Fragment
-        implements LoaderCallbacks<Cursor> {
+        implements LoaderCallbacks<Cursor>, OnChildClickListener {
 
     static final int LOADER_HISTORY = 1;
     static final int LOADER_MOST_VISITED = 2;
@@ -82,6 +85,7 @@
     ListView mGroupList, mChildList;
     private ViewGroup mPrefsContainer;
     private FragmentBreadCrumbs mFragmentBreadCrumbs;
+    private ExpandableListView mHistoryList;
 
     // Implementation of WebIconDatabase.IconListener
     class IconReceiver implements IconListener {
@@ -187,7 +191,7 @@
         switch (loader.getId()) {
             case LOADER_HISTORY: {
                 mAdapter.changeCursor(data);
-                if (!mAdapter.isEmpty()
+                if (!mAdapter.isEmpty() && mGroupList != null
                         && mGroupList.getCheckedItemPosition() == ListView.INVALID_POSITION) {
                     selectGroup(0);
                 }
@@ -229,7 +233,31 @@
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
         mRoot = inflater.inflate(R.layout.history, container, false);
+        mAdapter = new HistoryAdapter(getActivity());
         ViewStub stub = (ViewStub) mRoot.findViewById(R.id.pref_stub);
+        if (stub != null) {
+            inflateTwoPane(stub);
+        } else {
+            inflateSinglePane();
+        }
+
+        // Start the loaders
+        getLoaderManager().restartLoader(LOADER_HISTORY, null, this);
+        getLoaderManager().restartLoader(LOADER_MOST_VISITED, null, this);
+
+        // Register to receive icons in case they haven't all been loaded.
+        CombinedBookmarkHistoryView.getIconListenerSet().addListener(mIconReceiver);
+        return mRoot;
+    }
+
+    private void inflateSinglePane() {
+        mHistoryList = (ExpandableListView) mRoot.findViewById(R.id.history);
+        mHistoryList.setAdapter(mAdapter);
+        mHistoryList.setOnChildClickListener(this);
+        registerForContextMenu(mHistoryList);
+    }
+
+    private void inflateTwoPane(ViewStub stub) {
         stub.setLayoutResource(com.android.internal.R.layout.preference_list_content);
         stub.inflate();
         mGroupList = (ListView) mRoot.findViewById(android.R.id.list);
@@ -238,7 +266,6 @@
         mFragmentBreadCrumbs.setMaxVisible(1);
         mFragmentBreadCrumbs.setActivity(getActivity());
         mPrefsContainer.setVisibility(View.VISIBLE);
-        mAdapter = new HistoryAdapter(getActivity());
         mGroupList.setAdapter(new HistoryGroupWrapper(mAdapter));
         mGroupList.setOnItemClickListener(mGroupItemClickListener);
         mGroupList.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
@@ -249,14 +276,6 @@
         registerForContextMenu(mChildList);
         ViewGroup prefs = (ViewGroup) mRoot.findViewById(com.android.internal.R.id.prefs);
         prefs.addView(mChildList);
-
-        // Start the loaders
-        getLoaderManager().restartLoader(LOADER_HISTORY, null, this);
-        getLoaderManager().restartLoader(LOADER_MOST_VISITED, null, this);
-
-        // Register to receive icons in case they haven't all been loaded.
-        CombinedBookmarkHistoryView.getIconListenerSet().addListener(mIconReceiver);
-        return mRoot;
     }
 
     private OnItemClickListener mGroupItemClickListener = new OnItemClickListener() {
@@ -279,6 +298,13 @@
     };
 
     @Override
+    public boolean onChildClick(ExpandableListView parent, View view,
+            int groupPosition, int childPosition, long id) {
+        mCallbacks.onUrlSelected(((HistoryItem) view).getUrl(), false);
+        return true;
+    }
+
+    @Override
     public void onDestroy() {
         super.onDestroy();
         CombinedBookmarkHistoryView.getIconListenerSet().removeListener(mIconReceiver);
@@ -341,17 +367,30 @@
         }
     }
 
+    View getTargetView(ContextMenuInfo menuInfo) {
+        if (menuInfo instanceof AdapterContextMenuInfo) {
+            return ((AdapterContextMenuInfo) menuInfo).targetView;
+        }
+        if (menuInfo instanceof ExpandableListContextMenuInfo) {
+            return ((ExpandableListContextMenuInfo) menuInfo).targetView;
+        }
+        return null;
+    }
+
     @Override
     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
-        AdapterContextMenuInfo i = (AdapterContextMenuInfo) menuInfo;
+
+        View targetView = getTargetView(menuInfo);
+        if (!(targetView instanceof HistoryItem)) {
+            return;
+        }
+        HistoryItem historyItem = (HistoryItem) targetView;
 
         // Inflate the menu
         Activity parent = getActivity();
         MenuInflater inflater = parent.getMenuInflater();
         inflater.inflate(R.menu.historycontext, menu);
 
-        HistoryItem historyItem = (HistoryItem) i.targetView;
-
         // Setup the header
         if (mContextHeader == null) {
             mContextHeader = new HistoryItem(parent, false);
@@ -382,12 +421,11 @@
 
     @Override
     public boolean onContextItemSelected(MenuItem item) {
-        AdapterContextMenuInfo i =
-            (AdapterContextMenuInfo) item.getMenuInfo();
-        if (i == null) {
+        ContextMenuInfo menuInfo = item.getMenuInfo();
+        if (menuInfo == null) {
             return false;
         }
-        HistoryItem historyItem = (HistoryItem) i.targetView;
+        HistoryItem historyItem = (HistoryItem) getTargetView(menuInfo);
         String url = historyItem.getUrl();
         String title = historyItem.getName();
         Activity activity = getActivity();