Esempio n. 1
0
 @Override
 public boolean onKeyUp(int keyCode, KeyEvent event) {
   if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
     if (!event.isTracking()) {
       return true;
     }
     if (!event.isLongPress()) {
       AudioManager audio = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
       audio.adjustVolume(
           AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
       return true;
     }
   }
   if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
     if (!event.isTracking()) {
       return true;
     }
     if (!event.isLongPress()) {
       AudioManager audio = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
       audio.adjustVolume(
           AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
       return true;
     }
   }
   return false;
 }
 @Override
 public void onPrepared(MediaPlayer mediaPlayer) {
   System.out.println(sPhelper.getSongState() + "songstate==============");
   mediaPlayer.setOnCompletionListener(
       new OnCompletionListener() {
         public void onCompletion(MediaPlayer mp) {
           // cleanupPlayer(mp);
           System.out.println("=================:::::complete");
         }
       });
   mediaPlayer.setOnErrorListener(
       new OnErrorListener() {
         public boolean onError(MediaPlayer mp, int what, int extra) {
           //  cleanupPlayer(mp);
           System.out.println("=================:::::error");
           return true;
         }
       });
   if (sPhelper.getSongState() != null) {
     if (sPhelper.getSongState().equals("true")) {
       AudioManager audiomanager =
           (AudioManager) context.getSystemService(Service.AUDIO_SERVICE);
       audiomanager.setMode(AudioManager.MODE_NORMAL);
       audiomanager.setStreamVolume(
           AudioManager.STREAM_MUSIC,
           audiomanager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
           0);
       audiomanager.adjustVolume(AudioManager.ADJUST_RAISE, 0);
       mediaPlayer.start();
       System.out.println(sPhelper.getSongState() + "=====================>>>>>shengyin");
       if (!mediaPlayer.isPlaying()) {
         mediaPlayer.release();
       }
     }
     if (sPhelper.getSongState().equals("false")) {
       mediaPlayer.release();
     }
   }
 }
  @TestTargets({
    @TestTargetNew(
        level = TestLevel.COMPLETE,
        method = "setStreamVolume",
        args = {int.class, int.class, int.class}),
    @TestTargetNew(
        level = TestLevel.COMPLETE,
        method = "getStreamMaxVolume",
        args = {int.class}),
    @TestTargetNew(
        level = TestLevel.COMPLETE,
        method = "getStreamVolume",
        args = {int.class}),
    @TestTargetNew(
        level = TestLevel.COMPLETE,
        method = "adjustStreamVolume",
        args = {int.class, int.class, int.class}),
    @TestTargetNew(
        level = TestLevel.COMPLETE,
        method = "adjustSuggestedStreamVolume",
        args = {int.class, int.class, int.class}),
    @TestTargetNew(
        level = TestLevel.COMPLETE,
        method = "adjustVolume",
        args = {int.class, int.class})
  })
  public void testVolume() throws Exception {
    int[] streams = {
      AudioManager.STREAM_ALARM,
      AudioManager.STREAM_MUSIC,
      AudioManager.STREAM_SYSTEM,
      AudioManager.STREAM_VOICE_CALL,
      AudioManager.STREAM_RING
    };

    mAudioManager.adjustVolume(ADJUST_RAISE, 100);
    mAudioManager.adjustSuggestedStreamVolume(ADJUST_LOWER, USE_DEFAULT_STREAM_TYPE, FLAG_SHOW_UI);

    for (int i = 0; i < streams.length; i++) {
      int maxVolume = mAudioManager.getStreamMaxVolume(streams[i]);

      mAudioManager.setStreamVolume(streams[i], 1, FLAG_SHOW_UI);
      assertEquals(1, mAudioManager.getStreamVolume(streams[i]));

      mAudioManager.setStreamVolume(streams[i], maxVolume, FLAG_SHOW_UI);
      mAudioManager.adjustStreamVolume(streams[i], ADJUST_RAISE, FLAG_SHOW_UI);
      assertEquals(maxVolume, mAudioManager.getStreamVolume(streams[i]));

      mAudioManager.adjustSuggestedStreamVolume(ADJUST_LOWER, streams[i], FLAG_SHOW_UI);
      assertEquals(maxVolume - 1, mAudioManager.getStreamVolume(streams[i]));

      // volume lower
      mAudioManager.setStreamVolume(streams[i], maxVolume, FLAG_SHOW_UI);
      for (int k = maxVolume; k > 0; k--) {
        mAudioManager.adjustStreamVolume(streams[i], ADJUST_LOWER, FLAG_SHOW_UI);
        assertEquals(k - 1, mAudioManager.getStreamVolume(streams[i]));
      }

      mAudioManager.setRingerMode(RINGER_MODE_NORMAL);
      assertEquals(RINGER_MODE_NORMAL, mAudioManager.getRingerMode());
      mAudioManager.setStreamVolume(streams[i], 1, FLAG_SHOW_UI);
      assertEquals(1, mAudioManager.getStreamVolume(streams[i]));
      if (streams[i] == AudioManager.STREAM_RING) {
        mAudioManager.adjustStreamVolume(streams[i], ADJUST_LOWER, FLAG_SHOW_UI);
        assertEquals(0, mAudioManager.getStreamVolume(streams[i]));
        // adjusting the volume to zero should result in either silent or vibrate mode
        assertTrue(
            mAudioManager.getRingerMode() == RINGER_MODE_VIBRATE
                || mAudioManager.getRingerMode() == RINGER_MODE_SILENT);
        mAudioManager.setRingerMode(RINGER_MODE_NORMAL);
        assertEquals(RINGER_MODE_NORMAL, mAudioManager.getRingerMode());
        assertEquals(1, mAudioManager.getStreamVolume(streams[i]));
      } else {
        mAudioManager.adjustStreamVolume(streams[i], ADJUST_LOWER, FLAG_SHOW_UI);
        assertEquals(0, mAudioManager.getStreamVolume(streams[i]));
        // lowering the volume should NOT have changed the ringer mode
        assertEquals(RINGER_MODE_NORMAL, mAudioManager.getRingerMode());
        // API quirk: volume must be decremented from 1 to get ringer mode change
        mAudioManager.setStreamVolume(streams[i], 1, FLAG_SHOW_UI);
        mAudioManager.adjustStreamVolume(streams[i], ADJUST_LOWER, FLAG_ALLOW_RINGER_MODES);
        // lowering the volume should have changed the ringer mode
        assertTrue(
            mAudioManager.getRingerMode() == RINGER_MODE_VIBRATE
                || mAudioManager.getRingerMode() == RINGER_MODE_SILENT);
        mAudioManager.adjustStreamVolume(streams[i], ADJUST_LOWER, FLAG_ALLOW_RINGER_MODES);
        // adjusting the volume to zero should result in either silent or vibrate mode
        assertTrue(
            mAudioManager.getRingerMode() == RINGER_MODE_VIBRATE
                || mAudioManager.getRingerMode() == RINGER_MODE_SILENT);
        mAudioManager.adjustStreamVolume(streams[i], ADJUST_RAISE, FLAG_ALLOW_RINGER_MODES);
        // There are two possible ways the device may work. It may have a silent/vibrate
        // mode or it may have distinct silent and vibrate modes.
        assertTrue(
            mAudioManager.getRingerMode() == RINGER_MODE_NORMAL
                || mAudioManager.getRingerMode() == RINGER_MODE_VIBRATE);
        // Increase the volume one more time to get out of the vibrate mode which may
        // be separate from silent mode.
        mAudioManager.adjustStreamVolume(streams[i], ADJUST_RAISE, FLAG_ALLOW_RINGER_MODES);
        assertEquals(RINGER_MODE_NORMAL, mAudioManager.getRingerMode());
      }

      // volume raise
      mAudioManager.setStreamVolume(streams[i], 0, FLAG_SHOW_UI);
      for (int k = 0; k < maxVolume; k++) {
        mAudioManager.adjustStreamVolume(streams[i], ADJUST_RAISE, FLAG_SHOW_UI);
        assertEquals(1 + k, mAudioManager.getStreamVolume(streams[i]));
      }

      // volume same
      mAudioManager.setStreamVolume(streams[i], maxVolume, FLAG_SHOW_UI);
      for (int k = 0; k < maxVolume; k++) {
        mAudioManager.adjustStreamVolume(streams[i], ADJUST_SAME, FLAG_SHOW_UI);
        assertEquals(maxVolume, mAudioManager.getStreamVolume(streams[i]));
      }

      mAudioManager.setStreamVolume(streams[i], maxVolume, FLAG_SHOW_UI);
    }

    // adjust volume
    int maxVolume = mAudioManager.getStreamMaxVolume(STREAM_MUSIC);
    mAudioManager.adjustVolume(ADJUST_RAISE, 100);

    MediaPlayer mp = MediaPlayer.create(mContext, MP3_TO_PLAY);
    mp.setAudioStreamType(STREAM_MUSIC);
    mp.setLooping(true);
    mp.start();
    Thread.sleep(TIME_TO_PLAY);
    assertTrue(mAudioManager.isMusicActive());

    // adjust volume as ADJUST_SAME
    for (int k = 0; k < maxVolume; k++) {
      mAudioManager.adjustVolume(ADJUST_SAME, FLAG_SHOW_UI);
      assertEquals(maxVolume, mAudioManager.getStreamVolume(STREAM_MUSIC));
    }

    // adjust volume as ADJUST_RAISE
    mAudioManager.setStreamVolume(STREAM_MUSIC, 1, FLAG_SHOW_UI);
    for (int k = 0; k < maxVolume - 1; k++) {
      mAudioManager.adjustVolume(ADJUST_RAISE, FLAG_SHOW_UI);
      assertEquals(2 + k, mAudioManager.getStreamVolume(STREAM_MUSIC));
    }

    // adjust volume as ADJUST_LOWER
    mAudioManager.setStreamVolume(STREAM_MUSIC, maxVolume, FLAG_SHOW_UI);
    maxVolume = mAudioManager.getStreamVolume(STREAM_MUSIC);

    mAudioManager.adjustVolume(ADJUST_LOWER, FLAG_SHOW_UI);
    assertEquals(maxVolume - 1, mAudioManager.getStreamVolume(STREAM_MUSIC));
    mp.stop();
    mp.release();
    Thread.sleep(TIME_TO_PLAY);
    assertFalse(mAudioManager.isMusicActive());
  }
