1 /*
2  * Copyright (C) 2010 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 #include "SensorRecord.h"
18 
19 #include "SensorEventConnection.h"
20 
21 namespace android {
22 
SensorRecord(const sp<const SensorEventConnection> & connection)23 SensorService::SensorRecord::SensorRecord(
24         const sp<const SensorEventConnection>& connection)
25 {
26     mConnections.add(connection);
27 }
28 
addConnection(const sp<const SensorEventConnection> & connection)29 bool SensorService::SensorRecord::addConnection(
30         const sp<const SensorEventConnection>& connection)
31 {
32     if (mConnections.indexOf(connection) < 0) {
33         mConnections.add(connection);
34         return true;
35     }
36     return false;
37 }
38 
removeConnection(const wp<const SensorEventConnection> & connection)39 bool SensorService::SensorRecord::removeConnection(
40         const wp<const SensorEventConnection>& connection)
41 {
42     ssize_t index = mConnections.indexOf(connection);
43     if (index >= 0) {
44         mConnections.removeItemsAt(index, 1);
45     }
46     // Remove this connections from the queue of flush() calls made on this sensor.
47     for (Vector< wp<const SensorEventConnection> >::iterator it = mPendingFlushConnections.begin();
48             it != mPendingFlushConnections.end(); ) {
49         if (*it == connection) {
50             it = mPendingFlushConnections.erase(it);
51         } else {
52             ++it;
53         }
54     }
55     return mConnections.size() ? false : true;
56 }
57 
addPendingFlushConnection(const sp<const SensorEventConnection> & connection)58 void SensorService::SensorRecord::addPendingFlushConnection(
59         const sp<const SensorEventConnection>& connection) {
60     mPendingFlushConnections.add(connection);
61 }
62 
removeFirstPendingFlushConnection()63 void SensorService::SensorRecord::removeFirstPendingFlushConnection() {
64     if (mPendingFlushConnections.size() > 0) {
65         mPendingFlushConnections.removeAt(0);
66     }
67 }
68 
69 wp<const SensorService::SensorEventConnection>
getFirstPendingFlushConnection()70         SensorService::SensorRecord::getFirstPendingFlushConnection() {
71     if (mPendingFlushConnections.size() > 0) {
72         return mPendingFlushConnections[0];
73     }
74     return nullptr;
75 }
76 
clearAllPendingFlushConnections()77 void SensorService::SensorRecord::clearAllPendingFlushConnections() {
78     mPendingFlushConnections.clear();
79 }
80 
81 } // namespace android
82