diff --git a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitActivity.java b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitActivity.java index ca588d18c..000d42713 100644 --- a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitActivity.java +++ b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitActivity.java @@ -15,14 +15,11 @@ import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.ContentValues; -import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.res.TypedArray; import android.graphics.Color; import android.location.Location; -import android.location.LocationListener; -import android.location.LocationManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; @@ -42,7 +39,6 @@ import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; -import android.widget.ListAdapter; import android.widget.ListView; import android.widget.PopupWindow.OnDismissListener; import android.widget.TextView; @@ -67,10 +63,11 @@ import com.todoroo.astrid.service.StatisticsConstants; import com.todoroo.astrid.service.StatisticsService; import com.todoroo.astrid.service.TaskService; import com.todoroo.astrid.service.ThemeService; +import com.todoroo.astrid.taskrabbit.TaskRabbitLocationManager.LocationResult; import com.todoroo.astrid.ui.FragmentPopover; import com.todoroo.astrid.welcome.HelpInfoPopover; -public class TaskRabbitActivity extends FragmentActivity implements LocationListener { +public class TaskRabbitActivity extends FragmentActivity { public interface TaskRabbitSetListener { @@ -113,7 +110,7 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList private FragmentPopover menuPopover; private TextView menuTitle; private ListView menuList; - private ListAdapter adapter; + private ArrayAdapter adapter; private int currentSelectedItem = 0; @@ -125,6 +122,7 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList private LinearLayout row; public static final int REQUEST_CODE_TASK_RABBIT_OAUTH = 5; + public static final int REQUEST_CODE_ENABLE_GPS = 6; /** Act.fm current user name */ public static final String TASK_RABBIT_TOKEN = "task_rabbit_token"; //$NON-NLS-1$ @@ -259,11 +257,11 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList private void setupForDialogOrFullscreen() { -// isDialog = AndroidUtilities.isTabletSized(this); -// if (isDialog) -// setTheme(ThemeService.getDialogTheme()); -// else - ThemeService.applyTheme(this); + // isDialog = AndroidUtilities.isTabletSized(this); + // if (isDialog) + // setTheme(ThemeService.getDialogTheme()); + // else + ThemeService.applyTheme(this); } @@ -329,7 +327,9 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList row.removeAllViews(); } - menuTitle.setText(getResources().getStringArray(R.array.tr_preset_types)[mode]); + if (menuTitle != null) { + menuTitle.setText(getResources().getStringArray(R.array.tr_preset_types)[mode]); + } int[] presetValues = getPresetValues(mode); TypedArray keys = getResources().obtainTypedArray(R.array.tr_default_set_key); JSONObject parameters = defaultValuesToJSON(keys, presetValues); @@ -341,8 +341,8 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList if(row.getParent() == null) taskControls.addView(row); else { -// View separator = getLayoutInflater().inflate(R.layout.tea_separator, row); -// separator.setLayoutParams(new LayoutParams(1, LayoutParams.FILL_PARENT)); + // View separator = getLayoutInflater().inflate(R.layout.tea_separator, row); + // separator.setLayoutParams(new LayoutParams(1, LayoutParams.FILL_PARENT)); } LinearLayout displayRow = (LinearLayout)((TaskEditControlSet)set).getDisplayView(); @@ -467,9 +467,11 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList String[] keys = getResources().getStringArray(R.array.tr_default_set_key); - String descriptionKey = getString(R.string.tr_set_key_description); - String category = String.format("Category: %s\n", menuTitle.getText().toString()); //$NON-NLS-1$ - parameters.put(descriptionKey, category); + if (menuTitle != null) { + String descriptionKey = getString(R.string.tr_set_key_description); + String category = String.format("Category: %s\n", menuTitle.getText().toString()); //$NON-NLS-1$ + parameters.put(descriptionKey, category); + } for (int i = 0; i < controls.size(); i++) { if (presetValues[i] == -1) continue; TaskRabbitSetListener set = controls.get(i); @@ -530,8 +532,8 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList if(progressDialog == null) - progressDialog = DialogUtilities.progressDialog(this, - getString(R.string.DLG_please_wait)); + progressDialog = DialogUtilities.progressDialog(this, + getString(R.string.DLG_please_wait)); new Thread(new Runnable() { @Override public void run() { @@ -590,11 +592,11 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList switch (msg.what) { case -1: - AlertDialog.Builder adb = new AlertDialog.Builder(TaskRabbitActivity.this); - adb.setTitle(getString(R.string.tr_alert_title_fail)); - adb.setMessage(getString(R.string.tr_alert_message_fail)); - adb.setPositiveButton(getString(R.string.tr_alert_button_fail),null); - adb.show(); + AlertDialog.Builder adb = new AlertDialog.Builder(TaskRabbitActivity.this); + adb.setTitle(getString(R.string.tr_alert_title_fail)); + adb.setMessage(getString(R.string.tr_alert_message_fail)); + adb.setPositiveButton(getString(R.string.tr_alert_button_fail),null); + adb.show(); break; case 0: break; case 1: @@ -624,17 +626,31 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList } } private void loadLocation() { - LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); - if ( !locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) && - !locationManager.isProviderEnabled( LocationManager.NETWORK_PROVIDER )) { - buildAlertMessageNoGps(); - } - currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); + TaskRabbitLocationManager locationManager = new TaskRabbitLocationManager(this); - locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); - updateControlSetLocation(currentLocation); + if ( !locationManager.isLocationUpdatesEnabled()) { + buildAlertMessageNoGps(); + } + else { + currentLocation = locationManager.getLastKnownLocation(); + if (currentLocation == null) { + locationManager.getLocation(new LocationResult(){ + @Override + public void gotLocation(final Location location){ + //Got the location! + currentLocation = location; + updateControlSetLocation(currentLocation); + setupListView(); + } + } + ); + } + else { + updateControlSetLocation(currentLocation); + } + } } @@ -645,7 +661,7 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(final DialogInterface dialog, final int id) { - startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); + startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), REQUEST_CODE_ENABLE_GPS); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { @@ -701,6 +717,12 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList return; } } + else if (requestCode == REQUEST_CODE_ENABLE_GPS) { + + loadLocation(); + setupListView(); + } + else { for (TaskRabbitSetListener set : controls) { if (set instanceof ActivityResultSetListener) { @@ -709,8 +731,6 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList } } } - loadLocation(); - setupListView(); } @Override @@ -724,17 +744,6 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList } - /* - * (non-Javadoc) - * @see android.location.LocationListener#onLocationChanged(android.location.Location) - */ - @Override - public void onLocationChanged(Location location) { - - currentLocation = location; - updateControlSetLocation(currentLocation); - - } public void updateControlSetLocation (Location location) { for (TaskRabbitSetListener controlSet : controls) { if (TaskRabbitLocationControlSet.class.isAssignableFrom(controlSet.getClass())) { @@ -742,19 +751,6 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList } } } - @Override - public void onProviderDisabled(String provider) { - return; - } - @Override - public void onProviderEnabled(String provider) { - return; - } - @Override - public void onStatusChanged(String provider, int status, Bundle extras) { - return; - } - public boolean isLoggedIn() { return !TextUtils.isEmpty(Preferences.getStringValue(TASK_RABBIT_TOKEN)); @@ -801,9 +797,10 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList private void setupListView() { String[] keys = getResources().getStringArray(R.array.tr_preset_types); boolean locationEnabled = getIntent().getBooleanExtra(TaskRabbitControlSet.LOCATION_ENABLED, false); - if (!locationEnabled && !TaskRabbitControlSet.supportsCurrentLocation(currentLocation)) { + if (!locationEnabled && !TaskRabbitLocationManager.supportsCurrentLocation(currentLocation)) { keys = new String[]{ getResources().getString(R.string.tr_type_virtual)}; } + if (adapter == null) { adapter = new ArrayAdapter(this, R.layout.task_rabbit_menu_row, keys); menuList = new ListView(this); menuList.setAdapter(adapter); @@ -820,6 +817,14 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList menuPopover.dismiss(); } }); + } + else { + adapter = new ArrayAdapter(this, R.layout.task_rabbit_menu_row, keys); + menuList.setAdapter(adapter); + /*adapter.clear(); + adapter.addAll(keys); + adapter.notifyDataSetChanged();*/ + } } diff --git a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitControlSet.java b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitControlSet.java index b3fbc8abc..1c63de0a5 100644 --- a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitControlSet.java @@ -6,12 +6,8 @@ import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; -import android.content.Context; import android.content.Intent; import android.location.Location; -import android.location.LocationListener; -import android.location.LocationManager; -import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.app.Fragment; @@ -22,7 +18,6 @@ import android.widget.TextView; import android.widget.Toast; import com.timsu.astrid.R; -import com.timsu.astrid.data.location.GeoPoint; import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.service.RestClient; @@ -32,11 +27,11 @@ import com.todoroo.astrid.actfm.OAuthLoginActivity; import com.todoroo.astrid.activity.TaskEditFragment; import com.todoroo.astrid.data.Task; import com.todoroo.astrid.helper.TaskEditControlSet; +import com.todoroo.astrid.taskrabbit.TaskRabbitLocationManager.LocationResult; -public class TaskRabbitControlSet extends TaskEditControlSet implements AssignedChangedListener, LocationListener { +public class TaskRabbitControlSet extends TaskEditControlSet implements AssignedChangedListener { - private static final int RADIUS_250_MILES = 400000; public interface TaskRabbitSetListener { public void readFromModel(JSONObject json, String key); @@ -58,18 +53,9 @@ public class TaskRabbitControlSet extends TaskEditControlSet implements Assigned public boolean isEnabledForTRLocation = false; public static final String LOCATION_ENABLED = "location_enabled"; //$NON-NLS-1$ - private static final GeoPoint[] supportedLocations = { - new GeoPoint(42358430, -71059770), // - new GeoPoint(37739230, -122439880), - new GeoPoint(40714350, -74005970), - new GeoPoint(41878110, -8762980), - new GeoPoint(34052230, -118243680), - new GeoPoint(33717470, -117831140) - }; - private final Fragment fragment; protected final TextView displayText; - private LocationManager locationManager; + private TaskRabbitLocationManager locationManager; public static final int REQUEST_CODE_TASK_RABBIT_ACTIVITY = 5; public static final String DATA_RESPONSE = "response"; //$NON-NLS-1$ @@ -276,62 +262,27 @@ public class TaskRabbitControlSet extends TaskEditControlSet implements Assigned private void loadLocation() { - locationManager = (LocationManager) fragment.getActivity().getSystemService(Context.LOCATION_SERVICE); - currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); + + locationManager = new TaskRabbitLocationManager(fragment.getActivity()); + currentLocation = locationManager.getLastKnownLocation(); if (currentLocation == null) { - try { - locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); - } catch (IllegalArgumentException e) { - // No gps - isEnabledForTRLocation = false; + locationManager.getLocation(new LocationResult(){ + @Override + public void gotLocation(final Location location){ + //Got the location! + currentLocation = location; + isEnabledForTRLocation = TaskRabbitLocationManager.supportsCurrentLocation(currentLocation); + } } + ); } else { - isEnabledForTRLocation = supportsCurrentLocation(currentLocation); - } - } - - public static boolean supportsCurrentLocation(Location location) { - if (location == null) return false; - for (GeoPoint point : supportedLocations){ - Location city = new Location(""); //$NON-NLS-1$ - city.setLatitude(point.getLatitudeE6()/1E6); - city.setLongitude(point.getLongitudeE6()/1E6); - float distance = location.distanceTo(city); - if (distance < RADIUS_250_MILES) { - return true; - } + isEnabledForTRLocation = TaskRabbitLocationManager.supportsCurrentLocation(currentLocation); } - return false; } - @Override - public void onLocationChanged(Location location) { - currentLocation = location; - isEnabledForTRLocation = supportsCurrentLocation(currentLocation); - locationManager.removeUpdates(this); - locationManager = null; - - } - - @Override - public void onProviderDisabled(String provider) { - // - - } - - @Override - public void onProviderEnabled(String provider) { - // - - } - @Override - public void onStatusChanged(String provider, int status, Bundle extras) { - // - - } @Override public boolean didPostToTaskRabbit() { diff --git a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitLocationManager.java b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitLocationManager.java new file mode 100644 index 000000000..ea2d31c36 --- /dev/null +++ b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitLocationManager.java @@ -0,0 +1,152 @@ +package com.todoroo.astrid.taskrabbit; + +import java.util.Timer; +import java.util.TimerTask; + +import android.content.Context; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; +import android.os.Bundle; + +import com.timsu.astrid.data.location.GeoPoint; + +public class TaskRabbitLocationManager { + Timer timer1; + LocationManager lm; + LocationResult locationResult; + boolean gps_enabled=false; + boolean network_enabled=false; + Context context; + + private static final int RADIUS_250_MILES = 400000; + + public TaskRabbitLocationManager(Context context) { + this.context = context; + lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); + + } + private static final GeoPoint[] supportedLocations = { + new GeoPoint(42358430, -71059770), // + new GeoPoint(37739230, -122439880), + new GeoPoint(40714350, -74005970), + new GeoPoint(41878110, -8762980), + new GeoPoint(34052230, -118243680), + new GeoPoint(33717470, -117831140) + }; + public boolean isLocationUpdatesEnabled() { + boolean provider_enabled=false; + try{provider_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){} + try{provider_enabled=provider_enabled || lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){} + return provider_enabled; + } + public Location getLastKnownLocation() + { + boolean gps_supported=false; + boolean network_supported=false; + Location location = null; + try{gps_supported=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){} + try{network_supported=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){} + if(!gps_supported && !network_supported) + return null; + + if(gps_supported) {location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); + } + if(location == null && network_supported) {location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); + } + return location; + } + + public static boolean supportsCurrentLocation(Location location) { + if (location == null) return false; + for (GeoPoint point : supportedLocations){ + Location city = new Location(""); //$NON-NLS-1$ + city.setLatitude(point.getLatitudeE6()/1E6); + city.setLongitude(point.getLongitudeE6()/1E6); + float distance = location.distanceTo(city); + if (distance < RADIUS_250_MILES) { + return true; + } + } + return false; + } + + public boolean getLocation(LocationResult result) + { + locationResult=result; + try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){} + try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){} + + if(!gps_enabled && !network_enabled) + return false; + + if(gps_enabled) + lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps); + if(network_enabled) + lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork); + timer1=new Timer(); + timer1.schedule(new GetLastLocation(), 20000); + return true; + } + + LocationListener locationListenerGps = new LocationListener() { + public void onLocationChanged(Location location) { + timer1.cancel(); + locationResult.gotLocation(location); + lm.removeUpdates(this); + lm.removeUpdates(locationListenerNetwork); + } + public void onProviderDisabled(String provider) {} + public void onProviderEnabled(String provider) {} + public void onStatusChanged(String provider, int status, Bundle extras) {} + }; + + LocationListener locationListenerNetwork = new LocationListener() { + public void onLocationChanged(Location location) { + timer1.cancel(); + locationResult.gotLocation(location); + lm.removeUpdates(this); + lm.removeUpdates(locationListenerGps); + } + public void onProviderDisabled(String provider) {} + public void onProviderEnabled(String provider) {} + public void onStatusChanged(String provider, int status, Bundle extras) {} + }; + + class GetLastLocation extends TimerTask { + @Override + public void run() { + lm.removeUpdates(locationListenerGps); + lm.removeUpdates(locationListenerNetwork); + + Location net_loc=null, gps_loc=null; + if(gps_enabled) + gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); + if(network_enabled) + net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); + + //if there are both values use the latest one + if(gps_loc!=null && net_loc!=null){ + if(gps_loc.getTime()>net_loc.getTime()) + locationResult.gotLocation(gps_loc); + else + locationResult.gotLocation(net_loc); + return; + } + + if(gps_loc!=null){ + locationResult.gotLocation(gps_loc); + return; + } + if(net_loc!=null){ + locationResult.gotLocation(net_loc); + return; + } + locationResult.gotLocation(null); + } + } + + public static abstract class LocationResult{ + public abstract void gotLocation(Location location); + } +}