Skip to content

Commit

Permalink
slightly simplified, cleaned up extraneous imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Murphy committed Oct 25, 2011
1 parent 6423ced commit 3685228
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,29 @@

package com.commonsware.android.weather;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.Bundle;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Binder;

public class WeatherBinder extends Binder {
private String forecast=null;
private HttpClient client=null;
private String format=null;

void onCreate(Context ctxt) {
client=new DefaultHttpClient();
format=ctxt.getString(R.string.url);
}

void onDestroy() {
client.getConnectionManager().shutdown();
WeatherBinder(String format) {
this.format=format;
}

void getForecast(Location loc, WeatherListener listener) {
new FetchForecastTask(listener).execute(loc);
}
Expand Down Expand Up @@ -101,6 +88,7 @@ class FetchForecastTask extends AsyncTask<Location, Void, ArrayList<Forecast>> {

@Override
protected ArrayList<Forecast> doInBackground(Location... locs) {
DefaultHttpClient client=new DefaultHttpClient();
ArrayList<Forecast> result=null;

try {
Expand All @@ -117,6 +105,8 @@ protected ArrayList<Forecast> doInBackground(Location... locs) {
this.e=e;
}

client.getConnectionManager().shutdown();

return(result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,19 @@

package com.commonsware.android.weather;

import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.RemoteException;
import android.os.IBinder;
import android.util.Log;
import android.webkit.WebView;
import java.util.ArrayList;

public class WeatherDemo extends Activity {
private WebView browser;
Expand All @@ -51,7 +46,7 @@ public void onCreate(Bundle savedInstanceState) {
state=new State();
getApplicationContext()
.bindService(new Intent(this, WeatherService.class),
state.svcConn, BIND_AUTO_CREATE);
state, BIND_AUTO_CREATE);
}
else if (state.lastForecast!=null) {
showForecast();
Expand All @@ -73,7 +68,7 @@ public void onDestroy() {
}

if (!isConfigurationChanging) {
getApplicationContext().unbindService(state.svcConn);
getApplicationContext().unbindService(state);
}
}

Expand Down Expand Up @@ -144,7 +139,8 @@ public void onStatusChanged(String provider, int status,
}
};

static class State implements WeatherListener {
static class State
implements WeatherListener, ServiceConnection {
WeatherBinder weather=null;
WeatherDemo activity=null;
String lastForecast=null;
Expand All @@ -162,16 +158,14 @@ public void handleError(Exception e) {
activity.goBlooey(e);
}

ServiceConnection svcConn=new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder rawBinder) {
weather=(WeatherBinder)rawBinder;
}

public void onServiceDisconnected(ComponentName className) {
weather=null;
}
};
public void onServiceConnected(ComponentName className,
IBinder rawBinder) {
weather=(WeatherBinder)rawBinder;
}

public void onServiceDisconnected(ComponentName className) {
weather=null;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,19 @@
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import java.util.ArrayList;

public class WeatherService extends Service {
private final WeatherBinder binder=new WeatherBinder();
private WeatherBinder binder=null;

@Override
public void onCreate() {
super.onCreate();

binder.onCreate(this);
binder=new WeatherBinder(getString(R.string.url));
}

@Override
public IBinder onBind(Intent intent) {
return(binder);
}

@Override
public void onDestroy() {
super.onDestroy();

binder.onDestroy();
}
}

0 comments on commit 3685228

Please sign in to comment.