1 /* 2 * Copyright (C) 2019 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.android.server.wifi.util; 17 18 import android.annotation.NonNull; 19 import android.content.Context; 20 21 import com.android.wifi.resources.R; 22 23 /** Utilities for computations involving RSSI. */ 24 public class RssiUtil { RssiUtil()25 private RssiUtil() {} 26 27 /** Calculate RSSI level from RSSI using overlaid RSSI level thresholds. */ calculateSignalLevel(Context context, int rssi)28 public static int calculateSignalLevel(Context context, int rssi) { 29 int[] thresholds = getRssiLevelThresholds(context); 30 31 for (int level = 0; level < thresholds.length; level++) { 32 if (rssi < thresholds[level]) { 33 return level; 34 } 35 } 36 return thresholds.length; 37 } 38 39 @NonNull getRssiLevelThresholds(Context context)40 private static int[] getRssiLevelThresholds(Context context) { 41 // getIntArray() will never return null, it will throw instead 42 return context.getResources().getIntArray(R.array.config_wifiRssiLevelThresholds); 43 } 44 } 45