public void init() { Log.d(TAG, "init"); if (initialized) { return; } // Store current audio state so we can restore it when close() is called. savedAudioMode = audioManager.getMode(); savedIsSpeakerPhoneOn = audioManager.isSpeakerphoneOn(); savedIsMicrophoneMute = audioManager.isMicrophoneMute(); // Request audio focus before making any device switch. audioManager.requestAudioFocus( null, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); // Start by setting MODE_IN_COMMUNICATION as default audio mode. It is // required to be in this mode when playout and/or recording starts for // best possible VoIP performance. // TODO(henrika): we migh want to start with RINGTONE mode here instead. audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); // Always disable microphone mute during a WebRTC call. setMicrophoneMute(false); // Do initial selection of audio device. This setting can later be changed // either by adding/removing a wired headset or by covering/uncovering the // proximity sensor. updateAudioDeviceState(hasWiredHeadset()); // Register receiver for broadcast intents related to adding/removing a // wired headset (Intent.ACTION_HEADSET_PLUG). registerForWiredHeadsetIntentBroadcast(); initialized = true; }
public void init() { Log.d(TAG, "init"); if (initialized) { return; } // Store current audio state so we can restore it when close() is called. savedAudioMode = audioManager.getMode(); savedIsSpeakerPhoneOn = audioManager.isSpeakerphoneOn(); savedIsMicrophoneMute = audioManager.isMicrophoneMute(); // Request audio focus before making any device switch. audioManager.requestAudioFocus( null, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); // Start by setting RINGTONE as default audio mode. The native WebRTC // audio layer will switch to COMMUNICATION mode when the first streaming // session starts and return to RINGTONE mode when all streaming stops. audioManager.setMode(AudioManager.MODE_RINGTONE); // Always disable microphone mute during a WebRTC call. setMicrophoneMute(false); // Do initial selection of audio device. This setting can later be changed // either by adding/removing a wired headset or by covering/uncovering the // proximity sensor. updateAudioDeviceState(hasWiredHeadset()); // Register receiver for broadcast intents related to adding/removing a // wired headset (Intent.ACTION_HEADSET_PLUG). registerForWiredHeadsetIntentBroadcast(); initialized = true; }
/** Save current audio mode in order to be able to restore it once done */ @SuppressWarnings("deprecation") private synchronized void saveAudioState() { if (prefs.getBoolean("isSavedAudioState", false)) { // If we have already set, do not set it again !!! return; } ContentResolver ctntResolver = service.getContentResolver(); Editor ed = prefs.edit(); // ed.putInt("savedVibrateRing", // audioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER)); // ed.putInt("savedVibradeNotif", // audioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION)); // ed.putInt("savedRingerMode", audioManager.getRingerMode()); ed.putInt( "savedWifiPolicy", android.provider.Settings.System.getInt( ctntResolver, android.provider.Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_DEFAULT)); int inCallStream = Compatibility.getInCallStream(userWantBluetooth); ed.putInt("savedVolume", audioManager.getStreamVolume(inCallStream)); int targetMode = getAudioTargetMode(); if (service.getPrefs().useRoutingApi()) { ed.putInt("savedRoute", audioManager.getRouting(targetMode)); } else { ed.putBoolean("savedSpeakerPhone", audioManager.isSpeakerphoneOn()); } ed.putInt("savedMode", audioManager.getMode()); ed.putBoolean("isSavedAudioState", true); ed.commit(); }
public ec(Context context) { boolean z = true; AudioManager audioManager = (AudioManager) context.getSystemService("audio"); ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity"); DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); Locale locale = Locale.getDefault(); PackageManager packageManager = context.getPackageManager(); TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService("phone"); Intent registerReceiver = context.registerReceiver(null, new IntentFilter("android.intent.action.BATTERY_CHANGED")); this.rb = audioManager.getMode(); this.rc = m947a(packageManager, "geo:0,0?q=donuts") != null; this.rd = m947a(packageManager, "http://www.google.com") != null; this.re = telephonyManager.getNetworkOperator(); this.rf = locale.getCountry(); this.rg = et.bV(); this.rh = audioManager.isMusicActive(); this.ri = audioManager.isSpeakerphoneOn(); this.rj = locale.getLanguage(); this.rk = m948a(packageManager); this.rl = audioManager.getStreamVolume(3); this.rm = m946a(context, connectivityManager, packageManager); this.rn = telephonyManager.getNetworkType(); this.ro = telephonyManager.getPhoneType(); this.rp = audioManager.getRingerMode(); this.rq = audioManager.getStreamVolume(2); this.rr = displayMetrics.density; this.rs = displayMetrics.widthPixels; this.rt = displayMetrics.heightPixels; if (registerReceiver != null) { int intExtra = registerReceiver.getIntExtra(NotificationCompatApi21.CATEGORY_STATUS, -1); this.ru = (double) (((float) registerReceiver.getIntExtra("level", -1)) / ((float) registerReceiver.getIntExtra("scale", -1))); if (!(intExtra == 2 || intExtra == 5)) { z = false; } this.rv = z; } else { this.ru = -1.0d; this.rv = false; } if (VERSION.SDK_INT >= 16) { this.rw = connectivityManager.isActiveNetworkMetered(); if (connectivityManager.getActiveNetworkInfo() != null) { this.rx = connectivityManager.getActiveNetworkInfo().getDetailedState().ordinal(); return; } else { this.rx = -1; return; } } this.rw = false; this.rx = -1; }
@TestTargets({ @TestTargetNew( level = TestLevel.COMPLETE, method = "setMode", args = {int.class}), @TestTargetNew( level = TestLevel.COMPLETE, method = "getMode", args = {}) }) public void testAccessMode() throws Exception { mAudioManager.setMode(MODE_RINGTONE); assertEquals(MODE_RINGTONE, mAudioManager.getMode()); mAudioManager.setMode(MODE_IN_CALL); assertEquals(MODE_IN_CALL, mAudioManager.getMode()); mAudioManager.setMode(MODE_NORMAL); assertEquals(MODE_NORMAL, mAudioManager.getMode()); }
public static a g(Context context) { if (f == null) { f = (AudioManager) context.getSystemService("audio"); } a aVar = a.f; int mode = f.getMode(); if (c()) { return a.e; } if (f.isMusicActive() || f.isSpeakerphoneOn() || mode == 2 || mode == 1) { return a.d; } mode = f.getRingerMode(); return (mode == 0 || mode == 1) ? a.d : a.b; }
private void setAudioFocus(boolean focus) { if (focus) { previousAudioMode = audioManager.getMode(); // Request audio focus before making any device switch. audioManager.requestAudioFocus( null, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); /* * Use MODE_IN_COMMUNICATION as the default audio mode. It is required * to be in this mode when playout and/or recording starts for the best * possible VoIP performance. Some devices have difficulties with * speaker mode if this is not set. */ audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); } else { audioManager.setMode(previousAudioMode); audioManager.abandonAudioFocus(null); } }
private void toggleAudioMute() { if (mAudioManager == null) { mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); } // We only act in normal mode (rings, calls, ... are handled by aosp) if (mAudioManager.getMode() == AudioManager.MODE_NORMAL) { // TODO: If an alarm is sound then don't toggle the volume mute. In this case, // is better to ignore the key event and let the alarm app to handle it. // Just toggle between normal and silent (by now we are not going to handle // vibration here) int newValue = mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL ? AudioManager.RINGER_MODE_NORMAL : AudioManager.RINGER_MODE_SILENT; mAudioManager.setRingerMode(newValue); int status = newValue == AudioManager.RINGER_MODE_NORMAL ? ASUSDEC_STATUS_OFF : ASUSDEC_STATUS_ON; notifyKey(ASUSDEC_VOLUME_MUTE, status); } }
public static int getMode() { AudioManager am = (AudioManager) Receiver.mContext.getSystemService(Context.AUDIO_SERVICE); if (Integer.parseInt(Build.VERSION.SDK) >= 5) return am.isSpeakerphoneOn() ? AudioManager.MODE_NORMAL : AudioManager.MODE_IN_CALL; else return am.getMode(); }
public ec(Context context) { boolean flag1 = true; super(); AudioManager audiomanager = (AudioManager)context.getSystemService("audio"); ConnectivityManager connectivitymanager = (ConnectivityManager)context.getSystemService("connectivity"); DisplayMetrics displaymetrics = context.getResources().getDisplayMetrics(); Locale locale = Locale.getDefault(); PackageManager packagemanager = context.getPackageManager(); TelephonyManager telephonymanager = (TelephonyManager)context.getSystemService("phone"); Intent intent = context.registerReceiver(null, new IntentFilter("android.intent.action.BATTERY_CHANGED")); rb = audiomanager.getMode(); boolean flag; if (a(packagemanager, "geo:0,0?q=donuts") != null) { flag = true; } else { flag = false; } rc = flag; if (a(packagemanager, "http://www.google.com") != null) { flag = true; } else { flag = false; } rd = flag; re = telephonymanager.getNetworkOperator(); rf = locale.getCountry(); rh = audiomanager.isMusicActive(); ri = audiomanager.isSpeakerphoneOn(); rj = locale.getLanguage(); rk = a(packagemanager); rl = audiomanager.getStreamVolume(3); rm = a(context, connectivitymanager, packagemanager); rn = telephonymanager.getNetworkType(); ro = telephonymanager.getPhoneType(); rp = audiomanager.getRingerMode(); rq = audiomanager.getStreamVolume(2); rr = displaymetrics.density; rs = displaymetrics.widthPixels; rt = displaymetrics.heightPixels; if (intent != null) { int i = intent.getIntExtra("status", -1); int j = intent.getIntExtra("level", -1); int k = intent.getIntExtra("scale", -1); ru = (float)j / (float)k; flag = flag1; if (i != 2) { if (i == 5) { flag = flag1; } else { flag = false; } } rv = flag; } else { ru = -1D; rv = false; } if (android.os.Build.VERSION.SDK_INT >= 16) { rw = connectivitymanager.isActiveNetworkMetered(); if (connectivitymanager.getActiveNetworkInfo() != null) { rx = connectivitymanager.getActiveNetworkInfo().getDetailedState().ordinal(); return; } else { rx = -1; return; } } else { rw = false; rx = -1; return; } }
/** Set the audio mode as in call */ @SuppressWarnings("deprecation") private synchronized void actualSetAudioInCall() { // Ensure not already set if (isSetAudioMode) { return; } stopRing(); saveAudioState(); // Set the rest of the phone in a better state to not interferate with // current call // Do that only if we were not already in silent mode /* * Not needed anymore with on flight gsm call capture * if(audioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT) { * audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, * AudioManager.VIBRATE_SETTING_ON); * audioManager.setVibrateSetting(AudioManager * .VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF); * audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE); } */ // LOCKS // Wifi management if necessary ContentResolver ctntResolver = service.getContentResolver(); Settings.System.putInt( ctntResolver, Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_NEVER); // Acquire wifi lock WifiManager wman = (WifiManager) service.getSystemService(Context.WIFI_SERVICE); if (wifiLock == null) { wifiLock = wman.createWifiLock( (Compatibility.isCompatible(9)) ? WifiManager.WIFI_MODE_FULL_HIGH_PERF : WifiManager.WIFI_MODE_FULL, "com.csipsimple.InCallLock"); wifiLock.setReferenceCounted(false); } WifiInfo winfo = wman.getConnectionInfo(); if (winfo != null) { DetailedState dstate = WifiInfo.getDetailedStateOf(winfo.getSupplicantState()); // We assume that if obtaining ip addr, we are almost connected so // can keep wifi lock if (dstate == DetailedState.OBTAINING_IPADDR || dstate == DetailedState.CONNECTED) { if (!wifiLock.isHeld()) { wifiLock.acquire(); } } // This wake lock purpose is to prevent PSP wifi mode if (service.getPrefs().getPreferenceBooleanValue(SipConfigManager.KEEP_AWAKE_IN_CALL)) { if (screenLock == null) { PowerManager pm = (PowerManager) service.getSystemService(Context.POWER_SERVICE); screenLock = pm.newWakeLock( PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "com.csipsimple.onIncomingCall.SCREEN"); screenLock.setReferenceCounted(false); } // Ensure single lock if (!screenLock.isHeld()) { screenLock.acquire(); } } } if (!useWebRTCImpl) { // Audio routing int targetMode = getAudioTargetMode(); Log.d(THIS_FILE, "Set mode audio in call to " + targetMode); if (service.getPrefs().generateForSetCall()) { boolean needOutOfSilent = (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT); if (needOutOfSilent) { audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); } ToneGenerator toneGenerator = new ToneGenerator(AudioManager.STREAM_VOICE_CALL, 1); toneGenerator.startTone(41 /* ToneGenerator.TONE_CDMA_CONFIRM */); toneGenerator.stopTone(); toneGenerator.release(); // Restore silent mode if (needOutOfSilent) { audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); } } // Set mode if (targetMode != AudioManager.MODE_IN_CALL && useSgsWrkAround) { // For galaxy S we need to set in call mode before to reset // stack audioManager.setMode(AudioManager.MODE_IN_CALL); } audioManager.setMode(targetMode); // Routing if (service.getPrefs().useRoutingApi()) { audioManager.setRouting( targetMode, userWantSpeaker ? AudioManager.ROUTE_SPEAKER : AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL); } else { audioManager.setSpeakerphoneOn(userWantSpeaker ? true : false); } audioManager.setMicrophoneMute(false); if (bluetoothWrapper != null && userWantBluetooth && bluetoothWrapper.canBluetooth()) { Log.d(THIS_FILE, "Try to enable bluetooth"); bluetoothWrapper.setBluetoothOn(true); } } else { // WebRTC implementation for routing int apiLevel = Compatibility.getApiLevel(); // SetAudioMode // ***IMPORTANT*** When the API level for honeycomb (H) has been // decided, // the condition should be changed to include API level 8 to H-1. if (android.os.Build.BRAND.equalsIgnoreCase("Samsung") && (8 == apiLevel)) { // Set Samsung specific VoIP mode for 2.2 devices int mode = 4; audioManager.setMode(mode); if (audioManager.getMode() != mode) { Log.e(THIS_FILE, "Could not set audio mode for Samsung device"); } } // SetPlayoutSpeaker if ((3 == apiLevel) || (4 == apiLevel)) { // 1.5 and 1.6 devices if (userWantSpeaker) { // route audio to back speaker audioManager.setMode(AudioManager.MODE_NORMAL); } else { // route audio to earpiece audioManager.setMode(AudioManager.MODE_IN_CALL); } } else { // 2.x devices if ((android.os.Build.BRAND.equalsIgnoreCase("samsung")) && ((5 == apiLevel) || (6 == apiLevel) || (7 == apiLevel))) { // Samsung 2.0, 2.0.1 and 2.1 devices if (userWantSpeaker) { // route audio to back speaker audioManager.setMode(AudioManager.MODE_IN_CALL); audioManager.setSpeakerphoneOn(userWantSpeaker); } else { // route audio to earpiece audioManager.setSpeakerphoneOn(userWantSpeaker); audioManager.setMode(AudioManager.MODE_NORMAL); } } else { // Non-Samsung and Samsung 2.2 and up devices audioManager.setSpeakerphoneOn(userWantSpeaker); } } } // Set stream solo/volume/focus int inCallStream = Compatibility.getInCallStream(userWantBluetooth); if (doFocusAudio) { if (!accessibilityManager.isEnabled()) { audioManager.setStreamSolo(inCallStream, true); } audioFocusWrapper.focus(userWantBluetooth); } Log.d(THIS_FILE, "Initial volume level : " + service.getPrefs().getInitialVolumeLevel()); setStreamVolume( inCallStream, (int) (audioManager.getStreamMaxVolume(inCallStream) * service.getPrefs().getInitialVolumeLevel()), 0); isSetAudioMode = true; // System.gc(); }
/** * Shows an Astrid notification. Pulls in ring tone and quiet hour settings from preferences. You * can make it say anything you like. * * @param ringTimes number of times to ring (-1 = nonstop) */ public static void showNotification( int notificationId, Intent intent, int type, String title, String text, int ringTimes) { Context context = ContextManager.getContext(); if (notificationManager == null) notificationManager = new AndroidNotificationManager(context); // quiet hours? unless alarm clock boolean quietHours = false; int quietHoursStart = Preferences.getIntegerFromString(R.string.p_rmd_quietStart, -1); int quietHoursEnd = Preferences.getIntegerFromString(R.string.p_rmd_quietEnd, -1); if (quietHoursStart != -1 && quietHoursEnd != -1 && ringTimes >= 0) { int hour = new Date().getHours(); if (quietHoursStart <= quietHoursEnd) { if (hour >= quietHoursStart && hour < quietHoursEnd) quietHours = true; } else { // wrap across 24/hour boundary if (hour >= quietHoursStart || hour < quietHoursEnd) quietHours = true; } } PendingIntent pendingIntent = PendingIntent.getActivity( context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT); // set up properties (name and icon) for the notification int icon; switch (Preferences.getIntegerFromString(R.string.p_rmd_icon, ICON_SET_ASTRID)) { case ICON_SET_PINK: icon = R.drawable.notif_pink_alarm; break; case ICON_SET_BORING: icon = R.drawable.notif_boring_alarm; break; default: icon = R.drawable.notif_astrid; } // create notification object Notification notification = new Notification(icon, text, System.currentTimeMillis()); notification.setLatestEventInfo(context, title, text, pendingIntent); notification.flags |= Notification.FLAG_AUTO_CANCEL; if (Preferences.getBoolean(R.string.p_rmd_persistent, true)) { notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_SHOW_LIGHTS; notification.ledOffMS = 5000; notification.ledOnMS = 700; notification.ledARGB = Color.YELLOW; } else notification.defaults = Notification.DEFAULT_LIGHTS; AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // detect call state TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); int callState = tm.getCallState(); boolean voiceReminder = Preferences.getBoolean(R.string.p_voiceRemindersEnabled, false); // if multi-ring is activated, set up the flags for insistent // notification, and increase the volume to full volume, so the user // will actually pay attention to the alarm if (ringTimes != 1 && (type != ReminderService.TYPE_RANDOM)) { notification.audioStreamType = AudioManager.STREAM_ALARM; audioManager.setStreamVolume( AudioManager.STREAM_ALARM, audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), 0); // insistent rings until notification is disabled if (ringTimes < 0) { notification.flags |= Notification.FLAG_INSISTENT; voiceReminder = false; } } else { notification.audioStreamType = AudioManager.STREAM_NOTIFICATION; } // quiet hours = no sound if (quietHours || callState != TelephonyManager.CALL_STATE_IDLE) { notification.sound = null; voiceReminder = false; } else { String notificationPreference = Preferences.getStringValue(R.string.p_rmd_ringtone); if (audioManager.getStreamVolume(AudioManager.STREAM_RING) == 0) { notification.sound = null; voiceReminder = false; } else if (notificationPreference != null) { if (notificationPreference.length() > 0) { Uri notificationSound = Uri.parse(notificationPreference); notification.sound = notificationSound; } else { notification.sound = null; } } else { notification.defaults |= Notification.DEFAULT_SOUND; } } // quiet hours && ! due date or snooze = no vibrate if (quietHours && !(type == ReminderService.TYPE_DUE || type == ReminderService.TYPE_SNOOZE)) { notification.vibrate = null; } else if (callState != TelephonyManager.CALL_STATE_IDLE) { notification.vibrate = null; } else { if (Preferences.getBoolean(R.string.p_rmd_vibrate, true) && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) { notification.vibrate = new long[] {0, 1000, 500, 1000, 500, 1000}; } else { notification.vibrate = null; } } if (Constants.DEBUG) Log.w("Astrid", "Logging notification: " + text); // $NON-NLS-1$ //$NON-NLS-2$ for (int i = 0; i < Math.max(ringTimes, 1); i++) { notificationManager.notify(notificationId, notification); AndroidUtilities.sleepDeep(500); } if (voiceReminder) { AndroidUtilities.sleepDeep(2000); for (int i = 0; i < 50; i++) { AndroidUtilities.sleepDeep(500); if (audioManager.getMode() != AudioManager.MODE_RINGTONE) break; } try { VoiceOutputService.getVoiceOutputInstance().queueSpeak(text); } catch (VerifyError e) { // unavailable } } }