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