package com.hci.pwf;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.location.Address;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
public class MainActivity extends Activity {
public static final String SETTING_INFOS = "SETTING_Infos";
public static final String STR_GENDERRADIO = "STR_GENDERRADIO";
public static final String STR_PERFERENCE_WIND = "STR_PERFERENCE_WIND";
public static final String STR_PERFERENCE_HUMIDITY = "STR_PERFERENCE_HUMIDITY";
public static final String STR_TIME = "STR_TIME";
public static final String STR_ALARM = "STR_ALARM";
public static final String STR_PREDICTION_MODE = "STR_PREDICTION_MODE";
/** Called when the activity is first created. */
private NotificationManager notificationManager;
private static int notificationID = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationID);
// --->get local info
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Location location = new Location(LocationManager.GPS_PROVIDER);
Location location = LocationUtil.getLocationProvider(locationManager);
String addInfo;
if (location != null) {
GeoPoint point = LocationUtil.getGeoByLocation(location);
addInfo = this.getAddressInfo(point);
// addInfo = "berlin";
} else {
addInfo = "berlin";
}
// LocationListener locListener = new LocationListener() {
// public void onLocationChanged(Location location) {
// if (location != null) {
// int lat = (int) (location.getLatitude() * 1000000);
// int lng = (int) (location.getLongitude() * 1000000);
// GeoPoint pt = new GeoPoint(lat, lng);
//
// Location currentLocation = new Location(
// LocationManager.GPS_PROVIDER);
//
// }
// }
// public void onProviderDisabled(String provider) {
// }
// public void onProviderEnabled(String provider) {
// }
// public void onStatusChanged(String provider, int status,
// Bundle extras) {
// }
// };
// <---get local info
/**
* here can not get location info ,can not fix it yet.
*/
SearchWeather s = new SearchWeather();
ArrayList<WeatherInfo> list = new ArrayList<WeatherInfo>();
list = s.getWeather(addInfo);
/**
* here also use Berlin as current location.
*/
CurrentWeather cw = (CurrentWeather) list.get(1);
ForecastWeather fw1 = (ForecastWeather) list.get(2);
ForecastWeather fw2 = (ForecastWeather) list.get(3);
ForecastWeather fw3 = (ForecastWeather) list.get(4);
ForecastWeather fw4 = (ForecastWeather) list.get(5);
SharedPreferences settings = getSharedPreferences(SETTING_INFOS, 0);
String gender = settings.getString(STR_GENDERRADIO, "");
String preference_wind = settings.getString(STR_PERFERENCE_WIND, "");
String preference_humidity = settings.getString(
STR_PERFERENCE_HUMIDITY, "");
String prediction_mode = settings.getString(STR_PREDICTION_MODE, "");
if (prediction_mode != null) {
if (prediction_mode.equals("simple")) {
} else if (prediction_mode.equals("showfourdays")) {
}
}
TextView currentLocation = (TextView) super
.findViewById(R.id.currentLocation);
currentLocation.setText("Your Location is Berlin");
TextView currentCondition = (TextView) super
.findViewById(R.id.currentCondition);
currentCondition.setText("Current: " + cw.condition);
TextView currentTemp = (TextView) super.findViewById(R.id.currentTemp);
currentTemp.setText("Temp: " + cw.temp_c + "C");
TextView currentWind = (TextView) super.findViewById(R.id.currentWind);
if (preference_wind != null) {
if (preference_wind.equals("true")) {
String wind = cw.wind_condition;
int cursor = wind.lastIndexOf(":") + 2;
String windHeard = wind.substring(cursor, cursor + 1);
int windTaial = wind.lastIndexOf("at");
String newWind = wind.substring(windTaial + 3);
// currentWind.setText(windHeard+" \n"+newWind);
currentWind.setText(wind);
} else {
currentWind.setText("");
}
}
TextView currentHumi = (TextView) super
.findViewById(R.id.currentHumidity);
if (preference_humidity != null) {
if (preference_humidity.equals("true")) {
String Humi = cw.humidity;
currentHumi.setText(Humi);
} else {
currentHumi.setText("");
}
}
TextView currentHightTemp = (TextView) super
.findViewById(R.id.currentHightTemp);
currentHightTemp.setText("High: " + fw1.high_temp + "C");
TextView currentLowTemp = (TextView) super
.findViewById(R.id.currentLowTemp);
currentLowTemp.setText("Low: " + fw1.low_temp + "C");
TextView TodayWeather = (TextView) super.findViewById(R.id.Today_Weather);
TextView Fav_Info = (TextView) super.findViewById(R.id.Fav_Info);
TextView FavWeb = (TextView) super.findViewById(R.id.Fav_Web);
if(fw1.condition == null){
Fav_Info.setText("It can't get weather now, please check net!");
}else if(gender.equals("")){
TodayWeather.setText("Today the weather is " + fw1.condition + ".");
Fav_Info.setText("Please do a simple setting to get more!");
}else{
ArrayList<String> Advice = this.getAdvice(fw1.condition, gender);
TodayWeather.setText("Today the weather is " + fw1.condition + ".");
Fav_Info.setText(Advice.get(0));
FavWeb.setText(Advice.get(1));
}
/*
if(fw1.condition != null && gender != null ){
ArrayList<String> Advice = this.getAdvice(fw1.condition, gender);
TodayWeather.setText("Today the weather is " + fw1.condition + ".");
Fav_Info.setText(Advice.get(0));
FavWeb.setText(Advice.get(1));
}else if(fw1.condition != null){
Fav_Info.setText("Please do a simple setting to get more!");
}else{
Fav_Info.setText("It can't get weather now, please check net!");
}
*/
/*
if (gender != null) {
if (gender.equals("male")) {
Fav_Info
.setText("Today the weather is " + fw1.condition + ". \n It's good for sport! "
+ "www.nike.com");
} else {
Fav_Info
.setText("Today the weather is " + fw1.condition +". \n It's good for shopping! "
+ "www.galeria-kaufhof.de");
}
}
*/
if (prediction_mode != null) {
if (prediction_mode.equals("simple")) {
TextView date_1 = (TextView) super.findViewById(R.id.date_1);
date_1.setText("");
TextView date_2 = (TextView) super.findViewById(R.id.date_2);
date_2.setText("");
TextView date_3 = (TextView) super.findViewById(R.id.date_3);
date_3.setText("");
TextView info_1 = (TextView) super.findViewById(R.id.info_1);
info_1.setText("");
TextView info_2 = (TextView) super.findViewById(R.id.info_2);
info_2.setText("");
TextView info_3 = (TextView) super.findViewById(R.id.info_3);
info_3.setText("");
} else if (prediction_mode.equals("showfourdays")) {
TextView date_1 = (TextView) super.findViewById(R.id.date_1);
date_1.setText(fw2.date);
TextView date_2 = (TextView) super.findViewById(R.id.date_2);
date_2.setText(fw3.date);
TextView date_3 = (TextView) super.findViewById(R.id.date_3);
date_3.setText(fw4.date);
TextView info_1 = (TextView) super.findViewById(R.id.info_1);
info_1.setText(fw2.low_temp + "/" + fw2.high_temp +"" );
TextView info_2 = (TextView) super.findViewById(R.id.info_2);
info_2.setText(fw3.low_temp + "/" + fw2.high_temp + "");
TextView info_3 = (TextView) super.findViewById(R.id.info_3);
info_3.setText(fw4.low_temp + "/" + fw3.high_temp + "");
ImageView weather_1 = (ImageView) super
.findViewById(R.id.weather_1);
Bitmap org2 = BitMap(fw2.icon);
weather_1.setImageDrawable(resizeBitmap(org2, 100));
ImageView weather_2 = (ImageView) super
.findViewById(R.id.weather_2);
Bitmap org3 = BitMap(fw3.icon);
weather_2.setImageDrawable(resizeBitmap(org3, 100));
ImageView weather_3 = (ImageView) super
.findViewById(R.id.weather_3);
Bitmap org4 = BitMap(fw4.icon);
weather_3.setImageDrawable(resizeBitmap(org4, 100));
}
} else {
TextView date_1 = (TextView) super.findViewById(R.id.date_1);
date_1.setText(fw2.date);
TextView date_2 = (TextView) super.findViewById(R.id.date_2);
date_2.setText(fw3.date);
TextView date_3 = (TextView) super.findViewById(R.id.date_3);
date_3.setText(fw4.date);
TextView info_1 = (TextView) super.findViewById(R.id.info_1);
info_1.setText(fw2.low_temp + "/" + fw2.high_temp + "");
TextView info_2 = (TextView) super.findViewById(R.id.info_2);
info_2.setText(fw3.low_temp + "/" + fw2.high_temp + "");
TextView info_3 = (TextView) super.findViewById(R.id.info_3);
info_3.setText(fw4.low_temp + "/" + fw3.high_temp + "");
ImageView weather_1 = (ImageView) super
.findViewById(R.id.weather_1);
Bitmap org2 = BitMap(fw2.icon);
weather_1.setImageDrawable(resizeBitmap(org2, 100));
ImageView weather_2 = (ImageView) super
.findViewById(R.id.weather_2);
Bitmap org3 = BitMap(fw3.icon);
weather_2.setImageDrawable(resizeBitmap(org3, 100));
ImageView weather_3 = (ImageView) super
.findViewById(R.id.weather_3);
Bitmap org4 = BitMap(fw4.icon);
weather_3.setImageDrawable(resizeBitmap(org4, 100));
}
ImageView CurrentWeather = (ImageView) super
.findViewById(R.id.CurrentWeather);
Bitmap bitmapOrg = BitMap(cw.icon);
CurrentWeather.setImageDrawable(resizeBitmap(bitmapOrg, 140));
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, 0, 0, "Search");
menu.add(0, 1, 0, "Setting");
menu.add(0, 2, 0, "Exit");
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case 0:
// add search event
Toast.makeText(this, "search", Toast.LENGTH_SHORT).show();
Intent intententer = new Intent(MainActivity.this,
SearchActivity.class);
startActivity(intententer);
break;
case 1:
Toast.makeText(this, "setting", Toast.LENGTH_SHORT).show();
Intent intententering = new Intent(MainActivity.this,
SettingActivity.class);
startActivityForResult(intententering, 0);
finish();
break;
case 2:
finish();
break;
default:
// add default event
break;
}
return false;
}
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
public Bitmap BitMap(String url) {
URL PictureUrl = null;
Bitmap bitmap = null;
try {
PictureUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) PictureUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
public BitmapDrawable resizeBitmap(Bitmap org, int resize) {
int width = org.getWidth();
int height = org.getHeight();
float scaleWidth = ((float) resize) / width;
float scaleHeight = ((float) resize) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(org, 0, 0, width, height,
matrix, true);
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
return bmd;
}
public String getAddressInfo(GeoPoint gp) {
Address address = LocationUtil.getAddressbyGeoPoint(this, gp);
StringBuilder sb = new StringBuilder();
if (address != null) {
sb.append(address.getLocality());
}
return sb.toString();
}
public ArrayList<String> getAdvice(String sCondition, String sGender){
try{
//String RawUrl = URLEncoder.encode("http://www.genau.cc/weatherremind/remindservice.php?condition="+sCondition.trim()+"&gender="+sGender.trim(), "UTF-8");
String ParCondition = sCondition.trim().replace(" ", "%20");
URL url = new URL("http://www.genau.cc/weatherremind/remindservice.php?condition="+ParCondition+"&gender="+sGender.trim());
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int statusCode = httpConn.getResponseCode();
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
AdviceContentHandler ach =new AdviceContentHandler();
xr.setContentHandler(ach);
xr.parse(new InputSource(url.openStream()));
ArrayList<String> RemindList = ach.List;
return RemindList;
}catch(Exception e){
return null;
}
}
}
|