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 
17 package com.android.server.broadcastradio.hal1;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 
22 import com.android.server.utils.Slogf;
23 
24 import java.util.Map;
25 import java.util.Set;
26 
27 final class Convert {
28 
29     private static final String TAG = "BcRadio1Srv.Convert";
30 
31     /**
32      * Converts string map to an array that's easily accessible by native code.
33      *
34      * Calling this java method once is more efficient than converting map object on the native
35      * side, which requires several separate java calls for each element.
36      *
37      * @param map map to convert.
38      * @return array (sized the same as map) of two-element string arrays
39      *         (first element is the key, second is value).
40      */
stringMapToNative(@ullable Map<String, String> map)41     static @NonNull String[][] stringMapToNative(@Nullable Map<String, String> map) {
42         if (map == null) {
43             Slogf.v(TAG, "map is null, returning zero-elements array");
44             return new String[0][0];
45         }
46 
47         Set<Map.Entry<String, String>> entries = map.entrySet();
48         int len = entries.size();
49         String[][] arr = new String[len][2];
50 
51         int i = 0;
52         for (Map.Entry<String, String> entry : entries) {
53             arr[i][0] = entry.getKey();
54             arr[i][1] = entry.getValue();
55             i++;
56         }
57 
58         Slogf.v(TAG, "converted " + i + " element(s)");
59         return arr;
60     }
61 }
62