Home | History | Annotate | Download | only in nfc
      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.res.Configuration;
     22 import android.os.Vibrator;
     23 
     24 /**
     25  * Manages vibration, sound and animation for P2P events.
     26  */
     27 public class P2pEventManager implements P2pEventListener, SendUi.Callback {
     28     static final String TAG = "NfcP2pEventManager";
     29     static final boolean DBG = true;
     30 
     31     static final long[] VIBRATION_PATTERN = {0, 100, 10000};
     32 
     33     final Context mContext;
     34     final NfcService mNfcService;
     35     final P2pEventListener.Callback mCallback;
     36     final Vibrator mVibrator;
     37     final NotificationManager mNotificationManager;
     38     final SendUi mSendUi;
     39 
     40     // only used on UI thread
     41     boolean mSending;
     42     boolean mNdefSent;
     43     boolean mNdefReceived;
     44 
     45     public P2pEventManager(Context context, P2pEventListener.Callback callback) {
     46         mNfcService = NfcService.getInstance();
     47         mContext = context;
     48         mCallback = callback;
     49         mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
     50         mNotificationManager = (NotificationManager) mContext.getSystemService(
     51                 Context.NOTIFICATION_SERVICE);
     52 
     53         mSending = false;
     54         mSendUi = new SendUi(context, this);
     55     }
     56 
     57     @Override
     58     public void onP2pInRange() {
     59         mNfcService.playSound(NfcService.SOUND_START);
     60         mNdefSent = false;
     61         mNdefReceived = false;
     62 
     63         mVibrator.vibrate(VIBRATION_PATTERN, -1);
     64         mSendUi.takeScreenshot();
     65     }
     66 
     67     @Override
     68     public void onP2pSendConfirmationRequested() {
     69         final int uiModeType = mContext.getResources().getConfiguration().uiMode
     70                 & Configuration.UI_MODE_TYPE_MASK;
     71         if (uiModeType == Configuration.UI_MODE_TYPE_APPLIANCE) {
     72             // "Appliances" don't intrinsically have a way of confirming this, so we
     73             // will just auto-confirm.
     74             mCallback.onP2pSendConfirmed();
     75         } else {
     76             mSendUi.showPreSend();
     77         }
     78     }
     79 
     80     @Override
     81     public void onP2pSendComplete() {
     82         mNfcService.playSound(NfcService.SOUND_END);
     83         mVibrator.vibrate(VIBRATION_PATTERN, -1);
     84         mSendUi.finish(SendUi.FINISH_SEND_SUCCESS);
     85         mSending = false;
     86         mNdefSent = true;
     87     }
     88 
     89     @Override
     90     public void onP2pHandoverNotSupported() {
     91         mNfcService.playSound(NfcService.SOUND_ERROR);
     92         mVibrator.vibrate(VIBRATION_PATTERN, -1);
     93         mSendUi.finishAndToast(SendUi.FINISH_SCALE_UP,
     94                 mContext.getString(R.string.beam_handover_not_supported));
     95         mSending = false;
     96         mNdefSent = false;
     97     }
     98 
     99     @Override
    100     public void onP2pReceiveComplete(boolean playSound) {
    101         mVibrator.vibrate(VIBRATION_PATTERN, -1);
    102         if (playSound) mNfcService.playSound(NfcService.SOUND_END);
    103         // TODO we still don't have a nice receive solution
    104         // The sanest solution right now is just to scale back up what we had
    105         // and start the new activity. It is not perfect, but at least it is
    106         // consistent behavior. All other variants involve making the old
    107         // activity screenshot disappear, and then removing the animation
    108         // window hoping the new activity has started by then. This just goes
    109         // wrong too often and can look weird.
    110         mSendUi.finish(SendUi.FINISH_SCALE_UP);
    111         mNdefReceived = true;
    112     }
    113 
    114     @Override
    115     public void onP2pOutOfRange() {
    116         if (mSending) {
    117             mNfcService.playSound(NfcService.SOUND_ERROR);
    118             mSending = false;
    119         }
    120         if (!mNdefSent && !mNdefReceived) {
    121             mSendUi.finish(SendUi.FINISH_SCALE_UP);
    122         }
    123     }
    124 
    125     @Override
    126     public void onSendConfirmed() {
    127         if (!mSending) {
    128             mSendUi.showStartSend();
    129             mCallback.onP2pSendConfirmed();
    130         }
    131         mSending = true;
    132 
    133     }
    134 }
    135