import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
+import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
+import android.content.IntentFilter;
import android.content.IntentSender.SendIntentException;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
+import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
-import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
// Request code to use when launching the resolution activity
private static final int REQUEST_RESOLVE_ERROR = 1001;
private WeatherLocation mRestoreUI;
+ private BroadcastReceiver mReceiver;
// Google Play Services Map
private GoogleMap mMap;
// TODO: read and store from different threads? Hopefully always from UI thread.
private Marker mMarker;
- private ProgressBar mActivityIndicator;
- // true progress bar visible/false progress bar gone
- private boolean mIsProgressBarVisible;
+ // IT IS IMPOSSIBLE IF SCREEN MAY ROTATE!!!! ANDROID SUCKS!!!!
+// private ProgressBar mActivityIndicator;
+// // true progress bar visible/false progress bar gone
+// private boolean mIsProgressBarVisible;
// Google Play Services Location
private GoogleApiClient mGoogleApiClient;
this.mMap.setOnMapLongClickListener(new MapActivityOnMapLongClickListener(this));
// Progress bar. View and status (we keep status even after screen rotations.
- this.mActivityIndicator = (ProgressBar) this.findViewById(R.id.weather_map_progress);
- if (savedInstanceState != null) {
- this.mIsProgressBarVisible = savedInstanceState.getBoolean("mIsProgressBarVisible", false);
- }
- if (this.mIsProgressBarVisible) {
- this.mActivityIndicator.setVisibility(View.VISIBLE);
- } else {
- this.mActivityIndicator.setVisibility(View.GONE);
- }
+// this.mActivityIndicator = (ProgressBar) this.findViewById(R.id.weather_map_progress);
+// if (savedInstanceState != null) {
+// this.mIsProgressBarVisible = savedInstanceState.getBoolean("mIsProgressBarVisible", false);
+// }
+// if (this.mIsProgressBarVisible) {
+// this.mActivityIndicator.setVisibility(View.VISIBLE);
+// } else {
+// this.mActivityIndicator.setVisibility(View.GONE);
+// }
}
@Override
// just once
this.mRestoreUI = null;
} else {
- final DatabaseQueries query = new DatabaseQueries(this);
+ final DatabaseQueries query = new DatabaseQueries(this.getApplicationContext());
weatherLocation = query.queryDataBase();
}
savedInstanceState.putSerializable("WeatherLocation", location);
}
// Save progress bar status.
- savedInstanceState.putBoolean("mIsProgressBarVisible", this.mIsProgressBarVisible);
+// savedInstanceState.putBoolean("mIsProgressBarVisible", this.mIsProgressBarVisible);
// Google Play Services
// To keep track of the boolean across activity restarts (such as when
final String cityString = city.getText().toString();
final String countryString = country.getText().toString();
- final DatabaseQueries query = new DatabaseQueries(this);
+ final DatabaseQueries query = new DatabaseQueries(this.getApplicationContext());
final WeatherLocation weatherLocation = query.queryDataBase();
if (weatherLocation != null) {
weatherLocation
protected void onPreExecute() {
// TODO: The same with Overview and Current? I guess so...
// Show the activity indicator
- final MapActivity activity = (MapActivity) this.localContext;
- activity.mActivityIndicator.setVisibility(View.VISIBLE);
- activity.mIsProgressBarVisible = true;
+// final MapActivity activity = (MapActivity) this.localContext;
+// activity.mActivityIndicator.setVisibility(View.VISIBLE);
+// activity.mIsProgressBarVisible = true;
}
@Override
@Override
protected void onPostExecute(final WeatherLocation weatherLocation) {
- final MapActivity activity = (MapActivity) this.localContext;
- activity.mActivityIndicator.setVisibility(View.GONE);
- activity.mIsProgressBarVisible = false;
-
// TODO: Is AsyncTask calling this method even when RunTimeException in doInBackground method?
- // I hope so, otherwise I must catch(Throwable) in doInBackground method :(
- if (weatherLocation == null) {
- // TODO: if user changed activity, where is this going to appear?
- final DialogFragment newFragment = ErrorDialogFragment.newInstance(R.string.error_dialog_location_error);
- newFragment.show(activity.getSupportFragmentManager(), "errorDialog");
- return;
- }
+ // I hope so, otherwise I must catch(Throwable) in doInBackground method :(
+ if (weatherLocation == null) {
+ // Nothing to do
+ // TODO: Should I show some error message? I am not doing it on WP8 Should I do it on WP8?
+ return;
+ }
- activity.updateUI(weatherLocation);
+ // Call updateUI on the UI thread.
+ final Intent weatherLocationData = new Intent("de.example.exampletdd.UPDATEWEATHERLOCATION");
+ weatherLocationData.putExtra("weatherLocation", weatherLocation);
+ LocalBroadcastManager.getInstance(this.localContext).sendBroadcastSync(weatherLocationData);
}
private WeatherLocation getLocation(final double latitude, final double longitude) throws IOException {
if (!mResolvingError) {
mGoogleApiClient.connect();
}
+
+ this.mReceiver = new BroadcastReceiver() {
+
+ @Override
+ public void onReceive(final Context context, final Intent intent) {
+ final String action = intent.getAction();
+ if (action.equals("de.example.exampletdd.UPDATEWEATHERLOCATION")) {
+ final WeatherLocation weatherLocation = (WeatherLocation) intent.getSerializableExtra("weatherLocation");
+
+// MapActivity.this.mActivityIndicator.setVisibility(View.GONE);
+// MapActivity.this.mIsProgressBarVisible = false;
+
+ // TODO: Is AsyncTask calling this method even when RunTimeException in doInBackground method?
+ // I hope so, otherwise I must catch(Throwable) in doInBackground method :(
+ if (weatherLocation == null) {
+ final DialogFragment newFragment = ErrorDialogFragment.newInstance(R.string.error_dialog_location_error);
+ newFragment.setRetainInstance(true);
+ newFragment.show(MapActivity.this.getSupportFragmentManager(), "errorDialog");
+ return;
+ }
+
+ MapActivity.this.updateUI(weatherLocation);
+ }
+ }
+ };
+
+ // Register receiver
+ final IntentFilter filter = new IntentFilter();
+ filter.addAction("de.example.exampletdd.UPDATEWEATHERLOCATION");
+ LocalBroadcastManager.getInstance(this.getApplicationContext()).registerReceiver(this.mReceiver, filter);
}
/**
mGoogleApiClient.unregisterConnectionFailedListener(this);
mGoogleApiClient.disconnect();
+ LocalBroadcastManager.getInstance(this.getApplicationContext()).unregisterReceiver(this.mReceiver);
+
super.onStop();
}
/**
* Called by Location Services if the attempt to
* Location Services fails.
+ *
+ * IT NEVER DOES ANYTHING. ANDROID SUCKS!!!
*/
@Override
public void onConnectionFailed(final ConnectionResult result) {
* LocationUpdateRemover and LocationUpdateRequester may call startResolutionForResult() to
* start an Activity that handles Google Play services problems. The result of this
* call returns here, to onActivityResult.
+ *
+ * IT NEVER DOES ANYTHING. ANDROID SUCKS!!!
*/
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
import org.apache.http.client.ClientProtocolException;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
+import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
public class CurrentFragment extends Fragment {
private static final String TAG = "CurrentFragment";
+ private BroadcastReceiver mReceiver;
@Override
public void onCreate(final Bundle savedInstanceState) {
}
@Override
+ public void onStart() {
+ super.onStart();
+
+ this.mReceiver = new BroadcastReceiver() {
+
+ @Override
+ public void onReceive(final Context context, final Intent intent) {
+ final String action = intent.getAction();
+ if (action.equals("de.example.exampletdd.UPDATECURRENT")) {
+ final Current current = (Current) intent.getSerializableExtra("current");
+
+ if (current != null) {
+ CurrentFragment.this.updateUI(current);
+
+ final WeatherInformationApplication application =
+ (WeatherInformationApplication) getActivity().getApplication();
+ application.setCurrent(current);
+
+ final DatabaseQueries query = new DatabaseQueries(CurrentFragment.this.getActivity().getApplicationContext());
+ final WeatherLocation weatherLocation = query.queryDataBase();
+ weatherLocation.setLastCurrentUIUpdate(new Date());
+ query.updateDataBase(weatherLocation);
+ }
+ }
+ }
+ };
+
+ // Register receiver
+ final IntentFilter filter = new IntentFilter();
+ filter.addAction("de.example.exampletdd.UPDATECURRENT");
+ LocalBroadcastManager.getInstance(this.getActivity().getApplicationContext())
+ .registerReceiver(this.mReceiver, filter);
+ }
+
+ @Override
public void onResume() {
super.onResume();
- final DatabaseQueries query = new DatabaseQueries(this.getActivity());
+ final DatabaseQueries query = new DatabaseQueries(this.getActivity().getApplicationContext());
final WeatherLocation weatherLocation = query.queryDataBase();
if (weatherLocation == null) {
// Nothing to do.
// Load remote data (aynchronous)
// Gets the data from the web.
final CurrentTask task = new CurrentTask(
- this,
+ this.getActivity().getApplicationContext(),
new CustomHTTPClient(AndroidHttpClient.newInstance("Android 4.3 WeatherInformation Agent")),
new ServiceParser(new JPOSWeatherParser()));
super.onSaveInstanceState(savedInstanceState);
}
+ @Override
+ public void onStop() {
+ LocalBroadcastManager.getInstance(this.getActivity().getApplicationContext()).unregisterReceiver(this.mReceiver);
+
+ super.onStop();
+ }
+
private void updateUI(final Current current) {
final SharedPreferences sharedPreferences = PreferenceManager
// I mean, if OverviewTask shows one progress dialog and CurrentTask does the same I will have
// have two progress dialogs... How may I solve this problem? I HATE ANDROID.
private class CurrentTask extends AsyncTask<Object, Void, Current> {
- private final Fragment localFragment;
+ // Store the context passed to the AsyncTask when the system instantiates it.
+ private final Context localContext;
final CustomHTTPClient weatherHTTPClient;
final ServiceParser weatherService;
- public CurrentTask(final Fragment fragment, final CustomHTTPClient weatherHTTPClient,
+ public CurrentTask(final Context context, final CustomHTTPClient weatherHTTPClient,
final ServiceParser weatherService) {
- this.localFragment = fragment;
+ this.localContext = context;
this.weatherHTTPClient = weatherHTTPClient;
this.weatherService = weatherService;
}
}
// Call updateUI on the UI thread.
- updateUI(current);
-
- final WeatherInformationApplication application =
- (WeatherInformationApplication) getActivity().getApplication();
- application.setCurrent(current);
-
- final DatabaseQueries query = new DatabaseQueries(this.localFragment.getActivity());
- final WeatherLocation weatherLocation = query.queryDataBase();
- weatherLocation.setLastCurrentUIUpdate(new Date());
- query.updateDataBase(weatherLocation);
+ final Intent currentData = new Intent("de.example.exampletdd.UPDATECURRENT");
+ currentData.putExtra("current", current);
+ LocalBroadcastManager.getInstance(this.localContext).sendBroadcastSync(currentData);
}
}
}
import org.apache.http.client.ClientProtocolException;
+import android.content.BroadcastReceiver;
import android.content.ComponentName;
+import android.content.Context;
import android.content.Intent;
+import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
-import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
+import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
public class OverviewFragment extends ListFragment {
private static final String TAG = "OverviewFragment";
+ private BroadcastReceiver mReceiver;
@Override
public void onCreate(final Bundle savedInstanceState) {
}
@Override
+ public void onStart() {
+ super.onStart();
+
+ this.mReceiver = new BroadcastReceiver() {
+
+ @Override
+ public void onReceive(final Context context, final Intent intent) {
+ final String action = intent.getAction();
+ if (action.equals("de.example.exampletdd.UPDATEFORECAST")) {
+ final Forecast forecast = (Forecast) intent.getSerializableExtra("forecast");
+
+ if (forecast != null) {
+ OverviewFragment.this.updateUI(forecast);
+
+ final WeatherInformationApplication application =
+ (WeatherInformationApplication) getActivity().getApplication();
+ application.setForecast(forecast);
+
+ final DatabaseQueries query = new DatabaseQueries(OverviewFragment.this.getActivity().getApplicationContext());
+ final WeatherLocation weatherLocation = query.queryDataBase();
+ weatherLocation.setLastForecastUIUpdate(new Date());
+ query.updateDataBase(weatherLocation);
+ }
+ }
+ }
+ };
+
+ // Register receiver
+ final IntentFilter filter = new IntentFilter();
+ filter.addAction("de.example.exampletdd.UPDATEFORECAST");
+ LocalBroadcastManager.getInstance(this.getActivity().getApplicationContext())
+ .registerReceiver(this.mReceiver, filter);
+ }
+
+ @Override
public void onResume() {
super.onResume();
- final DatabaseQueries query = new DatabaseQueries(this.getActivity());
+ final DatabaseQueries query = new DatabaseQueries(this.getActivity().getApplicationContext());
final WeatherLocation weatherLocation = query.queryDataBase();
if (weatherLocation == null) {
// Nothing to do.
// Load remote data (aynchronous)
// Gets the data from the web.
final OverviewTask task = new OverviewTask(
- this,
+ this.getActivity().getApplicationContext(),
new CustomHTTPClient(AndroidHttpClient.newInstance("Android 4.3 WeatherInformation Agent")),
new ServiceParser(new JPOSWeatherParser()));
}
@Override
+ public void onStop() {
+ LocalBroadcastManager.getInstance(this.getActivity().getApplicationContext()).unregisterReceiver(this.mReceiver);
+
+ super.onStop();
+ }
+
+ @Override
public void onListItemClick(final ListView l, final View v, final int position, final long id) {
final SpecificFragment fragment = (SpecificFragment) this
.getFragmentManager().findFragmentById(R.id.weather_specific_fragment);
// I mean, if OverviewTask shows one progress dialog and CurrentTask does the same I will have
// have two progress dialogs... How may I solve this problem? I HATE ANDROID.
private class OverviewTask extends AsyncTask<Object, Void, Forecast> {
- private final Fragment localFragment;
+ // Store the context passed to the AsyncTask when the system instantiates it.
+ private final Context localContext;
private final CustomHTTPClient weatherHTTPClient;
private final ServiceParser weatherService;
- public OverviewTask(final Fragment fragment, final CustomHTTPClient weatherHTTPClient,
+ public OverviewTask(final Context context, final CustomHTTPClient weatherHTTPClient,
final ServiceParser weatherService) {
- this.localFragment = fragment;
+ this.localContext = context;
this.weatherHTTPClient = weatherHTTPClient;
this.weatherService = weatherService;
}
@Override
protected void onPreExecute() {
- final OverviewFragment overview = (OverviewFragment) this.localFragment;
- overview.setListAdapter(null);
- // TODO: string static resource
- overview.setEmptyText("No data available");
- overview.setListShownNoAnimation(false);
+ // IMPOSSIBLE IF I USE JUST Context (I like Context because it doesn't die like Activity does when screen rotates)
+// final OverviewFragment overview = (OverviewFragment) this.localFragment;
+// overview.setListAdapter(null);
+// // TODO: string static resource
+// overview.setEmptyText("No data available");
+// overview.setListShownNoAnimation(false);
}
@Override
protected void onPostExecute(final Forecast forecast) {
// TODO: Is AsyncTask calling this method even when RunTimeException in doInBackground method?
// I hope so, otherwise I must catch(Throwable) in doInBackground method :(
- final OverviewFragment overview = (OverviewFragment) this.localFragment;
- // TODO: string static resource
- overview.setEmptyText("Error trying to download remote data");
- overview.setListShownNoAnimation(true);
+ // IMPOSSIBLE IF I USE JUST Context (I like Context because it doesn't die like Activity does when screen rotates)
+// final OverviewFragment overview = (OverviewFragment) this.localFragment;
+// // TODO: string static resource
+// overview.setEmptyText("Error trying to download remote data");
+// overview.setListShownNoAnimation(true);
if (forecast == null) {
// Nothing to do
}
// Call updateUI on the UI thread.
- updateUI(forecast);
-
- final WeatherInformationApplication application =
- (WeatherInformationApplication) getActivity().getApplication();
- application.setForecast(forecast);
-
- final DatabaseQueries query = new DatabaseQueries(this.localFragment.getActivity());
- final WeatherLocation weatherLocation = query.queryDataBase();
- weatherLocation.setLastForecastUIUpdate(new Date());
- query.updateDataBase(weatherLocation);
+ final Intent forecastData = new Intent("de.example.exampletdd.UPDATEFORECAST");
+ forecastData.putExtra("forecast", forecast);
+ LocalBroadcastManager.getInstance(this.localContext).sendBroadcastSync(forecastData);
}
}
}