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.cts.verifier.nfc;
     18 
     19 import com.android.cts.verifier.PassFailButtons;
     20 import com.android.cts.verifier.R;
     21 import com.android.cts.verifier.nfc.tech.NfcUtils;
     22 
     23 import android.app.AlertDialog;
     24 import android.app.Dialog;
     25 import android.app.PendingIntent;
     26 import android.content.Intent;
     27 import android.nfc.NdefMessage;
     28 import android.nfc.NfcAdapter;
     29 import android.nfc.NfcManager;
     30 import android.os.Bundle;
     31 import android.os.Parcelable;
     32 import android.widget.TextView;
     33 
     34 /**
     35  * Test activity that waits to receive a particular NDEF Push message from another NFC device.
     36  */
     37 public class NdefPushReceiverActivity extends PassFailButtons.Activity {
     38 
     39     private static final int NFC_NOT_ENABLED_DIALOG_ID = 1;
     40 
     41     private static final int RESULT_DIALOG_ID = 2;
     42 
     43     private static final String IS_MATCH_ARG = "isMatch";
     44 
     45     private NfcAdapter mNfcAdapter;
     46 
     47     private PendingIntent mPendingIntent;
     48 
     49     @Override
     50     protected void onCreate(Bundle savedInstanceState) {
     51         super.onCreate(savedInstanceState);
     52         setContentView(R.layout.pass_fail_text);
     53         setInfoResources(R.string.nfc_ndef_push_receiver, R.string.nfc_ndef_push_receiver_info, 0);
     54         setPassFailButtonClickListeners();
     55         getPassButton().setEnabled(false);
     56 
     57         TextView text = (TextView) findViewById(R.id.text);
     58         text.setText(R.string.nfc_ndef_push_receiver_instructions);
     59 
     60         NfcManager nfcManager = (NfcManager) getSystemService(NFC_SERVICE);
     61         mNfcAdapter = nfcManager.getDefaultAdapter();
     62         mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass())
     63                 .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
     64     }
     65 
     66     @Override
     67     protected void onResume() {
     68         super.onResume();
     69 
     70         if (!mNfcAdapter.isEnabled()) {
     71             showDialog(NFC_NOT_ENABLED_DIALOG_ID);
     72         }
     73 
     74         /* Only the sender requires mNfcAdapter.isNdefPushEnabled == true,
     75          * so no need to check it here in the receiver. */
     76 
     77         mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
     78     }
     79 
     80     @Override
     81     protected void onPause() {
     82         super.onPause();
     83         mNfcAdapter.disableForegroundDispatch(this);
     84     }
     85 
     86     @Override
     87     protected void onNewIntent(Intent intent) {
     88         super.onNewIntent(intent);
     89 
     90         NdefMessage[] messages = getNdefMessages(intent);
     91         boolean isMatch = messages != null
     92                 && messages.length > 0
     93                 && NfcUtils.areMessagesEqual(messages[0], NdefPushSenderActivity.TEST_MESSAGE);
     94 
     95         getPassButton().setEnabled(isMatch);
     96 
     97         Bundle args = new Bundle();
     98         args.putBoolean(IS_MATCH_ARG, isMatch);
     99         showDialog(RESULT_DIALOG_ID, args);
    100     }
    101 
    102     private NdefMessage[] getNdefMessages(Intent intent) {
    103         Parcelable[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    104         if (rawMessages != null) {
    105             NdefMessage[] messages = new NdefMessage[rawMessages.length];
    106             for (int i = 0; i < messages.length; i++) {
    107                 messages[i] = (NdefMessage) rawMessages[i];
    108             }
    109             return messages;
    110         } else {
    111             return null;
    112         }
    113     }
    114 
    115     @Override
    116     public Dialog onCreateDialog(int id, Bundle args) {
    117         switch (id) {
    118             case NFC_NOT_ENABLED_DIALOG_ID:
    119                 return NfcDialogs.createNotEnabledDialog(this);
    120 
    121             case RESULT_DIALOG_ID:
    122                 // Set placeholder titles and messages for now. Final titles and messages will
    123                 // be set in onPrepareDialog.
    124                 return new AlertDialog.Builder(this)
    125                         .setIcon(android.R.drawable.ic_dialog_info)
    126                         .setTitle(R.string.nfc_result_failure)
    127                         .setMessage("")
    128                         .setPositiveButton(android.R.string.ok, null)
    129                         .show();
    130 
    131             default:
    132                 return super.onCreateDialog(id, args);
    133         }
    134     }
    135 
    136     @Override
    137     protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
    138         switch (id) {
    139             case RESULT_DIALOG_ID:
    140                 boolean isMatch = args.getBoolean(IS_MATCH_ARG);
    141                 AlertDialog alert = (AlertDialog) dialog;
    142                 alert.setTitle(isMatch
    143                         ? R.string.nfc_result_success
    144                         : R.string.nfc_result_failure);
    145                 alert.setMessage(isMatch
    146                         ? getString(R.string.nfc_ndef_push_receive_success)
    147                         : getString(R.string.nfc_ndef_push_receive_failure));
    148                 break;
    149 
    150             default:
    151                 super.onPrepareDialog(id, dialog, args);
    152                 break;
    153         }
    154     }
    155 }
    156