1 /* 2 * Copyright (C) 2011 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.android.nfc; 18 19 import android.app.NotificationManager; 20 import android.content.Context; 21 import android.content.SharedPreferences; 22 import android.media.AudioManager; 23 import android.media.SoundPool; 24 import android.os.Handler; 25 import android.os.Message; 26 import android.os.Vibrator; 27 import com.android.nfc3.R; 28 29 /** 30 * Manages vibration, sound and animation for P2P events. 31 */ 32 public class P2pEventManager implements P2pEventListener, SendUi.Callback { 33 static final String TAG = "NfcP2pEventManager"; 34 static final boolean DBG = true; 35 36 static final long[] VIBRATION_PATTERN = {0, 100, 10000}; 37 38 final Context mContext; 39 final P2pEventListener.Callback mCallback; 40 final int mStartSound; 41 final int mEndSound; 42 final int mErrorSound; 43 final SoundPool mSoundPool; // playback synchronized on this 44 final Vibrator mVibrator; 45 final NotificationManager mNotificationManager; 46 final SendUi mSendUi; 47 48 // only used on UI thread 49 boolean mSending; 50 boolean mNdefSent; 51 boolean mNdefReceived; 52 53 public P2pEventManager(Context context, P2pEventListener.Callback callback) { 54 mContext = context; 55 mCallback = callback; 56 mSoundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0); 57 mStartSound = mSoundPool.load(mContext, R.raw.start, 1); 58 mEndSound = mSoundPool.load(mContext, R.raw.end, 1); 59 mErrorSound = mSoundPool.load(mContext, R.raw.error, 1); 60 mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); 61 mNotificationManager = (NotificationManager) mContext.getSystemService( 62 Context.NOTIFICATION_SERVICE); 63 64 mSending = false; 65 mSendUi = new SendUi(context, this); 66 } 67 68 @Override 69 public void onP2pInRange() { 70 playSound(mStartSound); 71 mNdefSent = false; 72 mNdefReceived = false; 73 74 mVibrator.vibrate(VIBRATION_PATTERN, -1); 75 mSendUi.takeScreenshot(); 76 } 77 78 @Override 79 public void onP2pSendConfirmationRequested() { 80 mSendUi.showPreSend(); 81 } 82 83 @Override 84 public void onP2pSendComplete() { 85 playSound(mEndSound); 86 mVibrator.vibrate(VIBRATION_PATTERN, -1); 87 mSendUi.showPostSend(); 88 mSending = false; 89 mNdefSent = true; 90 } 91 92 @Override 93 public void onP2pReceiveComplete() { 94 mVibrator.vibrate(VIBRATION_PATTERN, -1); 95 playSound(mEndSound); 96 // TODO we still don't have a nice receive solution 97 // The sanest solution right now is just to scale back up what we had 98 // and start the new activity. It is not perfect, but at least it is 99 // consistent behavior. All other variants involve making the old 100 // activity screenshot disappear, and then removing the animation 101 // window hoping the new activity has started by then. This just goes 102 // wrong too often and can looks weird. 103 mSendUi.finish(SendUi.FINISH_SCALE_UP); 104 mNdefReceived = true; 105 } 106 107 @Override 108 public void onP2pOutOfRange() { 109 if (mSending) { 110 playSound(mErrorSound); 111 mSending = false; 112 } 113 mSendUi.finish(SendUi.FINISH_SCALE_UP); 114 } 115 116 @Override 117 public void onSendConfirmed() { 118 if (!mSending) { 119 mSendUi.showStartSend(); 120 mCallback.onP2pSendConfirmed(); 121 } 122 mSending = true; 123 124 } 125 126 void playSound(int sound) { 127 mSoundPool.play(sound, 1.0f, 1.0f, 0, 0, 1.0f); 128 } 129 } 130