From a9d0507f56ac99827849b4686ce09e06e02d5083 Mon Sep 17 00:00:00 2001 From: aFedasenka Date: Tue, 26 Jun 2012 00:28:22 +0200 Subject: [PATCH] Time-limit description fixed. Refresh map after parking point deletion fixed. Choose Type of Parking dialog relayouted. Time picker control now works in both 24- and 12-hour mode (it is taken from the device settings). Bugfixes. --- OsmAnd/res/layout/parking_set_type.xml | 70 ++-- OsmAnd/res/values/strings.xml | 12 + .../src/net/osmand/plus/OsmandSettings.java | 12 +- .../plus/parkingpoint/CalendarEvent.java | 369 ------------------ .../parkingpoint/ParkingPositionLayer.java | 16 +- .../parkingpoint/ParkingPositionPlugin.java | 112 ++++-- 6 files changed, 156 insertions(+), 435 deletions(-) delete mode 100644 OsmAnd/src/net/osmand/plus/parkingpoint/CalendarEvent.java diff --git a/OsmAnd/res/layout/parking_set_type.xml b/OsmAnd/res/layout/parking_set_type.xml index 49b6dfd975..32f253d492 100644 --- a/OsmAnd/res/layout/parking_set_type.xml +++ b/OsmAnd/res/layout/parking_set_type.xml @@ -1,27 +1,53 @@ - + android:layout_height="wrap_content" + android:gravity="center_horizontal" > - + - + - + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 9033c48472..9794bd5904 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -9,10 +9,22 @@ 1. All your modified/created strings are in the top of the file (to make easier find what's translated). PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy --> + Pick up the car from parking + Warning + Notification to pick up your car was previously added to your Calendar. It will remain there until you will delete it manually. + Set the time limit of parking + Do you want to remove the location of the parked car? + Delete a parking marker + Choose the type of parking + Time-limited + Time-unlimited Add a notification to Calendar application Time-limited parking Time-unlimited parking The position of your parked car. %1$s + To pick up the car at: + PM + AM Parking point This plugin allows to store the location of your parked car. Parking Position Plugin diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 6cebad0b12..0bdace6c54 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -983,6 +983,7 @@ public class OsmandSettings { public final static String PARKING_POINT_LON = "parking_point_lon"; //$NON-NLS-1$ public final static String PARKING_TYPE = "parking_type"; //$NON-NLS-1$ public final static String PARKING_TIME = "parking_limit_time"; //$//$NON-NLS-1$ + public final static String PARKING_EVENT_ADDED = "parking_event_added"; //$//$NON-NLS-1$ public LatLon getParkingPosition() { float lat = globalPreferences.getFloat(PARKING_POINT_LAT, 0); @@ -997,12 +998,21 @@ public class OsmandSettings { return globalPreferences.getBoolean(PARKING_TYPE, false); } + public boolean isParkingEventAdded() { + return globalPreferences.getBoolean(PARKING_EVENT_ADDED, false); + } + + public boolean addParkingEvent(boolean added) { + return globalPreferences.edit().putBoolean(PARKING_EVENT_ADDED, added).commit(); + } + public long getParkingTime() { return globalPreferences.getLong(PARKING_TIME, -1); } public boolean clearParkingPosition() { - return globalPreferences.edit().remove(PARKING_POINT_LAT).remove(PARKING_POINT_LON).remove(PARKING_TYPE).remove(PARKING_TIME).commit(); + return globalPreferences.edit().remove(PARKING_POINT_LAT).remove(PARKING_POINT_LON).remove(PARKING_TYPE) + .remove(PARKING_TIME).remove(PARKING_EVENT_ADDED).commit(); } public boolean setParkingPosition(double latitude, double longitude) { diff --git a/OsmAnd/src/net/osmand/plus/parkingpoint/CalendarEvent.java b/OsmAnd/src/net/osmand/plus/parkingpoint/CalendarEvent.java deleted file mode 100644 index 74f0cb7089..0000000000 --- a/OsmAnd/src/net/osmand/plus/parkingpoint/CalendarEvent.java +++ /dev/null @@ -1,369 +0,0 @@ -package net.osmand.plus.parkingpoint; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map.Entry; - -import android.content.ContentValues; -import android.content.Context; -import android.database.Cursor; -import android.net.Uri; -import android.os.Build; -import android.util.Log; - -public class CalendarEvent -{ - private int calendarID = -1; - static String contentProvider; - static Uri remindersUri; - static Uri eventsUri; - static Uri calendars; - static String eventsTable; - static long EVENT_DURATION = 3600000; - static long REMINDER_TIME = 0; - - public Context context = null; - - /** - * @param a - * Constructor - */ - public CalendarEvent(Context c) - { - int sdk; - - try - { - sdk = new Integer(Build.VERSION.SDK_INT).intValue(); - } - catch(Exception e) - { - sdk = 9; - } - - this.context = c; - if(sdk >= 8) - { - //2.2 or higher - eventsTable = "view_events"; - contentProvider = "com.android.calendar"; - } - else - { - //anything older - eventsTable = "Events"; - contentProvider = "calendar"; - } - - remindersUri = Uri.parse(String.format("content://%s/reminders",contentProvider)); - eventsUri = Uri.parse(String.format("content://%s/events",contentProvider)); - calendars = Uri.parse(String.format("content://%s/calendars",contentProvider)); - } - - - /** - * @param strTitle - * as the Title of the event - * @param strDescription - * as the description of the event - * @param startTime - * as the time in millis the event begins - */ - public long insertEvent(String strTitle, String strDescription, long startTime, long endTime) throws Exception - { - if (calendarID != -1) - { - // Source: http://sgap.springnote.com/pages/5150959 - - ContentValues event = new ContentValues(); - event.put(EventColumns.ID, calendarID); - event.put(EventColumns.TITLE, strTitle); - event.put(EventColumns.DESC, strDescription); - event.put(EventColumns.START, startTime); - event.put(EventColumns.END, endTime); - event.put(EventColumns.ALLDAY, "0"); - event.put(EventColumns.STATUS, "1"); - event.put(EventColumns.VIS, "0"); - event.put(EventColumns.TRANS, "0"); - event.put(EventColumns.ALARM, "1"); - return Long.parseLong(context.getContentResolver().insert(eventsUri, event).getLastPathSegment()); - - } - return -1; - //throw exception - - } - - public long insertEvent(HashMap args) - { - //Convert to use ContentValues? - if (calendarID != -1) - { // Source: http://sgap.springnote.com/pages/5150959 - ContentValues event = new ContentValues(); - event.put(EventColumns.ID, calendarID); - - event.putAll(HashMapToContentValues(args)); - event.put(EventColumns.ALLDAY, "0"); - event.put(EventColumns.STATUS, "1"); - event.put(EventColumns.VIS, "0"); - event.put(EventColumns.TRANS, "0"); - event.put(EventColumns.ALARM, "1"); - - return Long.parseLong(context.getContentResolver().insert(eventsUri, event).getLastPathSegment()); - } - //throw exception - return -1; - - } - - public void addReminder(long eventID) - { - try - { - if (contains(eventID)) - { - ContentValues values = new ContentValues(); - - values.put("event_id", eventID); - values.put("method", 1); - values.put("minutes", REMINDER_TIME); - context.getContentResolver().insert(remindersUri, values); - - Log.d("Calendar Event", "Reminder Added for event " + eventID); - - } - else - Log.d("Calendar Event", "Reminder Not Added"); - } - catch(Exception e) - { - - } - - } - - public void updateReminder(long eventID, int minutes) - { - - Uri updateReminderUri = Uri.withAppendedPath(remindersUri, String.valueOf(eventID)); - ContentValues values = new ContentValues(); - values.put(ReminderColumns.ID, eventID); - values.put(ReminderColumns.METHOD, 1); - values.put(ReminderColumns.TIME, minutes); - - context.getContentResolver().update(updateReminderUri, values, null, null); - Log.d("Calendar Event", "Alarm Updated"); - } - - public void deleteReminder(long eventID) - { - - Uri reminderUri = Uri.withAppendedPath(remindersUri, String.valueOf(eventID)); - context.getContentResolver().delete(reminderUri, null, null); - Log.d("Calendar Event", "Reminder deleted"); - } - - public boolean containsAlarm(long eventID) - { - String selection = ReminderColumns.ID + "=" + eventID; - String[] projection = new String[] { ReminderColumns.METHOD }; - Cursor cursor = context.getContentResolver().query(remindersUri, projection, selection, null, null); - - if (cursor.getCount() > 0) - { - cursor.close(); - Log.d("Calendar Event", "Contains Reminder"); - return true; - } - if(cursor != null) - cursor.close(); - Log.d("Calendar Event", "Does not contain a reminder for " + eventID); - return false; - } - - /** - * Removes the eventID passed Returns the number of rows removed - * - * @param iEventID - * as the eventID to remove - */ - public int removeEvent(long iEventID) - { - Log.d("studentspet", "removing event.. " + iEventID); - if (calendarID != -1 && iEventID != -1) - { - if (this.contains(iEventID)) - { - Uri deleteEventUri = Uri.withAppendedPath(eventsUri, String.valueOf(iEventID)); - return context.getContentResolver().delete(deleteEventUri, null, null); - } - } - return -1; - } - - /** - * Returns boolean specifying if the passed EventID is in the Calendar - * - * @param iEventID - */ - public boolean contains(long iEventID) - { - if (calendarID != -1) - { - //Wrong table name for android 2.2 - String[] projection = new String[] { EventColumns.TITLE, CalendarColumns.ID }; - Cursor managedCursor = context.getContentResolver().query(eventsUri, projection, eventsTable+"._id=" + iEventID, null, null); - while (managedCursor.moveToNext()) - { - managedCursor.close(); - return true; - } - if(managedCursor != null) - managedCursor.close(); - } - - - return false; - } - - /** - * Returns the number of rows updated - * - * @param iEventID - * as the eventID to update - * @param whereArgs - * as the Set arguments see Where class - */ - public int updateEvent(long iEventID, HashMap args) - { - if (calendarID != -1) - { - if (contains(iEventID)) - { - Uri updateEventUri = Uri.withAppendedPath(eventsUri, String.valueOf(iEventID)); - return context.getContentResolver().update(updateEventUri, HashMapToContentValues(args), null, null); - } - - } - - return -1; - - } - - private ContentValues HashMapToContentValues(HashMap hm) - { - ContentValues cv = new ContentValues(); - for (Entry kvp : hm.entrySet()) - { - cv.put(kvp.getKey(), kvp.getValue()); - } - - return cv; - - } - - /** - * @return ArrayList as list of calendars available - */ - //needs rewritten - public ArrayList getCalendars() - { - ArrayList calendarList = new ArrayList(); - String[] projection = new String[] { CalendarColumns.ID, CalendarColumns.NAME, CalendarColumns.DISPLAYNAME}; - Cursor managedCursor = context.getContentResolver().query(calendars, projection, null, null, null); - - if (managedCursor.getCount() > 0) - { - int nameColumn = managedCursor.getColumnIndex(CalendarColumns.NAME); - int displayNameColumn = managedCursor.getColumnIndex(CalendarColumns.DISPLAYNAME); - int idColumn = managedCursor.getColumnIndex(CalendarColumns.ID); - while (managedCursor.moveToNext()) - { - if (!managedCursor.isNull(nameColumn)) - calendarList.add(new CalendarListItem(managedCursor.getString(nameColumn), managedCursor.getLong(idColumn))); - else - calendarList.add(new CalendarListItem(managedCursor.getString(displayNameColumn), managedCursor.getLong(idColumn))); - } - } - if(managedCursor != null) - managedCursor.close(); - - return calendarList; - - } - - /** - * - * @return calendarID as selected calendar's id - */ - public long getSelectedCalendar() - { - return calendarID; - } - - /** - * @param newCalendarID - * as new calendar - */ - public void setSelectedCalendar(int newCalendarID) - { - calendarID = newCalendarID; - } - - class CalendarException extends Exception - { - private static final long serialVersionUID = 0; - - public CalendarException(String desc) - { - super(desc); - } - } - - - - - public static class CalendarListItem - { - - String name; - long id; - - public CalendarListItem(String calendarName, long calendarID) - { - this.name = calendarName; - this.id = calendarID; - } - } - - public static class EventColumns - { - public static final String ID = "calendar_id"; - public static final String TITLE = "title"; - public static final String DESC = "description"; - public static final String START = "dtstart"; - public static final String END = "dtend"; - public static final String ALLDAY = "allDay"; - public static final String STATUS = "eventStatus"; - public static final String VIS = "visibility"; - public static final String TRANS = "transparency"; - public static final String ALARM = "hasAlarm"; - } - - public static class ReminderColumns - { - public static final String ID = "event_id"; - public static final String METHOD = "method"; - public static final String TIME = "minutes"; - } - - public static class CalendarColumns - { - public static final String ID = "_id"; - public static final String TITLE = "title"; - public static final String NAME = "name"; - public static final String DISPLAYNAME = "displayName"; - } - - -} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/parkingpoint/ParkingPositionLayer.java b/OsmAnd/src/net/osmand/plus/parkingpoint/ParkingPositionLayer.java index 7086e29dbe..09afbd3062 100644 --- a/OsmAnd/src/net/osmand/plus/parkingpoint/ParkingPositionLayer.java +++ b/OsmAnd/src/net/osmand/plus/parkingpoint/ParkingPositionLayer.java @@ -26,6 +26,7 @@ import android.graphics.Paint.Style; import android.graphics.PointF; import android.graphics.RectF; import android.location.Location; +import android.text.format.DateFormat; import android.text.format.Time; import android.util.DisplayMetrics; import android.view.View; @@ -106,6 +107,8 @@ public class ParkingPositionLayer extends OsmandMapLayer implements ContextMenuL public void onDraw(Canvas canvas, RectF latLonBounds, RectF tilesRect, DrawSettings nightMode) { // settings.clearParkingPosition(); parkingPoint = settings.getParkingPosition(); + if (parkingPoint == null) + return; timeLimit = settings.getParkingType(); Bitmap parkingIcon; if (!timeLimit) { @@ -113,8 +116,6 @@ public class ParkingPositionLayer extends OsmandMapLayer implements ContextMenuL } else { parkingIcon = parkingLimitIcon; } - if (parkingPoint == null) - return; double latitude = parkingPoint.getLatitude(); double longitude = parkingPoint.getLongitude(); if (isLocationVisible(latitude, longitude)) { @@ -167,14 +168,13 @@ public class ParkingPositionLayer extends OsmandMapLayer implements ContextMenuL long parkingTime = settings.getParkingTime(); Time time = new Time(); time.set(parkingTime); - timeLimitDesc.append("To pick up the car at: "); + timeLimitDesc.append(map.getString(R.string.osmand_parking_position_description_add)); timeLimitDesc.append(time.hour); timeLimitDesc.append(":"); - timeLimitDesc.append(time.minute); - if (time.hour>12) - timeLimitDesc.append(" p.m."); - else - timeLimitDesc.append(" a.m."); + timeLimitDesc.append(time.minute); + if (!DateFormat.is24HourFormat(map.getApplicationContext())) { + timeLimitDesc.append(time.hour >= 12 ? map.getString(R.string.osmand_parking_pm) : map.getString(R.string.osmand_parking_am)); + } } return map.getString(R.string.osmand_parking_position_description, timeLimitDesc.toString()); } diff --git a/OsmAnd/src/net/osmand/plus/parkingpoint/ParkingPositionPlugin.java b/OsmAnd/src/net/osmand/plus/parkingpoint/ParkingPositionPlugin.java index 1e07d30705..c492e948e5 100644 --- a/OsmAnd/src/net/osmand/plus/parkingpoint/ParkingPositionPlugin.java +++ b/OsmAnd/src/net/osmand/plus/parkingpoint/ParkingPositionPlugin.java @@ -12,8 +12,10 @@ import net.osmand.plus.activities.MapActivity; import net.osmand.plus.views.OsmandMapTileView; import android.app.AlertDialog; import android.app.AlertDialog.Builder; +import android.app.Dialog; import android.content.DialogInterface; import android.content.Intent; +import android.text.format.DateFormat; import android.view.View; import android.widget.CheckBox; import android.widget.ImageButton; @@ -21,7 +23,7 @@ import android.widget.TimePicker; /** * - * The plugin facilitates a storage of the location of a parked car + * The plugin facilitates a storage of the location of a parked car. * * @author Alena Fedasenka */ @@ -99,20 +101,26 @@ public class ParkingPositionPlugin extends OsmandPlugin { } /** - * Method dialog for adding of a parking location + * Method dialog for adding of a parking location. + * It allows user to choose a type of parking (time-limited or time-unlimited). */ private void showAddParkingDialog(final MapActivity mapActivity, final double latitude, final double longitude) { + final boolean wasEventPreviouslyAdded = settings.isParkingEventAdded(); final View addParking = mapActivity.getLayoutInflater().inflate(R.layout.parking_set_type, null); - Builder choose = new AlertDialog.Builder(mapActivity); - choose.setView(addParking); - choose.setTitle("Choose the type of parking"); + final Dialog choose = new Dialog(mapActivity); + choose.setContentView(addParking); + choose.setCancelable(true); + choose.setTitle(mapActivity.getString(R.string.osmand_parking_choose_type)); ImageButton limitButton = (ImageButton) addParking.findViewById(R.id.parking_lim_button); limitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + if (wasEventPreviouslyAdded) { + showDeleteEventWarning(mapActivity); + } setParkingPosition(mapActivity, latitude, longitude, true); - showChooseParkingTypeDialog(mapActivity); + showSetTimeLimitDialog(mapActivity, choose); } }); @@ -120,6 +128,11 @@ public class ParkingPositionPlugin extends OsmandPlugin { noLimitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + choose.dismiss(); + if (wasEventPreviouslyAdded) { + showDeleteEventWarning(mapActivity); + } + settings.addParkingEvent(false); setParkingPosition(mapActivity, latitude, longitude, false); } }); @@ -127,38 +140,50 @@ public class ParkingPositionPlugin extends OsmandPlugin { choose.show(); } - /** - * Method creates confirmation dialog for deletion of a parking location + * Method creates confirmation dialog for deletion of a parking location. */ private void showDeleteDialog(final MapActivity mapActivity) { Builder confirm = new AlertDialog.Builder(mapActivity); - confirm.setTitle("Delete parking location"); - confirm.setMessage("Do you want to remove the location of the parked car?"); + confirm.setTitle(mapActivity.getString(R.string.osmand_parking_delete)); + confirm.setMessage(mapActivity.getString(R.string.osmand_parking_delete_confirm)); confirm.setCancelable(true); - confirm.setPositiveButton(R.string.default_buttons_yes, - new DialogInterface.OnClickListener() { + confirm.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { + showDeleteEventWarning(mapActivity); settings.clearParkingPosition(); - deleteCalendarEvent(); + mapActivity.getMapView().refreshMap(); } }); confirm.setNegativeButton(R.string.default_buttons_cancel, null); confirm.show(); } - - private void showChooseParkingTypeDialog(final MapActivity mapActivity) { + + /** + * Opens the dialog to set a time limit for time-limited parking. + * The dialog has option to add a notification to Calendar app. + * Anyway the time-limit can be seen from parking point description. + * @param mapActivity + * @param choose + */ + private void showSetTimeLimitDialog(final MapActivity mapActivity, final Dialog choose) { final View setTimeParking = mapActivity.getLayoutInflater().inflate(R.layout.parking_set_time_limit, null); Builder setTime = new AlertDialog.Builder(mapActivity); setTime.setView(setTimeParking); - setTime.setTitle("Set the time limit of parking"); + setTime.setTitle(mapActivity.getString(R.string.osmand_parking_time_limit_title)); setTime.setNegativeButton(R.string.default_buttons_cancel, null); + final TimePicker timePicker = (TimePicker) setTimeParking.findViewById(R.id.parking_time_picker); + + //to set the same 24-hour or 12-hour mode as it is set in the device + timePicker.setIs24HourView(DateFormat.is24HourFormat(app)); + timePicker.setCurrentHour(Calendar.getInstance().get(Calendar.HOUR_OF_DAY)); + setTime.setPositiveButton(R.string.default_buttons_ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - TimePicker timePicker = (TimePicker) setTimeParking.findViewById(R.id.parking_time_picker); + choose.dismiss(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR, timePicker.getCurrentHour()); cal.set(Calendar.MINUTE, timePicker.getCurrentMinute()); @@ -166,41 +191,58 @@ public class ParkingPositionPlugin extends OsmandPlugin { CheckBox addCalendarEvent = (CheckBox)setTimeParking.findViewById(R.id.check_event_in_calendar); if (addCalendarEvent.isChecked()) addCalendarEvent(setTimeParking); + settings.addParkingEvent(true); } }); setTime.create(); setTime.show(); } + /** + * Opens a Calendar app with added notification to pick up the car from time-limited parking. + * @param view + */ private void addCalendarEvent(View view) { Intent intent = new Intent(Intent.ACTION_EDIT); - intent.setType("vnd.android.cursor.item/event"); - intent.putExtra("calendar_id", 1); - intent.putExtra("beginTime", settings.getParkingTime()); - intent.putExtra("allDay", false); - intent.putExtra("endTime", settings.getParkingTime()+60*60*1000); - intent.putExtra("title", "Pickup the car from parking"); - intent.putExtra("name", "parkingEvent"); + intent.setType("vnd.android.cursor.item/event"); //$NON-NLS-1$ + intent.putExtra("calendar_id", 1); //$NON-NLS-1$ + intent.putExtra("title", view.getContext().getString(R.string.osmand_parking_event)); //$NON-NLS-1$ + intent.putExtra("beginTime", settings.getParkingTime()); //$NON-NLS-1$ + intent.putExtra("endTime", settings.getParkingTime()+60*60*1000); //$NON-NLS-1$ view.getContext().startActivity(intent); } - - private void deleteCalendarEvent() { -// TODO delete calendar event -// Uri calendars = Uri.parse(String.format("content://%s/calendars", "com.android.calendar")); -// String[] projection = new String[] { CalendarColumns.ID, CalendarColumns.NAME, CalendarColumns.DISPLAYNAME}; -// TODO next line throws SecurityException -// Cursor managedCursor = view.getContext().getContentResolver().query(calendars, projection, null, null, null); + + /** + * Method shows warning, if previously the event for time-limited parking was added to Calendar app. + * @param mapActivity + */ + private void showDeleteEventWarning(final MapActivity mapActivity) { + if (settings.isParkingEventAdded()) { + Builder deleteEventWarning = new AlertDialog.Builder(mapActivity); + deleteEventWarning.setTitle(mapActivity.getString(R.string.osmand_parking_warning)); + deleteEventWarning.setMessage(mapActivity.getString(R.string.osmand_parking_warning_text)); + deleteEventWarning.setNeutralButton(R.string.default_buttons_ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + } + }); + deleteEventWarning.create(); + deleteEventWarning.show(); + } } + /** + * Method sets a parking point on a ParkingLayer. + * @param mapActivity + * @param latitude + * @param longitude + * @param isLimited + */ private void setParkingPosition(final MapActivity mapActivity, final double latitude, final double longitude, boolean isLimited) { -// to set a new parking position first the event for old parking (in case of a time-limit parking) should be deleted! - deleteCalendarEvent(); settings.setParkingPosition(latitude, longitude); settings.setParkingType(isLimited); if (mapActivity.getMapView().getLayers().contains(parkingLayer)) { parkingLayer.setParkingPointOnLayer(settings.getParkingPosition()); } } - - }