Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | */ |
Pavel Maltsev | 0477e29 | 2016-05-27 12:22:36 -0700 | [diff] [blame] | 16 | package android.car.cluster.renderer; |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 17 | |
| 18 | import android.annotation.Nullable; |
Pavel Maltsev | 8cf8691 | 2016-04-01 18:01:51 -0700 | [diff] [blame] | 19 | import android.car.navigation.CarNavigationInstrumentCluster; |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 20 | import android.graphics.Bitmap; |
Pavel Maltsev | 1fd2ff6 | 2017-07-05 19:09:07 -0700 | [diff] [blame^] | 21 | import android.os.Bundle; |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 22 | import android.os.Handler; |
| 23 | import android.os.Looper; |
| 24 | import android.os.Message; |
| 25 | |
Pavel Maltsev | 0477e29 | 2016-05-27 12:22:36 -0700 | [diff] [blame] | 26 | import java.lang.ref.WeakReference; |
Pavel Maltsev | 8cf8691 | 2016-04-01 18:01:51 -0700 | [diff] [blame] | 27 | import java.util.concurrent.CountDownLatch; |
| 28 | |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 29 | /** |
| 30 | * A wrapper over {@link NavigationRenderer} that runs all its methods in the context of provided |
| 31 | * looper. It is guaranteed that all calls will be invoked in order they were called. |
| 32 | */ |
Pavel Maltsev | 0477e29 | 2016-05-27 12:22:36 -0700 | [diff] [blame] | 33 | /* package */ class ThreadSafeNavigationRenderer extends NavigationRenderer { |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 34 | |
| 35 | private final Handler mHandler; |
Pavel Maltsev | 8cf8691 | 2016-04-01 18:01:51 -0700 | [diff] [blame] | 36 | private final NavigationRenderer mRenderer; |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 37 | |
| 38 | private final static int MSG_NAV_START = 1; |
| 39 | private final static int MSG_NAV_STOP = 2; |
| 40 | private final static int MSG_NAV_NEXT_TURN = 3; |
| 41 | private final static int MSG_NAV_NEXT_TURN_DISTANCE = 4; |
Pavel Maltsev | 1fd2ff6 | 2017-07-05 19:09:07 -0700 | [diff] [blame^] | 42 | private final static int MSG_EVENT = 5; |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 43 | |
| 44 | /** Creates thread-safe {@link NavigationRenderer}. Returns null if renderer == null */ |
| 45 | @Nullable |
Pavel Maltsev | 0477e29 | 2016-05-27 12:22:36 -0700 | [diff] [blame] | 46 | static NavigationRenderer createFor(Looper looper, NavigationRenderer renderer) { |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 47 | return renderer == null ? null : new ThreadSafeNavigationRenderer(looper, renderer); |
| 48 | } |
| 49 | |
| 50 | private ThreadSafeNavigationRenderer(Looper looper, NavigationRenderer renderer) { |
Pavel Maltsev | 8cf8691 | 2016-04-01 18:01:51 -0700 | [diff] [blame] | 51 | mRenderer = renderer; |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 52 | mHandler = new NavigationRendererHandler(looper, renderer); |
| 53 | } |
| 54 | |
| 55 | @Override |
Pavel Maltsev | 8cf8691 | 2016-04-01 18:01:51 -0700 | [diff] [blame] | 56 | public CarNavigationInstrumentCluster getNavigationProperties() { |
Pavel Maltsev | 0477e29 | 2016-05-27 12:22:36 -0700 | [diff] [blame] | 57 | if (mHandler.getLooper() == Looper.myLooper()) { |
| 58 | return mRenderer.getNavigationProperties(); |
| 59 | } else { |
| 60 | return runAndWaitResult(mHandler, |
| 61 | new RunnableWithResult<CarNavigationInstrumentCluster>() { |
| 62 | @Override |
| 63 | protected CarNavigationInstrumentCluster createResult() { |
| 64 | return mRenderer.getNavigationProperties(); |
| 65 | } |
| 66 | }); |
| 67 | } |
Pavel Maltsev | 8cf8691 | 2016-04-01 18:01:51 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 71 | public void onStartNavigation() { |
| 72 | mHandler.sendMessage(mHandler.obtainMessage(MSG_NAV_START)); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void onStopNavigation() { |
| 77 | mHandler.sendMessage(mHandler.obtainMessage(MSG_NAV_STOP)); |
| 78 | } |
| 79 | |
| 80 | @Override |
Keun-young Park | 3388e78 | 2016-10-05 16:28:32 -0700 | [diff] [blame] | 81 | public void onNextTurnChanged(int event, CharSequence eventName, int turnAngle, int turnNumber, |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 82 | Bitmap image, int turnSide) { |
| 83 | mHandler.sendMessage(mHandler.obtainMessage(MSG_NAV_NEXT_TURN, |
Keun-young Park | 3388e78 | 2016-10-05 16:28:32 -0700 | [diff] [blame] | 84 | new NextTurn(event, eventName, turnAngle, turnNumber, image, turnSide))); |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
Pavel Maltsev | a928f13 | 2016-08-01 16:46:10 -0700 | [diff] [blame] | 88 | public void onNextTurnDistanceChanged(int distanceMeters, int timeSeconds, |
| 89 | int displayDistanceMillis, int displayDistanceUnit) { |
| 90 | ManeuverDistance distance = new ManeuverDistance(distanceMeters, timeSeconds, |
| 91 | displayDistanceMillis, displayDistanceUnit); |
| 92 | mHandler.sendMessage(mHandler.obtainMessage(MSG_NAV_NEXT_TURN_DISTANCE, distance)); |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Pavel Maltsev | 1fd2ff6 | 2017-07-05 19:09:07 -0700 | [diff] [blame^] | 95 | @Override |
| 96 | public void onEvent(int eventType, Bundle bundle) { |
| 97 | mHandler.sendMessage(mHandler.obtainMessage(MSG_EVENT, eventType, 0, bundle)); |
| 98 | } |
| 99 | |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 100 | private static class NavigationRendererHandler extends RendererHandler<NavigationRenderer> { |
| 101 | |
| 102 | NavigationRendererHandler(Looper looper, NavigationRenderer renderer) { |
| 103 | super(looper, renderer); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void handleMessage(Message msg, NavigationRenderer renderer) { |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 108 | switch (msg.what) { |
| 109 | case MSG_NAV_START: |
| 110 | renderer.onStartNavigation(); |
| 111 | break; |
| 112 | case MSG_NAV_STOP: |
| 113 | renderer.onStopNavigation(); |
| 114 | break; |
| 115 | case MSG_NAV_NEXT_TURN: |
| 116 | NextTurn nt = (NextTurn) msg.obj; |
Keun-young Park | 3388e78 | 2016-10-05 16:28:32 -0700 | [diff] [blame] | 117 | renderer.onNextTurnChanged(nt.event, nt.eventName, nt.turnAngle, nt.turnNumber, |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 118 | nt.bitmap, nt.turnSide); |
| 119 | break; |
| 120 | case MSG_NAV_NEXT_TURN_DISTANCE: |
Pavel Maltsev | a928f13 | 2016-08-01 16:46:10 -0700 | [diff] [blame] | 121 | ManeuverDistance d = (ManeuverDistance) msg.obj; |
| 122 | renderer.onNextTurnDistanceChanged( |
| 123 | d.meters, d.seconds, d.displayDistanceMillis, d.displayDistanceUnit); |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 124 | break; |
Pavel Maltsev | 1fd2ff6 | 2017-07-05 19:09:07 -0700 | [diff] [blame^] | 125 | case MSG_EVENT: |
| 126 | Bundle bundle = (Bundle) msg.obj; |
| 127 | renderer.onEvent(msg.arg1, bundle); |
| 128 | break; |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 129 | default: |
| 130 | throw new IllegalArgumentException("Msg: " + msg.what); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
Jason Tholstrup | 6ad2b51 | 2016-07-14 14:27:37 -0700 | [diff] [blame] | 135 | private static <E> E runAndWaitResult(Handler handler, final RunnableWithResult<E> runnable) { |
Pavel Maltsev | 0477e29 | 2016-05-27 12:22:36 -0700 | [diff] [blame] | 136 | final CountDownLatch latch = new CountDownLatch(1); |
Jason Tholstrup | 6ad2b51 | 2016-07-14 14:27:37 -0700 | [diff] [blame] | 137 | |
| 138 | Runnable wrappedRunnable = new Runnable() { |
| 139 | @Override |
| 140 | public void run() { |
| 141 | runnable.run(); |
| 142 | latch.countDown(); |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | handler.post(wrappedRunnable); |
| 147 | |
Pavel Maltsev | 8cf8691 | 2016-04-01 18:01:51 -0700 | [diff] [blame] | 148 | try { |
Pavel Maltsev | 0477e29 | 2016-05-27 12:22:36 -0700 | [diff] [blame] | 149 | latch.await(); |
Pavel Maltsev | 8cf8691 | 2016-04-01 18:01:51 -0700 | [diff] [blame] | 150 | } catch (InterruptedException e) { |
| 151 | throw new RuntimeException(e); |
| 152 | } |
| 153 | return runnable.getResult(); |
| 154 | } |
| 155 | |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 156 | private static class NextTurn { |
| 157 | private final int event; |
Keun-young Park | 3388e78 | 2016-10-05 16:28:32 -0700 | [diff] [blame] | 158 | private final CharSequence eventName; |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 159 | private final int turnAngle; |
| 160 | private final int turnNumber; |
| 161 | private final Bitmap bitmap; |
| 162 | private final int turnSide; |
| 163 | |
Keun-young Park | 3388e78 | 2016-10-05 16:28:32 -0700 | [diff] [blame] | 164 | NextTurn(int event, CharSequence eventName, int turnAngle, int turnNumber, Bitmap bitmap, |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 165 | int turnSide) { |
| 166 | this.event = event; |
Keun-young Park | 3388e78 | 2016-10-05 16:28:32 -0700 | [diff] [blame] | 167 | this.eventName = eventName; |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 168 | this.turnAngle = turnAngle; |
| 169 | this.turnNumber = turnNumber; |
| 170 | this.bitmap = bitmap; |
| 171 | this.turnSide = turnSide; |
| 172 | } |
| 173 | } |
Pavel Maltsev | 8cf8691 | 2016-04-01 18:01:51 -0700 | [diff] [blame] | 174 | |
Pavel Maltsev | 0477e29 | 2016-05-27 12:22:36 -0700 | [diff] [blame] | 175 | private static abstract class RunnableWithResult<T> implements Runnable { |
Pavel Maltsev | 8cf8691 | 2016-04-01 18:01:51 -0700 | [diff] [blame] | 176 | private volatile T result; |
| 177 | |
| 178 | protected abstract T createResult(); |
| 179 | |
| 180 | @Override |
| 181 | public void run() { |
| 182 | result = createResult(); |
| 183 | } |
| 184 | |
| 185 | public T getResult() { |
| 186 | return result; |
| 187 | } |
| 188 | } |
Pavel Maltsev | 0477e29 | 2016-05-27 12:22:36 -0700 | [diff] [blame] | 189 | |
| 190 | private static abstract class RendererHandler<T> extends Handler { |
| 191 | |
| 192 | private final WeakReference<T> mRendererRef; |
| 193 | |
| 194 | RendererHandler(Looper looper, T renderer) { |
| 195 | super(looper); |
| 196 | mRendererRef = new WeakReference<>(renderer); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void handleMessage(Message msg) { |
| 201 | T renderer = mRendererRef.get(); |
| 202 | if (renderer != null) { |
| 203 | handleMessage(msg, renderer); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | public abstract void handleMessage(Message msg, T renderer); |
| 208 | } |
Pavel Maltsev | a928f13 | 2016-08-01 16:46:10 -0700 | [diff] [blame] | 209 | |
| 210 | private static class ManeuverDistance { |
| 211 | final int meters; |
| 212 | final int seconds; |
| 213 | final int displayDistanceMillis; |
| 214 | final int displayDistanceUnit; |
| 215 | |
| 216 | ManeuverDistance(int meters, int seconds, int displayDistanceMillis, |
| 217 | int displayDistanceUnit) { |
| 218 | this.meters = meters; |
| 219 | this.seconds = seconds; |
| 220 | this.displayDistanceMillis = displayDistanceMillis; |
| 221 | this.displayDistanceUnit = displayDistanceUnit; |
| 222 | } |
| 223 | } |
Pavel Maltsev | 1ecdd6c | 2016-03-02 16:33:44 -0800 | [diff] [blame] | 224 | } |