Merge "Revisions to ResolverActivity" into jb-dev
diff --git a/core/java/com/android/internal/app/ResolverActivity.java b/core/java/com/android/internal/app/ResolverActivity.java
index 51bbdf1..7867efd 100644
--- a/core/java/com/android/internal/app/ResolverActivity.java
+++ b/core/java/com/android/internal/app/ResolverActivity.java
@@ -19,15 +19,17 @@
 import com.android.internal.R;
 import com.android.internal.content.PackageMonitor;
 
+import android.app.ActivityManager;
 import android.content.ComponentName;
 import android.content.Context;
-import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.ActivityInfo;
 import android.content.pm.LabeledIntent;
 import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
 import android.content.pm.ResolveInfo;
+import android.content.res.Resources;
 import android.graphics.drawable.Drawable;
 import android.net.Uri;
 import android.os.Bundle;
@@ -38,8 +40,8 @@
 import android.view.ViewGroup;
 import android.widget.AdapterView;
 import android.widget.BaseAdapter;
-import android.widget.CheckBox;
-import android.widget.CompoundButton;
+import android.widget.Button;
+import android.widget.GridView;
 import android.widget.ImageView;
 import android.widget.ListView;
 import android.widget.TextView;
@@ -56,12 +58,20 @@
  * which there is more than one matching activity, allowing the user to decide
  * which to go to.  It is not normally used directly by application developers.
  */
