Home | History | Annotate | Download | only in com.example.android.wearable.findphone
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.wearable.findphone;
     18 
     19 import android.media.AudioManager;
     20 import android.media.MediaPlayer;
     21 import android.media.RingtoneManager;
     22 import android.net.Uri;
     23 import android.util.Log;
     24 
     25 import com.google.android.gms.wearable.DataEvent;
     26 import com.google.android.gms.wearable.DataEventBuffer;
     27 import com.google.android.gms.wearable.DataMap;
     28 import com.google.android.gms.wearable.WearableListenerService;
     29 
     30 import java.io.IOException;
     31 
     32 /**
     33  * Listens for disconnection from home device.
     34  */
     35 public class SoundAlarmListenerService extends WearableListenerService {
     36 
     37     private static final String TAG = "ExampleFindPhoneApp";
     38 
     39     private static final String FIELD_ALARM_ON = "alarm_on";
     40 
     41     private AudioManager mAudioManager;
     42     private static int mOrigVolume;
     43     private int mMaxVolume;
     44     private Uri mAlarmSound;
     45     private MediaPlayer mMediaPlayer;
     46 
     47     @Override
     48     public void onCreate() {
     49         super.onCreate();
     50         mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
     51         mOrigVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
     52         mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
     53         mAlarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
     54         mMediaPlayer = new MediaPlayer();
     55     }
     56 
     57     @Override
     58     public void onDestroy() {
     59         // Reset the alarm volume to the user's original setting.
     60         mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, mOrigVolume, 0);
     61         mMediaPlayer.release();
     62         super.onDestroy();
     63     }
     64 
     65     @Override
     66     public void onDataChanged(DataEventBuffer dataEvents) {
     67         if (Log.isLoggable(TAG, Log.DEBUG)) {
     68             Log.d(TAG, "onDataChanged: " + dataEvents + " for " + getPackageName());
     69         }
     70         for (DataEvent event : dataEvents) {
     71             if (event.getType() == DataEvent.TYPE_DELETED) {
     72                 Log.i(TAG, event + " deleted");
     73             } else if (event.getType() == DataEvent.TYPE_CHANGED) {
     74                 Boolean alarmOn =
     75                         DataMap.fromByteArray(event.getDataItem().getData()).get(FIELD_ALARM_ON);
     76                 if (alarmOn) {
     77                     mOrigVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
     78                     mMediaPlayer.reset();
     79                     // Sound alarm at max volume.
     80                     mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, mMaxVolume, 0);
     81                     mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
     82                     try {
     83                         mMediaPlayer.setDataSource(getApplicationContext(), mAlarmSound);
     84                         mMediaPlayer.prepare();
     85                     } catch (IOException e) {
     86                         Log.e(TAG, "Failed to prepare media player to play alarm.", e);
     87                     }
     88                     mMediaPlayer.start();
     89                 } else {
     90                     // Reset the alarm volume to the user's original setting.
     91                     mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, mOrigVolume, 0);
     92                     if (mMediaPlayer.isPlaying()) {
     93                         mMediaPlayer.stop();
     94                     }
     95                 }
     96             }
     97         }
     98     }
     99 
    100 }
    101