diff --git a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitActivity.java b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitActivity.java index 6612a9f3a..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); } @@ -628,18 +626,30 @@ 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 )) { + + TaskRabbitLocationManager locationManager = new TaskRabbitLocationManager(this); + + if ( !locationManager.isLocationUpdatesEnabled()) { buildAlertMessageNoGps(); } else { - - currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); - - locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); - updateControlSetLocation(currentLocation); + 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); + } } } @@ -651,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() { @@ -707,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) { @@ -715,8 +731,6 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList } } } - loadLocation(); - setupListView(); } @Override @@ -730,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())) { @@ -748,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)); @@ -807,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); @@ -826,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 index bf15c3a61..ea2d31c36 100644 --- a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitLocationManager.java +++ b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitLocationManager.java @@ -9,25 +9,74 @@ 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); - public boolean getLocation(Context context, LocationResult result) + } + 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() { - //I use LocationResult callback class to pass location value from MyLocation to user code. - locationResult=result; - if(lm==null) - lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); + 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; + } - //exceptions will be thrown if provider is not permitted. + 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){} - //don't start listeners if no provider is enabled if(!gps_enabled && !network_enabled) return false; @@ -67,33 +116,33 @@ public class TaskRabbitLocationManager { 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); + 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); } }