• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 android.nfc.tech.cts.interactive;
18 
19 import android.nfc.NdefMessage;
20 import android.nfc.NdefRecord;
21 
22 import java.util.Arrays;
23 
24 /**
25  * Class with utility methods for testing equality of messages and displaying byte payloads
26  */
27 public class NfcUtils {
28 
29     /**
30      * Checks if contents of two NdefMessages are equal
31      * @param message - first message
32      * @param otherMessage - second message
33      * @return - if first message is equal to second message
34      */
areMessagesEqual(NdefMessage message, NdefMessage otherMessage)35     public static boolean areMessagesEqual(NdefMessage message, NdefMessage otherMessage) {
36         return message != null && otherMessage != null
37                 && areRecordArraysEqual(message.getRecords(), otherMessage.getRecords());
38     }
39 
areRecordArraysEqual(NdefRecord[] records, NdefRecord[] otherRecords)40     private static boolean areRecordArraysEqual(NdefRecord[] records, NdefRecord[] otherRecords) {
41         if (records.length == otherRecords.length) {
42             for (int i = 0; i < records.length; i++) {
43                 if (!areRecordsEqual(records[i], otherRecords[i])) {
44                     return false;
45                 }
46             }
47             return true;
48         } else {
49             return false;
50         }
51     }
52 
areRecordsEqual(NdefRecord record, NdefRecord otherRecord)53     private static boolean areRecordsEqual(NdefRecord record, NdefRecord otherRecord) {
54         return Arrays.equals(record.toByteArray(), otherRecord.toByteArray());
55     }
56 
displayByteArray(byte[] bytes)57     static CharSequence displayByteArray(byte[] bytes) {
58         StringBuilder builder = new StringBuilder().append("[");
59         for (int i = 0; i < bytes.length; i++) {
60             builder.append(Byte.toString(bytes[i]));
61             if (i + 1 < bytes.length) {
62                 builder.append(", ");
63             }
64         }
65         return builder.append("]");
66     }
67 }
68