| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import android.annotation.CallbackExecutor; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.content.Context; |
| 24 |
import android.hardware.vibrator.IVibrator; |
| 25 |
import android.util.ArrayMap; |
| 26 |
import android.util.Log; |
| 27 |
import android.util.Range; |
| 28 |
import android.util.Slog; |
| 29 |
import android.util.SparseArray; |
| 30 |
import android.util.SparseBooleanArray; |
| 31 |
import android.util.SparseIntArray; |
| 32 |
|
| 33 |
import com.android.internal.annotations.GuardedBy; |
| 34 |
import com.android.internal.annotations.VisibleForTesting; |
| 35 |
|
| 36 |
import java.util.ArrayList; |
| 37 |
import java.util.Arrays; |
| 38 |
import java.util.Objects; |
| 39 |
import java.util.concurrent.Executor; |
| 40 |
import java.util.function.Function; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public class SystemVibrator extends Vibrator { |
| 48 |
private static final String TAG = "Vibrator"; |
| 49 |
|
| 50 |
private final VibratorManager mVibratorManager; |
| 51 |
private final Context mContext; |
| 52 |
|
| 53 |
@GuardedBy("mBrokenListeners") |
| 54 |
private final ArrayList<MultiVibratorStateListener> mBrokenListeners = new ArrayList<>(); |
| 55 |
|
| 56 |
@GuardedBy("mRegisteredListeners") |
| 57 |
private final ArrayMap<OnVibratorStateChangedListener, MultiVibratorStateListener> |
| 58 |
mRegisteredListeners = new ArrayMap<>(); |
| 59 |
|
| 60 |
private final Object mLock = new Object(); |
| 61 |
@GuardedBy("mLock") |
| 62 |
private VibratorInfo mVibratorInfo; |
| 63 |
|
| 64 |
@UnsupportedAppUsage |
| 65 |
public SystemVibrator(Context context) { |
| 66 |
super(context); |
| 67 |
mContext = context; |
| 68 |
mVibratorManager = mContext.getSystemService(VibratorManager.class); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
protected VibratorInfo getInfo() { |
| 73 |
synchronized (mLock) { |
| 74 |
if (mVibratorInfo != null) { |
| 75 |
return mVibratorInfo; |
| 76 |
} |
| 77 |
if (mVibratorManager == null) { |
| 78 |
Log.w(TAG, "Failed to retrieve vibrator info; no vibrator manager."); |
| 79 |
return VibratorInfo.EMPTY_VIBRATOR_INFO; |
| 80 |
} |
| 81 |
int[] vibratorIds = mVibratorManager.getVibratorIds(); |
| 82 |
if (vibratorIds.length == 0) { |
| 83 |
|
| 84 |
|
| 85 |
return mVibratorInfo = new NoVibratorInfo(); |
| 86 |
} |
| 87 |
VibratorInfo[] vibratorInfos = new VibratorInfo[vibratorIds.length]; |
| 88 |
for (int i = 0; i < vibratorIds.length; i++) { |
| 89 |
Vibrator vibrator = mVibratorManager.getVibrator(vibratorIds[i]); |
| 90 |
if (vibrator instanceof NullVibrator) { |
| 91 |
Log.w(TAG, "Vibrator manager service not ready; " |
| 92 |
+ "Info not yet available for vibrator: " + vibratorIds[i]); |
| 93 |
|
| 94 |
|
| 95 |
return VibratorInfo.EMPTY_VIBRATOR_INFO; |
| 96 |
} |
| 97 |
vibratorInfos[i] = vibrator.getInfo(); |
| 98 |
} |
| 99 |
if (vibratorInfos.length == 1) { |
| 100 |
|
| 101 |
return mVibratorInfo = new VibratorInfo( -1, vibratorInfos[0]); |
| 102 |
} |
| 103 |
|
| 104 |
return mVibratorInfo = new MultiVibratorInfo(vibratorInfos); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public boolean hasVibrator() { |
| 110 |
if (mVibratorManager == null) { |
| 111 |
Log.w(TAG, "Failed to check if vibrator exists; no vibrator manager."); |
| 112 |
return false; |
| 113 |
} |
| 114 |
return mVibratorManager.getVibratorIds().length > 0; |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public boolean isVibrating() { |
| 119 |
if (mVibratorManager == null) { |
| 120 |
Log.w(TAG, "Failed to vibrate; no vibrator manager."); |
| 121 |
return false; |
| 122 |
} |
| 123 |
for (int vibratorId : mVibratorManager.getVibratorIds()) { |
| 124 |
if (mVibratorManager.getVibrator(vibratorId).isVibrating()) { |
| 125 |
return true; |
| 126 |
} |
| 127 |
} |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public void addVibratorStateListener(@NonNull OnVibratorStateChangedListener listener) { |
| 133 |
Objects.requireNonNull(listener); |
| 134 |
if (mContext == null) { |
| 135 |
Log.w(TAG, "Failed to add vibrate state listener; no vibrator context."); |
| 136 |
return; |
| 137 |
} |
| 138 |
addVibratorStateListener(mContext.getMainExecutor(), listener); |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public void addVibratorStateListener( |
| 143 |
@NonNull @CallbackExecutor Executor executor, |
| 144 |
@NonNull OnVibratorStateChangedListener listener) { |
| 145 |
Objects.requireNonNull(listener); |
| 146 |
Objects.requireNonNull(executor); |
| 147 |
if (mVibratorManager == null) { |
| 148 |
Log.w(TAG, "Failed to add vibrate state listener; no vibrator manager."); |
| 149 |
return; |
| 150 |
} |
| 151 |
MultiVibratorStateListener delegate = null; |
| 152 |
try { |
| 153 |
synchronized (mRegisteredListeners) { |
| 154 |
|
| 155 |
if (mRegisteredListeners.containsKey(listener)) { |
| 156 |
Log.w(TAG, "Listener already registered."); |
| 157 |
return; |
| 158 |
} |
| 159 |
delegate = new MultiVibratorStateListener(executor, listener); |
| 160 |
delegate.register(mVibratorManager); |
| 161 |
mRegisteredListeners.put(listener, delegate); |
| 162 |
delegate = null; |
| 163 |
} |
| 164 |
} finally { |
| 165 |
if (delegate != null && delegate.hasRegisteredListeners()) { |
| 166 |
|
| 167 |
|
| 168 |
synchronized (mBrokenListeners) { |
| 169 |
mBrokenListeners.add(delegate); |
| 170 |
} |
| 171 |
} |
| 172 |
tryUnregisterBrokenListeners(); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public void removeVibratorStateListener(@NonNull OnVibratorStateChangedListener listener) { |
| 178 |
Objects.requireNonNull(listener); |
| 179 |
if (mVibratorManager == null) { |
| 180 |
Log.w(TAG, "Failed to remove vibrate state listener; no vibrator manager."); |
| 181 |
return; |
| 182 |
} |
| 183 |
synchronized (mRegisteredListeners) { |
| 184 |
if (mRegisteredListeners.containsKey(listener)) { |
| 185 |
MultiVibratorStateListener delegate = mRegisteredListeners.get(listener); |
| 186 |
delegate.unregister(mVibratorManager); |
| 187 |
mRegisteredListeners.remove(listener); |
| 188 |
} |
| 189 |
} |
| 190 |
tryUnregisterBrokenListeners(); |
| 191 |
} |
| 192 |
|
| 193 |
@Override |
| 194 |
public boolean hasAmplitudeControl() { |
| 195 |
return getInfo().hasAmplitudeControl(); |
| 196 |
} |
| 197 |
|
| 198 |
@Override |
| 199 |
public boolean setAlwaysOnEffect(int uid, String opPkg, int alwaysOnId, VibrationEffect effect, |
| 200 |
VibrationAttributes attrs) { |
| 201 |
if (mVibratorManager == null) { |
| 202 |
Log.w(TAG, "Failed to set always-on effect; no vibrator manager."); |
| 203 |
return false; |
| 204 |
} |
| 205 |
CombinedVibration combinedEffect = CombinedVibration.createParallel(effect); |
| 206 |
return mVibratorManager.setAlwaysOnEffect(uid, opPkg, alwaysOnId, combinedEffect, attrs); |
| 207 |
} |
| 208 |
|
| 209 |
@Override |
| 210 |
public void vibrate(int uid, String opPkg, @NonNull VibrationEffect effect, |
| 211 |
String reason, @NonNull VibrationAttributes attributes) { |
| 212 |
if (mVibratorManager == null) { |
| 213 |
Log.w(TAG, "Failed to vibrate; no vibrator manager."); |
| 214 |
return; |
| 215 |
} |
| 216 |
CombinedVibration combinedEffect = CombinedVibration.createParallel(effect); |
| 217 |
mVibratorManager.vibrate(uid, opPkg, combinedEffect, reason, attributes); |
| 218 |
} |
| 219 |
|
| 220 |
@Override |
| 221 |
public void cancel() { |
| 222 |
if (mVibratorManager == null) { |
| 223 |
Log.w(TAG, "Failed to cancel vibrate; no vibrator manager."); |
| 224 |
return; |
| 225 |
} |
| 226 |
mVibratorManager.cancel(); |
| 227 |
} |
| 228 |
|
| 229 |
@Override |
| 230 |
public void cancel(int usageFilter) { |
| 231 |
if (mVibratorManager == null) { |
| 232 |
Log.w(TAG, "Failed to cancel vibrate; no vibrator manager."); |
| 233 |
return; |
| 234 |
} |
| 235 |
mVibratorManager.cancel(usageFilter); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
private void tryUnregisterBrokenListeners() { |
| 249 |
synchronized (mBrokenListeners) { |
| 250 |
try { |
| 251 |
for (int i = mBrokenListeners.size(); --i >= 0; ) { |
| 252 |
mBrokenListeners.get(i).unregister(mVibratorManager); |
| 253 |
mBrokenListeners.remove(i); |
| 254 |
} |
| 255 |
} catch (RuntimeException e) { |
| 256 |
Log.w(TAG, "Failed to unregister broken listener", e); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
private static class SingleVibratorStateListener implements OnVibratorStateChangedListener { |
| 263 |
private final MultiVibratorStateListener mAllVibratorsListener; |
| 264 |
private final int mVibratorIdx; |
| 265 |
|
| 266 |
SingleVibratorStateListener(MultiVibratorStateListener listener, int vibratorIdx) { |
| 267 |
mAllVibratorsListener = listener; |
| 268 |
mVibratorIdx = vibratorIdx; |
| 269 |
} |
| 270 |
|
| 271 |
@Override |
| 272 |
public void onVibratorStateChanged(boolean isVibrating) { |
| 273 |
mAllVibratorsListener.onVibrating(mVibratorIdx, isVibrating); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
@VisibleForTesting |
| 283 |
public static class NoVibratorInfo extends VibratorInfo { |
| 284 |
public NoVibratorInfo() { |
| 285 |
|
| 286 |
super( -1, |
| 287 |
0, |
| 288 |
new SparseBooleanArray(), |
| 289 |
new SparseBooleanArray(), |
| 290 |
new SparseIntArray(), |
| 291 |
0, |
| 292 |
0, |
| 293 |
0, |
| 294 |
0, |
| 295 |
Float.NaN, |
| 296 |
new FrequencyProfile( Float.NaN, |
| 297 |
Float.NaN, |
| 298 |
Float.NaN, |
| 299 |
null)); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
@VisibleForTesting |
| 312 |
public static class MultiVibratorInfo extends VibratorInfo { |
| 313 |
|
| 314 |
private static final float EPSILON = 1e-5f; |
| 315 |
|
| 316 |
public MultiVibratorInfo(VibratorInfo[] vibrators) { |
| 317 |
|
| 318 |
this(vibrators, frequencyProfileIntersection(vibrators)); |
| 319 |
} |
| 320 |
|
| 321 |
private MultiVibratorInfo(VibratorInfo[] vibrators, |
| 322 |
VibratorInfo.FrequencyProfile mergedProfile) { |
| 323 |
super( -1, |
| 324 |
capabilitiesIntersection(vibrators, mergedProfile.isEmpty()), |
| 325 |
supportedEffectsIntersection(vibrators), |
| 326 |
supportedBrakingIntersection(vibrators), |
| 327 |
supportedPrimitivesAndDurationsIntersection(vibrators), |
| 328 |
integerLimitIntersection(vibrators, VibratorInfo::getPrimitiveDelayMax), |
| 329 |
integerLimitIntersection(vibrators, VibratorInfo::getCompositionSizeMax), |
| 330 |
integerLimitIntersection(vibrators, VibratorInfo::getPwlePrimitiveDurationMax), |
| 331 |
integerLimitIntersection(vibrators, VibratorInfo::getPwleSizeMax), |
| 332 |
floatPropertyIntersection(vibrators, VibratorInfo::getQFactor), |
| 333 |
mergedProfile); |
| 334 |
} |
| 335 |
|
| 336 |
private static int capabilitiesIntersection(VibratorInfo[] infos, |
| 337 |
boolean frequencyProfileIsEmpty) { |
| 338 |
int intersection = ~0; |
| 339 |
for (VibratorInfo info : infos) { |
| 340 |
intersection &= info.getCapabilities(); |
| 341 |
} |
| 342 |
if (frequencyProfileIsEmpty) { |
| 343 |
|
| 344 |
intersection &= ~IVibrator.CAP_FREQUENCY_CONTROL; |
| 345 |
} |
| 346 |
return intersection; |
| 347 |
} |
| 348 |
|
| 349 |
@Nullable |
| 350 |
private static SparseBooleanArray supportedBrakingIntersection(VibratorInfo[] infos) { |
| 351 |
for (VibratorInfo info : infos) { |
| 352 |
if (!info.isBrakingSupportKnown()) { |
| 353 |
|
| 354 |
return null; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
SparseBooleanArray intersection = new SparseBooleanArray(); |
| 359 |
SparseBooleanArray firstVibratorBraking = infos[0].getSupportedBraking(); |
| 360 |
|
| 361 |
brakingIdLoop: |
| 362 |
for (int i = 0; i < firstVibratorBraking.size(); i++) { |
| 363 |
int brakingId = firstVibratorBraking.keyAt(i); |
| 364 |
if (!firstVibratorBraking.valueAt(i)) { |
| 365 |
|
| 366 |
continue brakingIdLoop; |
| 367 |
} |
| 368 |
|
| 369 |
for (int j = 1; j < infos.length; j++) { |
| 370 |
if (!infos[j].hasBrakingSupport(brakingId)) { |
| 371 |
|
| 372 |
continue brakingIdLoop; |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
intersection.put(brakingId, true); |
| 377 |
} |
| 378 |
|
| 379 |
return intersection; |
| 380 |
} |
| 381 |
|
| 382 |
@Nullable |
| 383 |
private static SparseBooleanArray supportedEffectsIntersection(VibratorInfo[] infos) { |
| 384 |
for (VibratorInfo info : infos) { |
| 385 |
if (!info.isEffectSupportKnown()) { |
| 386 |
|
| 387 |
return null; |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
SparseBooleanArray intersection = new SparseBooleanArray(); |
| 392 |
SparseBooleanArray firstVibratorEffects = infos[0].getSupportedEffects(); |
| 393 |
|
| 394 |
effectIdLoop: |
| 395 |
for (int i = 0; i < firstVibratorEffects.size(); i++) { |
| 396 |
int effectId = firstVibratorEffects.keyAt(i); |
| 397 |
if (!firstVibratorEffects.valueAt(i)) { |
| 398 |
|
| 399 |
continue effectIdLoop; |
| 400 |
} |
| 401 |
|
| 402 |
for (int j = 1; j < infos.length; j++) { |
| 403 |
if (infos[j].isEffectSupported(effectId) != VIBRATION_EFFECT_SUPPORT_YES) { |
| 404 |
|
| 405 |
continue effectIdLoop; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
intersection.put(effectId, true); |
| 410 |
} |
| 411 |
|
| 412 |
return intersection; |
| 413 |
} |
| 414 |
|
| 415 |
@NonNull |
| 416 |
private static SparseIntArray supportedPrimitivesAndDurationsIntersection( |
| 417 |
VibratorInfo[] infos) { |
| 418 |
SparseIntArray intersection = new SparseIntArray(); |
| 419 |
SparseIntArray firstVibratorPrimitives = infos[0].getSupportedPrimitives(); |
| 420 |
|
| 421 |
primitiveIdLoop: |
| 422 |
for (int i = 0; i < firstVibratorPrimitives.size(); i++) { |
| 423 |
int primitiveId = firstVibratorPrimitives.keyAt(i); |
| 424 |
int primitiveDuration = firstVibratorPrimitives.valueAt(i); |
| 425 |
if (primitiveDuration == 0) { |
| 426 |
|
| 427 |
continue primitiveIdLoop; |
| 428 |
} |
| 429 |
|
| 430 |
for (int j = 1; j < infos.length; j++) { |
| 431 |
int vibratorPrimitiveDuration = infos[j].getPrimitiveDuration(primitiveId); |
| 432 |
if (vibratorPrimitiveDuration == 0) { |
| 433 |
|
| 434 |
continue primitiveIdLoop; |
| 435 |
} else { |
| 436 |
|
| 437 |
primitiveDuration = Math.max(primitiveDuration, vibratorPrimitiveDuration); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
intersection.put(primitiveId, primitiveDuration); |
| 442 |
} |
| 443 |
return intersection; |
| 444 |
} |
| 445 |
|
| 446 |
private static int integerLimitIntersection(VibratorInfo[] infos, |
| 447 |
Function<VibratorInfo, Integer> propertyGetter) { |
| 448 |
int limit = 0; |
| 449 |
for (VibratorInfo info : infos) { |
| 450 |
int vibratorLimit = propertyGetter.apply(info); |
| 451 |
if ((limit == 0) || (vibratorLimit > 0 && vibratorLimit < limit)) { |
| 452 |
|
| 453 |
|
| 454 |
limit = vibratorLimit; |
| 455 |
} |
| 456 |
} |
| 457 |
return limit; |
| 458 |
} |
| 459 |
|
| 460 |
private static float floatPropertyIntersection(VibratorInfo[] infos, |
| 461 |
Function<VibratorInfo, Float> propertyGetter) { |
| 462 |
float property = propertyGetter.apply(infos[0]); |
| 463 |
if (Float.isNaN(property)) { |
| 464 |
|
| 465 |
return Float.NaN; |
| 466 |
} |
| 467 |
for (int i = 1; i < infos.length; i++) { |
| 468 |
if (Float.compare(property, propertyGetter.apply(infos[i])) != 0) { |
| 469 |
|
| 470 |
return Float.NaN; |
| 471 |
} |
| 472 |
} |
| 473 |
return property; |
| 474 |
} |
| 475 |
|
| 476 |
@NonNull |
| 477 |
private static FrequencyProfile frequencyProfileIntersection(VibratorInfo[] infos) { |
| 478 |
float freqResolution = floatPropertyIntersection(infos, |
| 479 |
info -> info.getFrequencyProfile().getFrequencyResolutionHz()); |
| 480 |
float resonantFreq = floatPropertyIntersection(infos, |
| 481 |
VibratorInfo::getResonantFrequencyHz); |
| 482 |
Range<Float> freqRange = frequencyRangeIntersection(infos, freqResolution); |
| 483 |
|
| 484 |
if ((freqRange == null) || Float.isNaN(freqResolution)) { |
| 485 |
return new FrequencyProfile(resonantFreq, Float.NaN, freqResolution, null); |
| 486 |
} |
| 487 |
|
| 488 |
int amplitudeCount = |
| 489 |
Math.round(1 + (freqRange.getUpper() - freqRange.getLower()) / freqResolution); |
| 490 |
float[] maxAmplitudes = new float[amplitudeCount]; |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
Arrays.fill(maxAmplitudes, Float.MAX_VALUE); |
| 496 |
|
| 497 |
for (VibratorInfo info : infos) { |
| 498 |
Range<Float> vibratorFreqRange = info.getFrequencyProfile().getFrequencyRangeHz(); |
| 499 |
float[] vibratorMaxAmplitudes = info.getFrequencyProfile().getMaxAmplitudes(); |
| 500 |
int vibratorStartIdx = Math.round( |
| 501 |
(freqRange.getLower() - vibratorFreqRange.getLower()) / freqResolution); |
| 502 |
int vibratorEndIdx = vibratorStartIdx + maxAmplitudes.length - 1; |
| 503 |
|
| 504 |
if ((vibratorStartIdx < 0) || (vibratorEndIdx >= vibratorMaxAmplitudes.length)) { |
| 505 |
Slog.w(TAG, "Error calculating the intersection of vibrator frequency" |
| 506 |
+ " profiles: attempted to fetch from vibrator " |
| 507 |
+ info.getId() + " max amplitude with bad index " + vibratorStartIdx); |
| 508 |
return new FrequencyProfile(resonantFreq, Float.NaN, Float.NaN, null); |
| 509 |
} |
| 510 |
|
| 511 |
for (int i = 0; i < maxAmplitudes.length; i++) { |
| 512 |
maxAmplitudes[i] = Math.min(maxAmplitudes[i], |
| 513 |
vibratorMaxAmplitudes[vibratorStartIdx + i]); |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
return new FrequencyProfile(resonantFreq, freqRange.getLower(), |
| 518 |
freqResolution, maxAmplitudes); |
| 519 |
} |
| 520 |
|
| 521 |
@Nullable |
| 522 |
private static Range<Float> frequencyRangeIntersection(VibratorInfo[] infos, |
| 523 |
float frequencyResolution) { |
| 524 |
Range<Float> firstRange = infos[0].getFrequencyProfile().getFrequencyRangeHz(); |
| 525 |
if (firstRange == null) { |
| 526 |
|
| 527 |
return null; |
| 528 |
} |
| 529 |
float intersectionLower = firstRange.getLower(); |
| 530 |
float intersectionUpper = firstRange.getUpper(); |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
for (int i = 1; i < infos.length; i++) { |
| 536 |
Range<Float> vibratorRange = infos[i].getFrequencyProfile().getFrequencyRangeHz(); |
| 537 |
if (vibratorRange == null) { |
| 538 |
|
| 539 |
return null; |
| 540 |
} |
| 541 |
|
| 542 |
if ((vibratorRange.getLower() >= intersectionUpper) |
| 543 |
|| (vibratorRange.getUpper() <= intersectionLower)) { |
| 544 |
|
| 545 |
return null; |
| 546 |
} |
| 547 |
|
| 548 |
float frequencyDelta = Math.abs(intersectionLower - vibratorRange.getLower()); |
| 549 |
if ((frequencyDelta % frequencyResolution) > EPSILON) { |
| 550 |
|
| 551 |
return null; |
| 552 |
} |
| 553 |
|
| 554 |
intersectionLower = Math.max(intersectionLower, vibratorRange.getLower()); |
| 555 |
intersectionUpper = Math.min(intersectionUpper, vibratorRange.getUpper()); |
| 556 |
} |
| 557 |
|
| 558 |
if ((intersectionUpper - intersectionLower) < frequencyResolution) { |
| 559 |
|
| 560 |
return null; |
| 561 |
} |
| 562 |
|
| 563 |
return Range.create(intersectionLower, intersectionUpper); |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
@VisibleForTesting |
| 576 |
public static class MultiVibratorStateListener { |
| 577 |
private final Object mLock = new Object(); |
| 578 |
private final Executor mExecutor; |
| 579 |
private final OnVibratorStateChangedListener mDelegate; |
| 580 |
|
| 581 |
@GuardedBy("mLock") |
| 582 |
private final SparseArray<SingleVibratorStateListener> mVibratorListeners = |
| 583 |
new SparseArray<>(); |
| 584 |
|
| 585 |
@GuardedBy("mLock") |
| 586 |
private int mInitializedMask; |
| 587 |
@GuardedBy("mLock") |
| 588 |
private int mVibratingMask; |
| 589 |
|
| 590 |
public MultiVibratorStateListener(@NonNull Executor executor, |
| 591 |
@NonNull OnVibratorStateChangedListener listener) { |
| 592 |
mExecutor = executor; |
| 593 |
mDelegate = listener; |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
public boolean hasRegisteredListeners() { |
| 598 |
synchronized (mLock) { |
| 599 |
return mVibratorListeners.size() > 0; |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
public void register(VibratorManager vibratorManager) { |
| 605 |
int[] vibratorIds = vibratorManager.getVibratorIds(); |
| 606 |
synchronized (mLock) { |
| 607 |
for (int i = 0; i < vibratorIds.length; i++) { |
| 608 |
int vibratorId = vibratorIds[i]; |
| 609 |
SingleVibratorStateListener listener = new SingleVibratorStateListener(this, i); |
| 610 |
try { |
| 611 |
vibratorManager.getVibrator(vibratorId).addVibratorStateListener(mExecutor, |
| 612 |
listener); |
| 613 |
mVibratorListeners.put(vibratorId, listener); |
| 614 |
} catch (RuntimeException e) { |
| 615 |
try { |
| 616 |
unregister(vibratorManager); |
| 617 |
} catch (RuntimeException e1) { |
| 618 |
Log.w(TAG, |
| 619 |
"Failed to unregister listener while recovering from a failed " |
| 620 |
+ "register call", e1); |
| 621 |
} |
| 622 |
throw e; |
| 623 |
} |
| 624 |
} |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
public void unregister(VibratorManager vibratorManager) { |
| 630 |
synchronized (mLock) { |
| 631 |
for (int i = mVibratorListeners.size(); --i >= 0; ) { |
| 632 |
int vibratorId = mVibratorListeners.keyAt(i); |
| 633 |
SingleVibratorStateListener listener = mVibratorListeners.valueAt(i); |
| 634 |
vibratorManager.getVibrator(vibratorId).removeVibratorStateListener(listener); |
| 635 |
mVibratorListeners.removeAt(i); |
| 636 |
} |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
public void onVibrating(int vibratorIdx, boolean vibrating) { |
| 642 |
mExecutor.execute(() -> { |
| 643 |
boolean shouldNotifyStateChange; |
| 644 |
boolean isAnyVibrating; |
| 645 |
synchronized (mLock) { |
| 646 |
|
| 647 |
int allInitializedMask = (1 << mVibratorListeners.size()) - 1; |
| 648 |
|
| 649 |
|
| 650 |
boolean previousIsAnyVibrating = (mVibratingMask != 0); |
| 651 |
boolean previousAreAllInitialized = (mInitializedMask == allInitializedMask); |
| 652 |
|
| 653 |
|
| 654 |
int vibratorMask = (1 << vibratorIdx); |
| 655 |
mInitializedMask |= vibratorMask; |
| 656 |
|
| 657 |
|
| 658 |
boolean previousVibrating = (mVibratingMask & vibratorMask) != 0; |
| 659 |
if (previousVibrating != vibrating) { |
| 660 |
mVibratingMask ^= vibratorMask; |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
isAnyVibrating = (mVibratingMask != 0); |
| 665 |
boolean areAllInitialized = (mInitializedMask == allInitializedMask); |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
boolean isStateChanging = (previousIsAnyVibrating != isAnyVibrating); |
| 671 |
shouldNotifyStateChange = |
| 672 |
areAllInitialized && (!previousAreAllInitialized || isStateChanging); |
| 673 |
} |
| 674 |
|
| 675 |
if (shouldNotifyStateChange) { |
| 676 |
mDelegate.onVibratorStateChanged(isAnyVibrating); |
| 677 |
} |
| 678 |
}); |
| 679 |
} |
| 680 |
} |
| 681 |
} |