1 /*
2  * Copyright (C) 2014 Google Inc. All Rights Reserved.
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.speedtracker;
18 
19 import com.example.android.wearable.speedtracker.common.LocationEntry;
20 import com.example.android.wearable.speedtracker.common.Utils;
21 import com.example.android.wearable.speedtracker.db.LocationDbHelper;
22 
23 import java.util.ArrayList;
24 import java.util.Calendar;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 
29 /**
30  * A class that wraps database access and provides a cache for various GPS data.
31  */
32 public class LocationDataManager {
33 
34     private final Map<String, List<LocationEntry>> mPointsMap =
35             new HashMap<String, List<LocationEntry>>();
36 
37     private LocationDbHelper mDbHelper;
38 
LocationDataManager(LocationDbHelper dbHelper)39     public LocationDataManager(LocationDbHelper dbHelper) {
40         mDbHelper = dbHelper;
41     }
42 
43     /**
44      * Returns a list of {@link com.example.android.wearable.speedtracker.common.LocationEntry}
45      * objects for the day that the {@link java.util.Calendar} object points at. Internally it uses
46      * a cache to speed up subsequent calls. If there is no cached value, it gets the result from
47      * the database.
48      */
getPoints(Calendar calendar)49     public final List<LocationEntry> getPoints(Calendar calendar) {
50         String day = Utils.getHashedDay(calendar);
51         synchronized (mPointsMap) {
52             if (mPointsMap.get(day) == null) {
53                 // there is no cache for this day, so lets get it from DB
54                 List<LocationEntry> points = mDbHelper.read(calendar);
55                 mPointsMap.put(day, points);
56             }
57         }
58         return mPointsMap.get(day);
59     }
60 
61     /**
62      * Clears the data for the day that the {@link java.util.Calendar} object falls on. This method
63      * removes the entries from the database and updates the cache accordingly.
64      */
clearPoints(Calendar calendar)65     public final int clearPoints(Calendar calendar) {
66         synchronized (mPointsMap) {
67             String day = Utils.getHashedDay(calendar);
68             mPointsMap.remove(day);
69             return mDbHelper.delete(day);
70         }
71     }
72 
73     /**
74      * Adds a {@link com.example.android.wearable.speedtracker.common.LocationEntry} point to the
75      * database and cache if it is a new point.
76      */
addPoint(LocationEntry entry)77     public final void addPoint(LocationEntry entry) {
78         synchronized (mPointsMap) {
79             List<LocationEntry> points = getPoints(entry.calendar);
80             if (points == null || points.isEmpty()) {
81                 mDbHelper.insert(entry);
82                 if (points == null) {
83                     points = new ArrayList<LocationEntry>();
84                 }
85                 points.add(entry);
86                 mPointsMap.put(entry.day, points);
87             } else {
88                 if (!points.contains(entry)) {
89                     mDbHelper.insert(entry);
90                     points.add(entry);
91                 }
92             }
93         }
94     }
95 }
96