/******************************************************************************* * Copyright (c) 2011 Michel DAVID mimah35-at-gmail.com * * 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. * * 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 fr.gotorennes.remote; import java.io.IOException; import java.io.InputStream; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; import fr.gotorennes.domain.Ligne; import fr.gotorennes.persistence.BusDao; public class BusStationService { private Map<Long, Bitmap> iconCache = new ConcurrentHashMap<Long, Bitmap>(); private Map<Long, String> urlCache = new ConcurrentHashMap<Long, String>(); private static BusStationService instance; private BusStationService(Context context) { BusDao busDao = BusDao.getInstance(context, null); for (Ligne ligne : busDao.getLignes()) { urlCache.put(ligne.id, "bus/" + ligne.longCode + ".png"); } } public static synchronized BusStationService getInstance(Context context) { if (instance == null) { instance = new BusStationService(context); } return instance; } public Bitmap getBitmapIcon(Context context, long idLigne) { if (iconCache.containsKey(idLigne)) { return iconCache.get(idLigne); } try { InputStream is = context.getAssets().open(urlCache.get(idLigne)); Bitmap bitmap = BitmapFactory.decodeStream(is); if (bitmap != null) { iconCache.put(idLigne, bitmap); } return bitmap; } catch (IOException e) { Log.e("GoToRennes", "Erreur de chargement de l'icon", e); } return null; } }