/* 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.adapter; import android.app.Activity; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; import com.swisscom.safeconnect.BuildConfig; import com.swisscom.safeconnect.R; import com.swisscom.safeconnect.model.PhoneCode; import com.swisscom.safeconnect.utils.Config; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Locale; /** * Created by vadim on 24.03.15. */ public class PhoneCodeAdapter extends ArrayAdapter<PhoneCode> { private List<PhoneCode> phoneCodes = new ArrayList<>(250); private boolean haveErrors = false; public PhoneCodeAdapter(Context context, int resource) { super(context, resource); loadList(context); } static class ViewHolder { TextView code; } public int getCodeByIndex(int index) { if (index >= 0 && index < phoneCodes.size()) { return phoneCodes.get(index).getPhoneCode(); } return 0; } @Override public int getCount() { return phoneCodes.size(); } @Override public View getView(int position, View convertView, ViewGroup parent) { PhoneCode phoneCode = phoneCodes.get(position); // helps to reuse objects and avoid allocating a memory if (convertView == null) { LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater(); convertView = inflater.inflate(R.layout.list_phone_code, parent, false); TextView code = (TextView) convertView.findViewById(R.id.tv_phone_code); ViewHolder h = new ViewHolder(); h.code = code; convertView.setTag(h); } ViewHolder vh = (ViewHolder) convertView.getTag(); vh.code.setText(String.format(Locale.getDefault(), "%s (+%d)", phoneCode.getCountryName(), phoneCode.getPhoneCode())); return convertView; } private void loadList(Context context) { phoneCodes.clear(); InputStream is = null; InputStreamReader isr = null; BufferedReader reader = null; try { is = context.getResources().openRawResource(R.raw.phonecode); isr = new InputStreamReader(is); reader = new BufferedReader(isr); String line; while ((line = reader.readLine()) != null) { String[] rowData = line.split(";"); if (rowData.length < 2) continue; int code = 0; try { code = Integer.parseInt(rowData[1]); } catch (NumberFormatException e) { if (BuildConfig.DEBUG) Log.e(Config.TAG, "failed to parse phone code " + rowData[1]); haveErrors = true; } phoneCodes.add(new PhoneCode(rowData[0], code)); } } catch (IOException e) { if (BuildConfig.DEBUG) Log.e(Config.TAG, "unable to read phone codes"); haveErrors = true; } finally { try { if (reader != null) reader.close(); if (isr != null) isr.close(); if (is != null) is.close(); } catch (IOException e) { if (BuildConfig.DEBUG) Log.e(Config.TAG, "could not close the stream"); haveErrors = true; } } } public boolean getHaveErrors() { return haveErrors; } }