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.tech;
18 
19 import android.nfc.NdefMessage;
20 import android.nfc.NdefRecord;
21 
22 import java.util.Arrays;
23 
24 /** Class with utility methods for testing equality of messages and displaying byte payloads. */
25 public class NfcUtils {
26 
areMessagesEqual(NdefMessage message, NdefMessage otherMessage)27     public static boolean areMessagesEqual(NdefMessage message, NdefMessage otherMessage) {
28         return message != null && otherMessage != null
29                 && areRecordArraysEqual(message.getRecords(), otherMessage.getRecords());
30     }
31 
areRecordArraysEqual(NdefRecord[] records, NdefRecord[] otherRecords)32     private static boolean areRecordArraysEqual(NdefRecord[] records, NdefRecord[] otherRecords) {
33         if (records.length == otherRecords.length) {
34             for (int i = 0; i < records.length; i++) {
35                 if (!areRecordsEqual(records[i], otherRecords[i])) {
36                     return false;
37                 }
38             }
39             return true;
40         } else {
41             return false;
42         }
43     }
44 
areRecordsEqual(NdefRecord record, NdefRecord otherRecord)45     private static boolean areRecordsEqual(NdefRecord record, NdefRecord otherRecord) {
46         return Arrays.equals(record.toByteArray(), otherRecord.toByteArray());
47     }
48 
displayByteArray(byte[] bytes)49     static CharSequence displayByteArray(byte[] bytes) {
50         StringBuilder builder = new StringBuilder().append("[");
51         for (int i = 0; i < bytes.length; i++) {
52             builder.append(Byte.toString(bytes[i]));
53             if (i + 1 < bytes.length) {
54                 builder.append(", ");
55             }
56         }
57         return builder.append("]");
58     }
59 }
60