-public class ResolverActivity extends AlertActivity implements
-        DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener {
+public class ResolverActivity extends AlertActivity implements AdapterView.OnItemClickListener {
+    private static final String TAG = "ResolverActivity";
+
     private ResolveListAdapter mAdapter;
-    private CheckBox mAlwaysCheck;
-    private TextView mClearDefaultHint;
     private PackageManager mPm;
+    private boolean mAlwaysUseOption;
+    private boolean mShowExtended;
+    private GridView mGrid;
+    private Button mAlwaysButton;
+    private Button mOnceButton;
+    private int mIconDpi;
+    private int mIconSize;
+
+    private static final int MAX_COLUMNS = 4;
 
     private boolean mRegistered;
     private final PackageMonitor mPackageMonitor = new PackageMonitor() {
@@ -91,33 +101,37 @@
     protected void onCreate(Bundle savedInstanceState, Intent intent,
             CharSequence title, Intent[] initialIntents, List<ResolveInfo> rList,
             boolean alwaysUseOption) {
+        setTheme(R.style.Theme_DeviceDefault_Light_Dialog_Alert);
         super.onCreate(savedInstanceState);
         mPm = getPackageManager();
+        mAlwaysUseOption = alwaysUseOption;
         intent.setComponent(null);
 
         AlertController.AlertParams ap = mAlertParams;
 
         ap.mTitle = title;
-        ap.mOnClickListener = this;
 
         mPackageMonitor.register(this, getMainLooper(), false);
         mRegistered = true;
 
-        if (alwaysUseOption) {
-            LayoutInflater inflater = (LayoutInflater) getSystemService(
-                    Context.LAYOUT_INFLATER_SERVICE);
-            ap.mView = inflater.inflate(R.layout.always_use_checkbox, null);
-            mAlwaysCheck = (CheckBox)ap.mView.findViewById(com.android.internal.R.id.alwaysUse);
-            mAlwaysCheck.setText(R.string.alwaysUse);
-            mAlwaysCheck.setOnCheckedChangeListener(this);
-            mClearDefaultHint = (TextView)ap.mView.findViewById(
-                                                        com.android.internal.R.id.clearDefaultHint);
-            mClearDefaultHint.setVisibility(View.GONE);
-        }
+        final ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
+        mIconDpi = am.getLauncherLargeIconDensity();
+        mIconSize = am.getLauncherLargeIconSize();
+
         mAdapter = new ResolveListAdapter(this, intent, initialIntents, rList);
         int count = mAdapter.getCount();
         if (count > 1) {
-            ap.mAdapter = mAdapter;
+            ap.mView = getLayoutInflater().inflate(R.layout.resolver_grid, null);
+            mGrid = (GridView) ap.mView.findViewById(R.id.resolver_grid);
+            mGrid.setAdapter(mAdapter);
+            mGrid.setOnItemClickListener(this);
+            mGrid.setOnItemLongClickListener(new ItemLongClickListener());
+
+            if (alwaysUseOption) {
+                mGrid.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
+            }
+
+            resizeGrid();
         } else if (count == 1) {
             startActivity(mAdapter.intentForPosition(0));
             mPackageMonitor.unregister();
@@ -125,17 +139,57 @@
             finish();
             return;
         } else {
-            ap.mMessage = getResources().getText(com.android.internal.R.string.noApplications);
+            ap.mMessage = getResources().getText(R.string.noApplications);
         }
 
         setupAlert();
 
-        ListView lv = mAlert.getListView();
-        if (lv != null) {
-            lv.setOnItemLongClickListener(new ItemLongClickListener());
+        if (alwaysUseOption) {
+            final ViewGroup buttonLayout = (ViewGroup) findViewById(R.id.button_bar);
+            buttonLayout.setVisibility(View.VISIBLE);
+            mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always);
+            mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once);
         }
     }
 
+    void resizeGrid() {
+        final int itemCount = mAdapter.getCount();
+        mGrid.setNumColumns(Math.min(itemCount, MAX_COLUMNS));
+    }
+
+    Drawable getIcon(Resources res, int resId) {
+        Drawable result;
+        try {
+            result = res.getDrawableForDensity(resId, mIconDpi);
+        } catch (Resources.NotFoundException e) {
+            result = null;
+        }
+
+        return result;
+    }
+
+    Drawable loadIconForResolveInfo(ResolveInfo ri) {
+        Drawable dr;
+        try {
+            if (ri.resolvePackageName != null && ri.icon != 0) {
+                dr = getIcon(mPm.getResourcesForApplication(ri.resolvePackageName), ri.icon);
+                if (dr != null) {
+                    return dr;
+                }
+            }
+            final int iconRes = ri.getIconResource();
+            if (iconRes != 0) {
+                dr = getIcon(mPm.getResourcesForApplication(ri.activityInfo.packageName), iconRes);
+                if (dr != null) {
+                    return dr;
+                }
+            }
+        } catch (NameNotFoundException e) {
+            Log.e(TAG, "Couldn't find resources for package", e);
+        }
+        return ri.loadIcon(mPm);
+    }
+
     @Override
     protected void onRestart() {
         super.onRestart();
@@ -155,11 +209,28 @@
         }
     }
 
-    public void onClick(DialogInterface dialog, int which) {
+    @Override
+    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+        if (mAlwaysUseOption) {
+            final int checkedPos = mGrid.getCheckedItemPosition();
+            final boolean enabled = checkedPos != GridView.INVALID_POSITION;
+            mAlwaysButton.setEnabled(enabled);
+            mOnceButton.setEnabled(enabled);
+        } else {
+            startSelected(position, false);
+        }
+    }
+
+    public void onButtonClick(View v) {
+        final int id = v.getId();
+        startSelected(mGrid.getCheckedItemPosition(), id == R.id.button_always);
+        dismiss();
+    }
+
+    void startSelected(int which, boolean always) {
         ResolveInfo ri = mAdapter.resolveInfoForPosition(which);
         Intent intent = mAdapter.intentForPosition(which);
-        boolean alwaysCheck = (mAlwaysCheck != null && mAlwaysCheck.isChecked());
-        onIntentSelected(ri, intent, alwaysCheck);
+        onIntentSelected(ri, intent, always);
         finish();
     }
 
@@ -249,6 +320,12 @@
         }
     }
 
