summaryrefslogtreecommitdiff
path: root/navit/android/src/org/navitproject/navit/NavitAppConfig.java
blob: bcbe6b8a5190f1e88a91fccf125b893d4265dd9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package org.navitproject.navit;

import android.app.Application;
import android.content.SharedPreferences;

import org.navitproject.navit.NavitAddressSearchActivity.NavitAddress;

import java.util.ArrayList;
import java.util.List;


public class NavitAppConfig extends Application {

    private static final int         MAX_LAST_ADDRESSES = 10;
    private static final String      TAG                = "Navit";

    private List<NavitAddress> mLastAddresses     = null;
    private int                      mLastAddressField;
    private SharedPreferences        mSettings;

    @Override
    public void onCreate() {
        mSettings = getSharedPreferences(Navit.NAVIT_PREFS, MODE_PRIVATE);
        super.onCreate();
    }

    public List<NavitAddress> getLastAddresses() {
        if (mLastAddresses == null) {
            mLastAddresses = new ArrayList<NavitAddress>();
            int mLastAddressField = mSettings.getInt("LastAddress", -1);
            if (mLastAddressField >= 0) {
                int index = mLastAddressField;
                do {
                    String addr_str = mSettings.getString("LastAddress_" + String.valueOf(index), "");

                    if (addr_str.length() > 0) {
                        mLastAddresses.add(new NavitAddress(
                                1,
                                mSettings.getFloat("LastAddress_Lat_" + String.valueOf(index), 0),
                                mSettings.getFloat("LastAddress_Lon_" + String.valueOf(index), 0),
                                addr_str));
                    }

                    if (--index < 0) index = MAX_LAST_ADDRESSES - 1;

                } while (index != mLastAddressField);
            }
        }
        return mLastAddresses;
    }

    public void addLastAddress(NavitAddress newAddress) {
        getLastAddresses();

        mLastAddresses.add(newAddress);
        if (mLastAddresses.size() > MAX_LAST_ADDRESSES) mLastAddresses.remove(0);

        mLastAddressField++;
        if (mLastAddressField >= MAX_LAST_ADDRESSES) mLastAddressField = 0;

        SharedPreferences.Editor editSettings = mSettings.edit();

        editSettings.putInt("LastAddress", mLastAddressField);
        editSettings.putString("LastAddress_" + String.valueOf(mLastAddressField), newAddress.addr);
        editSettings.putFloat("LastAddress_Lat_" + String.valueOf(mLastAddressField), newAddress.lat);
        editSettings.putFloat("LastAddress_Lon_" + String.valueOf(mLastAddressField), newAddress.lon);

        editSettings.apply();
    }
}