Ignore:
Timestamp:
2011-04-06T21:23:49+02:00 (13 years ago)
Author:
glebius
Message:

Make a compromise between smoothness of updating and CPU consumption. Redrawing
only the LiveGPS layer is usually cheaper than scrolling the MapView with all its
layers, so introduce two independable tunables to configure update frequency, here
are their names and default values:

livegps.center_interval_msec=5000
livegps.refresh_interval_msec=250

The centering is made once per 5 seconds (old conservative value), but
the cursor can be redrawn up to 4 times per second.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/livegps/src/livegps/LiveGpsLayer.java

    r25805 r25808  
    2626    public static final String KEY_LIVEGPS_COLOR = "color.livegps.position";
    2727
    28     private static final int DEFAULT_SLEEP_TIME = 500;  /* Default sleep time is 0.5 seconds. */
    29     private static final String oldConfigKey = "livegps.refreshinterval";     /* in seconds */
    30     private static final String ConfigKey = "livegps.refresh_interval_msec";  /* in msec */
    31     private int sleepTime;
     28    private static final int DEFAULT_REFRESH_INTERVAL = 250;
     29    private static final int DEFAULT_CENTER_INTERVAL = 5000;
     30    private static final String oldC_REFRESH_INTERVAL = "livegps.refreshinterval";     /* in seconds */
     31    private static final String C_REFRESH_INTERVAL = "livegps.refresh_interval_msec";  /* in msec */
     32    private static final String C_CENTER_INTERVAL = "livegps.center_interval_msec";  /* in msec */
     33    private int refreshInterval;
     34    private int centerInterval;
     35    private long lastRedraw = 0;
     36    private long lastCenter = 0;
    3237
    3338    LatLon lastPos;
     
    3944    boolean autocenter;
    4045    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    41     private long lastUpdate = 0;
    4246
    4347    public LiveGpsLayer(GpxData data) {
     
    5155        data.tracks.add(trackBeingWritten);
    5256
    53         initSleepTime();
     57        initIntervals();
    5458    }
    5559
    5660    void setCurrentPosition(double lat, double lon) {
    57         // System.out.println("adding pos " + lat + "," + lon);
    5861        LatLon thisPos = new LatLon(lat, lon);
    59         if ((lastPos != null) && (thisPos.equalsEpsilon(lastPos))) {
     62        if ((lastPos != null) && (thisPos.equalsEpsilon(lastPos)))
    6063            // no change in position
    6164            // maybe show a "paused" cursor or some such
    6265            return;
    63         }
    6466
    6567        lastPos = thisPos;
     
    6769        lastPoint.attr.put("time", dateFormat.format(new Date()));
    6870        trackSegment.addWaypoint(lastPoint);
    69         if (autocenter && allowRedraw()) {
     71        if (autocenter && allowCenter())
    7072            center();
    71         }
    7273    }
    7374
     
    145146                    setCourse(data.getCourse());
    146147                }
    147                 if (!autocenter && allowRedraw()) {
     148                if (allowRedraw())
    148149                    Main.map.repaint();
    149                 }
    150150            }
    151151        }
     
    162162        long current = date.getTime();
    163163
    164         if (current - lastUpdate >= sleepTime) {
    165                 lastUpdate = current;
     164        if (current - lastRedraw >= refreshInterval) {
     165                lastRedraw = current;
    166166                return true;
    167167        } else
     
    170170
    171171    /**
    172      * Retrieve the sleepTime from the configuration. Be compatible with old
    173      * version that stored value in seconds. If no such configuration key exists,
    174      * it will be initialized here.
    175      */
    176     private void initSleepTime() {
    177         if ((sleepTime = Main.pref.getInteger(ConfigKey, 0)) == 0) {
    178                 if ((sleepTime = Main.pref.getInteger(oldConfigKey, 0)) != 0) {
    179                         sleepTime *= 1000;
    180                         Main.pref.put(oldConfigKey, null);
     172     * Check, if a autocentering is currently allowed.
     173     *
     174     * @return true, if a autocentering is permitted, false, if a autocentering
     175     * should be suppressed.
     176     */
     177    private boolean allowCenter() {
     178        Date date = new Date();
     179        long current = date.getTime();
     180
     181        if (current - lastCenter >= centerInterval) {
     182                lastCenter = current;
     183                return true;
     184        } else
     185                return false;
     186    }
     187
     188    /**
     189     * Retrieve the refreshInterval and centerInterval from the configuration. Be compatible
     190     * with old version that stored refreshInterval in seconds. If no such configuration key
     191     * exists, it will be initialized here.
     192     */
     193    private void initIntervals() {
     194        if ((refreshInterval = Main.pref.getInteger(C_REFRESH_INTERVAL, 0)) == 0) {
     195                if ((refreshInterval = Main.pref.getInteger(oldC_REFRESH_INTERVAL, 0)) != 0) {
     196                        refreshInterval *= 1000;
     197                        Main.pref.put(oldC_REFRESH_INTERVAL, null);
    181198                } else
    182                         sleepTime = DEFAULT_SLEEP_TIME;
    183         }
    184 
    185         // creates the setting, if none present.
    186         Main.pref.putInteger(ConfigKey, sleepTime);
     199                        refreshInterval = DEFAULT_REFRESH_INTERVAL;
     200        }
     201
     202        if ((centerInterval = Main.pref.getInteger(C_CENTER_INTERVAL, 0)) == 0)
     203                centerInterval = DEFAULT_CENTER_INTERVAL;
     204
     205        Main.pref.putInteger(C_REFRESH_INTERVAL, refreshInterval);
     206        Main.pref.putInteger(C_CENTER_INTERVAL, centerInterval);
    187207    }
    188208}
Note: See TracChangeset for help on using the changeset viewer.