示例#1
0
  void updateConnectionState() {
    final int oldState = serviceState;

    switch (state) {
      case MumbleConnectionHost.STATE_CONNECTING:
        serviceState = CONNECTION_STATE_CONNECTING;
        break;
      case MumbleConnectionHost.STATE_CONNECTED:
        settings.addObserver(this);
        serviceState = synced ? CONNECTION_STATE_CONNECTED : CONNECTION_STATE_SYNCHRONIZING;
        if (settings.isDeafened()) {
          setDeafened(true);
        } else if (settings.isMuted()) {
          setMuted(true);
        }
        updateFavourites();
        if (synced) {
          // Initialize audio input
          mAudioInput = new AudioInput(this, mProtocol.codec);
          if (settings.isVoiceActivity())
            mAudioInput.startRecording(); // Immediately begin record if using voice activity

          showNotification();
        }
        break;
      case MumbleConnectionHost.STATE_DISCONNECTED:
        settings.deleteObserver(this);
        serviceState = CONNECTION_STATE_DISCONNECTED;
        if (mAudioInput != null) {
          try {
            mAudioInput.stopRecordingAndBlock();
          } catch (InterruptedException e) {
            e.printStackTrace();
          } finally {
            mAudioInput.terminate();
            mAudioInput = null;
          }
        }
        hideNotification();
        break;
      default:
        Assert.fail();
    }

    if (oldState != serviceState) {
      broadcastState();
    }
  }
示例#2
0
  @Override
  public void onCreate(Bundle savedInstanceState) {
    settings = Settings.getInstance(this);
    settings.addObserver(this);

    // Use theme from settings
    int theme = 0;
    if (settings.getTheme().equals(Settings.ARRAY_THEME_LIGHTDARK)) {
      theme = R.style.Theme_Sherlock_Light_DarkActionBar;
    } else if (settings.getTheme().equals(Settings.ARRAY_THEME_DARK)) {
      theme = R.style.Theme_Sherlock;
    }
    setTheme(theme);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_channel);

    // Bind to service
    Intent serviceIntent = new Intent(this, MumbleService.class);
    bindService(serviceIntent, conn, 0);

    // Register bluetooth SCO monitor
    registerReceiver(
        bluetoothReceiver, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED));

    // Handle differences in CallMode

    String callMode = settings.getCallMode();
    setVolumeControlStream(
        Settings.ARRAY_CALL_MODE_SPEAKER.equals(callMode)
            ? AudioManager.STREAM_MUSIC
            : AudioManager.STREAM_VOICE_CALL);

    // Set up proximity sensor
    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    proximityLock = powerManager.newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, Globals.LOG_TAG);

    // Set up PTT button.

    mTalkButton = (Button) findViewById(R.id.pushtotalk);
    pttView = findViewById(R.id.pushtotalk_view);
    mTalkButton.setOnTouchListener(
        new OnTouchListener() {

          private static final int TOGGLE_INTERVAL =
              250; // 250ms is the interval needed to toggle push to talk.
          private long lastTouch = 0;

          @Override
          public boolean onTouch(View v, MotionEvent event) {
            if (mService == null) {
              return false;
            }

            if (event.getAction() == MotionEvent.ACTION_DOWN && !settings.isPushToTalkToggle()) {
              setPushToTalk(true);
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
              if (settings.isPushToTalkToggle()) setPushToTalk(!mService.isRecording());
              else {
                if (System.currentTimeMillis() - lastTouch <= TOGGLE_INTERVAL) {
                  // Do nothing. We leave the push to talk on, as it has toggled.
                } else {
                  setPushToTalk(false);
                  lastTouch = System.currentTimeMillis();
                }
              }
            }

            return true; // We return true so that the selector that changes the background does not
            // fire.
          }
        });

    updatePTTConfiguration();

    mViewPager = (ViewPager) findViewById(R.id.pager);
    FragmentManager fragmentManager = getSupportFragmentManager();

    listFragment = (ChannelListFragment) fragmentManager.findFragmentByTag(LIST_FRAGMENT_TAG);
    chatFragment = (ChannelChatFragment) fragmentManager.findFragmentByTag(CHAT_FRAGMENT_TAG);

    FragmentTransaction remove = fragmentManager.beginTransaction();
    if (listFragment == null) listFragment = new ChannelListFragment();
    else remove.remove(listFragment);

    if (chatFragment == null) chatFragment = new ChannelChatFragment();
    else remove.remove(chatFragment);

    if (!remove.isEmpty()) {
      remove.commit();
      fragmentManager.executePendingTransactions();
    }

    if (mViewPager != null) {
      // Set up the ViewPager with the sections adapter.
      mViewPager.setOnPageChangeListener(
          new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
              // Hide keyboard if moving to channel list.
              if (arg0 == 0) {
                InputMethodManager imm =
                    (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(mViewPager.getApplicationWindowToken(), 0);
              }
              // Update indicator
              channelsIndicator.setVisibility(arg0 == 0 ? View.VISIBLE : View.INVISIBLE);
              chatIndicator.setVisibility(arg0 == 1 ? View.VISIBLE : View.INVISIBLE);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {}

            @Override
            public void onPageScrollStateChanged(int arg0) {}
          });

      // Set up tabs
      getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

      View channelTabView = getLayoutInflater().inflate(R.layout.channel_tab_view, null);
      channelsIndicator = channelTabView.findViewById(R.id.tab_channels_indicator);
      chatIndicator = channelTabView.findViewById(R.id.tab_chat_indicator);

      ImageButton channelsButton = (ImageButton) channelTabView.findViewById(R.id.tab_channels);
      ImageButton chatButton = (ImageButton) channelTabView.findViewById(R.id.tab_chat);
      channelsButton.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              mViewPager.setCurrentItem(0, true);
            }
          });
      chatButton.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              mViewPager.setCurrentItem(1, true);
            }
          });

      getSupportActionBar().setCustomView(channelTabView);
      // Setup a pager that will return a fragment for each of the two primary sections of the app.
      mSectionsPagerAdapter =
          new PlumbleSectionsPagerAdapter(
              this, getSupportFragmentManager(), listFragment, chatFragment);
      mViewPager.setAdapter(mSectionsPagerAdapter);

    } else {
      // Create tablet UI
      leftSplit = findViewById(R.id.left_split);
      rightSplit = findViewById(R.id.right_split);
      fragmentManager
          .beginTransaction()
          .add(R.id.chat_fragment, chatFragment, CHAT_FRAGMENT_TAG)
          .add(R.id.list_fragment, listFragment, LIST_FRAGMENT_TAG)
          .commit();
    }

    if (savedInstanceState != null) {
      chatTarget = (User) savedInstanceState.getParcelable(SAVED_STATE_CHAT_TARGET);
    }
  }