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 
     22 import android.app.Dialog;
     23 import android.nfc.NdefMessage;
     24 import android.nfc.NdefRecord;
     25 import android.nfc.NfcAdapter;
     26 import android.nfc.NfcEvent;
     27 import android.nfc.NfcManager;
     28 import android.os.Bundle;
     29 import android.widget.TextView;
     30 
     31 import java.nio.charset.Charset;
     32 
     33 /**
     34  * Test activity that sends a particular NDEF Push message to another NFC device.
     35  */
     36 public class NdefPushSenderActivity extends PassFailButtons.Activity implements
     37         NfcAdapter.CreateNdefMessageCallback {
     38 
     39     static final NdefMessage TEST_MESSAGE = getTestMessage();
     40 
     41     private static final int NFC_NOT_ENABLED_DIALOG_ID = 1;
     42     private static final int NDEF_PUSH_NOT_ENABLED_DIALOG_ID = 2;
     43 
     44     private NfcAdapter mNfcAdapter;
     45 
     46     @Override
     47     protected void onCreate(Bundle savedInstanceState) {
     48         super.onCreate(savedInstanceState);
     49         setContentView(R.layout.pass_fail_text);
     50         setInfoResources(R.string.nfc_ndef_push_sender, R.string.nfc_ndef_push_sender_info, 0);
     51         setPassFailButtonClickListeners();
     52 
     53         TextView text = (TextView) findViewById(R.id.text);
     54         text.setText(R.string.nfc_ndef_push_sender_instructions);
     55 
     56         NfcManager nfcManager = (NfcManager) getSystemService(NFC_SERVICE);
     57         mNfcAdapter = nfcManager.getDefaultAdapter();
     58     }
     59 
     60     private static NdefMessage getTestMessage() {
     61         byte[] mimeBytes = "application/com.android.cts.verifier.nfc"
     62                 .getBytes(Charset.forName("US-ASCII"));
     63         byte[] id = new byte[] {1, 3, 3, 7};
     64         byte[] payload = "CTS Verifier NDEF Push Tag".getBytes(Charset.forName("US-ASCII"));
     65         return new NdefMessage(new NdefRecord[] {
     66                 new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, id, payload)
     67         });
     68     }
     69 
     70     @Override
     71     protected void onResume() {
     72         super.onResume();
     73 
     74         if (!mNfcAdapter.isEnabled()) {
     75             showDialog(NFC_NOT_ENABLED_DIALOG_ID);
     76         } else if (!mNfcAdapter.isNdefPushEnabled()) {
     77             /* Sender must have NDEF push enabled */
     78             showDialog(NDEF_PUSH_NOT_ENABLED_DIALOG_ID);
     79         }
     80 
     81         mNfcAdapter.setNdefPushMessageCallback(this, this);
     82     }
     83 
     84     @Override
     85     protected void onPause() {
     86         super.onPause();
     87     }
     88 
     89     @Override
     90     public Dialog onCreateDialog(int id, Bundle args) {
     91         switch (id) {
     92             case NFC_NOT_ENABLED_DIALOG_ID:
     93                 return NfcDialogs.createNotEnabledDialog(this);
     94             case NDEF_PUSH_NOT_ENABLED_DIALOG_ID:
     95                 return NfcDialogs.createNdefPushNotEnabledDialog(this);
     96             default:
     97                 return super.onCreateDialog(id, args);
     98         }
     99     }
    100 
    101     @Override
    102     public NdefMessage createNdefMessage(NfcEvent event) {
    103         return getTestMessage();
    104     }
    105 }
    106