Example #1
0
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    instance = this;
    Log.i("Jane", "Received start id " + startId + ": " + intent);
    Log.d("Jane", "Service received start command.");

    tts =
        new TextToSpeech(
            this,
            new TextToSpeech.OnInitListener() {

              @Override
              public void onInit(int status) {}
            });
    tts.setOnUtteranceCompletedListener(this);

    // set up Bluetooth here
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
      bt = new Gingerbread();
    } else {
      bt = new Honeycomb();
    }

    bt.setContext(getApplicationContext());

    try {
      bt.getProxy();
    } catch (Exception e) {
      e.printStackTrace();
    }

    AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    ComponentName mediaButtonResponder =
        new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName());
    am.registerMediaButtonEventReceiver(mediaButtonResponder);

    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    WakeLock lock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "JaneLock");
    lock.acquire(10 * 60 * 1000);

    localBroadcastManager = LocalBroadcastManager.getInstance(this);
    localChatReceiver = new ChatReceiver(this);

    String username = intent.getExtras().getString("username");
    String password = intent.getExtras().getString("password");
    uiMessenger = intent.getExtras().getParcelable("messenger");

    nameCache = new HashMap<String, String>();
    chatCache = new HashMap<String, Chat>();

    smack = org.jivesoftware.smack.SmackAndroid.init(this);

    this.registerReceiver(stateIntents, new IntentFilter(JaneIntent));
    this.registerReceiver(stateIntents, new IntentFilter(Bluetooth.BLUETOOTH_STATE));

    LoginThread login = new LoginThread(username, password, this);
    login.start();

    return START_STICKY;
  }
Example #2
0
  @Override
  public void onDestroy() {
    // Cancel the persistent notification.
    notificationManager.cancel(JANE_NOTIFICATION_CODE);

    // disconnect bluetooth proxy
    try {
      bt.releaseProxy();
    } catch (Exception e) {
      e.printStackTrace();
    }

    // close smack
    AsyncTask<Void, Void, Integer> smackShutdown =
        new AsyncTask<Void, Void, Integer>() {

          @Override
          protected Integer doInBackground(Void... params) {
            connection.disconnect();
            return 1;
          }
        };

    smackShutdown.execute();
    smack.onDestroy();

    // unregister JaneIntent receiver
    this.unregisterReceiver(stateIntents);

    // unregister tts
    tts.shutdown();

    // Tell the user we stopped.
    Toast.makeText(this, "Jane service stopped.", Toast.LENGTH_SHORT).show();
    super.onDestroy();
  }
Example #3
0
  public void listen() {

    Log.d("Jane", "BT available: " + bt.isAvailable());

    if (bt.isAvailable() && !bluetoothConnected) {
      bt.startVoiceRecognition();
      return;
    }

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(
        RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.willwhitney.jane");

    SpeechRecognizer recognizer =
        SpeechRecognizer.createSpeechRecognizer(this.getApplicationContext());
    RecognitionListener listener =
        new RecognitionListener() {

          @Override
          public void onResults(Bundle results) {
            if (bt.isAvailable()) {
              bt.stopVoiceRecognition();
            }
            ArrayList<String> voiceResults =
                results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            if (voiceResults == null) {
              Log.e("Jane", "No voice results");
            } else {
              Log.d("Jane", "Printing matches: ");
              for (String match : voiceResults) {
                Log.d("Jane", match);
              }
            }
            JaneService.this.handleSpeech(voiceResults);
          }

          @Override
          public void onReadyForSpeech(Bundle params) {
            Log.d("Jane", "Ready for speech");
          }

          @Override
          public void onError(int error) {
            Log.d("Jane", "Error listening for speech: " + error);
          }

          @Override
          public void onBeginningOfSpeech() {
            Log.d("Jane", "Speech starting");
          }

          @Override
          public void onBufferReceived(byte[] buffer) {}

          @Override
          public void onEndOfSpeech() {
            Log.d("Jane", "Speech ended.");
          }

          @Override
          public void onEvent(int eventType, Bundle params) {}

          @Override
          public void onPartialResults(Bundle partialResults) {}

          @Override
          public void onRmsChanged(float rmsdB) {}
        };
    recognizer.setRecognitionListener(listener);
    recognizer.startListening(intent);
  }