/* * Southampton University Map App * Copyright (C) 2011 Christopher Baines * * 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 2 * 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, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ package net.cbaines.suma; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.sql.SQLException; import android.content.Context; import android.content.SharedPreferences; import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.Display; import android.view.View; import android.view.WindowManager; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ImageButton; import android.widget.ImageView.ScaleType; import android.widget.ProgressBar; import android.widget.TextView; import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; import com.j256.ormlite.dao.Dao; public class BuildingActivity extends OrmLiteBaseActivity implements OnCheckedChangeListener, Preferences { final static String TAG = "BusTimeActivity"; private Context instance; private ImageButton imageButton; private TextView buildingName; private Building building; private ProgressBar progressBar; private TextView message; private CheckBox favouritesCheckBox; private Drawable buildingImage; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.building_activity); instance = this; String ID; // The identifier of the Building Log.i(TAG, "getIntent().getDataString() " + getIntent().getDataString()); if (getIntent().getDataString().startsWith("http://data")) { String[] uriParts = getIntent().getDataString().split("/"); ID = uriParts[uriParts.length - 1].replace(".html", ""); } else { String[] uriParts = getIntent().getDataString().split("/"); ID = uriParts[uriParts.length - 1]; } final SharedPreferences favouritesPrefs = instance.getSharedPreferences(FAVOURITES_PREFERENCES, 0); favouritesCheckBox = (CheckBox) findViewById(R.id.buildingActivityFavouriteCheckBox); favouritesCheckBox.setChecked(favouritesPrefs.getBoolean(ID, false)); favouritesCheckBox.setOnCheckedChangeListener(this); buildingName = (TextView) findViewById(R.id.buildingActivityName); progressBar = (ProgressBar) findViewById(R.id.buildingActivityLoadBar); progressBar.setVisibility(View.VISIBLE); message = (TextView) findViewById(R.id.buildingActivityMessage); message.setVisibility(View.GONE); imageButton = (ImageButton) findViewById(R.id.buildingActivityImage); imageButton.setVisibility(View.GONE); Log.i(TAG, "Building id " + ID); try { Dao buildingDao = getHelper().getBuildingDao(); building = buildingDao.queryForId(ID); buildingName.setText("Building " + building.id + " - " + building.name); } catch (SQLException e1) { e1.printStackTrace(); } } protected void onResume() { super.onResume(); buildingImage = (Drawable) getLastNonConfigurationInstance(); if (buildingImage == null) { GetBuildingImageTask buildingImageTask = new GetBuildingImageTask(); buildingImageTask.execute(building); } else { imageButton.setImageDrawable(buildingImage); imageButton.setVisibility(View.VISIBLE); imageButton.setScaleType(ScaleType.CENTER_INSIDE); imageButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { } }); } } class GetBuildingImageTask extends AsyncTask { String errorMessage; @Override protected Drawable doInBackground(Building... buildings) { Building building = buildings[0]; Log.i(TAG, "Getting image for " + building); try { Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); int orientation = display.getOrientation(); Log.i(TAG, "orientation " + orientation); Log.i(TAG, "width " + display.getWidth() + " height " + display.getHeight()); int width; width = display.getWidth(); URL imageURL; Log.i(TAG, "Screen width " + width); if (width >= 1000) { imageURL = new URL("http://data.southampton.ac.uk/images/buildings/1000/" + building.id + ".jpg"); } else if (width >= 800) { imageURL = new URL("http://data.southampton.ac.uk/images/buildings/1000/" + building.id + ".jpg"); } else if (width >= 600) { imageURL = new URL("http://data.southampton.ac.uk/images/buildings/600/" + building.id + ".jpg"); } else if (width >= 400) { imageURL = new URL("http://data.southampton.ac.uk/images/buildings/600/" + building.id + ".jpg"); } else if (width >= 300) { imageURL = new URL("http://data.southampton.ac.uk/images/buildings/300/" + building.id + ".jpg"); } else if (width >= 200) { imageURL = new URL("http://data.southampton.ac.uk/images/buildings/200/" + building.id + ".jpg"); } else { imageURL = new URL("http://data.southampton.ac.uk/images/buildings/100/" + building.id + ".jpg"); } Log.i(TAG, "URL " + imageURL); InputStream is = (InputStream) imageURL.getContent(); Drawable image = Drawable.createFromStream(is, "src"); return image; } catch (MalformedURLException e) { e.printStackTrace(); errorMessage = "MalformedURLException"; } catch (IOException e) { e.printStackTrace(); errorMessage = "No Image (Either none avalible, or a connection issue)"; } return null; } protected void onPostExecute(Drawable image) { progressBar.setVisibility(View.GONE); if (image != null) { Log.i(TAG, "Got a image for " + building + ", now displaying it"); buildingImage = image; imageButton = (ImageButton) findViewById(R.id.buildingActivityImage); imageButton.setImageDrawable(image); imageButton.setVisibility(View.VISIBLE); imageButton.setScaleType(ScaleType.CENTER_INSIDE); imageButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { } }); } else { Log.e(TAG, "Error, null image, " + errorMessage); message.setText(errorMessage); message.setVisibility(View.VISIBLE); } } } @Override public Object onRetainNonConfigurationInstance() { return buildingImage; } public void onCheckedChanged(CompoundButton button, boolean checked) { SharedPreferences favouritesPreferences = getSharedPreferences(FAVOURITES_PREFERENCES, 0); if (checked) { favouritesPreferences.edit().putBoolean(building.id, true).commit(); } else { favouritesPreferences.edit().remove(building.id).commit(); } } }