/*
Swisscom Safe Connect
Copyright (C) 2015 Swisscom
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.swisscom.safeconnect.sharing;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.LabeledIntent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.swisscom.safeconnect.R;
import java.util.List;
public class SharingAdapter extends ArrayAdapter<Intent> {
private Context mContext;
public SharingAdapter(Context context, int textViewResourceId, List<Intent> objects) {
super(context, textViewResourceId, objects);
mContext = context;
}
static class ViewHolder {
TextView text;
ImageView icon;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Intent intent = getItem(position);
View rowView = convertView;
// helps to reuse objects and avoid allocating a memory
if (rowView == null) {
LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
rowView = inflater.inflate(R.layout.item_sharing, parent, false);
ViewHolder h = new ViewHolder();
h.text = (TextView) rowView.findViewById(R.id.item_app_name);
h.icon = (ImageView) rowView.findViewById(R.id.item_app_icon);
rowView.setTag(h);
}
final ViewHolder h = (ViewHolder) rowView.getTag();
if (intent == null) {
h.text.setText("");
h.icon.setImageDrawable(null);
return rowView;
}
if (intent instanceof LabeledIntent){
h.text.setText(((LabeledIntent)intent).getNonLocalizedLabel());
h.icon.setImageResource(((LabeledIntent) intent).getIconResource());
} else {
PackageManager pm = mContext.getPackageManager();
List<ResolveInfo> resInfo = pm.queryIntentActivities(intent, 0);
if (!resInfo.isEmpty()){
ResolveInfo info = resInfo.get(0);
Drawable icon = info.loadIcon(pm);
h.icon.setImageDrawable(icon);
h.text.setText(info.loadLabel(pm));
}
}
return rowView;
}
}