1// Copyright 2014 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5package org.chromium.chrome.browser.share; 6 7import android.content.Context; 8import android.content.pm.PackageManager; 9import android.content.pm.PackageManager.NameNotFoundException; 10import android.content.pm.ResolveInfo; 11import android.content.res.Resources; 12import android.graphics.drawable.Drawable; 13import android.view.LayoutInflater; 14import android.view.View; 15import android.view.ViewGroup; 16import android.widget.ArrayAdapter; 17import android.widget.ImageView; 18import android.widget.TextView; 19 20import org.chromium.chrome.R; 21 22import java.util.List; 23 24/** 25 * Adapter that provides the list of activities via which a web page can be shared. 26 */ 27class ShareDialogAdapter extends ArrayAdapter<ResolveInfo> { 28 private final LayoutInflater mInflater; 29 private final PackageManager mManager; 30 31 /** 32 * @param context Context used to for layout inflation. 33 * @param manager PackageManager used to query for activity information. 34 * @param objects The list of possible share intents. 35 */ 36 public ShareDialogAdapter(Context context, PackageManager manager, List<ResolveInfo> objects) { 37 super(context, R.layout.share_dialog_item, objects); 38 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 39 mManager = manager; 40 } 41 42 @Override 43 public View getView(int position, View convertView, ViewGroup parent) { 44 View view; 45 if (convertView == null) { 46 view = mInflater.inflate(R.layout.share_dialog_item, parent, false); 47 } else { 48 view = convertView; 49 } 50 TextView text = (TextView) view.findViewById(R.id.text); 51 ImageView icon = (ImageView) view.findViewById(R.id.icon); 52 53 text.setText(getItem(position).loadLabel(mManager)); 54 icon.setImageDrawable(loadIconForResolveInfo(getItem(position))); 55 return view; 56 } 57 58 private Drawable loadIconForResolveInfo(ResolveInfo info) { 59 try { 60 final int iconRes = info.getIconResource(); 61 if (iconRes != 0) { 62 Resources res = mManager.getResourcesForApplication(info.activityInfo.packageName); 63 Drawable icon = res.getDrawable(iconRes); 64 return icon; 65 } 66 } catch (NameNotFoundException e) { 67 // Could not find the icon. loadIcon call below will return the default app icon. 68 } 69 return info.loadIcon(mManager); 70 } 71 72}