1 /*
2  * Copyright (C) 2020 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 package android.car.test.util;
17 
18 import android.annotation.NonNull;
19 import android.content.Intent;
20 import android.os.Bundle;
21 
22 import java.util.Iterator;
23 import java.util.Set;
24 
25 /**
26  * Provides helper methods for core Android classes.
27  */
28 public final class AndroidHelper {
29 
30     /**
31      * Gets a string representing the intent, including its extras.
32      */
toString(@onNull Intent intent)33     public static String toString(@NonNull Intent intent) {
34         return intent.toString() + " Extras: " + toString(intent.getExtras());
35     }
36 
37     /**
38      * Gets a string representing the bundle, including its key-value pairs.
39      */
toString(@onNull Bundle bundle)40     public static String toString(@NonNull Bundle bundle) {
41         Set<String> keySet = bundle.keySet();
42         StringBuilder string = new StringBuilder("Bundle[");
43         if (keySet.isEmpty()) {
44             string.append("empty");
45         } else {
46             string.append(keySet.size()).append(" keys: ");
47             Iterator<String> iterator = keySet.iterator();
48             while (iterator.hasNext()) {
49                 String key = iterator.next();
50                 string.append(key).append('=').append(bundle.get(key));
51                 if (iterator.hasNext()) {
52                     string.append(", ");
53                 }
54             }
55         }
56         return string.append(']').toString();
57     }
58 
AndroidHelper()59     private AndroidHelper() {
60         throw new UnsupportedOperationException("contains only static methods");
61     }
62 }
63