1 /*
2  * Copyright (C) 2017 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 com.example.android.autofillframework
17 
18 import android.os.Bundle
19 import com.google.gson.Gson
20 import com.google.gson.GsonBuilder
21 import java.util.Arrays
22 
23 object CommonUtil {
24 
25     val TAG = "AutofillSample"
26 
27     val EXTRA_DATASET_NAME = "dataset_name"
28     val EXTRA_FOR_RESPONSE = "for_response"
29 
bundleToStringnull30     private fun bundleToString(builder: StringBuilder, data: Bundle) {
31         val keySet = data.keySet()
32         builder.append("[Bundle with ").append(keySet.size).append(" keys:")
33         for (key in keySet) {
34             builder.append(' ').append(key).append('=')
35             val value = data.get(key)
36             if (value is Bundle) {
37                 bundleToString(builder, value)
38             } else {
39                 val string = if (value is Array<*>) Arrays.toString(value) else value
40                 builder.append(string)
41             }
42         }
43         builder.append(']')
44     }
45 
bundleToStringnull46     fun bundleToString(data: Bundle?): String {
47         if (data == null) {
48             return "N/A"
49         }
50         val builder = StringBuilder()
51         bundleToString(builder, data)
52         return builder.toString()
53     }
54 
createGsonnull55     fun createGson(): Gson {
56         return GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create()
57     }
58 }