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 17 package com.android.car.settings.datausage; 18 19 import android.content.Context; 20 import android.net.NetworkTemplate; 21 import android.telephony.SubscriptionInfo; 22 import android.telephony.SubscriptionManager; 23 import android.telephony.SubscriptionPlan; 24 import android.telephony.TelephonyManager; 25 import android.text.BidiFormatter; 26 import android.text.format.DateUtils; 27 import android.text.format.Formatter; 28 29 import androidx.annotation.Nullable; 30 import androidx.annotation.VisibleForTesting; 31 32 import com.android.car.settings.R; 33 import com.android.internal.util.CollectionUtils; 34 35 import java.util.Calendar; 36 import java.util.List; 37 38 /** Provides helpful utilities related to data usage. */ 39 public final class DataUsageUtils { 40 41 @VisibleForTesting 42 static final long PETA = 1000000000000000L; 43 DataUsageUtils()44 private DataUsageUtils() { 45 } 46 47 /** 48 * Returns the mobile network template given the subscription id. 49 */ getMobileNetworkTemplate(TelephonyManager telephonyManager, int subscriptionId)50 public static NetworkTemplate getMobileNetworkTemplate(TelephonyManager telephonyManager, 51 int subscriptionId) { 52 NetworkTemplate mobileAll = NetworkTemplate.buildTemplateMobileAll( 53 telephonyManager.getSubscriberId(subscriptionId)); 54 return NetworkTemplate.normalize(mobileAll, telephonyManager.getMergedSubscriberIds()); 55 } 56 57 /** 58 * Returns the default subscription if available else returns 59 * {@link SubscriptionManager#INVALID_SUBSCRIPTION_ID}. 60 */ getDefaultSubscriptionId(SubscriptionManager subscriptionManager)61 public static int getDefaultSubscriptionId(SubscriptionManager subscriptionManager) { 62 if (subscriptionManager == null) { 63 return SubscriptionManager.INVALID_SUBSCRIPTION_ID; 64 } 65 SubscriptionInfo subscriptionInfo = subscriptionManager.getDefaultDataSubscriptionInfo(); 66 if (subscriptionInfo == null) { 67 List<SubscriptionInfo> list = subscriptionManager.getAllSubscriptionInfoList(); 68 if (list.size() == 0) { 69 return SubscriptionManager.INVALID_SUBSCRIPTION_ID; 70 } 71 subscriptionInfo = list.get(0); 72 } 73 return subscriptionInfo.getSubscriptionId(); 74 } 75 76 /** 77 * Format byte value to readable string using IEC units. 78 */ bytesToIecUnits(Context context, long byteValue)79 public static CharSequence bytesToIecUnits(Context context, long byteValue) { 80 Formatter.BytesResult res = Formatter.formatBytes(context.getResources(), byteValue, 81 Formatter.FLAG_IEC_UNITS); 82 return BidiFormatter.getInstance().unicodeWrap(context.getString( 83 com.android.internal.R.string.fileSizeSuffix, res.value, res.units)); 84 } 85 86 /** 87 * Generate a string that lists byte value to readable string using IEC units, and date range 88 * from specified start date to current date. 89 */ formatDataUsageInCycle(Context context, long byteValue, long fromTimeInMillis)90 public static CharSequence formatDataUsageInCycle(Context context, long byteValue, 91 long fromTimeInMillis) { 92 CharSequence usage = bytesToIecUnits(context, byteValue); 93 int flags = DateUtils.FORMAT_ABBREV_MONTH | DateUtils.FORMAT_NO_YEAR; 94 String fromText = DateUtils.formatDateTime(context, fromTimeInMillis, flags); 95 String toText = DateUtils.formatDateTime(context, Calendar.getInstance().getTimeInMillis(), 96 flags); 97 return context.getString(R.string.network_and_internet_data_usage_time_range_summary, usage, 98 fromText, toText); 99 } 100 101 /** 102 * Returns the primary subscription plan. Returns {@code null} if {@link SubscriptionPlan} 103 * doesn't exist for a given subscriptionId or if the first {@link SubscriptionPlan} has 104 * invalid properties. 105 */ 106 @Nullable getPrimaryPlan(SubscriptionManager subManager, int subscriptionId)107 public static SubscriptionPlan getPrimaryPlan(SubscriptionManager subManager, 108 int subscriptionId) { 109 List<SubscriptionPlan> plans = subManager.getSubscriptionPlans(subscriptionId); 110 if (CollectionUtils.isEmpty(plans)) { 111 return null; 112 } 113 // First plan in the list is the primary plan 114 SubscriptionPlan plan = plans.get(0); 115 return plan.getDataLimitBytes() > 0 116 && saneSize(plan.getDataUsageBytes()) 117 && plan.getCycleRule() != null ? plan : null; 118 } 119 saneSize(long value)120 private static boolean saneSize(long value) { 121 return value >= 0L && value < PETA; 122 } 123 } 124