diff options
author | Kai Wang | 2018-06-06 14:35:09 -0500 |
---|---|---|
committer | Android (Google) Code Review | 2018-06-06 14:35:09 -0500 |
commit | f5a142d88a464781d592052a41fe37c81b56c74b (patch) | |
tree | ca4f6d8a0f78ead55bf314c61c9a6c257a7b243c | |
parent | 7a9c4f15c59f58d376132af4bdef731d1fb2791d (diff) | |
parent | da0eecb6294719791537bd8de5b881e35355bc86 (diff) | |
download | platform-packages-services-car-f5a142d88a464781d592052a41fe37c81b56c74b.tar.gz platform-packages-services-car-f5a142d88a464781d592052a41fe37c81b56c74b.tar.xz platform-packages-services-car-f5a142d88a464781d592052a41fe37c81b56c74b.zip |
Merge "Clean up in Car*Manager" into pi-dev
-rw-r--r-- | car-lib/src/android/car/hardware/CarSensorManager.java | 32 | ||||
-rw-r--r-- | car-lib/src/android/car/hardware/CarVendorExtensionManager.java | 67 | ||||
-rw-r--r-- | car-lib/src/android/car/hardware/property/CarPropertyManager.java | 4 | ||||
-rw-r--r-- | car-lib/src/com/android/car/internal/CarRatedFloatListeners.java (renamed from car-lib/src/com/android/car/internal/CarRatedListeners2.java) | 18 |
4 files changed, 64 insertions, 57 deletions
diff --git a/car-lib/src/android/car/hardware/CarSensorManager.java b/car-lib/src/android/car/hardware/CarSensorManager.java index 7cf59408..f6cc8dfd 100644 --- a/car-lib/src/android/car/hardware/CarSensorManager.java +++ b/car-lib/src/android/car/hardware/CarSensorManager.java | |||
@@ -268,11 +268,9 @@ public final class CarSensorManager implements CarManagerBase { | |||
268 | 268 | ||
269 | @Override | 269 | @Override |
270 | public void onChangeEvent(CarPropertyValue value) { | 270 | public void onChangeEvent(CarPropertyValue value) { |
271 | synchronized (this) { | 271 | CarSensorManager manager = mManager.get(); |
272 | CarSensorManager manager = mManager.get(); | 272 | if (manager != null) { |
273 | if (manager != null) { | 273 | manager.handleOnChangeEvent(value, mListener); |
274 | manager.handleOnChangeEvent(value, mListener); | ||
275 | } | ||
276 | } | 274 | } |
277 | } | 275 | } |
278 | 276 | ||
@@ -469,22 +467,27 @@ public final class CarSensorManager implements CarManagerBase { | |||
469 | } | 467 | } |
470 | 468 | ||
471 | private CarSensorEvent createCarSensorEvent(CarPropertyValue propertyValue) { | 469 | private CarSensorEvent createCarSensorEvent(CarPropertyValue propertyValue) { |
472 | Class<?> actualClass = propertyValue.getValue().getClass(); | ||
473 | CarSensorEvent event = null; | 470 | CarSensorEvent event = null; |
474 | if (actualClass == Float.class) { | 471 | Object o = propertyValue.getValue(); |
472 | if (o instanceof Float) { | ||
475 | event = new CarSensorEvent(propertyValue.getPropertyId(), | 473 | event = new CarSensorEvent(propertyValue.getPropertyId(), |
476 | propertyValue.getTimestamp(), 1, 0, 0); | 474 | propertyValue.getTimestamp(), 1, 0, 0); |
477 | event.floatValues[0] = (float) propertyValue.getValue(); | 475 | event.floatValues[0] = (float) o; |
478 | } else if (actualClass == Integer.class) { | 476 | } else if (o instanceof Integer) { |
479 | event = new CarSensorEvent(propertyValue.getPropertyId(), | 477 | event = new CarSensorEvent(propertyValue.getPropertyId(), |
480 | propertyValue.getTimestamp(), 0, 1, 0); | 478 | propertyValue.getTimestamp(), 0, 1, 0); |
481 | event.intValues[0] = (int) propertyValue.getValue(); | 479 | event.intValues[0] = (int) o; |
482 | } else if (actualClass == Boolean.class) { | 480 | } else if (o instanceof Boolean) { |
483 | event = new CarSensorEvent(propertyValue.getPropertyId(), | 481 | event = new CarSensorEvent(propertyValue.getPropertyId(), |
484 | propertyValue.getTimestamp(), 0, 1, 0); | 482 | propertyValue.getTimestamp(), 0, 1, 0); |
485 | event.intValues[0] = (boolean) propertyValue.getValue() ? 1 : 0; | 483 | event.intValues[0] = (boolean) o ? 1 : 0; |
486 | } else { | 484 | } else if (o instanceof Long[]) { |
487 | // TODO: handle int64_vec and mixed type | 485 | Long[] value = (Long[]) o; |
486 | event = new CarSensorEvent(propertyValue.getPropertyId(), | ||
487 | propertyValue.getTimestamp(), 0, 0, value.length); | ||
488 | for (int i = 0; i < value.length; i++) { | ||
489 | event.longValues[i] = value[i]; | ||
490 | } | ||
488 | } | 491 | } |
489 | return event; | 492 | return event; |
490 | } | 493 | } |
@@ -509,6 +512,7 @@ public final class CarSensorManager implements CarManagerBase { | |||
509 | for (CarPropertyConfig p : propertyConfigs) { | 512 | for (CarPropertyConfig p : propertyConfigs) { |
510 | if (p.getPropertyId() == type) { | 513 | if (p.getPropertyId() == type) { |
511 | b = createWheelDistanceTickBundle(p.getConfigArray()); | 514 | b = createWheelDistanceTickBundle(p.getConfigArray()); |
515 | break; | ||
512 | } | 516 | } |
513 | } | 517 | } |
514 | break; | 518 | break; |
diff --git a/car-lib/src/android/car/hardware/CarVendorExtensionManager.java b/car-lib/src/android/car/hardware/CarVendorExtensionManager.java index ce2831c6..9f067644 100644 --- a/car-lib/src/android/car/hardware/CarVendorExtensionManager.java +++ b/car-lib/src/android/car/hardware/CarVendorExtensionManager.java | |||
@@ -28,6 +28,7 @@ import android.util.ArraySet; | |||
28 | import com.android.internal.annotations.GuardedBy; | 28 | import com.android.internal.annotations.GuardedBy; |
29 | 29 | ||
30 | import java.lang.ref.WeakReference; | 30 | import java.lang.ref.WeakReference; |
31 | import java.util.ArrayList; | ||
31 | import java.util.Collection; | 32 | import java.util.Collection; |
32 | import java.util.List; | 33 | import java.util.List; |
33 | 34 | ||
@@ -44,41 +45,18 @@ public final class CarVendorExtensionManager implements CarManagerBase { | |||
44 | private final static boolean DBG = false; | 45 | private final static boolean DBG = false; |
45 | private final static String TAG = CarVendorExtensionManager.class.getSimpleName(); | 46 | private final static String TAG = CarVendorExtensionManager.class.getSimpleName(); |
46 | private final CarPropertyManager mPropertyManager; | 47 | private final CarPropertyManager mPropertyManager; |
47 | private CarPropertyEventListenerToBase mListenerToBase = null; | ||
48 | 48 | ||
49 | @GuardedBy("mLock") | 49 | @GuardedBy("mLock") |
50 | private final ArraySet<CarVendorExtensionCallback> mCallbacks = new ArraySet<>(); | 50 | private final ArraySet<CarVendorExtensionCallback> mCallbacks = new ArraySet<>(); |
51 | private final Object mLock = new Object(); | 51 | private final Object mLock = new Object(); |
52 | 52 | ||
53 | private static class CarPropertyEventListenerToBase implements | 53 | @GuardedBy("mLock") |
54 | CarPropertyManager.CarPropertyEventListener{ | 54 | private CarPropertyEventListenerToBase mListenerToBase = null; |
55 | private final WeakReference<CarVendorExtensionManager> mManager; | ||
56 | |||
57 | CarPropertyEventListenerToBase(CarVendorExtensionManager manager) { | ||
58 | mManager = new WeakReference<>(manager); | ||
59 | } | ||
60 | |||
61 | @Override | ||
62 | public void onChangeEvent(CarPropertyValue value) { | ||
63 | CarVendorExtensionManager manager = mManager.get(); | ||
64 | if (manager != null) { | ||
65 | manager.handleOnChangeEvent(value); | ||
66 | } | ||
67 | } | ||
68 | |||
69 | @Override | ||
70 | public void onErrorEvent(int propertyId, int zone) { | ||
71 | CarVendorExtensionManager manager = mManager.get(); | ||
72 | if (manager != null) { | ||
73 | manager.handleOnErrorEvent(propertyId, zone); | ||
74 | } | ||
75 | } | ||
76 | } | ||
77 | 55 | ||
78 | private void handleOnChangeEvent(CarPropertyValue value) { | 56 | private void handleOnChangeEvent(CarPropertyValue value) { |
79 | Collection<CarVendorExtensionCallback> callbacks; | 57 | Collection<CarVendorExtensionCallback> callbacks; |
80 | synchronized (this) { | 58 | synchronized (mLock) { |
81 | callbacks = new ArraySet<>(mCallbacks); | 59 | callbacks = new ArrayList<>(mCallbacks); |
82 | } | 60 | } |
83 | for (CarVendorExtensionCallback l: callbacks) { | 61 | for (CarVendorExtensionCallback l: callbacks) { |
84 | l.onChangeEvent(value); | 62 | l.onChangeEvent(value); |
@@ -87,14 +65,13 @@ public final class CarVendorExtensionManager implements CarManagerBase { | |||
87 | 65 | ||
88 | private void handleOnErrorEvent(int propertyId, int zone) { | 66 | private void handleOnErrorEvent(int propertyId, int zone) { |
89 | Collection<CarVendorExtensionCallback> listeners; | 67 | Collection<CarVendorExtensionCallback> listeners; |
90 | synchronized (this) { | 68 | synchronized (mLock) { |
91 | listeners = new ArraySet<>(mCallbacks); | 69 | listeners = new ArrayList<>(mCallbacks); |
92 | } | 70 | } |
93 | if (!listeners.isEmpty()) { | 71 | for (CarVendorExtensionCallback l: listeners) { |
94 | for (CarVendorExtensionCallback l: listeners) { | 72 | l.onErrorEvent(propertyId, zone); |
95 | l.onErrorEvent(propertyId, zone); | ||
96 | } | ||
97 | } | 73 | } |
74 | |||
98 | } | 75 | } |
99 | 76 | ||
100 | /** | 77 | /** |
@@ -242,4 +219,28 @@ public final class CarVendorExtensionManager implements CarManagerBase { | |||
242 | public void onCarDisconnected() { | 219 | public void onCarDisconnected() { |
243 | mPropertyManager.onCarDisconnected(); | 220 | mPropertyManager.onCarDisconnected(); |
244 | } | 221 | } |
222 | private static class CarPropertyEventListenerToBase implements | ||
223 | CarPropertyManager.CarPropertyEventListener { | ||
224 | private final WeakReference<CarVendorExtensionManager> mManager; | ||
225 | |||
226 | CarPropertyEventListenerToBase(CarVendorExtensionManager manager) { | ||
227 | mManager = new WeakReference<>(manager); | ||
228 | } | ||
229 | |||
230 | @Override | ||
231 | public void onChangeEvent(CarPropertyValue value) { | ||
232 | CarVendorExtensionManager manager = mManager.get(); | ||
233 | if (manager != null) { | ||
234 | manager.handleOnChangeEvent(value); | ||
235 | } | ||
236 | } | ||
237 | |||
238 | @Override | ||
239 | public void onErrorEvent(int propertyId, int zone) { | ||
240 | CarVendorExtensionManager manager = mManager.get(); | ||
241 | if (manager != null) { | ||
242 | manager.handleOnErrorEvent(propertyId, zone); | ||
243 | } | ||
244 | } | ||
245 | } | ||
245 | } | 246 | } |
diff --git a/car-lib/src/android/car/hardware/property/CarPropertyManager.java b/car-lib/src/android/car/hardware/property/CarPropertyManager.java index d59ce58f..5c094e75 100644 --- a/car-lib/src/android/car/hardware/property/CarPropertyManager.java +++ b/car-lib/src/android/car/hardware/property/CarPropertyManager.java | |||
@@ -30,7 +30,7 @@ import android.util.ArraySet; | |||
30 | import android.util.Log; | 30 | import android.util.Log; |
31 | import android.util.SparseArray; | 31 | import android.util.SparseArray; |
32 | 32 | ||
33 | import com.android.car.internal.CarRatedListeners2; | 33 | import com.android.car.internal.CarRatedFloatListeners; |
34 | import com.android.car.internal.SingleMessageHandler; | 34 | import com.android.car.internal.SingleMessageHandler; |
35 | 35 | ||
36 | import java.lang.ref.WeakReference; | 36 | import java.lang.ref.WeakReference; |
@@ -370,7 +370,7 @@ public class CarPropertyManager implements CarManagerBase { | |||
370 | } | 370 | } |
371 | 371 | ||
372 | 372 | ||
373 | private class CarPropertyListeners extends CarRatedListeners2<CarPropertyEventListener> { | 373 | private class CarPropertyListeners extends CarRatedFloatListeners<CarPropertyEventListener> { |
374 | CarPropertyListeners(float rate) { | 374 | CarPropertyListeners(float rate) { |
375 | super(rate); | 375 | super(rate); |
376 | } | 376 | } |
diff --git a/car-lib/src/com/android/car/internal/CarRatedListeners2.java b/car-lib/src/com/android/car/internal/CarRatedFloatListeners.java index 8708cd4f..c59a2fa3 100644 --- a/car-lib/src/com/android/car/internal/CarRatedListeners2.java +++ b/car-lib/src/com/android/car/internal/CarRatedFloatListeners.java | |||
@@ -23,22 +23,24 @@ import java.util.Map; | |||
23 | 23 | ||
24 | /** | 24 | /** |
25 | * Represent listeners for a property grouped by their rate. | 25 | * Represent listeners for a property grouped by their rate. |
26 | * @param <EventListenerType> | 26 | * T is a type of EventListener such as CarPropertyEventListener |
27 | * in {@link android.car.hardware.property.CarPropertyManager} | ||
28 | * @param <T> | ||
27 | * @hide | 29 | * @hide |
28 | */ | 30 | */ |
29 | public class CarRatedListeners2<EventListenerType> { | 31 | public class CarRatedFloatListeners<T> { |
30 | private final Map<EventListenerType, Float> mListenersToRate = new HashMap<>(4); | 32 | private final Map<T, Float> mListenersToRate = new HashMap<>(4); |
31 | 33 | ||
32 | private float mUpdateRate; | 34 | private float mUpdateRate; |
33 | 35 | ||
34 | protected long mLastUpdateTime = -1; | 36 | protected long mLastUpdateTime = -1; |
35 | 37 | ||
36 | protected CarRatedListeners2(float rate) { | 38 | protected CarRatedFloatListeners(float rate) { |
37 | mUpdateRate = rate; | 39 | mUpdateRate = rate; |
38 | } | 40 | } |
39 | 41 | ||
40 | /** Check listener */ | 42 | /** Check listener */ |
41 | public boolean contains(EventListenerType listener) { | 43 | public boolean contains(T listener) { |
42 | return mListenersToRate.containsKey(listener); | 44 | return mListenersToRate.containsKey(listener); |
43 | } | 45 | } |
44 | /** Return current rate after updating */ | 46 | /** Return current rate after updating */ |
@@ -52,7 +54,7 @@ public class CarRatedListeners2<EventListenerType> { | |||
52 | * @param listener | 54 | * @param listener |
53 | * @return true if rate was updated. Otherwise, returns false. | 55 | * @return true if rate was updated. Otherwise, returns false. |
54 | */ | 56 | */ |
55 | public boolean remove(EventListenerType listener) { | 57 | public boolean remove(T listener) { |
56 | mListenersToRate.remove(listener); | 58 | mListenersToRate.remove(listener); |
57 | if (mListenersToRate.isEmpty()) { | 59 | if (mListenersToRate.isEmpty()) { |
58 | return false; | 60 | return false; |
@@ -76,7 +78,7 @@ public class CarRatedListeners2<EventListenerType> { | |||
76 | * @param updateRate | 78 | * @param updateRate |
77 | * @return true if rate was updated. Otherwise, returns false. | 79 | * @return true if rate was updated. Otherwise, returns false. |
78 | */ | 80 | */ |
79 | public boolean addAndUpdateRate(EventListenerType listener, float updateRate) { | 81 | public boolean addAndUpdateRate(T listener, float updateRate) { |
80 | Float oldUpdateRate = mListenersToRate.put(listener, updateRate); | 82 | Float oldUpdateRate = mListenersToRate.put(listener, updateRate); |
81 | if (mUpdateRate < updateRate) { | 83 | if (mUpdateRate < updateRate) { |
82 | mUpdateRate = updateRate; | 84 | mUpdateRate = updateRate; |
@@ -87,7 +89,7 @@ public class CarRatedListeners2<EventListenerType> { | |||
87 | return false; | 89 | return false; |
88 | } | 90 | } |
89 | 91 | ||
90 | public Collection<EventListenerType> getListeners() { | 92 | public Collection<T> getListeners() { |
91 | return mListenersToRate.keySet(); | 93 | return mListenersToRate.keySet(); |
92 | } | 94 | } |
93 | } | 95 | } |