+    void showAppDetails(ResolveInfo ri) {
+        Intent in = new Intent().setAction("android.settings.APPLICATION_DETAILS_SETTINGS")
+                .setData(Uri.fromParts("package", ri.activityInfo.packageName, null));
+        startActivity(in);
+    }
+
     private final class DisplayResolveInfo {
         ResolveInfo ri;
         CharSequence displayLabel;
@@ -285,12 +362,18 @@
         }
 
         public void handlePackagesChanged() {
+            final int oldItemCount = getCount();
             rebuildList();
             notifyDataSetChanged();
             if (mList.size() <= 0) {
                 // We no longer have any items...  just finish the activity.
                 finish();
             }
+
+            final int newItemCount = getCount();
+            if (newItemCount != oldItemCount) {
+                resizeGrid();
+            }
         }
 
         private void rebuildList() {
@@ -299,7 +382,7 @@
             } else {
                 mCurrentResolveList = mPm.queryIntentActivities(
                         mIntent, PackageManager.MATCH_DEFAULT_ONLY
-                        | (mAlwaysCheck != null ? PackageManager.GET_RESOLVED_FILTER : 0));
+                        | (mAlwaysUseOption ? PackageManager.GET_RESOLVED_FILTER : 0));
             }
             int N;
             if ((mCurrentResolveList != null) && ((N = mCurrentResolveList.size()) > 0)) {
@@ -363,6 +446,7 @@
                 r0 = mCurrentResolveList.get(0);
                 int start = 0;
                 CharSequence r0Label =  r0.loadLabel(mPm);
+                mShowExtended = false;
                 for (int i = 1; i < N; i++) {
                     if (r0Label == null) {
                         r0Label = r0.activityInfo.packageName;
@@ -393,6 +477,7 @@
                 // No duplicate labels. Use label for entry at start
                 mList.add(new DisplayResolveInfo(ro, roLabel, null, null));
             } else {
+                mShowExtended = true;
                 boolean usePkg = false;
                 CharSequence startApp = ro.activityInfo.applicationInfo.loadLabel(mPm);
                 if (startApp == null) {
@@ -473,6 +558,11 @@
             if (convertView == null) {
                 view = mInflater.inflate(
                         com.android.internal.R.layout.resolve_list_item, parent, false);
+
+                // Fix the icon size even if we have different sized resources
+                ImageView icon = (ImageView)view.findViewById(R.id.icon);
+                ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) icon.getLayoutParams();
+                lp.width = lp.height = mIconSize;
             } else {
                 view = convertView;
             }
@@ -485,37 +575,25 @@
             TextView text2 = (TextView)view.findViewById(com.android.internal.R.id.text2);
             ImageView icon = (ImageView)view.findViewById(R.id.icon);
             text.setText(info.displayLabel);
-            if (info.extendedInfo != null) {
+            if (mShowExtended) {
                 text2.setVisibility(View.VISIBLE);
                 text2.setText(info.extendedInfo);
             } else {
                 text2.setVisibility(View.GONE);
             }
             if (info.displayIcon == null) {
-                info.displayIcon = info.ri.loadIcon(mPm);
+                info.displayIcon = loadIconForResolveInfo(info.ri);
             }
             icon.setImageDrawable(info.displayIcon);
         }
     }
 
-    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
-        if (mClearDefaultHint == null) return;
-
-        if(isChecked) {
-            mClearDefaultHint.setVisibility(View.VISIBLE);
-        } else {
-            mClearDefaultHint.setVisibility(View.GONE);
-        }
-    }
-
     class ItemLongClickListener implements AdapterView.OnItemLongClickListener {
 
         @Override
         public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
             ResolveInfo ri = mAdapter.resolveInfoForPosition(position);
-            Intent in = new Intent().setAction("android.settings.APPLICATION_DETAILS_SETTINGS")
-                    .setData(Uri.fromParts("package", ri.activityInfo.packageName, null));
-            startActivity(in);
+            showAppDetails(ri);
             return true;
         }
 
diff --git a/core/res/res/layout/resolve_list_item.xml b/core/res/res/layout/resolve_list_item.xml
index c0404be..e3d3ddf 100644
--- a/core/res/res/layout/resolve_list_item.xml
+++ b/core/res/res/layout/resolve_list_item.xml
@@ -18,39 +18,40 @@
 */
 -->
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:gravity="center_vertical"
-    android:orientation="horizontal"
-    android:minHeight="?android:attr/listPreferredItemHeight"
-    android:layout_height="wrap_content"
-    android:layout_width="match_parent"
-    android:paddingLeft="16dip"
-    android:paddingRight="16dip">
+              android:gravity="center"
+              android:orientation="vertical"
+              android:layout_height="wrap_content"
+              android:layout_width="match_parent"
+              android:background="?android:attr/activatedBackgroundIndicator"
+              android:padding="16dp">
 
-    <!-- Activity icon when presenting dialog -->
+    <!-- Extended activity info to distinguish between duplicate activity names -->
+    <TextView android:id="@android:id/text2"
+              android:textAppearance="?android:attr/textAppearance"
+              android:layout_width="wrap_content"
+              android:layout_height="wrap_content"
+              android:gravity="center"
+              android:minLines="2"
+              android:maxLines="2"
+              android:paddingLeft="8dip"
+              android:paddingRight="8dip" />
+
+    <!-- Activity icon when presenting dialog
+         Size will be filled in by ResolverActivity -->
     <ImageView android:id="@+id/icon"
-        android:layout_width="@android:dimen/app_icon_size"
-        android:layout_height="@android:dimen/app_icon_size"
-        android:scaleType="fitCenter" />
+               android:layout_width="0dp"
+               android:layout_height="0dp"
+               android:scaleType="fitCenter" />
 
-    <LinearLayout
-        android:orientation="vertical"
-        android:gravity="center_vertical"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content" >
-        <!-- Activity name -->
-        <TextView android:id="@android:id/text1"
-            android:textAppearance="?android:attr/textAppearanceListItemSmall"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:maxLines="2"
-            android:paddingLeft="16dip" />
-        <!-- Extended activity info to distinguish between duplicate activity names -->
-        <TextView android:id="@android:id/text2"
-            android:textAppearance="?android:attr/textAppearanceSmall"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:maxLines="2"
-            android:paddingLeft="16dip" />
-    </LinearLayout>
+    <!-- Activity name -->
+    <TextView android:id="@android:id/text1"
+              android:textAppearance="?android:attr/textAppearanceSmall"
+              android:layout_width="wrap_content"
+              android:layout_height="wrap_content"
+              android:gravity="center"
+              android:minLines="2"
+              android:maxLines="2"
+              android:paddingLeft="8dip"
+              android:paddingRight="8dip" />
 </LinearLayout>
 
diff --git a/core/res/res/layout/resolver_grid.xml b/core/res/res/layout/resolver_grid.xml
new file mode 100644
index 0000000..f10a59f
--- /dev/null
+++ b/core/res/res/layout/resolver_grid.xml
@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+* Copyright 2012, 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:layout_width="match_parent"
+              android:layout_height="match_parent"
+              android:orientation="vertical"
+              android:divider="?android:attr/dividerHorizontal"
+              android:showDividers="middle"
+              android:dividerPadding="0dip">
+    <GridView
+        android:layout_gravity="center"
+        android:layout_height="wrap_content"
+        android:layout_width="wrap_content"
+        android:id="@+id/resolver_grid"
+        android:numColumns="4"
+        android:columnWidth="128dp"
+        android:padding="16dp"
+        android:clipToPadding="false" />
+    <LinearLayout
+        android:id="@+id/button_bar"
+        android:visibility="gone"
+        style="?android:attr/buttonBarStyle"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="horizontal"
+        android:layoutDirection="locale"
+        android:measureWithLargestChild="true">
+        <Button android:id="@+id/button_always"
+                android:layout_width="wrap_content"
+                android:layout_gravity="right"
+                android:layout_weight="1"
+                android:maxLines="2"
+                android:minHeight="@dimen/alert_dialog_button_bar_height"
+                style="?android:attr/buttonBarButtonStyle"
+                android:textSize="14sp"
+                android:layout_height="wrap_content"
+                android:enabled="false"
+                android:text="@string/activity_resolver_use_always"
+                android:onClick="onButtonClick" />
+        <Button android:id="@+id/button_once"
+                android:layout_width="wrap_content"
+                android:layout_gravity="left"
+                android:layout_weight="1"
+                android:maxLines="2"
+                style="?android:attr/buttonBarButtonStyle"
+                android:textSize="14sp"
+                android:minHeight="@dimen/alert_dialog_button_bar_height"
+                android:layout_height="wrap_content"
+                android:enabled="false"
+                android:text="@string/activity_resolver_use_once"
+                android:onClick="onButtonClick" />
+    </LinearLayout>
+</LinearLayout>
\ No newline at end of file
diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml
index 82d3779..5e89dd1 100644
--- a/core/res/res/values/public.xml
+++ b/core/res/res/values/public.xml
@@ -1508,6 +1508,11 @@
   <java-symbol type="bool" name="config_enableDreams" />
   <java-symbol type="string" name="config_defaultDreamComponent" />
 
+  <java-symbol type="layout" name="resolver_grid" />
+  <java-symbol type="id" name="resolver_grid" />
+  <java-symbol type="id" name="button_once" />
+  <java-symbol type="id" name="button_always" />
+
   <!-- From SystemUI -->
   <java-symbol type="anim" name="push_down_in" />
   <java-symbol type="anim" name="push_down_out" />
diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml
index 1652ef0..41aa19b 100755
--- a/core/res/res/values/strings.xml
+++ b/core/res/res/values/strings.xml
@@ -3560,4 +3560,12 @@
     <!-- STK setup Call -->
     <string name="SetupCallDefault">Accept call?</string>
 
+    <!-- Title for a button to choose the currently selected activity
+         as the default in the activity resolver. [CHAR LIMIT=25] -->
+    <string name="activity_resolver_use_always">Use Always</string>
+
+    <!-- Title for a button to choose the currently selected activity
+         from the activity resolver to use just this once. [CHAR LIMIT=25] -->
+    <string name="activity_resolver_use_once">Just Once</string>
+
 </resources>