blob: eecb76fa4babcf20157682c81c51260115874e3b [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
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#define LOG_TAG "PointerController"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages about pointer updates
22#define DEBUG_POINTER_UPDATES 0
23
24#include "PointerController.h"
25
26#include <cutils/log.h>
27
28#include <SkBitmap.h>
29#include <SkCanvas.h>
30#include <SkColor.h>
31#include <SkPaint.h>
32#include <SkXfermode.h>
33
34namespace android {
35
36// --- PointerController ---
37
Jeff Brown05dc66a2011-03-02 14:41:58 -080038// Time to wait before starting the fade when the pointer is inactive.
Jeff Brown86ea1f52011-04-12 22:39:53 -070039static const nsecs_t INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL = 15 * 1000 * 1000000LL; // 15 seconds
40static const nsecs_t INACTIVITY_TIMEOUT_DELAY_TIME_SHORT = 3 * 1000 * 1000000LL; // 3 seconds
41
42// Time to wait between animation frames.
43static const nsecs_t ANIMATION_FRAME_INTERVAL = 1000000000LL / 60;
44
45// Time to spend fading out the spot completely.
46static const nsecs_t SPOT_FADE_DURATION = 200 * 1000000LL; // 200 ms
Jeff Brown05dc66a2011-03-02 14:41:58 -080047
48// Time to spend fading out the pointer completely.
Jeff Brown86ea1f52011-04-12 22:39:53 -070049static const nsecs_t POINTER_FADE_DURATION = 500 * 1000000LL; // 500 ms
Jeff Brown05dc66a2011-03-02 14:41:58 -080050
51
Jeff Brown86ea1f52011-04-12 22:39:53 -070052// --- PointerController ---
53
54PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
55 const sp<Looper>& looper, const sp<SpriteController>& spriteController) :
56 mPolicy(policy), mLooper(looper), mSpriteController(spriteController) {
Jeff Browna6dbfdd2011-04-11 11:54:25 -070057 mHandler = new WeakMessageHandler(this);
58
Jeff Brownb4ff35d2011-01-02 16:37:43 -080059 AutoMutex _l(mLock);
60
Jeff Brown86ea1f52011-04-12 22:39:53 -070061 mLocked.animationPending = false;
62
Jeff Brownb4ff35d2011-01-02 16:37:43 -080063 mLocked.displayWidth = -1;
64 mLocked.displayHeight = -1;
65 mLocked.displayOrientation = DISPLAY_ORIENTATION_0;
66
Jeff Brown86ea1f52011-04-12 22:39:53 -070067 mLocked.presentation = PRESENTATION_POINTER;
68 mLocked.presentationChanged = false;
69
70 mLocked.inactivityTimeout = INACTIVITY_TIMEOUT_NORMAL;
71
Jeff Brown538881e2011-05-25 18:23:38 -070072 mLocked.pointerFadeDirection = 0;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080073 mLocked.pointerX = 0;
74 mLocked.pointerY = 0;
Jeff Brown538881e2011-05-25 18:23:38 -070075 mLocked.pointerAlpha = 0.0f; // pointer is initially faded
Jeff Brown86ea1f52011-04-12 22:39:53 -070076 mLocked.pointerSprite = mSpriteController->createSprite();
77 mLocked.pointerIconChanged = false;
78
Jeff Brownb4ff35d2011-01-02 16:37:43 -080079 mLocked.buttonState = 0;
80
Jeff Brown86ea1f52011-04-12 22:39:53 -070081 loadResources();
Jeff Brownb4ff35d2011-01-02 16:37:43 -080082}
83
84PointerController::~PointerController() {
Jeff Brown05dc66a2011-03-02 14:41:58 -080085 mLooper->removeMessages(mHandler);
86
Jeff Browna6dbfdd2011-04-11 11:54:25 -070087 AutoMutex _l(mLock);
Jeff Brownb4ff35d2011-01-02 16:37:43 -080088
Jeff Brown86ea1f52011-04-12 22:39:53 -070089 mLocked.pointerSprite.clear();
90
91 for (size_t i = 0; i < mLocked.spots.size(); i++) {
92 delete mLocked.spots.itemAt(i);
93 }
94 mLocked.spots.clear();
95 mLocked.recycledSprites.clear();
Jeff Brownb4ff35d2011-01-02 16:37:43 -080096}
97
98bool PointerController::getBounds(float* outMinX, float* outMinY,
99 float* outMaxX, float* outMaxY) const {
100 AutoMutex _l(mLock);
101
102 return getBoundsLocked(outMinX, outMinY, outMaxX, outMaxY);
103}
104
105bool PointerController::getBoundsLocked(float* outMinX, float* outMinY,
106 float* outMaxX, float* outMaxY) const {
107 if (mLocked.displayWidth <= 0 || mLocked.displayHeight <= 0) {
108 return false;
109 }
110
111 *outMinX = 0;
112 *outMinY = 0;
113 switch (mLocked.displayOrientation) {
114 case DISPLAY_ORIENTATION_90:
115 case DISPLAY_ORIENTATION_270:
Jeff Brown9626b142011-03-03 02:09:54 -0800116 *outMaxX = mLocked.displayHeight - 1;
117 *outMaxY = mLocked.displayWidth - 1;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800118 break;
119 default:
Jeff Brown9626b142011-03-03 02:09:54 -0800120 *outMaxX = mLocked.displayWidth - 1;
121 *outMaxY = mLocked.displayHeight - 1;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800122 break;
123 }
124 return true;
125}
126
127void PointerController::move(float deltaX, float deltaY) {
128#if DEBUG_POINTER_UPDATES
129 LOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY);
130#endif
131 if (deltaX == 0.0f && deltaY == 0.0f) {
132 return;
133 }
134
135 AutoMutex _l(mLock);
136
137 setPositionLocked(mLocked.pointerX + deltaX, mLocked.pointerY + deltaY);
138}
139
140void PointerController::setButtonState(uint32_t buttonState) {
141#if DEBUG_POINTER_UPDATES
142 LOGD("Set button state 0x%08x", buttonState);
143#endif
144 AutoMutex _l(mLock);
145
146 if (mLocked.buttonState != buttonState) {
147 mLocked.buttonState = buttonState;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800148 }
149}
150
151uint32_t PointerController::getButtonState() const {
152 AutoMutex _l(mLock);
153
154 return mLocked.buttonState;
155}
156
157void PointerController::setPosition(float x, float y) {
158#if DEBUG_POINTER_UPDATES
159 LOGD("Set pointer position to x=%0.3f, y=%0.3f", x, y);
160#endif
161 AutoMutex _l(mLock);
162
163 setPositionLocked(x, y);
164}
165
166void PointerController::setPositionLocked(float x, float y) {
167 float minX, minY, maxX, maxY;
168 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
169 if (x <= minX) {
170 mLocked.pointerX = minX;
171 } else if (x >= maxX) {
172 mLocked.pointerX = maxX;
173 } else {
174 mLocked.pointerX = x;
175 }
176 if (y <= minY) {
177 mLocked.pointerY = minY;
178 } else if (y >= maxY) {
179 mLocked.pointerY = maxY;
180 } else {
181 mLocked.pointerY = y;
182 }
Jeff Brown86ea1f52011-04-12 22:39:53 -0700183 updatePointerLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800184 }
185}
186
187void PointerController::getPosition(float* outX, float* outY) const {
188 AutoMutex _l(mLock);
189
190 *outX = mLocked.pointerX;
191 *outY = mLocked.pointerY;
192}
193
Jeff Brown538881e2011-05-25 18:23:38 -0700194void PointerController::fade(Transition transition) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800195 AutoMutex _l(mLock);
196
Jeff Brown538881e2011-05-25 18:23:38 -0700197 // Remove the inactivity timeout, since we are fading now.
198 removeInactivityTimeoutLocked();
199
200 // Start fading.
201 if (transition == TRANSITION_IMMEDIATE) {
202 mLocked.pointerFadeDirection = 0;
203 mLocked.pointerAlpha = 0.0f;
204 updatePointerLocked();
205 } else {
206 mLocked.pointerFadeDirection = -1;
207 startAnimationLocked();
208 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800209}
210
Jeff Brown538881e2011-05-25 18:23:38 -0700211void PointerController::unfade(Transition transition) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800212 AutoMutex _l(mLock);
213
Jeff Brown86ea1f52011-04-12 22:39:53 -0700214 // Always reset the inactivity timer.
215 resetInactivityTimeoutLocked();
216
Jeff Brown538881e2011-05-25 18:23:38 -0700217 // Start unfading.
218 if (transition == TRANSITION_IMMEDIATE) {
219 mLocked.pointerFadeDirection = 0;
Jeff Brown86ea1f52011-04-12 22:39:53 -0700220 mLocked.pointerAlpha = 1.0f;
221 updatePointerLocked();
Jeff Brown538881e2011-05-25 18:23:38 -0700222 } else {
223 mLocked.pointerFadeDirection = 1;
224 startAnimationLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800225 }
226}
227
Jeff Brown86ea1f52011-04-12 22:39:53 -0700228void PointerController::setPresentation(Presentation presentation) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800229 AutoMutex _l(mLock);
230
Jeff Brown86ea1f52011-04-12 22:39:53 -0700231 if (mLocked.presentation != presentation) {
232 mLocked.presentation = presentation;
233 mLocked.presentationChanged = true;
234
235 if (presentation != PRESENTATION_SPOT) {
236 fadeOutAndReleaseAllSpotsLocked();
237 }
238
239 updatePointerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800240 }
241}
242
Jeff Brown86ea1f52011-04-12 22:39:53 -0700243void PointerController::setSpots(SpotGesture spotGesture,
244 const PointerCoords* spotCoords, const uint32_t* spotIdToIndex, BitSet32 spotIdBits) {
245#if DEBUG_POINTER_UPDATES
246 LOGD("setSpots: spotGesture=%d", spotGesture);
247 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
248 uint32_t id = idBits.firstMarkedBit();
249 idBits.clearBit(id);
250 const PointerCoords& c = spotCoords[spotIdToIndex[id]];
251 LOGD(" spot %d: position=(%0.3f, %0.3f), pressure=%0.3f", id,
252 c.getAxisValue(AMOTION_EVENT_AXIS_X),
253 c.getAxisValue(AMOTION_EVENT_AXIS_Y),
254 c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
255 }
256#endif
257
258 AutoMutex _l(mLock);
259
260 mSpriteController->openTransaction();
261
262 // Add or move spots for fingers that are down.
263 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
264 uint32_t id = idBits.firstMarkedBit();
265 idBits.clearBit(id);
266
267 const PointerCoords& c = spotCoords[spotIdToIndex[id]];
268 const SpriteIcon& icon = c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE) > 0
269 ? mResources.spotTouch : mResources.spotHover;
270 float x = c.getAxisValue(AMOTION_EVENT_AXIS_X);
271 float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y);
272
273 Spot* spot = getSpotLocked(id);
274 if (!spot) {
275 spot = createAndAddSpotLocked(id);
276 }
277
278 spot->updateSprite(&icon, x, y);
279 }
280
281 // Remove spots for fingers that went up.
282 for (size_t i = 0; i < mLocked.spots.size(); i++) {
283 Spot* spot = mLocked.spots.itemAt(i);
284 if (spot->id != Spot::INVALID_ID
285 && !spotIdBits.hasBit(spot->id)) {
286 fadeOutAndReleaseSpotLocked(spot);
287 }
288 }
289
290 mSpriteController->closeTransaction();
291}
292
293void PointerController::clearSpots() {
294#if DEBUG_POINTER_UPDATES
295 LOGD("clearSpots");
296#endif
297
298 AutoMutex _l(mLock);
299
300 fadeOutAndReleaseAllSpotsLocked();
301}
302
303void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
304 AutoMutex _l(mLock);
305
306 if (mLocked.inactivityTimeout != inactivityTimeout) {
307 mLocked.inactivityTimeout = inactivityTimeout;
308 resetInactivityTimeoutLocked();
309 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800310}
311
312void PointerController::setDisplaySize(int32_t width, int32_t height) {
313 AutoMutex _l(mLock);
314
315 if (mLocked.displayWidth != width || mLocked.displayHeight != height) {
316 mLocked.displayWidth = width;
317 mLocked.displayHeight = height;
318
319 float minX, minY, maxX, maxY;
320 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
321 mLocked.pointerX = (minX + maxX) * 0.5f;
322 mLocked.pointerY = (minY + maxY) * 0.5f;
323 } else {
324 mLocked.pointerX = 0;
325 mLocked.pointerY = 0;
326 }
327
Jeff Brown86ea1f52011-04-12 22:39:53 -0700328 fadeOutAndReleaseAllSpotsLocked();
329 updatePointerLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800330 }
331}
332
333void PointerController::setDisplayOrientation(int32_t orientation) {
334 AutoMutex _l(mLock);
335
336 if (mLocked.displayOrientation != orientation) {
Jeff Brown9626b142011-03-03 02:09:54 -0800337 // Apply offsets to convert from the pixel top-left corner position to the pixel center.
338 // This creates an invariant frame of reference that we can easily rotate when
339 // taking into account that the pointer may be located at fractional pixel offsets.
340 float x = mLocked.pointerX + 0.5f;
341 float y = mLocked.pointerY + 0.5f;
342 float temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800343
Jeff Brown9626b142011-03-03 02:09:54 -0800344 // Undo the previous rotation.
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800345 switch (mLocked.displayOrientation) {
346 case DISPLAY_ORIENTATION_90:
Jeff Brown9626b142011-03-03 02:09:54 -0800347 temp = x;
348 x = mLocked.displayWidth - y;
349 y = temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800350 break;
351 case DISPLAY_ORIENTATION_180:
Jeff Brown9626b142011-03-03 02:09:54 -0800352 x = mLocked.displayWidth - x;
353 y = mLocked.displayHeight - y;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800354 break;
355 case DISPLAY_ORIENTATION_270:
Jeff Brown9626b142011-03-03 02:09:54 -0800356 temp = x;
357 x = y;
358 y = mLocked.displayHeight - temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800359 break;
360 }
361
Jeff Brown9626b142011-03-03 02:09:54 -0800362 // Perform the new rotation.
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800363 switch (orientation) {
364 case DISPLAY_ORIENTATION_90:
Jeff Brown9626b142011-03-03 02:09:54 -0800365 temp = x;
366 x = y;
Jeff Browna6dbfdd2011-04-11 11:54:25 -0700367 y = mLocked.displayWidth - temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800368 break;
369 case DISPLAY_ORIENTATION_180:
Jeff Brown9626b142011-03-03 02:09:54 -0800370 x = mLocked.displayWidth - x;
371 y = mLocked.displayHeight - y;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800372 break;
373 case DISPLAY_ORIENTATION_270:
Jeff Brown9626b142011-03-03 02:09:54 -0800374 temp = x;
375 x = mLocked.displayHeight - y;
376 y = temp;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800377 break;
378 }
379
Jeff Brown9626b142011-03-03 02:09:54 -0800380 // Apply offsets to convert from the pixel center to the pixel top-left corner position
381 // and save the results.
382 mLocked.pointerX = x - 0.5f;
383 mLocked.pointerY = y - 0.5f;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800384 mLocked.displayOrientation = orientation;
385
Jeff Brown86ea1f52011-04-12 22:39:53 -0700386 updatePointerLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800387 }
388}
389
Jeff Brown86ea1f52011-04-12 22:39:53 -0700390void PointerController::setPointerIcon(const SpriteIcon& icon) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800391 AutoMutex _l(mLock);
392
Jeff Brown86ea1f52011-04-12 22:39:53 -0700393 mLocked.pointerIcon = icon.copy();
394 mLocked.pointerIconChanged = true;
395
396 updatePointerLocked();
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800397}
398
Jeff Brown05dc66a2011-03-02 14:41:58 -0800399void PointerController::handleMessage(const Message& message) {
400 switch (message.what) {
Jeff Brown86ea1f52011-04-12 22:39:53 -0700401 case MSG_ANIMATE:
402 doAnimate();
403 break;
404 case MSG_INACTIVITY_TIMEOUT:
405 doInactivityTimeout();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800406 break;
407 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800408}
409
Jeff Brown86ea1f52011-04-12 22:39:53 -0700410void PointerController::doAnimate() {
411 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800412
Jeff Brown86ea1f52011-04-12 22:39:53 -0700413 bool keepAnimating = false;
414 mLocked.animationPending = false;
415 nsecs_t frameDelay = systemTime(SYSTEM_TIME_MONOTONIC) - mLocked.animationTime;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800416
Jeff Brown86ea1f52011-04-12 22:39:53 -0700417 // Animate pointer fade.
Jeff Brown538881e2011-05-25 18:23:38 -0700418 if (mLocked.pointerFadeDirection < 0) {
Jeff Brown86ea1f52011-04-12 22:39:53 -0700419 mLocked.pointerAlpha -= float(frameDelay) / POINTER_FADE_DURATION;
Jeff Brown538881e2011-05-25 18:23:38 -0700420 if (mLocked.pointerAlpha <= 0.0f) {
421 mLocked.pointerAlpha = 0.0f;
422 mLocked.pointerFadeDirection = 0;
423 } else {
424 keepAnimating = true;
425 }
426 updatePointerLocked();
427 } else if (mLocked.pointerFadeDirection > 0) {
428 mLocked.pointerAlpha += float(frameDelay) / POINTER_FADE_DURATION;
429 if (mLocked.pointerAlpha >= 1.0f) {
430 mLocked.pointerAlpha = 1.0f;
431 mLocked.pointerFadeDirection = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800432 } else {
Jeff Brown86ea1f52011-04-12 22:39:53 -0700433 keepAnimating = true;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800434 }
Jeff Brown86ea1f52011-04-12 22:39:53 -0700435 updatePointerLocked();
436 }
437
438 // Animate spots that are fading out and being removed.
439 for (size_t i = 0; i < mLocked.spots.size(); i++) {
440 Spot* spot = mLocked.spots.itemAt(i);
441 if (spot->id == Spot::INVALID_ID) {
442 spot->alpha -= float(frameDelay) / SPOT_FADE_DURATION;
443 if (spot->alpha <= 0) {
444 mLocked.spots.removeAt(i--);
445 releaseSpotLocked(spot);
446 } else {
447 spot->sprite->setAlpha(spot->alpha);
448 keepAnimating = true;
449 }
450 }
451 }
452
453 if (keepAnimating) {
454 startAnimationLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800455 }
456}
457
Jeff Brown86ea1f52011-04-12 22:39:53 -0700458void PointerController::doInactivityTimeout() {
Jeff Brown538881e2011-05-25 18:23:38 -0700459 fade(TRANSITION_GRADUAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800460}
461
Jeff Brown86ea1f52011-04-12 22:39:53 -0700462void PointerController::startAnimationLocked() {
463 if (!mLocked.animationPending) {
464 mLocked.animationPending = true;
465 mLocked.animationTime = systemTime(SYSTEM_TIME_MONOTONIC);
466 mLooper->sendMessageDelayed(ANIMATION_FRAME_INTERVAL, mHandler, Message(MSG_ANIMATE));
467 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800468}
469
Jeff Brown86ea1f52011-04-12 22:39:53 -0700470void PointerController::resetInactivityTimeoutLocked() {
471 mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
472
473 nsecs_t timeout = mLocked.inactivityTimeout == INACTIVITY_TIMEOUT_SHORT
474 ? INACTIVITY_TIMEOUT_DELAY_TIME_SHORT : INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL;
475 mLooper->sendMessageDelayed(timeout, mHandler, MSG_INACTIVITY_TIMEOUT);
476}
477
Jeff Brown538881e2011-05-25 18:23:38 -0700478void PointerController::removeInactivityTimeoutLocked() {
Jeff Brown86ea1f52011-04-12 22:39:53 -0700479 mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
Jeff Brown86ea1f52011-04-12 22:39:53 -0700480}
481
482void PointerController::updatePointerLocked() {
483 mSpriteController->openTransaction();
484
485 mLocked.pointerSprite->setLayer(Sprite::BASE_LAYER_POINTER);
486 mLocked.pointerSprite->setPosition(mLocked.pointerX, mLocked.pointerY);
487
488 if (mLocked.pointerAlpha > 0) {
489 mLocked.pointerSprite->setAlpha(mLocked.pointerAlpha);
490 mLocked.pointerSprite->setVisible(true);
491 } else {
492 mLocked.pointerSprite->setVisible(false);
493 }
494
495 if (mLocked.pointerIconChanged || mLocked.presentationChanged) {
496 mLocked.pointerSprite->setIcon(mLocked.presentation == PRESENTATION_POINTER
497 ? mLocked.pointerIcon : mResources.spotAnchor);
498 mLocked.pointerIconChanged = false;
499 mLocked.presentationChanged = false;
500 }
501
502 mSpriteController->closeTransaction();
503}
504
505PointerController::Spot* PointerController::getSpotLocked(uint32_t id) {
506 for (size_t i = 0; i < mLocked.spots.size(); i++) {
507 Spot* spot = mLocked.spots.itemAt(i);
508 if (spot->id == id) {
509 return spot;
510 }
511 }
512 return NULL;
513}
514
515PointerController::Spot* PointerController::createAndAddSpotLocked(uint32_t id) {
516 // Remove spots until we have fewer than MAX_SPOTS remaining.
517 while (mLocked.spots.size() >= MAX_SPOTS) {
518 Spot* spot = removeFirstFadingSpotLocked();
519 if (!spot) {
520 spot = mLocked.spots.itemAt(0);
521 mLocked.spots.removeAt(0);
522 }
523 releaseSpotLocked(spot);
524 }
525
526 // Obtain a sprite from the recycled pool.
527 sp<Sprite> sprite;
528 if (! mLocked.recycledSprites.isEmpty()) {
529 sprite = mLocked.recycledSprites.top();
530 mLocked.recycledSprites.pop();
531 } else {
532 sprite = mSpriteController->createSprite();
533 }
534
535 // Return the new spot.
536 Spot* spot = new Spot(id, sprite);
537 mLocked.spots.push(spot);
538 return spot;
539}
540
541PointerController::Spot* PointerController::removeFirstFadingSpotLocked() {
542 for (size_t i = 0; i < mLocked.spots.size(); i++) {
543 Spot* spot = mLocked.spots.itemAt(i);
544 if (spot->id == Spot::INVALID_ID) {
545 mLocked.spots.removeAt(i);
546 return spot;
547 }
548 }
549 return NULL;
550}
551
552void PointerController::releaseSpotLocked(Spot* spot) {
553 spot->sprite->clearIcon();
554
555 if (mLocked.recycledSprites.size() < MAX_RECYCLED_SPRITES) {
556 mLocked.recycledSprites.push(spot->sprite);
557 }
558
559 delete spot;
560}
561
562void PointerController::fadeOutAndReleaseSpotLocked(Spot* spot) {
563 if (spot->id != Spot::INVALID_ID) {
564 spot->id = Spot::INVALID_ID;
565 startAnimationLocked();
566 }
567}
568
569void PointerController::fadeOutAndReleaseAllSpotsLocked() {
570 for (size_t i = 0; i < mLocked.spots.size(); i++) {
571 Spot* spot = mLocked.spots.itemAt(i);
572 fadeOutAndReleaseSpotLocked(spot);
573 }
574}
575
576void PointerController::loadResources() {
577 mPolicy->loadPointerResources(&mResources);
578}
579
580
581// --- PointerController::Spot ---
582
583void PointerController::Spot::updateSprite(const SpriteIcon* icon, float x, float y) {
584 sprite->setLayer(Sprite::BASE_LAYER_SPOT + id);
585 sprite->setAlpha(alpha);
586 sprite->setTransformationMatrix(SpriteTransformationMatrix(scale, 0.0f, 0.0f, scale));
587 sprite->setPosition(x, y);
588
589 this->x = x;
590 this->y = y;
591
592 if (icon != lastIcon) {
593 lastIcon = icon;
594 if (icon) {
595 sprite->setIcon(*icon);
596 sprite->setVisible(true);
597 } else {
598 sprite->setVisible(false);
599 }
600 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800601}
602
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800603} // namespace android