aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma/BuildingActivity.java
blob: 509bc411ab60ec0dd5017a680cc4c5c613dadfff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * 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<DatabaseHelper> 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;

	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);
		message = (TextView) findViewById(R.id.buildingActivityMessage);

		Log.i(TAG, "Building id " + ID);

		try {
			Dao<Building, String> buildingDao = getHelper().getBuildingDao();

			building = buildingDao.queryForId(ID);

			buildingName.setText("Building " + building.id + " - " + building.name);

		} catch (SQLException e1) {
			e1.printStackTrace();
		}

		GetBuildingImageTask buildingImageTask = new GetBuildingImageTask();
		buildingImageTask.execute(building);

	}

	class GetBuildingImageTask extends AsyncTask<Building, Integer, Drawable> {
		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 = "IOException";
			}

			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");

				imageButton = (ImageButton) findViewById(R.id.buildingActivityImage);
				imageButton.setImageDrawable(image);
				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 null;
	}

	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();
		}
	}

}