Changed WebsiteSettingsActivity to fragment

 Bug: 3231307
 Moved WebsiteSettingsActivity to a fragment

Change-Id: If1fa56ab48167c004fb65c756f325e108f14fe8f
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index b653461..aa7aa72 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -168,10 +168,6 @@
                   android:configChanges="orientation|keyboardHidden">
         </activity>
 
-        <activity android:name="WebsiteSettingsActivity" android:label=""
-                  android:configChanges="orientation|keyboardHidden">
-        </activity>
-
         <activity android:name="BookmarkSearch"
                   android:label="@string/bookmarks_search"
                   android:stateNotNeeded="true"
diff --git a/res/layout/website_settings.xml b/res/layout/website_settings.xml
new file mode 100644
index 0000000..ed95148
--- /dev/null
+++ b/res/layout/website_settings.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+** Copyright 2010, 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.
+*/
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical"
+    android:layout_height="match_parent"
+    android:layout_width="match_parent"
+    android:background="@android:color/transparent">
+
+    <ListView android:id="@android:id/list"
+        android:layout_width="match_parent"
+        android:layout_height="0px"
+        android:layout_weight="1"
+        android:clipToPadding="false"
+        android:drawSelectorOnTop="false"
+        android:cacheColorHint="@android:color/transparent"
+        android:scrollbarAlwaysDrawVerticalTrack="true" />
+
+    <Button android:id="@+id/clear_all_button"
+            android:layout_width="150dip"
+            android:layout_height="wrap_content"
+            android:layout_margin="5dip"
+            android:text="@string/website_settings_clear_all"
+            android:visibility="gone" />
+</LinearLayout>
diff --git a/res/menu/websitesettings.xml b/res/menu/websitesettings.xml
deleted file mode 100644
index 5acc8a5..0000000
--- a/res/menu/websitesettings.xml
+++ /dev/null
@@ -1,21 +0,0 @@
-<!-- Copyright (C) 2009 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.
--->
-
-<menu xmlns:android="http://schemas.android.com/apk/res/android">
-    <item android:id="@+id/website_settings_menu_clear_all"
-          android:title="@string/website_settings_clear_all"
-          android:icon="@android:drawable/ic_menu_close_clear_cancel" />
-</menu>
-
diff --git a/src/com/android/browser/WebStorageSizeManager.java b/src/com/android/browser/WebStorageSizeManager.java
index dcf2f8b..5f76f72 100644
--- a/src/com/android/browser/WebStorageSizeManager.java
+++ b/src/com/android/browser/WebStorageSizeManager.java
@@ -16,17 +16,19 @@
 
 package com.android.browser;
 
+import com.android.browser.preferences.WebsiteSettingsFragment;
+
 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
 import android.os.StatFs;
+import android.preference.PreferenceActivity;
 import android.util.Log;
 import android.webkit.WebStorage;
 
 import java.io.File;
-import java.util.Set;
 
 
 /**
@@ -82,7 +84,7 @@
  * the user can free some of the Web storage space by deleting all the data used
  * by an origin.
  */
