Skip to content

Most visited

Recently visited

navigation
Geofencing / Application / src / com.example.android.wearable.geofencing /

SimpleGeofenceStore.java

1
/*
2
 * Copyright (C) 2014 The Android Open Source Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package com.example.android.wearable.geofencing;
18
 
19
import static com.example.android.wearable.geofencing.Constants.INVALID_FLOAT_VALUE;
20
import static com.example.android.wearable.geofencing.Constants.INVALID_INT_VALUE;
21
import static com.example.android.wearable.geofencing.Constants.INVALID_LONG_VALUE;
22
import static com.example.android.wearable.geofencing.Constants.KEY_EXPIRATION_DURATION;
23
import static com.example.android.wearable.geofencing.Constants.KEY_LATITUDE;
24
import static com.example.android.wearable.geofencing.Constants.KEY_LONGITUDE;
25
import static com.example.android.wearable.geofencing.Constants.KEY_PREFIX;
26
import static com.example.android.wearable.geofencing.Constants.KEY_RADIUS;
27
import static com.example.android.wearable.geofencing.Constants.KEY_TRANSITION_TYPE;
28
 
29
import android.content.Context;
30
import android.content.SharedPreferences;
31
 
32
/**
33
 * Storage for geofence values, implemented in SharedPreferences.
34
 */
35
public class SimpleGeofenceStore {
36
 
37
    // The SharedPreferences object in which geofences are stored.
38
    private final SharedPreferences mPrefs;
39
    // The name of the SharedPreferences.
40
    private static final String SHARED_PREFERENCES = "SharedPreferences";
41
 
42
    /**
43
     * Create the SharedPreferences storage with private access only.
44
     */
45
    public SimpleGeofenceStore(Context context) {
46
        mPrefs = context.getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
47
    }
48
 
49
    /**
50
     * Returns a stored geofence by its id, or returns null if it's not found.
51
     * @param id The ID of a stored geofence.
52
     * @return A SimpleGeofence defined by its center and radius, or null if the ID is invalid.
53
     */
54
    public SimpleGeofence getGeofence(String id) {
55
        // Get the latitude for the geofence identified by id, or INVALID_FLOAT_VALUE if it doesn't
56
        // exist (similarly for the other values that follow).
57
        double lat = mPrefs.getFloat(getGeofenceFieldKey(id, KEY_LATITUDE),
58
                INVALID_FLOAT_VALUE);
59
        double lng = mPrefs.getFloat(getGeofenceFieldKey(id, KEY_LONGITUDE),
60
                INVALID_FLOAT_VALUE);
61
        float radius = mPrefs.getFloat(getGeofenceFieldKey(id, KEY_RADIUS),
62
                INVALID_FLOAT_VALUE);
63
        long expirationDuration =
64
                mPrefs.getLong(getGeofenceFieldKey(id, KEY_EXPIRATION_DURATION),
65
                        INVALID_LONG_VALUE);
66
        int transitionType = mPrefs.getInt(getGeofenceFieldKey(id, KEY_TRANSITION_TYPE),
67
                INVALID_INT_VALUE);
68
        // If none of the values is incorrect, return the object.
69
        if (lat != INVALID_FLOAT_VALUE
70
                && lng != INVALID_FLOAT_VALUE
71
                && radius != INVALID_FLOAT_VALUE
72
                && expirationDuration != INVALID_LONG_VALUE
73
                && transitionType != INVALID_INT_VALUE) {
74
            return new SimpleGeofence(id, lat, lng, radius, expirationDuration, transitionType);
75
        }
76
        // Otherwise, return null.
77
        return null;
78
    }
79
 
80
    /**
81
     * Save a geofence.
82
     * @param geofence The SimpleGeofence with the values you want to save in SharedPreferences.
83
     */
84
    public void setGeofence(String id, SimpleGeofence geofence) {
85
        // Get a SharedPreferences editor instance. Among other things, SharedPreferences
86
        // ensures that updates are atomic and non-concurrent.
87
        SharedPreferences.Editor prefs = mPrefs.edit();
88
        // Write the Geofence values to SharedPreferences.
89
        prefs.putFloat(getGeofenceFieldKey(id, KEY_LATITUDE), (float) geofence.getLatitude());
90
        prefs.putFloat(getGeofenceFieldKey(id, KEY_LONGITUDE), (float) geofence.getLongitude());
91
        prefs.putFloat(getGeofenceFieldKey(id, KEY_RADIUS), geofence.getRadius());
92
        prefs.putLong(getGeofenceFieldKey(id, KEY_EXPIRATION_DURATION),
93
                geofence.getExpirationDuration());
94
        prefs.putInt(getGeofenceFieldKey(id, KEY_TRANSITION_TYPE),
95
                geofence.getTransitionType());
96
        // Commit the changes.
97
        prefs.commit();
98
    }
99
 
100
    /**
101
     * Remove a flattened geofence object from storage by removing all of its keys.
102
     */
103
    public void clearGeofence(String id) {
104
        SharedPreferences.Editor prefs = mPrefs.edit();
105
        prefs.remove(getGeofenceFieldKey(id, KEY_LATITUDE));
106
        prefs.remove(getGeofenceFieldKey(id, KEY_LONGITUDE));
107
        prefs.remove(getGeofenceFieldKey(id, KEY_RADIUS));
108
        prefs.remove(getGeofenceFieldKey(id, KEY_EXPIRATION_DURATION));
109
        prefs.remove(getGeofenceFieldKey(id, KEY_TRANSITION_TYPE));
110
        prefs.commit();
111
    }
112
 
113
    /**
114
     * Given a Geofence object's ID and the name of a field (for example, KEY_LATITUDE), return
115
     * the key name of the object's values in SharedPreferences.
116
     * @param id The ID of a Geofence object.
117
     * @param fieldName The field represented by the key.
118
     * @return The full key name of a value in SharedPreferences.
119
     */
120
    private String getGeofenceFieldKey(String id, String fieldName) {
121
        return KEY_PREFIX + "_" + id + "_" + fieldName;
122
    }
123
 
124
}
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.