diff --git a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitActivity.java b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitActivity.java index 635b3e2fc..3b2ce4c77 100644 --- a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitActivity.java +++ b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitActivity.java @@ -49,7 +49,6 @@ import android.widget.PopupWindow.OnDismissListener; import android.widget.TextView; import android.widget.Toast; -import com.google.android.maps.GeoPoint; import com.timsu.astrid.R; import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.DependencyInjectionService; @@ -105,14 +104,6 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList private ExceptionService exceptionService; - 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)}; /** true if editing started with a new task */ boolean isNewTask = false; @@ -651,7 +642,7 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); - locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this); + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); updateControlSetLocation(currentLocation); } @@ -775,24 +766,6 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList return !TextUtils.isEmpty(Preferences.getStringValue(TASK_RABBIT_TOKEN)); } - public boolean supportsCurrentLocation() { - - //TODO test this - if(true) return true; - if (currentLocation == 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 = currentLocation.distanceTo(city); - if (distance < 400000) { //250 mi radius - return true; - } - } - return false; - } - - /* Menu Popover */ @@ -833,7 +806,8 @@ public class TaskRabbitActivity extends FragmentActivity implements LocationList private void setupListView() { String[] keys = getResources().getStringArray(R.array.tr_preset_types); - if (!supportsCurrentLocation()) { + boolean locationEnabled = getIntent().getBooleanExtra(TaskRabbitControlSet.LOCATION_ENABLED, false); + if (!locationEnabled) { keys = new String[]{ getResources().getString(R.string.tr_type_virtual)}; } adapter = new ArrayAdapter(this, R.layout.task_rabbit_menu_row, keys); diff --git a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitControlSet.java b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitControlSet.java index 312927878..c54df1ee1 100644 --- a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitControlSet.java @@ -7,9 +7,13 @@ import org.json.JSONObject; import android.app.Activity; import android.app.AlertDialog; +import android.content.Context; import android.content.DialogInterface; 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.provider.Settings; @@ -22,6 +26,7 @@ import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; +import com.google.android.maps.GeoPoint; import com.timsu.astrid.R; import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.DependencyInjectionService; @@ -33,7 +38,7 @@ import com.todoroo.astrid.activity.TaskEditFragment; import com.todoroo.astrid.data.Task; import com.todoroo.astrid.helper.TaskEditControlSet; -public class TaskRabbitControlSet extends TaskEditControlSet implements AssignedChangedListener { +public class TaskRabbitControlSet extends TaskEditControlSet implements AssignedChangedListener, LocationListener { public interface TaskRabbitSetListener { @@ -54,12 +59,26 @@ public class TaskRabbitControlSet extends TaskEditControlSet implements Assigned /** true if editing started with a new task */ - boolean isNewTask = false; + private final boolean isNewTask = false; private Location currentLocation; + public boolean isEnabledForTRLocation = false; + public static final String LOCATION_ENABLED = "location_enabled"; //$NON-NLS-1$ + + + 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; private LinearLayout row; protected final TextView displayText; + LocationManager locationManager; public static final int REQUEST_CODE_TASK_RABBIT_ACTIVITY = 5; public static final String DATA_RESPONSE = "response"; //$NON-NLS-1$ @@ -82,6 +101,7 @@ public class TaskRabbitControlSet extends TaskEditControlSet implements Assigned if (getView() != null) { getView().setOnClickListener(getDisplayClickListener()); } + loadLocation(); } @@ -104,9 +124,10 @@ public class TaskRabbitControlSet extends TaskEditControlSet implements Assigned } - private void showTaskRabbitActivity() { + public void showTaskRabbitActivity() { Intent intent = new Intent(fragment.getActivity(), TaskRabbitActivity.class); intent.putExtra(TaskEditFragment.TOKEN_ID, model.getId()); + intent.putExtra(LOCATION_ENABLED, isEnabledForTRLocation); fragment.startActivityForResult(intent, REQUEST_CODE_TASK_RABBIT_ACTIVITY); } protected OnClickListener getDisplayClickListener() { @@ -196,7 +217,6 @@ public class TaskRabbitControlSet extends TaskEditControlSet implements Assigned } public boolean activityResult (int requestCode, int resultCode, Intent data) { - Log.d("The actiivty result request code", "Rerjwklrw" + REQUEST_CODE_TASK_RABBIT_ACTIVITY); if (requestCode == REQUEST_CODE_TASK_RABBIT_ACTIVITY && resultCode == Activity.RESULT_OK){ String result = data.getStringExtra(OAuthLoginActivity.DATA_RESPONSE); if (TextUtils.isEmpty(result)) { @@ -276,4 +296,66 @@ public class TaskRabbitControlSet extends TaskEditControlSet implements Assigned } + + + private void loadLocation() { + Log.d("TRControlSet", "gJgHFDSJKGFHSJKFGHDSJKFGSJDGFSDJKFGDSJKFGSHJDFHS:LDFHS:FJKSDJFL:"); + locationManager = (LocationManager) fragment.getActivity().getSystemService(Context.LOCATION_SERVICE); + currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); + if (currentLocation == null) { + Log.d("TRControlSet", "Fail current location is null"); + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); + } + else { + Log.d("TRControlSet", "loading location and checking if we suppor it"); + isEnabledForTRLocation = supportsCurrentLocation(); + } + } + + public boolean supportsCurrentLocation() { + + //TODO test this + if (currentLocation == null) return false; + for (GeoPoint point : supportedLocations){ + Log.d("TRControlSet", "Searching if we support current location"); + Location city = new Location(""); //$NON-NLS-1$ + city.setLatitude(point.getLatitudeE6()/1E6); + city.setLongitude(point.getLongitudeE6()/1E6); + float distance = currentLocation.distanceTo(city); + if (distance < 400000) { //250 mi radius + return true; + } + } + return false; + } + + + + + @Override + public void onLocationChanged(Location location) { + + Log.d("TRControlSet", "Location changed and found"); + currentLocation = location; + isEnabledForTRLocation = supportsCurrentLocation(); + locationManager.removeUpdates(this); + locationManager = null; + + } + @Override + public void onProviderDisabled(String provider) { + // TODO Auto-generated method stub + + } + + @Override + public void onProviderEnabled(String provider) { + // TODO Auto-generated method stub + + } + @Override + public void onStatusChanged(String provider, int status, Bundle extras) { + // TODO Auto-generated method stub + + } } diff --git a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitLocationControlSet.java b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitLocationControlSet.java index c50459455..149b395d7 100644 --- a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitLocationControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitLocationControlSet.java @@ -89,7 +89,6 @@ public class TaskRabbitLocationControlSet extends TaskEditControlSet implements displayEdit.setText(getLocationText()); getAddressFromLocation(location); - Log.d("TASK RABBIT CONTROL SET FOUND CODE", "THE LAT IS: " + lat + " LNG IS: " + lng); return true; } @@ -142,19 +141,16 @@ public class TaskRabbitLocationControlSet extends TaskEditControlSet implements @Override public void saveToJSON(JSONObject json, String key) throws JSONException { json.put(key, getTaskLocation()); - Log.d("LOCATION SAVE", json.toString()); } @Override public void writeToJSON(JSONObject json, String key) throws JSONException { - Log.d("LOCATION", "LOCEIJREGHSK"); JSONArray locations = json.optJSONArray("other_locations_attributes"); if (locations == null) { locations = new JSONArray(); } locations.put(getTaskLocation()); json.put("other_locations_attributes", locations); - Log.d("LOCATION", "LALALALAL" + json.toString()); } @@ -173,13 +169,6 @@ public class TaskRabbitLocationControlSet extends TaskEditControlSet implements locationObject.put("lng", location.getLongitude()); locationObject.put("lat", location.getLatitude()); } - else { - // TODO FIX THIS DON"T PUT IN ADDRESSES - locationObject.put("address", "300 Beale"); - locationObject.put("city", "San Francisco"); - locationObject.put("state", "CA"); - locationObject.put("zip", "94158"); - } return locationObject; } catch (JSONException e) { diff --git a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitMapActivity.java b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitMapActivity.java index b30f8a63d..15795238f 100644 --- a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitMapActivity.java +++ b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitMapActivity.java @@ -17,6 +17,8 @@ import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.provider.Settings; +import android.text.TextUtils; +import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.ImageButton; @@ -37,6 +39,7 @@ public class TaskRabbitMapActivity extends MapActivity implements LocationListen public Location location; private EditText searchText; private TaskRabbitMapOverlayItem currentOverlayItem; + private String locationAddress; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) @@ -50,7 +53,7 @@ public class TaskRabbitMapActivity extends MapActivity implements LocationListen LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); - locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this); + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); List mapOverlays = mapView.getOverlays(); @@ -66,7 +69,7 @@ public class TaskRabbitMapActivity extends MapActivity implements LocationListen currentOverlayItem.addOverlay(overlayitem); mapOverlays.add(currentOverlayItem); - + getAddressFromLocation(lastKnownLocation); mapController.animateTo(point); mapController.setZoom(17); mapView.invalidate(); @@ -98,6 +101,11 @@ public class TaskRabbitMapActivity extends MapActivity implements LocationListen public String getSearchText() { return searchText.getText().toString(); } + public void setSearchTextForCurrentAddress() { + if(!TextUtils.isEmpty(locationAddress)) { + searchText.setText(locationAddress); + } + } private void buildAlertMessageNoGps() { final AlertDialog.Builder builder = new AlertDialog.Builder(this); @@ -158,6 +166,7 @@ public class TaskRabbitMapActivity extends MapActivity implements LocationListen if(addresses != null && addresses.size() > 0) { + updateAddress(addresses.get(0)); GeoPoint q = new GeoPoint( (int) (addresses.get(0).getLatitude() * 1E6), (int) (addresses.get(0).getLongitude() * 1E6)); @@ -192,6 +201,34 @@ public class TaskRabbitMapActivity extends MapActivity implements LocationListen thread.start(); } + + + + private void getAddressFromLocation(Location location){ + try { + Geocoder geocoder = new Geocoder(this, Locale.getDefault()); + // Acquire a reference to the system Location Manager + List
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); + if (addresses != null){ + for (Address address : addresses){ + updateAddress(address); + } + } + } catch (Exception e) { + Log.d("Location error", e.toString()); + } + } + private void updateAddress(Address address){ + if(address.getLocality() != null && address.getPostalCode() != null){ +// locationAddress = (address.getLocality() + ", " + address.getPostalCode()); + locationAddress = ""; + for (int i = 0; i < address.getMaxAddressLineIndex(); i++){ + locationAddress += address.getAddressLine(i) + ", "; + } + } + } + + @Override protected boolean isRouteDisplayed() { return false; diff --git a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitMapOverlayItem.java b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitMapOverlayItem.java index 38e9b7265..c96881330 100644 --- a/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitMapOverlayItem.java +++ b/astrid/plugin-src/com/todoroo/astrid/taskrabbit/TaskRabbitMapOverlayItem.java @@ -94,6 +94,8 @@ public class TaskRabbitMapOverlayItem extends ItemizedOverlay { } }).setNegativeButton(android.R.string.cancel, null); dialog.show(); + mActivity.setSearchTextForCurrentAddress(); + return true; } diff --git a/astrid/res/drawable/task_rabbit_image.jpg b/astrid/res/drawable/task_rabbit_image.jpg new file mode 100644 index 000000000..150303fcb Binary files /dev/null and b/astrid/res/drawable/task_rabbit_image.jpg differ diff --git a/astrid/res/drawable/task_rabbit_logo.jpg b/astrid/res/drawable/task_rabbit_logo.jpg new file mode 100644 index 000000000..d7e195049 Binary files /dev/null and b/astrid/res/drawable/task_rabbit_logo.jpg differ diff --git a/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java b/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java index a0fc9ca9e..dc2960364 100755 --- a/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Locale; import android.app.Activity; import android.app.AlertDialog; @@ -417,6 +418,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { webServices.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); webServices.setPadding(10, 5, 10, 10); + webServices.taskRabbitControl = taskRabbitControl; webServices.setTask(model); } else { webServices.refresh(); @@ -508,8 +510,6 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { // EditPeopleControlSet relies on the "tags" transitory created by the // TagsControlSet, so we put the tags control before the people control - taskRabbitControl = new TaskRabbitControlSet(this, R.layout.control_set_default_display); - controls.add(taskRabbitControl); controls.add(peopleControlSet = new EditPeopleControlSet(getActivity(), this, R.layout.control_set_assigned, @@ -517,8 +517,12 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { R.string.actfm_EPA_assign_label_long, REQUEST_LOG_IN)); controlSetMap.put(getString(R.string.TEA_ctrl_who_pref), peopleControlSet); - peopleControlSet.addListener(taskRabbitControl); + if(Locale.getDefault().getCountry().equals("US")) { + taskRabbitControl = new TaskRabbitControlSet(this, R.layout.control_set_default_display); + controls.add(taskRabbitControl); + peopleControlSet.addListener(taskRabbitControl); + } RepeatControlSet repeatControls = new RepeatControlSet(getActivity(), R.layout.control_set_repeat, diff --git a/astrid/src/com/todoroo/astrid/ui/WebServicesView.java b/astrid/src/com/todoroo/astrid/ui/WebServicesView.java index 1e165737d..bde7eb75d 100644 --- a/astrid/src/com/todoroo/astrid/ui/WebServicesView.java +++ b/astrid/src/com/todoroo/astrid/ui/WebServicesView.java @@ -42,6 +42,7 @@ import com.todoroo.andlib.service.RestClient; import com.todoroo.astrid.data.Task; import com.todoroo.astrid.helper.AmazonRequestsHelper; import com.todoroo.astrid.producteev.api.StringEscapeUtils; +import com.todoroo.astrid.taskrabbit.TaskRabbitControlSet; import com.todoroo.astrid.utility.Constants; @SuppressWarnings("nls") @@ -60,6 +61,7 @@ public class WebServicesView extends LinearLayout { private final DisplayMetrics metrics = new DisplayMetrics(); private LayoutInflater inflater; private Activity activity; + public TaskRabbitControlSet taskRabbitControl; private LinearLayout.LayoutParams rowParams; @@ -403,6 +405,8 @@ public class WebServicesView extends LinearLayout { * Initialize Google search results */ protected void initializeTaskRabbit() { + + if(taskRabbitControl != null && taskRabbitControl.isEnabledForTRLocation == true) { addSectionHeader("Task Rabbit"); final LinearLayout body = new LinearLayout(activity); @@ -417,14 +421,30 @@ public class WebServicesView extends LinearLayout { imageView.setImageResource(R.drawable.task_rabbit_logo); body.addView(imageView); - body.setVisibility(View.GONE); + body.setOnClickListener(new OnClickListener() { + + @Override + public void onClick(View v) { + taskRabbitControl.showTaskRabbitActivity(); + + } + }); + addView(body); + addSectionDivider(); - if (body.getVisibility() == View.VISIBLE){ - addSectionDivider(); + } + else { } } + + + + + + + protected View inflateRow(ViewGroup body, String imageUrl, String title, String subtitle, String tag) { View view = inflater.inflate(R.layout.web_service_row, body, false); @@ -500,13 +520,14 @@ public class WebServicesView extends LinearLayout { } - private void addSectionDivider() { + private View addSectionDivider() { View view = new View(getContext()); LayoutParams mlp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 1); mlp.setMargins(10, 20, 10, 20); view.setLayoutParams(mlp); view.setBackgroundColor(Color.GRAY); addView(view); + return view; } private void addSectionHeader(String string) {