/src/com/android/services/telephony/EmergencyTonePlayer.java

https://gitlab.com/Atomic-ROM/packages_services_Telephony · Java · 133 lines · 91 code · 19 blank · 23 comment · 10 complexity · 3cc11e0250703c497bd97a0cbbf56b39 MD5 · raw file

  1. /*
  2. * Copyright (C) 2014 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. package com.android.services.telephony;
  17. import android.content.Context;
  18. import android.media.AudioAttributes;
  19. import android.media.AudioManager;
  20. import android.media.ToneGenerator;
  21. import android.os.SystemVibrator;
  22. import android.os.Vibrator;
  23. import android.provider.Settings;
  24. /**
  25. * Plays an emergency tone when placing emergency calls on CDMA devices.
  26. */
  27. class EmergencyTonePlayer {
  28. private static final int EMERGENCY_TONE_OFF = 0;
  29. private static final int EMERGENCY_TONE_ALERT = 1;
  30. private static final int EMERGENCY_TONE_VIBRATE = 2;
  31. private static final int ALERT_RELATIVE_VOLUME_PERCENT = 100;
  32. private static final int VIBRATE_LENGTH_MILLIS = 1000;
  33. private static final int VIBRATE_PAUSE_MILLIS = 1000;
  34. private static final long[] VIBRATE_PATTERN =
  35. new long[] { VIBRATE_LENGTH_MILLIS, VIBRATE_PAUSE_MILLIS};
  36. private static final AudioAttributes VIBRATION_ATTRIBUTES =
  37. new AudioAttributes.Builder()
  38. .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
  39. .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
  40. .build();
  41. // We don't rely on getSystemService(Context.VIBRATOR_SERVICE) to make sure that this vibrator
  42. // object will be isolated from others.
  43. private final Vibrator mVibrator = new SystemVibrator();
  44. private final Context mContext;
  45. private final AudioManager mAudioManager;
  46. private ToneGenerator mToneGenerator;
  47. private int mSavedInCallVolume;
  48. private boolean mIsVibrating = false;
  49. EmergencyTonePlayer(Context context) {
  50. mContext = context;
  51. mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
  52. }
  53. public void start() {
  54. switch (getToneSetting()) {
  55. case EMERGENCY_TONE_VIBRATE:
  56. startVibrate();
  57. break;
  58. case EMERGENCY_TONE_ALERT:
  59. // Only start if we are not in silent mode.
  60. int ringerMode = mAudioManager.getRingerMode();
  61. if (ringerMode == AudioManager.RINGER_MODE_NORMAL) {
  62. startAlert();
  63. }
  64. break;
  65. case EMERGENCY_TONE_OFF:
  66. // nothing;
  67. break;
  68. }
  69. }
  70. public void stop() {
  71. stopVibrate();
  72. stopAlert();
  73. }
  74. private void startVibrate() {
  75. if (!mIsVibrating) {
  76. mVibrator.vibrate(VIBRATE_PATTERN, 0, VIBRATION_ATTRIBUTES);
  77. mIsVibrating = true;
  78. }
  79. }
  80. private void stopVibrate() {
  81. if (mIsVibrating) {
  82. mVibrator.cancel();
  83. mIsVibrating = false;
  84. }
  85. }
  86. private void startAlert() {
  87. if (mToneGenerator == null) {
  88. mToneGenerator = new ToneGenerator(
  89. AudioManager.STREAM_VOICE_CALL, ALERT_RELATIVE_VOLUME_PERCENT);
  90. // Set the volume to max and save the old volume setting.
  91. mSavedInCallVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
  92. mAudioManager.setStreamVolume(
  93. AudioManager.STREAM_VOICE_CALL,
  94. mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL),
  95. 0);
  96. mToneGenerator.startTone(ToneGenerator.TONE_CDMA_EMERGENCY_RINGBACK);
  97. } else {
  98. Log.d(this, "An alert is already running.");
  99. }
  100. }
  101. private void stopAlert() {
  102. if (mToneGenerator != null) {
  103. mToneGenerator.stopTone();
  104. mToneGenerator.release();
  105. mToneGenerator = null;
  106. mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, mSavedInCallVolume, 0);
  107. mSavedInCallVolume = 0;
  108. }
  109. }
  110. private int getToneSetting() {
  111. return Settings.Global.getInt(
  112. mContext.getContentResolver(), Settings.Global.EMERGENCY_TONE, EMERGENCY_TONE_OFF);
  113. }
  114. }