From e3979f5ea47f0e475b2f5fab3cd7996b5a93f40e Mon Sep 17 00:00:00 2001 From: Andrew McMillan Date: Tue, 29 Nov 2011 23:36:20 +1300 Subject: [PATCH] Add recently used timezones into the list presented to the user. --- src/com/morphoss/acal/widget/DateTimeDialog.java | 37 ++++++++++++++- .../morphoss/acal/widget/TimeZoneListAdapter.java | 55 +++++++++++++--------- 2 files changed, 68 insertions(+), 24 deletions(-) diff --git a/src/com/morphoss/acal/widget/DateTimeDialog.java b/src/com/morphoss/acal/widget/DateTimeDialog.java index 9ac7d91..d2a25b4 100644 --- a/src/com/morphoss/acal/widget/DateTimeDialog.java +++ b/src/com/morphoss/acal/widget/DateTimeDialog.java @@ -18,8 +18,13 @@ package com.morphoss.acal.widget; +import java.util.ArrayList; + import android.app.Dialog; import android.content.Context; +import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; +import android.preference.PreferenceManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; @@ -47,6 +52,7 @@ public class DateTimeDialog extends Dialog OnCheckedChangeListener, OnItemSelectedListener { + private static final String TZPREFNAME = "recentTimeZone-"; private Context context; private DateTimeSetListener dialogListener; @@ -60,10 +66,13 @@ public class DateTimeDialog extends Dialog private boolean allowDateVsDateTimeSwitching = true; private int oldTimeZoneWas = 0; + + private ArrayList recentZones; private AcalDateTime currentDateTime; private final boolean use24HourTime; private final boolean allowTimeZoneSetting; + private SharedPreferences prefs; public DateTimeDialog(Context context, AcalDateTime dateTimeValue, boolean twentyFourHourTime, boolean allowDateTimeSwitching, boolean allowTimeZones, DateTimeSetListener listener ) { @@ -75,6 +84,16 @@ public class DateTimeDialog extends Dialog allowDateVsDateTimeSwitching = allowDateTimeSwitching; allowTimeZoneSetting = allowTimeZones; + recentZones = new ArrayList(10); + prefs = PreferenceManager.getDefaultSharedPreferences(context); + String recentZonePref; + for( int i=0; i<10; i++ ) { + recentZonePref = prefs.getString(TZPREFNAME+i, null); + if ( recentZonePref != null ) { + recentZones.add(recentZonePref); + } + } + currentDateTime = (dateTimeValue == null ? new AcalDateTime() : dateTimeValue.clone()); populateLayout(); } @@ -99,7 +118,7 @@ public class DateTimeDialog extends Dialog dateOnlyCheckBox = (CheckBox) this.findViewById(R.id.DateTimeIsDate); timeZoneSpinner = (Spinner) this.findViewById(R.id.DateTimeZoneSelect); - tzListAdapter = new TimeZoneListAdapter(this.context, currentDateTime.getTimeZone()); + tzListAdapter = new TimeZoneListAdapter(this.context, currentDateTime.getTimeZone(), recentZones); timeZoneSpinner.setAdapter(tzListAdapter); timeZoneSpinner.setOnItemSelectedListener(this); @@ -142,9 +161,23 @@ public class DateTimeDialog extends Dialog @Override public void onClick(View v) { - if (v == setButton) + if (v == setButton) { dialogListener.onDateTimeSet(currentDateTime); + if ( !currentDateTime.isFloating() ) { + String currentZone = currentDateTime.getTimeZoneId(); + Editor e = prefs.edit(); + e.putString(TZPREFNAME+0, currentZone); + for( int i=0; i ourZones; - TimeZoneListAdapter( Context context, TimeZone currentTz ) { + TimeZoneListAdapter( Context context, TimeZone currentlySelectedTz, List recentlyUsedZones ) { super(); this.context = context; String[] zoneNames = context.getResources().getStringArray(R.array.timezoneNameList); ourZones = new ArrayList(ZoneData.zones.length + 10 ); - String currentTzId = (currentTz == null ? null : currentTz.getID()); + String incomingZone = (currentlySelectedTz == null ? null : currentlySelectedTz.getID()); Map zoneMap = new HashMap(ZoneData.zones.length); Zone aZone; @@ -53,33 +52,45 @@ public class TimeZoneListAdapter implements SpinnerAdapter { aZone = new Zone(zoneNames[i], ZoneData.zones[i][0]); zoneMap.put(ZoneData.zones[i][0],aZone); } - if ( currentTzId != null ) { - aZone = zoneMap.get(currentTzId); - if ( aZone == null ) aZone = new Zone(currentTzId,currentTzId); - else zoneMap.remove(currentTzId); + + // The first thing in the list is the current timezone in use + if ( incomingZone != null ) { + aZone = zoneMap.get(incomingZone); + if ( aZone == null ) aZone = new Zone(incomingZone,incomingZone); + else zoneMap.remove(incomingZone); ourZones.add(aZone); } - String floatingZone = context.getString(R.string.floatingTime); - ourZones.add(new Zone(floatingZone, null)); - - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - String recentZone; - for( int i=0; i<10; i++ ) { - recentZone = prefs.getString("recentTimeZone-"+i, null); - if ( recentZone != null ) { - aZone = zoneMap.get(recentZone); - if ( aZone != null && !recentZone.equals(currentTzId) && !recentZone.equals(floatingZone) ) { - zoneMap.remove(recentZone); - ourZones.add(aZone); - } + + // Next, we use the local timezone (unless it's the same, of course!) + String localZone = TimeZone.getDefault().getID(); + if ( localZone != null && !localZone.equals(incomingZone) ) { + aZone = zoneMap.get(localZone); + if ( aZone != null ) { + zoneMap.remove(localZone); + ourZones.add(aZone); } } - + + // Thirdly, the list of recently used timezones... + for( String recentZone : recentlyUsedZones ) { + aZone = zoneMap.get(recentZone); + if ( aZone != null ) { + zoneMap.remove(recentZone); + ourZones.add(aZone); + } + } + + // Fourthly, a floating zone. + String floatingZone = context.getString(R.string.floatingTime); + ourZones.add(new Zone(floatingZone, null)); + + // Fifthly, all of the zones we have which are not yet in the list. for( int i=0; i< ZoneData.zones.length; i++ ) { aZone = zoneMap.get(ZoneData.zones[i][0]); if ( aZone != null ) ourZones.add(aZone); } + // Finally, like a fullstop to the list, UTC. ourZones.add(new Zone("UTC", "UTC")); } -- 2.11.4.GIT