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