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