示例#1
0
  /** Change the audio volume amplification according to the fact we are using bluetooth */
  public void setSoftwareVolume() {

    if (service != null) {
      final boolean useBT = (bluetoothWrapper != null && bluetoothWrapper.isBluetoothOn());

      String speaker_key =
          useBT ? SipConfigManager.SND_BT_SPEAKER_LEVEL : SipConfigManager.SND_SPEAKER_LEVEL;
      String mic_key = useBT ? SipConfigManager.SND_BT_MIC_LEVEL : SipConfigManager.SND_MIC_LEVEL;

      final float speakVolume = service.getPrefs().getPreferenceFloatValue(speaker_key);
      final float micVolume =
          userWantMicrophoneMute ? 0 : service.getPrefs().getPreferenceFloatValue(mic_key);

      service
          .getExecutor()
          .execute(
              new SipRunnable() {

                @Override
                protected void doRun() throws SameThreadException {
                  service.confAdjustTxLevel(speakVolume);
                  service.confAdjustRxLevel(micVolume);

                  // Force the BT mode to normal
                  if (useBT) {
                    audioManager.setMode(AudioManager.MODE_NORMAL);
                  }
                }
              });
    }
  }
示例#2
0
 public void resetSettings() {
   userWantBluetooth =
       service.getPrefs().getPreferenceBooleanValue(SipConfigManager.AUTO_CONNECT_BLUETOOTH);
   userWantSpeaker =
       service.getPrefs().getPreferenceBooleanValue(SipConfigManager.AUTO_CONNECT_SPEAKER);
   userWantMicrophoneMute = false;
 }
示例#3
0
 public void startService() {
   if (bluetoothWrapper == null) {
     bluetoothWrapper = BluetoothWrapper.getInstance(service);
     bluetoothWrapper.setBluetoothChangeListener(this);
     bluetoothWrapper.register();
   }
   if (audioFocusWrapper == null) {
     audioFocusWrapper = AudioFocusWrapper.getInstance();
     audioFocusWrapper.init(service, audioManager);
   }
   modeSipInCall = service.getPrefs().getInCallMode();
   useSgsWrkAround =
       service.getPrefs().getPreferenceBooleanValue(SipConfigManager.USE_SGS_CALL_HACK);
   useWebRTCImpl = service.getPrefs().getPreferenceBooleanValue(SipConfigManager.USE_WEBRTC_HACK);
   doFocusAudio = service.getPrefs().getPreferenceBooleanValue(SipConfigManager.DO_FOCUS_AUDIO);
   userWantBluetooth =
       service.getPrefs().getPreferenceBooleanValue(SipConfigManager.AUTO_CONNECT_BLUETOOTH);
   userWantSpeaker =
       service.getPrefs().getPreferenceBooleanValue(SipConfigManager.AUTO_CONNECT_SPEAKER);
   restartAudioWhenRoutingChange =
       service
           .getPrefs()
           .getPreferenceBooleanValue(SipConfigManager.RESTART_AUDIO_ON_ROUTING_CHANGES);
   startBeforeInit =
       service.getPrefs().getPreferenceBooleanValue(SipConfigManager.SETUP_AUDIO_BEFORE_INIT);
 }
示例#4
0
  /** 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();
  }
示例#5
0
 /**
  * Start ringing announce for a given contact. It will also focus audio for us.
  *
  * @param remoteContact the contact to ring for. May resolve the contact ringtone if any.
  */
 public synchronized void startRing(String remoteContact) {
   saveAudioState();
   if (!ringer.isRinging()) {
     ringer.ring(remoteContact, service.getPrefs().getRingtone());
   } else {
     Log.d(THIS_FILE, "Already ringing ....");
   }
 }
示例#6
0
  private int getAudioTargetMode() {
    int targetMode = modeSipInCall;

    if (service.getPrefs().useModeApi()) {
      Log.d(THIS_FILE, "User want speaker now..." + userWantSpeaker);
      if (!service.getPrefs().generateForSetCall()) {
        return userWantSpeaker ? AudioManager.MODE_NORMAL : AudioManager.MODE_IN_CALL;
      } else {
        return userWantSpeaker ? AudioManager.MODE_IN_CALL : AudioManager.MODE_NORMAL;
      }
    }
    if (userWantBluetooth) {
      targetMode = AudioManager.MODE_NORMAL;
    }

    Log.d(THIS_FILE, "Target mode... : " + targetMode);
    return targetMode;
  }
示例#7
0
  public void adjustStreamVolume(int streamType, int direction, int flags) {
    broadcastVolumeWillBeUpdated(streamType, EXTRA_VALUE_UNKNOWN);
    audioManager.adjustStreamVolume(streamType, direction, flags);
    if (streamType == AudioManager.STREAM_RING) {
      // Update ringer
      ringer.updateRingerMode();
    }

    int inCallStream = Compatibility.getInCallStream(userWantBluetooth);
    if (streamType == inCallStream) {
      int maxLevel = audioManager.getStreamMaxVolume(inCallStream);
      float modifiedLevel = (audioManager.getStreamVolume(inCallStream) / (float) maxLevel) * 10.0f;
      // Update default stream level
      service.getPrefs().setPreferenceFloatValue(SipConfigManager.SND_STREAM_LEVEL, modifiedLevel);
    }
  }
示例#8
0
  /** Restore the state of the audio */
  @SuppressWarnings("deprecation")
  private final synchronized void restoreAudioState() {
    if (!prefs.getBoolean("isSavedAudioState", false)) {
      // If we have NEVER set, do not try to reset !
      return;
    }

    ContentResolver ctntResolver = service.getContentResolver();

    Settings.System.putInt(
        ctntResolver,
        Settings.System.WIFI_SLEEP_POLICY,
        prefs.getInt("savedWifiPolicy", Settings.System.WIFI_SLEEP_POLICY_DEFAULT));
    // audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
    // prefs.getInt("savedVibrateRing",
    // AudioManager.VIBRATE_SETTING_ONLY_SILENT));
    // audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
    // prefs.getInt("savedVibradeNotif", AudioManager.VIBRATE_SETTING_OFF));
    // audioManager.setRingerMode(prefs.getInt("savedRingerMode",
    // AudioManager.RINGER_MODE_NORMAL));

    int inCallStream = Compatibility.getInCallStream(userWantBluetooth);
    setStreamVolume(
        inCallStream,
        prefs.getInt("savedVolume", (int) (audioManager.getStreamMaxVolume(inCallStream) * 0.8)),
        0);

    int targetMode = getAudioTargetMode();
    if (service.getPrefs().useRoutingApi()) {
      audioManager.setRouting(
          targetMode,
          prefs.getInt("savedRoute", AudioManager.ROUTE_SPEAKER),
          AudioManager.ROUTE_ALL);
    } else {
      audioManager.setSpeakerphoneOn(prefs.getBoolean("savedSpeakerPhone", false));
    }
    audioManager.setMode(prefs.getInt("savedMode", AudioManager.MODE_NORMAL));

    Editor ed = prefs.edit();
    ed.putBoolean("isSavedAudioState", false);
    ed.commit();
  }
示例#9
0
  /** 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();
  }