/*
Swisscom Safe Connect
Copyright (C) 2014 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.adapter;
import android.app.Activity;
import android.content.Context;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.RadioButton;
import android.widget.TextView;
import com.swisscom.safeconnect.R;
import com.swisscom.safeconnect.fragment.PipeDialogFragment;
import com.swisscom.safeconnect.json.ResourceJsonParser;
import com.swisscom.safeconnect.model.PlumberDeviceResponse;
import com.swisscom.safeconnect.model.PlumberDevicesResponseList;
import com.swisscom.safeconnect.utils.Config;
import com.swisscom.safeconnect.utils.DateProvider;
import java.util.LinkedList;
import java.util.List;
/**
* class that shows faq and other similar stuff
* @author Vadym Uvin
*/
public class DeviceListAdapter extends ArrayAdapter<PlumberDeviceResponse> {
private int selectedItem = 0;
public DeviceListAdapter(Context context, int resource, PlumberDeviceResponse[] objects) {
super(context, resource, objects);
}
static class ViewHolder {
TextView text;
TextView descr;
RadioButton rb;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
PlumberDeviceResponse device = getItem(position);
// helps to reuse objects and avoid allocating a memory
if (rowView == null) {
LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
rowView = inflater.inflate(R.layout.item_device, parent, false);
ViewHolder h = new ViewHolder();
h.text = (TextView) rowView.findViewById(R.id.item_device);
h.descr = (TextView) rowView.findViewById(R.id.item_last_seen);
h.rb = (RadioButton) rowView.findViewById(R.id.rb_selection);
rowView.setTag(h);
}
ViewHolder h = (ViewHolder) rowView.getTag();
String title;
h.descr.setText(DateProvider.getFormattedDate(getContext(), device.getLastSessionTime()));
switch (device.getDeviceType()) {
case IOS:
title = "iOS ";
break;
case ANDROID:
title = "Android ";
break;
default:
title = "";
break;
}
if (device.isTablet()) {
title += getContext().getString(R.string.pref_tablet);
} else {
title += getContext().getString(R.string.pref_phone);
}
h.text.setText(title);
h.rb.setTag(position);
h.rb.setChecked(selectedItem == position);
h.rb.setTag(position);
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View rb = v.findViewById(R.id.rb_selection);
selectedItem = (int) rb.getTag();
notifyDataSetChanged();
}
});
h.rb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedItem = (int) v.getTag();
notifyDataSetChanged();
}
});
return rowView;
}
}