-class WebStorageSizeManager {
+public class WebStorageSizeManager {
     // Logging flags.
     private final static boolean LOGV_ENABLED = com.android.browser.Browser.LOGV_ENABLED;
     private final static boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
@@ -346,7 +348,7 @@
     // Reset the notification time; we use this iff the user
     // use clear all; we reset it to some time in the future instead
     // of just setting it to -1, as the clear all method is asynchronous
-    static void resetLastOutOfSpaceNotificationTime() {
+    public static void resetLastOutOfSpaceNotificationTime() {
         mLastOutOfSpaceNotificationTime = System.currentTimeMillis() -
             NOTIFICATION_INTERVAL + RESET_NOTIFICATION_INTERVAL;
     }
@@ -403,7 +405,9 @@
             CharSequence text = mContext.getString(
                     R.string.webstorage_outofspace_notification_text);
             long when = System.currentTimeMillis();
-            Intent intent = new Intent(mContext, WebsiteSettingsActivity.class);
+            Intent intent = new Intent(mContext, BrowserPreferencesPage.class);
+            intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
+                    WebsiteSettingsFragment.class.getName());
             PendingIntent contentIntent =
                 PendingIntent.getActivity(mContext, 0, intent, 0);
             Notification notification = new Notification(icon, title, when);
diff --git a/src/com/android/browser/preferences/AdvancedPreferencesFragment.java b/src/com/android/browser/preferences/AdvancedPreferencesFragment.java
index 59b6ce1..952e04a 100644
--- a/src/com/android/browser/preferences/AdvancedPreferencesFragment.java
+++ b/src/com/android/browser/preferences/AdvancedPreferencesFragment.java
@@ -18,9 +18,7 @@
 
 import com.android.browser.BrowserSettings;
 import com.android.browser.R;
-import com.android.browser.WebsiteSettingsActivity;
 
-import android.content.Intent;
 import android.os.Bundle;
 import android.preference.Preference;
 import android.preference.PreferenceFragment;
@@ -44,8 +42,7 @@
 
         PreferenceScreen websiteSettings = (PreferenceScreen) findPreference(
                 BrowserSettings.PREF_WEBSITE_SETTINGS);
-        Intent intent = new Intent(getActivity(), WebsiteSettingsActivity.class);
-        websiteSettings.setIntent(intent);
+        websiteSettings.setFragment(WebsiteSettingsFragment.class.getName());
     }
 
     /*
diff --git a/src/com/android/browser/WebsiteSettingsActivity.java b/src/com/android/browser/preferences/WebsiteSettingsFragment.java
similarity index 86%
rename from src/com/android/browser/WebsiteSettingsActivity.java
rename to src/com/android/browser/preferences/WebsiteSettingsFragment.java
index 95f8fb0..1965ffe 100644
--- a/src/com/android/browser/WebsiteSettingsActivity.java
+++ b/src/com/android/browser/preferences/WebsiteSettingsFragment.java
@@ -14,56 +14,62 @@
  * limitations under the License.
  */
 
-package com.android.browser;
+package com.android.browser.preferences;
+
+import com.android.browser.R;
+import com.android.browser.WebStorageSizeManager;
 
 import android.app.AlertDialog;
-import android.app.ListActivity;
+import android.app.FragmentTransaction;
+import android.app.ListFragment;
 import android.content.Context;
 import android.content.DialogInterface;
+import android.content.res.Resources;
 import android.database.Cursor;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.net.Uri;
 import android.os.AsyncTask;
 import android.os.Bundle;
+import android.preference.PreferenceActivity;
+import android.preference.PreferenceFragment;
 import android.provider.BrowserContract.Bookmarks;
 import android.util.Log;
-import android.view.KeyEvent;
 import android.view.LayoutInflater;
-import android.view.Menu;
-import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
+import android.view.View.OnClickListener;
 import android.view.ViewGroup;
 import android.webkit.GeolocationPermissions;
 import android.webkit.ValueCallback;
-import android.webkit.WebIconDatabase;
 import android.webkit.WebStorage;
-import android.widget.ArrayAdapter;
 import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemClickListener;
+import android.widget.ArrayAdapter;
 import android.widget.ImageView;
 import android.widget.TextView;
 
+import java.io.Serializable;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
-import java.util.Vector;
 
 /**
  * Manage the settings for an origin.
  * We use it to keep track of the 'HTML5' settings, i.e. database (webstorage)
  * and Geolocation.
  */
-public class WebsiteSettingsActivity extends ListActivity {
+public class WebsiteSettingsFragment extends ListFragment implements OnClickListener {
 
+    private static final String EXTRA_SITE = "site";
     private String LOGTAG = "WebsiteSettingsActivity";
     private static String sMBStored = null;
     private SiteAdapter mAdapter = null;
+    private Site mSite = null;
 
-    static class Site {
+    static class Site implements Serializable {
         private String mOrigin;
         private String mTitle;
         private Bitmap mIcon;
@@ -169,6 +175,10 @@
         private Site mCurrentSite;
 
         public SiteAdapter(Context context, int rsc) {
+            this(context, rsc, null);
+        }
+
+        public SiteAdapter(Context context, int rsc, Site site) {
             super(context, rsc);
             mResource = rsc;
             mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@@ -184,7 +194,10 @@
                     R.drawable.ic_list_gps_on);
             mLocationDisallowedIcon = BitmapFactory.decodeResource(getResources(),
                     R.drawable.ic_list_gps_denied);
-            askForOrigins();
+            mCurrentSite = site;
+            if (mCurrentSite == null) {
+                askForOrigins();
+            }
         }
 
         /**
@@ -409,6 +422,7 @@
             }
         }
 
+        @Override
         public View getView(int position, View convertView, ViewGroup parent) {
             View view;
             final TextView title;
@@ -434,7 +448,6 @@
             locationIcon.setVisibility(View.GONE);
 
             if (mCurrentSite == null) {
-                setTitle(getString(R.string.pref_extras_website_settings));
 
                 Site site = getItem(position);
                 title.setText(site.getPrettyTitle());
@@ -494,7 +507,6 @@
                 locationIcon.setVisibility(View.GONE);
                 usageIcon.setVisibility(View.GONE);
                 featureIcon.setVisibility(View.VISIBLE);
-                setTitle(mCurrentSite.getPrettyTitle());
                 String origin = mCurrentSite.getOrigin();
                 switch (mCurrentSite.getFeatureByIndex(position)) {
                     case Site.FEATURE_WEB_STORAGE:
@@ -551,7 +563,7 @@
                                     // origins list.
                                     mCurrentSite.removeFeature(Site.FEATURE_WEB_STORAGE);
                                     if (mCurrentSite.getFeatureCount() == 0) {
-                                        mCurrentSite = null;
+                                        finish();
                                     }
                                     askForOrigins();
                                     notifyDataSetChanged();
@@ -570,7 +582,7 @@
                                     GeolocationPermissions.getInstance().clear(mCurrentSite.getOrigin());
                                     mCurrentSite.removeFeature(Site.FEATURE_GEOLOCATION);
                                     if (mCurrentSite.getFeatureCount() == 0) {
-                                        mCurrentSite = null;
+                                        finish();
                                     }
                                     askForOrigins();
                                     notifyDataSetChanged();
@@ -581,8 +593,14 @@
                         break;
                 }
             } else {
-                mCurrentSite = (Site) view.getTag();
-                notifyDataSetChanged();
+                Site site = (Site) view.getTag();
+                PreferenceActivity activity = (PreferenceActivity) getActivity();
+                if (activity != null) {
+                    Bundle args = new Bundle();
+                    args.putSerializable(EXTRA_SITE, site);
+                    activity.startPreferencePanel(WebsiteSettingsFragment.class.getName(), args, 0,
+                            site.getPrettyTitle(), null, 0);
+                }
             }
         }
 
@@ -591,67 +609,64 @@
         }
     }
 
-    /**
-     * Intercepts the back key to immediately notify
-     * NativeDialog that we are done.
-     */
-    public boolean dispatchKeyEvent(KeyEvent event) {
-        if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
-            && (event.getAction() == KeyEvent.ACTION_DOWN)) {
-            if ((mAdapter != null) && (mAdapter.backKeyPressed())){
-                return true; // event consumed
-            }
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+            Bundle savedInstanceState) {
+        View view = inflater.inflate(R.layout.website_settings, container, false);
+        Bundle args = getArguments();
+        if (args != null) {
+            mSite = (Site) args.getSerializable(EXTRA_SITE);
         }
-        return super.dispatchKeyEvent(event);
+        if (mSite == null) {
+            View clear = view.findViewById(R.id.clear_all_button);
+            clear.setVisibility(View.VISIBLE);
+            clear.setOnClickListener(this);
+        }
+        return view;
     }
 
     @Override
-    protected void onCreate(Bundle icicle) {
-        super.onCreate(icicle);
+    public void onActivityCreated(Bundle savedInstanceState) {
+        super.onActivityCreated(savedInstanceState);
         if (sMBStored == null) {
             sMBStored = getString(R.string.webstorage_origin_summary_mb_stored);
         }
-        mAdapter = new SiteAdapter(this, R.layout.website_settings_row);
-        setListAdapter(mAdapter);
+        mAdapter = new SiteAdapter(getActivity(), R.layout.website_settings_row);
+        if (mSite != null) {
+            mAdapter.mCurrentSite = mSite;
+        }
+        getListView().setAdapter(mAdapter);
         getListView().setOnItemClickListener(mAdapter);
     }
 
-    @Override
-    public boolean onCreateOptionsMenu(Menu menu) {
-        MenuInflater inflater = getMenuInflater();
-        inflater.inflate(R.menu.websitesettings, menu);
-        return true;
-    }
-
-    @Override
-    public boolean onPrepareOptionsMenu(Menu menu) {
-        // If we are not on the sites list (rather on the page for a specific site) or
-        // we aren't listing any sites hide the clear all button (and hence the menu).
-        return  mAdapter.currentSite() == null && mAdapter.getCount() > 0;
-    }
-
-    @Override
-    public boolean onOptionsItemSelected(MenuItem item) {
-        switch (item.getItemId()) {
-            case R.id.website_settings_menu_clear_all:
-                // Show the prompt to clear all origins of their data and geolocation permissions.
-                new AlertDialog.Builder(this)
-                        .setTitle(R.string.website_settings_clear_all_dialog_title)
-                        .setMessage(R.string.website_settings_clear_all_dialog_message)
-                        .setPositiveButton(R.string.website_settings_clear_all_dialog_ok_button,
-                                new AlertDialog.OnClickListener() {
-                                    public void onClick(DialogInterface dlg, int which) {
-                                        WebStorage.getInstance().deleteAllData();
-                                        GeolocationPermissions.getInstance().clearAll();
-                                        WebStorageSizeManager.resetLastOutOfSpaceNotificationTime();
-                                        mAdapter.askForOrigins();
-                                        finish();
-                                    }})
-                        .setNegativeButton(R.string.website_settings_clear_all_dialog_cancel_button, null)
-                        .setIcon(android.R.drawable.ic_dialog_alert)
-                        .show();
-                return true;
+    private void finish() {
+        PreferenceActivity activity = (PreferenceActivity) getActivity();
+        if (activity != null) {
+            activity.finishPreferencePanel(this, 0, null);
         }
-        return false;
+    }
+
+    @Override
+    public void onClick(View v) {
+        switch (v.getId()) {
+        case R.id.clear_all_button:
+         // Show the prompt to clear all origins of their data and geolocation permissions.
+            new AlertDialog.Builder(getActivity())
+                    .setTitle(R.string.website_settings_clear_all_dialog_title)
+                    .setMessage(R.string.website_settings_clear_all_dialog_message)
+                    .setPositiveButton(R.string.website_settings_clear_all_dialog_ok_button,
+                            new AlertDialog.OnClickListener() {
+                                public void onClick(DialogInterface dlg, int which) {
+                                    WebStorage.getInstance().deleteAllData();
+                                    GeolocationPermissions.getInstance().clearAll();
+                                    WebStorageSizeManager.resetLastOutOfSpaceNotificationTime();
+                                    mAdapter.askForOrigins();
+                                    finish();
+                                }})
+                    .setNegativeButton(R.string.website_settings_clear_all_dialog_cancel_button, null)
+                    .setIcon(android.R.drawable.ic_dialog_alert)
+                    .show();
+            break;
+        }
     }
 }