Merge remote-tracking branch 'origin/master'

This commit is contained in:
Weblate 2015-08-25 16:51:25 +02:00
commit 19f706a0f8
10 changed files with 434 additions and 61 deletions

View file

@ -39,7 +39,7 @@ public class OpeningHoursParser {
*
* @author sander
*/
public static class OpeningHours {
public static class OpeningHours implements Serializable {
/**
* list of the different rules
@ -147,7 +147,18 @@ public class OpeningHoursParser {
return s.substring(0, s.length()-2);
}
public String toStringNoMoths() {
StringBuilder s = new StringBuilder();
if (rules.isEmpty()) {
return "";
}
for (OpeningHoursRule r : rules) {
s.append(r.toRuleString(true)).append("; ");
}
return s.substring(0, s.length()-2);
}
}
@ -158,7 +169,7 @@ public class OpeningHoursParser {
* - a collection of days/dates
* - a time range
*/
public static interface OpeningHoursRule {
public static interface OpeningHoursRule extends Serializable {
/**
* Check if, for this rule, the feature is opened for time "cal"
@ -200,7 +211,7 @@ public class OpeningHoursParser {
* This implementation only supports month, day of weeks and numeral times, or the value "off"
*
*/
public static class BasicOpeningHourRule implements OpeningHoursRule, Serializable {
public static class BasicOpeningHourRule implements OpeningHoursRule {
/**
* represents the list on which days it is open.
* Day number 0 is MONDAY
@ -396,28 +407,13 @@ public class OpeningHoursParser {
}
// Day
boolean open24_7 = true;
dash = false;
first = true;
for (int i = 0; i < 7; i++) {
if (days[i]) {
if (i > 0 && days[i - 1] && i < 6 && days[i + 1]) {
if (!dash) {
dash = true;
b.append("-"); //$NON-NLS-1$
}
continue;
}
if (first) {
first = false;
} else if (!dash) {
b.append(", "); //$NON-NLS-1$
}
b.append(daysStr[i]);
dash = false;
} else {
if (!days[i]) {
open24_7 = false;
break;
}
}
appendDaysString(b);
// Time
if (startTimes == null || startTimes.length == 0){
b.append(" off ");
@ -441,12 +437,35 @@ public class OpeningHoursParser {
}
return b.substring(0, b.length()-1);
}
@Override
public String toString() {
return toRuleString(false);
}
public void appendDaysString(StringBuilder builder) {
boolean dash = false;
boolean first = true;
for (int i = 0; i < 7; i++) {
if (days[i]) {
if (i > 0 && days[i - 1] && i < 6 && days[i + 1]) {
if (!dash) {
dash = true;
builder.append("-"); //$NON-NLS-1$
}
continue;
}
if (first) {
first = false;
} else if (!dash) {
builder.append(", "); //$NON-NLS-1$
}
builder.append(daysStr[i]);
dash = false;
}
}
}
/**
* Add a time range (startTime-endTime) to this rule
* @param startTime startTime to add

View file

@ -84,5 +84,9 @@
android:layout_height="wrap_content"
android:text="Add tag"/>
<!--XXX due to bug in NestedScroolView-->
<Space
android:layout_width="match_parent"
android:layout_height="500dp"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>

View file

@ -92,19 +92,24 @@
android:text="Open hours"
android:textColor="@color/color_black"/>
<ImageView
android:id="@+id/openHoursImageView"
style="@style/edit_poi_imageview_style"
android:layout_below="@id/openHoursTextView"
tools:src="@drawable/ic_action_time"/>
<EditText
android:id="@+id/openHoursEditText"
style="@style/create_poi_text_field"
<LinearLayout
android:id="@+id/openHoursContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/openHoursTextView"
android:hint="Mo-Su 08:00-20:00"
android:inputType="textMultiLine"/>
android:orientation="vertical"/>
<Button
android:id="@+id/addOpeningHoursButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/openHoursContainer"
android:layout_marginLeft="72dp"
android:text="Add opening hours"/>
<!--XXX due to bug in NestedScroolView-->
<Space
android:layout_width="match_parent"
android:layout_height="500dp"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>

View file

@ -7,7 +7,8 @@
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageButton
<ImageView
android:id="@+id/clockIconImageView"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="48dp"
android:layout_height="48dp"
@ -22,6 +23,7 @@
android:layout_weight="1">
<TextView
android:id="@+id/daysTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Mo-We"/>
@ -40,6 +42,7 @@
android:orientation="vertical">
<TextView
android:id="@+id/openingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="08:00"/>
@ -62,6 +65,7 @@
android:orientation="vertical">
<TextView
android:id="@+id/closingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="20:00"/>
@ -78,6 +82,6 @@
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="bottom"
android:src="@drawable/ic_action_close_dark"/>
android:src="@drawable/ic_action_remove_dark"/>
</LinearLayout>

View file

@ -106,7 +106,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment {
listAdapter = new LocalIndexesAdapter(getActivity());
listView.setAdapter(listAdapter);
expandAllGroups();
setListView(listView);
setListView(listView);
descriptionText = (TextView) view.findViewById(R.id.memory_size);
sizeProgress = (ProgressBar) view.findViewById(R.id.memory_progress);
updateDescriptionTextWithSize();

View file

@ -1,5 +1,6 @@
package net.osmand.plus.osmedit;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
@ -19,6 +20,7 @@ import net.osmand.osm.PoiType;
import net.osmand.osm.edit.OSMSettings;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.osmedit.data.EditPoiData;
import net.osmand.plus.osmedit.data.Tag;
@ -31,6 +33,7 @@ public class AdvancedDataFragment extends Fragment {
private TagAdapterLinearLayoutHack mAdapter;
private EditPoiData.TagsChangedListener mTagsChangedListener;
private boolean mIsUserInput = true;
private Drawable deleteDrawable;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
@ -56,6 +59,9 @@ public class AdvancedDataFragment extends Fragment {
valueEditText.clearFocus();
}
});
deleteDrawable = ((MapActivity)getActivity()).getMyApplication().getIconsCache()
.getContentIcon(R.drawable.ic_action_remove_dark);
deleteItemImageButton.setImageDrawable(deleteDrawable);
TextView nameTextView = (TextView) view.findViewById(R.id.nameTextView);
TextView amenityTagTextView = (TextView) view.findViewById(R.id.amenityTagTextView);
TextView amenityTextView = (TextView) view.findViewById(R.id.amenityTextView);
@ -174,6 +180,7 @@ public class AdvancedDataFragment extends Fragment {
ImageButton deleteItemImageButton =
(ImageButton) convertView.findViewById(R.id.deleteItemImageButton);
valueEditText.setText(tag.value);
deleteItemImageButton.setImageDrawable(deleteDrawable);
deleteItemImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

View file

@ -19,7 +19,6 @@ import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -31,6 +30,7 @@ import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import net.osmand.PlatformUtil;
import net.osmand.access.AccessibleToast;
import net.osmand.data.Amenity;
import net.osmand.osm.PoiType;
@ -48,6 +48,8 @@ import net.osmand.plus.osmedit.dialogs.PoiSubTypeDialogFragment;
import net.osmand.plus.osmedit.dialogs.PoiTypeDialogFragment;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
import java.text.MessageFormat;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
@ -55,6 +57,7 @@ import java.util.Map;
public class EditPoiFragment extends Fragment {
public static final String TAG = "EditPoiFragment";
private static final Log LOG = PlatformUtil.getLog(EditPoiFragment.class);
private static final String KEY_AMENITY_NODE = "amenity_node";
private static final String KEY_AMENITY = "amenity";
@ -228,7 +231,6 @@ public class EditPoiFragment extends Fragment {
// TODO implement saving
OsmPoint.Action action = node.getId() == -1 ? OsmPoint.Action.CREATE : OsmPoint.Action.MODIFY;
String description = "";
Log.v(TAG, "tags=" + editPoiData.tags);
for (Tag tag : editPoiData.tags) {
if (tag.tag.equals(POI_TYPE_TAG)) {
if (allTranslatedSubTypes.get(tag.value) != null) {
@ -372,8 +374,6 @@ public class EditPoiFragment extends Fragment {
}
public void updateType(Amenity amenity) {
// TODO implement
Log.v(TAG, "updateType(" + "amenity=" + amenity + ")");
mIsUserInput = false;
poiTypeEditText.setText(amenity.getSubType());
mIsUserInput = true;

View file

@ -1,5 +1,6 @@
package net.osmand.plus.osmedit;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
@ -9,21 +10,33 @@ import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.osmand.PlatformUtil;
import net.osmand.osm.edit.OSMSettings;
import net.osmand.plus.IconsCache;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.osmedit.data.EditPoiData;
import net.osmand.plus.osmedit.data.Tag;
import net.osmand.plus.osmedit.dialogs.OpeningHoursDaysDialogFragment;
import net.osmand.util.OpeningHoursParser;
import net.osmand.util.OpeningHoursParser.BasicOpeningHourRule;
import org.apache.commons.logging.Log;
import java.util.HashMap;
import java.util.Map;
public class NormalDataFragment extends Fragment {
private static final String TAG = "NormalDataFragment";
private static final Log LOG = PlatformUtil.getLog(NormalDataFragment.class);
private static final String OPENING_HOURS = "opening_hours";
private EditText streetEditText;
private EditText houseNumberEditText;
private EditText phoneEditText;
@ -31,7 +44,7 @@ public class NormalDataFragment extends Fragment {
private EditText descriptionEditText;
private EditPoiData.TagsChangedListener mTagsChangedListener;
private boolean mIsUserInput = true;
private EditText openHoursEditText;
OpeningHoursAdapter mOpeningHoursAdapter;
@Nullable
@Override
@ -49,8 +62,6 @@ public class NormalDataFragment extends Fragment {
webSiteImageView.setImageDrawable(iconsCache.getContentIcon(R.drawable.ic_world_globe_dark));
ImageView descriptionImageView = (ImageView) view.findViewById(R.id.descriptionImageView);
descriptionImageView.setImageDrawable(iconsCache.getContentIcon(R.drawable.ic_action_description));
ImageView openHoursImageView = (ImageView) view.findViewById(R.id.openHoursImageView);
openHoursImageView.setImageDrawable(iconsCache.getContentIcon(R.drawable.ic_action_time));
streetEditText = (EditText) view.findViewById(R.id.streetEditText);
streetEditText.addTextChangedListener(new MyOnFocusChangeListener(getData(),
@ -67,10 +78,27 @@ public class NormalDataFragment extends Fragment {
descriptionEditText = (EditText) view.findViewById(R.id.descriptionEditText);
descriptionEditText.addTextChangedListener(new MyOnFocusChangeListener(getData(),
OSMSettings.OSMTagKey.DESCRIPTION.getValue()));
openHoursEditText = (EditText) view.findViewById(R.id.openHoursEditText);
openHoursEditText.addTextChangedListener(new MyOnFocusChangeListener(getData(),
OSMSettings.OSMTagKey.OPENING_HOURS.getValue()));
Button addOpeningHoursButton = (Button) view.findViewById(R.id.addOpeningHoursButton);
addOpeningHoursButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BasicOpeningHourRule r = new BasicOpeningHourRule();
OpeningHoursDaysDialogFragment fragment = OpeningHoursDaysDialogFragment.createInstance(r, -1);
fragment.show(getChildFragmentManager(), "OpenTimeDialogFragment");
}
});
LinearLayout openHoursContainer = (LinearLayout) view.findViewById(R.id.openHoursContainer);
Drawable clockDrawable = iconsCache.getContentIcon(R.drawable.ic_action_time);
Drawable deleteDrawable = iconsCache.getContentIcon(R.drawable.ic_action_remove_dark);
if (savedInstanceState != null && savedInstanceState.containsKey(OPENING_HOURS)) {
mOpeningHoursAdapter = new OpeningHoursAdapter(
(OpeningHoursParser.OpeningHours) savedInstanceState.getSerializable(OPENING_HOURS),
openHoursContainer, getData(), clockDrawable, deleteDrawable);
mOpeningHoursAdapter.updateViews();
} else {
mOpeningHoursAdapter = new OpeningHoursAdapter(new OpeningHoursParser.OpeningHours(),
openHoursContainer, getData(), clockDrawable, deleteDrawable);
}
return view;
}
@ -82,17 +110,17 @@ public class NormalDataFragment extends Fragment {
public void onTagsChanged() {
TagMapProcessor tagMapProcessor = new TagMapProcessor();
tagMapProcessor.addFilter(OSMSettings.OSMTagKey.ADDR_STREET.getValue(),
streetEditText);
new EditTextTagFilter(streetEditText));
tagMapProcessor.addFilter(OSMSettings.OSMTagKey.ADDR_HOUSE_NUMBER.getValue(),
houseNumberEditText);
new EditTextTagFilter(houseNumberEditText));
tagMapProcessor.addFilter(OSMSettings.OSMTagKey.PHONE.getValue(),
phoneEditText);
new EditTextTagFilter(phoneEditText));
tagMapProcessor.addFilter(OSMSettings.OSMTagKey.WEBSITE.getValue(),
webSiteEditText);
new EditTextTagFilter(webSiteEditText));
tagMapProcessor.addFilter(OSMSettings.OSMTagKey.DESCRIPTION.getValue(),
descriptionEditText);
new EditTextTagFilter(descriptionEditText));
tagMapProcessor.addFilter(OSMSettings.OSMTagKey.OPENING_HOURS.getValue(),
openHoursEditText);
new OpenHoursTagFilter(mOpeningHoursAdapter));
mIsUserInput = false;
for (Tag tag : getData().tags) {
@ -111,28 +139,89 @@ public class NormalDataFragment extends Fragment {
getData().deleteListener(mTagsChangedListener);
}
private static class TagMapProcessor {
private final Map<String, EditText> mFilters = new HashMap<>();
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putSerializable(OPENING_HOURS, mOpeningHoursAdapter.openingHours);
super.onSaveInstanceState(outState);
}
public void addFilter(String tag, EditText editText) {
mFilters.put(tag, editText);
public void addBasicOpeningHoursRule(BasicOpeningHourRule item) {
LOG.debug("item=" + item.toRuleString(false));
mOpeningHoursAdapter.addOpeningHoursRule(item);
}
private static class TagMapProcessor {
private final Map<String, TagFilter> mFilters = new HashMap<>();
public void addFilter(String tag, TagFilter filter) {
mFilters.put(tag, filter);
}
public void process(Tag tag) {
if (mFilters.containsKey(tag.tag)) {
final EditText editText = mFilters.get(tag.tag);
editText.setText(tag.value);
final TagFilter filter = mFilters.get(tag.tag);
filter.process(tag);
mFilters.remove(tag.tag);
}
}
public void clearEmptyFields() {
for (String tag : mFilters.keySet()) {
mFilters.get(tag).setText(null);
mFilters.get(tag).onUntriggered();
}
}
}
private interface TagFilter {
void process(Tag tag);
void onUntriggered();
}
private static class EditTextTagFilter implements TagFilter {
private final EditText editText;
private EditTextTagFilter(EditText editText) {
this.editText = editText;
}
@Override
public void process(Tag tag) {
editText.setText(tag.value);
}
@Override
public void onUntriggered() {
editText.setText(null);
}
}
private static class OpenHoursTagFilter implements TagFilter {
private final OpeningHoursAdapter adapter;
private OpenHoursTagFilter(OpeningHoursAdapter adapter) {
this.adapter = adapter;
}
@Override
public void process(Tag tag) {
String openingHoursString = tag.value;
OpeningHoursParser.OpeningHours openingHours =
OpeningHoursParser.parseOpenedHours(openingHoursString);
if (openingHours == null) {
openingHours = new OpeningHoursParser.OpeningHours();
// TODO show error message
}
adapter.replaceOpeningHours(openingHours);
adapter.updateViews();
}
@Override
public void onUntriggered() {
}
}
private EditPoiFragment getEditPoiFragment() {
return (EditPoiFragment) getParentFragment();
}
@ -172,4 +261,93 @@ public class NormalDataFragment extends Fragment {
}
}
}
private class OpeningHoursAdapter {
private OpeningHoursParser.OpeningHours openingHours;
private final LinearLayout linearLayout;
private final EditPoiData data;
private final Drawable clockDrawable;
private final Drawable deleteDrawable;
public OpeningHoursAdapter(OpeningHoursParser.OpeningHours openingHours,
LinearLayout linearLayout, EditPoiData data,
Drawable clockDrawable, Drawable deleteDrawable) {
this.openingHours = openingHours;
this.linearLayout = linearLayout;
this.data = data;
this.clockDrawable = clockDrawable;
this.deleteDrawable = deleteDrawable;
}
public void addOpeningHoursRule(OpeningHoursParser.BasicOpeningHourRule rule) {
openingHours.addRule(rule);
updateViews();
}
public void replaceOpeningHours(OpeningHoursParser.OpeningHours openingHours) {
this.openingHours = openingHours;
}
public void updateViews() {
linearLayout.removeAllViews();
for (int i = 0; i < openingHours.getRules().size(); i++) {
linearLayout.addView(getView(i));
}
if (mIsUserInput) {
Tag openHours = new Tag(OSMSettings.OSMTagKey.OPENING_HOURS.getValue(),
openingHours.toStringNoMoths());
data.tags.remove(openHours);
data.tags.add(openHours);
data.notifyDatasetChanged(null);
}
}
private View getView(final int position) {
OpeningHoursParser.BasicOpeningHourRule rule =
(BasicOpeningHourRule) openingHours.getRules().get(position);
final View view = LayoutInflater.from(linearLayout.getContext())
.inflate(R.layout.open_time_list_item, null, false);
ImageView clockIconImageView = (ImageView) view.findViewById(R.id.clockIconImageView);
clockIconImageView.setImageDrawable(clockDrawable);
TextView daysTextView = (TextView) view.findViewById(R.id.daysTextView);
StringBuilder stringBuilder = new StringBuilder();
rule.appendDaysString(stringBuilder);
daysTextView.setText(stringBuilder.toString());
TextView openingTextView = (TextView) view.findViewById(R.id.openingTextView);
final int openingHour = rule.getStartTime() / 60;
int openingMinute = rule.getStartTime() - openingHour * 60;
openingTextView.setText(formatTime(openingHour, openingMinute));
TextView closingTextView = (TextView) view.findViewById(R.id.closingTextView);
int enHour = rule.getEndTime() / 60;
int enTime = rule.getEndTime() - enHour * 60;
closingTextView.setText(formatTime(enHour, enTime));
ImageButton deleteItemImageButton = (ImageButton) view.findViewById(R.id.deleteItemImageButton);
deleteItemImageButton.setImageDrawable(deleteDrawable);
deleteItemImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openingHours.getRules().remove(position);
updateViews();
}
});
return view;
}
}
private static String formatTime(int h, int t) {
StringBuilder b = new StringBuilder();
if (h < 10) {
b.append("0"); //$NON-NLS-1$
}
b.append(h).append(":"); //$NON-NLS-1$
if (t < 10) {
b.append("0"); //$NON-NLS-1$
}
b.append(t);
return b.toString();
}
}

View file

@ -0,0 +1,88 @@
package net.osmand.plus.osmedit.dialogs;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import net.osmand.plus.R;
import net.osmand.util.OpeningHoursParser;
import java.util.Calendar;
public class OpeningHoursDaysDialogFragment extends DialogFragment {
public static final String POSITION_TO_ADD = "position_to_add";
public static final String ITEM = "item";
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final OpeningHoursParser.BasicOpeningHourRule item =
(OpeningHoursParser.BasicOpeningHourRule) getArguments().getSerializable(ITEM);
final int positionToAdd = getArguments().getInt(POSITION_TO_ADD);
AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
boolean add = positionToAdd > -1;
Calendar inst = Calendar.getInstance();
final int first = inst.getFirstDayOfWeek();
final boolean[] dayToShow = new boolean[7];
String[] daysToShow = new String[7];
for (int i = 0; i < 7; i++) {
int d = (first + i - 1) % 7 + 1;
inst.set(Calendar.DAY_OF_WEEK, d);
daysToShow[i] = DateFormat.format("EEEE", inst).toString(); //$NON-NLS-1$
final int pos = (d + 5) % 7;
dayToShow[i] = item.getDays()[pos];
}
b.setMultiChoiceItems(daysToShow, dayToShow, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
dayToShow[which] = isChecked;
}
});
b.setPositiveButton(add ? getActivity().getString(R.string.shared_string_add)
: getActivity().getString(R.string.shared_string_apply),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean[] days = item.getDays();
for (int i = 0; i < 7; i++) {
days[(first + 5 + i) % 7] = dayToShow[i];
}
OpeningHoursHoursDialogFragment.createInstance(item, null, true)
.show(getFragmentManager(), "TimePickerDialogFragment");
if (positionToAdd != -1) {
// time.insert(item, positionToAdd);
// selectedRule = positionToAdd;
} else {
// time.notifyDataSetChanged();
}
// updateTimePickers();
}
});
b.setNegativeButton(getActivity().getString(R.string.shared_string_cancel), null);
return b.create();
}
public static OpeningHoursDaysDialogFragment createInstance(final OpeningHoursParser.BasicOpeningHourRule item,
final int positionToAdd) {
OpeningHoursDaysDialogFragment daysDialogFragment = new OpeningHoursDaysDialogFragment();
Bundle bundle = new Bundle();
bundle.putSerializable(ITEM, item);
bundle.putInt(POSITION_TO_ADD, positionToAdd);
daysDialogFragment.setArguments(bundle);
return daysDialogFragment;
}
}

View file

@ -0,0 +1,68 @@
package net.osmand.plus.osmedit.dialogs;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.TimePicker;
import net.osmand.plus.osmedit.NormalDataFragment;
import net.osmand.util.OpeningHoursParser;
import java.util.Calendar;
public class OpeningHoursHoursDialogFragment extends DialogFragment {
public static final String INITIAL_TIME = "initial_time";
public static final String IS_START = "is_start";
public static final String BASIC_OPENING_HOUR_RULE = "basic_opening_hour_rule";
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle args = getArguments();
final boolean isStart = args.getBoolean(IS_START);
final OpeningHoursParser.BasicOpeningHourRule item = (OpeningHoursParser.BasicOpeningHourRule)
args.getSerializable(BASIC_OPENING_HOUR_RULE);
TimePickerDialog.OnTimeSetListener callback = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
int time = minute + hourOfDay * 60;
if (isStart) {
item.setStartTime(time);
OpeningHoursHoursDialogFragment.createInstance(item, null, false)
.show(getFragmentManager(), "TimePickerDialogFragment");
} else {
item.setEndTime(time);
((NormalDataFragment) getParentFragment()).addBasicOpeningHoursRule(item);
}
}
};
Calendar initialState = (Calendar) args.getSerializable(INITIAL_TIME);
if (initialState == null) {
initialState = Calendar.getInstance();
initialState.set(Calendar.HOUR_OF_DAY, isStart? 8 : 20);
initialState.set(Calendar.MINUTE, 0);
}
TimePickerDialog timePickerDialog = new TimePickerDialog(getActivity(), callback,
initialState.get(Calendar.HOUR_OF_DAY),
initialState.get(Calendar.MINUTE),
DateFormat.is24HourFormat(getActivity()));
timePickerDialog.setTitle(isStart ? "Opening" : "Closing");
return timePickerDialog;
}
public static OpeningHoursHoursDialogFragment createInstance(OpeningHoursParser.BasicOpeningHourRule item,
Calendar initialTime,
boolean isStart) {
OpeningHoursHoursDialogFragment fragment = new OpeningHoursHoursDialogFragment();
Bundle bundle = new Bundle();
bundle.putSerializable(BASIC_OPENING_HOUR_RULE, item);
bundle.putBoolean(IS_START, isStart);
bundle.putSerializable(INITIAL_TIME, initialTime);
fragment.setArguments(bundle);
return fragment;
}
}