Esempio n. 4
0
  // private String senderPhoneNumber;
  @Override
  public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    String passCode = intent.getStringExtra("code");
    if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
      Bundle bundle = intent.getExtras(); // ---get the SMS message passed in---
      SmsMessage[] msgs = null;
      String msg_from;
      String check = SetAtt.ds;
      if (bundle != null) {
        // ---retrieve the SMS message received---
        try {

          Object[] pdus = (Object[]) bundle.get("pdus");

          msgs = new SmsMessage[pdus.length];
          for (int i = 0; i < msgs.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            msg_from = msgs[i].getOriginatingAddress();
            msgBody = msgs[i].getMessageBody();
          }

          Log.d(TAG, "Message : " + msgBody);
          int i = 9;
          boolean bool = true;

          char[] sms = msgBody.toCharArray();
          char[] code = check.toCharArray();
          for (int j = 0; j < code.length; j++) {
            if (code[j] != sms[j]) {
              bool = false;
            }
          }

          if (bool) {

            SmsManager smsMng = SmsManager.getDefault();
            smsMng.sendTextMessage(
                msgs[0].getOriginatingAddress(),
                null,
                "Your phone mode is changed to normal",
                null,
                null);
            AudioManager audioManager =
                (AudioManager) context.getSystemService(context.AUDIO_SERVICE);

            int j = 0;

            while (j < 5) {
              j++;
              audioManager.adjustVolume(1, 10);
            }
            audioManager.setStreamVolume(1, audioManager.getStreamMaxVolume(1), 10);
          }

        } catch (Exception e) {
          //	                            Log.d("Exception caught",e.getMessage());
        }

      } else {

      }
    }
  }