Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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.net; |
| 18 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.ACCESS_NETWORK_STATE; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 20 | import static android.Manifest.permission.CONNECTIVITY_INTERNAL; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 21 | import static android.Manifest.permission.DUMP; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 22 | import static android.Manifest.permission.MANAGE_NETWORK_POLICY; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 23 | import static android.Manifest.permission.READ_NETWORK_USAGE_HISTORY; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 24 | import static android.Manifest.permission.READ_PHONE_STATE; |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 25 | import static android.content.Intent.ACTION_PACKAGE_ADDED; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 26 | import static android.content.Intent.ACTION_UID_REMOVED; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 27 | import static android.content.Intent.ACTION_USER_ADDED; |
| 28 | import static android.content.Intent.ACTION_USER_REMOVED; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 29 | import static android.content.Intent.EXTRA_UID; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 30 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 31 | import static android.net.ConnectivityManager.TYPE_ETHERNET; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 32 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 33 | import static android.net.ConnectivityManager.TYPE_WIFI; |
| 34 | import static android.net.ConnectivityManager.TYPE_WIMAX; |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 35 | import static android.net.ConnectivityManager.isNetworkTypeMobile; |
| 36 | import static android.net.NetworkPolicy.CYCLE_NONE; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 37 | import static android.net.NetworkPolicy.LIMIT_DISABLED; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 38 | import static android.net.NetworkPolicy.SNOOZE_NEVER; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 39 | import static android.net.NetworkPolicy.WARNING_DISABLED; |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 40 | import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 41 | import static android.net.NetworkPolicyManager.POLICY_NONE; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 42 | import static android.net.NetworkPolicyManager.POLICY_ALLOW_BACKGROUND_BATTERY_SAVE; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 43 | import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 44 | import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 45 | import static android.net.NetworkPolicyManager.RULE_REJECT_METERED; |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 46 | import static android.net.NetworkPolicyManager.computeLastCycleBoundary; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 47 | import static android.net.NetworkPolicyManager.dumpPolicy; |
| 48 | import static android.net.NetworkPolicyManager.dumpRules; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 49 | import static android.net.NetworkTemplate.MATCH_ETHERNET; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 50 | import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER; |
| 51 | import static android.net.NetworkTemplate.MATCH_MOBILE_4G; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 52 | import static android.net.NetworkTemplate.MATCH_MOBILE_ALL; |
| 53 | import static android.net.NetworkTemplate.MATCH_WIFI; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 54 | import static android.net.NetworkTemplate.buildTemplateMobileAll; |
Jeff Sharkey | 241dde2 | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 55 | import static android.net.TrafficStats.MB_IN_BYTES; |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 56 | import static android.net.wifi.WifiManager.CHANGE_REASON_ADDED; |
| 57 | import static android.net.wifi.WifiManager.CHANGE_REASON_REMOVED; |
| 58 | import static android.net.wifi.WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION; |
| 59 | import static android.net.wifi.WifiManager.EXTRA_CHANGE_REASON; |
| 60 | import static android.net.wifi.WifiManager.EXTRA_NETWORK_INFO; |
| 61 | import static android.net.wifi.WifiManager.EXTRA_WIFI_CONFIGURATION; |
| 62 | import static android.net.wifi.WifiManager.EXTRA_WIFI_INFO; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 63 | import static android.telephony.TelephonyManager.SIM_STATE_READY; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 64 | import static android.text.format.DateUtils.DAY_IN_MILLIS; |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 65 | import static com.android.internal.util.ArrayUtils.appendInt; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 66 | import static com.android.internal.util.Preconditions.checkNotNull; |
Jeff Sharkey | ded7b75 | 2013-03-22 13:43:41 -0700 | [diff] [blame] | 67 | import static com.android.internal.util.XmlUtils.readBooleanAttribute; |
| 68 | import static com.android.internal.util.XmlUtils.readIntAttribute; |
| 69 | import static com.android.internal.util.XmlUtils.readLongAttribute; |
| 70 | import static com.android.internal.util.XmlUtils.writeBooleanAttribute; |
| 71 | import static com.android.internal.util.XmlUtils.writeIntAttribute; |
| 72 | import static com.android.internal.util.XmlUtils.writeLongAttribute; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 73 | import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 74 | import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_UPDATED; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 75 | import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
| 76 | import static org.xmlpull.v1.XmlPullParser.START_TAG; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 77 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 78 | import android.app.IActivityManager; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 79 | import android.app.INotificationManager; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 80 | import android.app.IProcessObserver; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 81 | import android.app.Notification; |
| 82 | import android.app.PendingIntent; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 83 | import android.content.BroadcastReceiver; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 84 | import android.content.ComponentName; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 85 | import android.content.Context; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 86 | import android.content.Intent; |
| 87 | import android.content.IntentFilter; |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 88 | import android.content.pm.ApplicationInfo; |
| 89 | import android.content.pm.PackageManager; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 90 | import android.content.pm.UserInfo; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 91 | import android.content.res.Resources; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 92 | import android.net.ConnectivityManager; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 93 | import android.net.IConnectivityManager; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 94 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 95 | import android.net.INetworkPolicyListener; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 96 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 97 | import android.net.INetworkStatsService; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 98 | import android.net.NetworkIdentity; |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 99 | import android.net.NetworkInfo; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 100 | import android.net.NetworkPolicy; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 101 | import android.net.NetworkQuotaInfo; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 102 | import android.net.NetworkState; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 103 | import android.net.NetworkTemplate; |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 104 | import android.net.wifi.WifiConfiguration; |
| 105 | import android.net.wifi.WifiInfo; |
| 106 | import android.net.wifi.WifiManager; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 107 | import android.os.Binder; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 108 | import android.os.Environment; |
| 109 | import android.os.Handler; |
Amith Yamasani | 450a16b | 2013-09-18 16:28:50 -0700 | [diff] [blame] | 110 | import android.os.HandlerThread; |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 111 | import android.os.INetworkManagementService; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 112 | import android.os.IPowerManager; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 113 | import android.os.Message; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 114 | import android.os.MessageQueue.IdleHandler; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 115 | import android.os.PowerManagerInternal; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 116 | import android.os.RemoteCallbackList; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 117 | import android.os.RemoteException; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 118 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 119 | import android.os.UserManager; |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 120 | import android.provider.Settings; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 121 | import android.telephony.TelephonyManager; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 122 | import android.text.format.Formatter; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 123 | import android.text.format.Time; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 124 | import android.util.ArrayMap; |
| 125 | import android.util.ArraySet; |
Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 126 | import android.util.AtomicFile; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 127 | import android.util.Log; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 128 | import android.util.NtpTrustedTime; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 129 | import android.util.Slog; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 130 | import android.util.SparseArray; |
| 131 | import android.util.SparseBooleanArray; |
| 132 | import android.util.SparseIntArray; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 133 | import android.util.TrustedTime; |
| 134 | import android.util.Xml; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 135 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 136 | import com.android.internal.R; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 137 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 138 | import com.android.internal.util.FastXmlSerializer; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 139 | import com.android.internal.util.IndentingPrintWriter; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 140 | import com.android.server.LocalServices; |
| 141 | import com.android.server.SystemConfig; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 142 | import com.google.android.collect.Lists; |
| 143 | import com.google.android.collect.Maps; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 144 | import com.google.android.collect.Sets; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 145 | |
| 146 | import org.xmlpull.v1.XmlPullParser; |
| 147 | import org.xmlpull.v1.XmlPullParserException; |
| 148 | import org.xmlpull.v1.XmlSerializer; |
| 149 | |
| 150 | import java.io.File; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 151 | import java.io.FileDescriptor; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 152 | import java.io.FileInputStream; |
| 153 | import java.io.FileNotFoundException; |
| 154 | import java.io.FileOutputStream; |
| 155 | import java.io.IOException; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 156 | import java.io.PrintWriter; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 157 | import java.util.ArrayList; |
| 158 | import java.util.Arrays; |
| 159 | import java.util.HashMap; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 160 | import java.util.HashSet; |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 161 | import java.util.List; |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 162 | import java.util.Objects; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 163 | |
| 164 | import libcore.io.IoUtils; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 165 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 166 | /** |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 167 | * Service that maintains low-level network policy rules, using |
| 168 | * {@link NetworkStatsService} statistics to drive those rules. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 169 | * <p> |
| 170 | * Derives active rules by combining a given policy with other system status, |
| 171 | * and delivers to listeners, such as {@link ConnectivityManager}, for |
| 172 | * enforcement. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 173 | */ |
| 174 | public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { |
| 175 | private static final String TAG = "NetworkPolicy"; |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 176 | private static final boolean LOGD = false; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 177 | private static final boolean LOGV = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 178 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 179 | private static final int VERSION_INIT = 1; |
| 180 | private static final int VERSION_ADDED_SNOOZE = 2; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 181 | private static final int VERSION_ADDED_RESTRICT_BACKGROUND = 3; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 182 | private static final int VERSION_ADDED_METERED = 4; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 183 | private static final int VERSION_SPLIT_SNOOZE = 5; |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 184 | private static final int VERSION_ADDED_TIMEZONE = 6; |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 185 | private static final int VERSION_ADDED_INFERRED = 7; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 186 | private static final int VERSION_SWITCH_APP_ID = 8; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 187 | private static final int VERSION_ADDED_NETWORK_ID = 9; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 188 | private static final int VERSION_SWITCH_UID = 10; |
| 189 | private static final int VERSION_LATEST = VERSION_SWITCH_UID; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 190 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 191 | @VisibleForTesting |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 192 | public static final int TYPE_WARNING = 0x1; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 193 | @VisibleForTesting |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 194 | public static final int TYPE_LIMIT = 0x2; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 195 | @VisibleForTesting |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 196 | public static final int TYPE_LIMIT_SNOOZED = 0x3; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 197 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 198 | private static final String TAG_POLICY_LIST = "policy-list"; |
| 199 | private static final String TAG_NETWORK_POLICY = "network-policy"; |
| 200 | private static final String TAG_UID_POLICY = "uid-policy"; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 201 | private static final String TAG_APP_POLICY = "app-policy"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 202 | |
| 203 | private static final String ATTR_VERSION = "version"; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 204 | private static final String ATTR_RESTRICT_BACKGROUND = "restrictBackground"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 205 | private static final String ATTR_NETWORK_TEMPLATE = "networkTemplate"; |
| 206 | private static final String ATTR_SUBSCRIBER_ID = "subscriberId"; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 207 | private static final String ATTR_NETWORK_ID = "networkId"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 208 | private static final String ATTR_CYCLE_DAY = "cycleDay"; |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 209 | private static final String ATTR_CYCLE_TIMEZONE = "cycleTimezone"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 210 | private static final String ATTR_WARNING_BYTES = "warningBytes"; |
| 211 | private static final String ATTR_LIMIT_BYTES = "limitBytes"; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 212 | private static final String ATTR_LAST_SNOOZE = "lastSnooze"; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 213 | private static final String ATTR_LAST_WARNING_SNOOZE = "lastWarningSnooze"; |
| 214 | private static final String ATTR_LAST_LIMIT_SNOOZE = "lastLimitSnooze"; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 215 | private static final String ATTR_METERED = "metered"; |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 216 | private static final String ATTR_INFERRED = "inferred"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 217 | private static final String ATTR_UID = "uid"; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 218 | private static final String ATTR_APP_ID = "appId"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 219 | private static final String ATTR_POLICY = "policy"; |
| 220 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 221 | private static final String TAG_ALLOW_BACKGROUND = TAG + ":allowBackground"; |
| 222 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 223 | private static final String ACTION_ALLOW_BACKGROUND = |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 224 | "com.android.server.net.action.ALLOW_BACKGROUND"; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 225 | private static final String ACTION_SNOOZE_WARNING = |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 226 | "com.android.server.net.action.SNOOZE_WARNING"; |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 227 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 228 | private static final long TIME_CACHE_MAX_AGE = DAY_IN_MILLIS; |
| 229 | |
Jeff Sharkey | 6f7af03 | 2011-11-01 18:25:15 -0700 | [diff] [blame] | 230 | private static final int MSG_RULES_CHANGED = 1; |
| 231 | private static final int MSG_METERED_IFACES_CHANGED = 2; |
| 232 | private static final int MSG_FOREGROUND_ACTIVITIES_CHANGED = 3; |
| 233 | private static final int MSG_PROCESS_DIED = 4; |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 234 | private static final int MSG_LIMIT_REACHED = 5; |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 235 | private static final int MSG_RESTRICT_BACKGROUND_CHANGED = 6; |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 236 | private static final int MSG_ADVISE_PERSIST_THRESHOLD = 7; |
Jeff Sharkey | 0abe556 | 2012-06-19 13:32:22 -0700 | [diff] [blame] | 237 | private static final int MSG_SCREEN_ON_CHANGED = 8; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 238 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 239 | private final Context mContext; |
| 240 | private final IActivityManager mActivityManager; |
| 241 | private final IPowerManager mPowerManager; |
| 242 | private final INetworkStatsService mNetworkStats; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 243 | private final INetworkManagementService mNetworkManager; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 244 | private final TrustedTime mTime; |
| 245 | |
| 246 | private IConnectivityManager mConnManager; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 247 | private INotificationManager mNotifManager; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 248 | private PowerManagerInternal mPowerManagerInternal; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 249 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 250 | private final Object mRulesLock = new Object(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 251 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 252 | private volatile boolean mScreenOn; |
| 253 | private volatile boolean mRestrictBackground; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 254 | private volatile boolean mRestrictPower; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 255 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 256 | private final boolean mSuppressDefaultPolicy; |
| 257 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 258 | /** Defined network policies. */ |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 259 | private final ArrayMap<NetworkTemplate, NetworkPolicy> mNetworkPolicy = new ArrayMap< |
| 260 | NetworkTemplate, NetworkPolicy>(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 261 | /** Currently active network rules for ifaces. */ |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 262 | private final ArrayMap<NetworkPolicy, String[]> mNetworkRules = new ArrayMap< |
| 263 | NetworkPolicy, String[]>(); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 264 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 265 | /** Defined UID policies. */ |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 266 | private final SparseIntArray mUidPolicy = new SparseIntArray(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 267 | /** Currently derived rules for each UID. */ |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 268 | private final SparseIntArray mUidRules = new SparseIntArray(); |
| 269 | |
| 270 | /** UIDs that have been white-listed to always be able to have network access in |
| 271 | * power save mode. */ |
| 272 | private final SparseBooleanArray mPowerSaveWhitelistAppIds = new SparseBooleanArray(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 273 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 274 | /** Set of ifaces that are metered. */ |
| 275 | private HashSet<String> mMeteredIfaces = Sets.newHashSet(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 276 | /** Set of over-limit templates that have been notified. */ |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 277 | private final HashSet<NetworkTemplate> mOverLimitNotified = Sets.newHashSet(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 278 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 279 | /** Set of currently active {@link Notification} tags. */ |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 280 | private final HashSet<String> mActiveNotifs = Sets.newHashSet(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 281 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 282 | /** Foreground at both UID and PID granularity. */ |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 283 | private final SparseBooleanArray mUidForeground = new SparseBooleanArray(); |
| 284 | private final SparseArray<SparseBooleanArray> mUidPidForeground = new SparseArray< |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 285 | SparseBooleanArray>(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 286 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 287 | private final RemoteCallbackList<INetworkPolicyListener> mListeners = new RemoteCallbackList< |
| 288 | INetworkPolicyListener>(); |
| 289 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 290 | private final Handler mHandler; |
| 291 | |
| 292 | private final AtomicFile mPolicyFile; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 293 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 294 | // TODO: keep whitelist of system-critical services that should never have |
| 295 | // rules enforced, such as system, phone, and radio UIDs. |
| 296 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 297 | // TODO: migrate notifications to SystemUI |
| 298 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 299 | public NetworkPolicyManagerService(Context context, IActivityManager activityManager, |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 300 | IPowerManager powerManager, INetworkStatsService networkStats, |
| 301 | INetworkManagementService networkManagement) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 302 | this(context, activityManager, powerManager, networkStats, networkManagement, |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 303 | NtpTrustedTime.getInstance(context), getSystemDir(), false); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | private static File getSystemDir() { |
| 307 | return new File(Environment.getDataDirectory(), "system"); |
| 308 | } |
| 309 | |
| 310 | public NetworkPolicyManagerService(Context context, IActivityManager activityManager, |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 311 | IPowerManager powerManager, INetworkStatsService networkStats, |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 312 | INetworkManagementService networkManagement, TrustedTime time, File systemDir, |
| 313 | boolean suppressDefaultPolicy) { |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 314 | mContext = checkNotNull(context, "missing context"); |
| 315 | mActivityManager = checkNotNull(activityManager, "missing activityManager"); |
| 316 | mPowerManager = checkNotNull(powerManager, "missing powerManager"); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 317 | mNetworkStats = checkNotNull(networkStats, "missing networkStats"); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 318 | mNetworkManager = checkNotNull(networkManagement, "missing networkManagement"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 319 | mTime = checkNotNull(time, "missing TrustedTime"); |
| 320 | |
Amith Yamasani | 450a16b | 2013-09-18 16:28:50 -0700 | [diff] [blame] | 321 | HandlerThread thread = new HandlerThread(TAG); |
| 322 | thread.start(); |
| 323 | mHandler = new Handler(thread.getLooper(), mHandlerCallback); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 324 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 325 | mSuppressDefaultPolicy = suppressDefaultPolicy; |
| 326 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 327 | mPolicyFile = new AtomicFile(new File(systemDir, "netpolicy.xml")); |
| 328 | } |
| 329 | |
| 330 | public void bindConnectivityManager(IConnectivityManager connManager) { |
| 331 | mConnManager = checkNotNull(connManager, "missing IConnectivityManager"); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 332 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 333 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 334 | public void bindNotificationManager(INotificationManager notifManager) { |
| 335 | mNotifManager = checkNotNull(notifManager, "missing INotificationManager"); |
| 336 | } |
| 337 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 338 | public void systemReady() { |
Jeff Sharkey | 8c1dc72 | 2012-05-04 14:49:37 -0700 | [diff] [blame] | 339 | if (!isBandwidthControlEnabled()) { |
| 340 | Slog.w(TAG, "bandwidth controls disabled, unable to enforce policy"); |
| 341 | return; |
| 342 | } |
| 343 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 344 | final PackageManager pm = mContext.getPackageManager(); |
| 345 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 346 | synchronized (mRulesLock) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 347 | SystemConfig sysConfig = SystemConfig.getInstance(); |
| 348 | ArraySet<String> allowPower = sysConfig.getAllowInPowerSave(); |
| 349 | for (int i=0; i<allowPower.size(); i++) { |
| 350 | String pkg = allowPower.valueAt(i); |
| 351 | try { |
| 352 | ApplicationInfo ai = pm.getApplicationInfo(pkg, 0); |
| 353 | if ((ai.flags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 354 | mPowerSaveWhitelistAppIds.put(UserHandle.getAppId(ai.uid), true); |
| 355 | } |
| 356 | } catch (PackageManager.NameNotFoundException e) { |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class); |
| 361 | mPowerManagerInternal.registerLowPowerModeObserver( |
| 362 | new PowerManagerInternal.LowPowerModeListener() { |
| 363 | @Override |
| 364 | public void onLowPowerModeChanged(boolean enabled) { |
| 365 | synchronized (mRulesLock) { |
| 366 | if (mRestrictPower != enabled) { |
| 367 | mRestrictPower = enabled; |
| 368 | updateRulesForGlobalChangeLocked(true); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | }); |
| 373 | mRestrictPower = mPowerManagerInternal.getLowPowerModeEnabled(); |
| 374 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 375 | // read policy from disk |
| 376 | readPolicyLocked(); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 377 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 378 | if (mRestrictBackground || mRestrictPower) { |
| 379 | updateRulesForGlobalChangeLocked(true); |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 380 | updateNotificationsLocked(); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 381 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 382 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 383 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 384 | updateScreenOn(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 385 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 386 | try { |
| 387 | mActivityManager.registerProcessObserver(mProcessObserver); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 388 | mNetworkManager.registerObserver(mAlertObserver); |
| 389 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 390 | // ignored; both services live in system_server |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 393 | // TODO: traverse existing processes to know foreground state, or have |
| 394 | // activitymanager dispatch current state when new observer attached. |
| 395 | |
| 396 | final IntentFilter screenFilter = new IntentFilter(); |
| 397 | screenFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 398 | screenFilter.addAction(Intent.ACTION_SCREEN_OFF); |
Jeff Sharkey | 0abe556 | 2012-06-19 13:32:22 -0700 | [diff] [blame] | 399 | mContext.registerReceiver(mScreenReceiver, screenFilter); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 400 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 401 | // watch for network interfaces to be claimed |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 402 | final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION_IMMEDIATE); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 403 | mContext.registerReceiver(mConnReceiver, connFilter, CONNECTIVITY_INTERNAL, mHandler); |
| 404 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 405 | // listen for package changes to update policy |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 406 | final IntentFilter packageFilter = new IntentFilter(); |
| 407 | packageFilter.addAction(ACTION_PACKAGE_ADDED); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 408 | packageFilter.addDataScheme("package"); |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 409 | mContext.registerReceiver(mPackageReceiver, packageFilter, null, mHandler); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 410 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 411 | // listen for UID changes to update policy |
| 412 | mContext.registerReceiver( |
| 413 | mUidRemovedReceiver, new IntentFilter(ACTION_UID_REMOVED), null, mHandler); |
| 414 | |
| 415 | // listen for user changes to update policy |
| 416 | final IntentFilter userFilter = new IntentFilter(); |
| 417 | userFilter.addAction(ACTION_USER_ADDED); |
| 418 | userFilter.addAction(ACTION_USER_REMOVED); |
| 419 | mContext.registerReceiver(mUserReceiver, userFilter, null, mHandler); |
| 420 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 421 | // listen for stats update events |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 422 | final IntentFilter statsFilter = new IntentFilter(ACTION_NETWORK_STATS_UPDATED); |
| 423 | mContext.registerReceiver( |
| 424 | mStatsReceiver, statsFilter, READ_NETWORK_USAGE_HISTORY, mHandler); |
| 425 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 426 | // listen for restrict background changes from notifications |
| 427 | final IntentFilter allowFilter = new IntentFilter(ACTION_ALLOW_BACKGROUND); |
| 428 | mContext.registerReceiver(mAllowReceiver, allowFilter, MANAGE_NETWORK_POLICY, mHandler); |
| 429 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 430 | // listen for snooze warning from notifications |
| 431 | final IntentFilter snoozeWarningFilter = new IntentFilter(ACTION_SNOOZE_WARNING); |
| 432 | mContext.registerReceiver(mSnoozeWarningReceiver, snoozeWarningFilter, |
| 433 | MANAGE_NETWORK_POLICY, mHandler); |
| 434 | |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 435 | // listen for configured wifi networks to be removed |
| 436 | final IntentFilter wifiConfigFilter = new IntentFilter(CONFIGURED_NETWORKS_CHANGED_ACTION); |
| 437 | mContext.registerReceiver( |
| 438 | mWifiConfigReceiver, wifiConfigFilter, CONNECTIVITY_INTERNAL, mHandler); |
| 439 | |
| 440 | // listen for wifi state changes to catch metered hint |
| 441 | final IntentFilter wifiStateFilter = new IntentFilter( |
| 442 | WifiManager.NETWORK_STATE_CHANGED_ACTION); |
| 443 | mContext.registerReceiver( |
| 444 | mWifiStateReceiver, wifiStateFilter, CONNECTIVITY_INTERNAL, mHandler); |
| 445 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 448 | private IProcessObserver mProcessObserver = new IProcessObserver.Stub() { |
| 449 | @Override |
| 450 | public void onForegroundActivitiesChanged(int pid, int uid, boolean foregroundActivities) { |
Jeff Sharkey | 6f7af03 | 2011-11-01 18:25:15 -0700 | [diff] [blame] | 451 | mHandler.obtainMessage(MSG_FOREGROUND_ACTIVITIES_CHANGED, |
| 452 | pid, uid, foregroundActivities).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 453 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 454 | |
| 455 | @Override |
Dianne Hackborn | 684bf34 | 2014-04-29 17:56:57 -0700 | [diff] [blame] | 456 | public void onProcessStateChanged(int pid, int uid, int procState) { |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | @Override |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 460 | public void onProcessDied(int pid, int uid) { |
Jeff Sharkey | 6f7af03 | 2011-11-01 18:25:15 -0700 | [diff] [blame] | 461 | mHandler.obtainMessage(MSG_PROCESS_DIED, pid, uid).sendToTarget(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 462 | } |
| 463 | }; |
| 464 | |
| 465 | private BroadcastReceiver mScreenReceiver = new BroadcastReceiver() { |
| 466 | @Override |
| 467 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | 29afa14 | 2012-12-04 17:21:21 -0800 | [diff] [blame] | 468 | // screen-related broadcasts are protected by system, no need |
| 469 | // for permissions check. |
| 470 | mHandler.obtainMessage(MSG_SCREEN_ON_CHANGED).sendToTarget(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 471 | } |
| 472 | }; |
| 473 | |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 474 | private BroadcastReceiver mPackageReceiver = new BroadcastReceiver() { |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 475 | @Override |
| 476 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 477 | // on background handler thread, and PACKAGE_ADDED is protected |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 478 | |
| 479 | final String action = intent.getAction(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 480 | final int uid = intent.getIntExtra(EXTRA_UID, -1); |
| 481 | if (uid == -1) return; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 482 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 483 | if (ACTION_PACKAGE_ADDED.equals(action)) { |
| 484 | // update rules for UID, since it might be subject to |
| 485 | // global background data policy |
| 486 | if (LOGV) Slog.v(TAG, "ACTION_PACKAGE_ADDED for uid=" + uid); |
| 487 | synchronized (mRulesLock) { |
| 488 | updateRulesForUidLocked(uid); |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 489 | } |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | }; |
| 493 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 494 | private BroadcastReceiver mUidRemovedReceiver = new BroadcastReceiver() { |
| 495 | @Override |
| 496 | public void onReceive(Context context, Intent intent) { |
| 497 | // on background handler thread, and UID_REMOVED is protected |
| 498 | |
| 499 | final int uid = intent.getIntExtra(EXTRA_UID, -1); |
| 500 | if (uid == -1) return; |
| 501 | |
| 502 | // remove any policy and update rules to clean up |
| 503 | if (LOGV) Slog.v(TAG, "ACTION_UID_REMOVED for uid=" + uid); |
| 504 | synchronized (mRulesLock) { |
| 505 | mUidPolicy.delete(uid); |
| 506 | updateRulesForUidLocked(uid); |
| 507 | writePolicyLocked(); |
| 508 | } |
| 509 | } |
| 510 | }; |
| 511 | |
| 512 | private BroadcastReceiver mUserReceiver = new BroadcastReceiver() { |
| 513 | @Override |
| 514 | public void onReceive(Context context, Intent intent) { |
| 515 | // on background handler thread, and USER_ADDED and USER_REMOVED |
| 516 | // broadcasts are protected |
| 517 | |
| 518 | final String action = intent.getAction(); |
| 519 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 520 | if (userId == -1) return; |
| 521 | |
| 522 | // Remove any policies for given user; both cleaning up after a |
| 523 | // USER_REMOVED, and one last sanity check during USER_ADDED |
| 524 | removePoliciesForUserLocked(userId); |
| 525 | |
| 526 | // Update global restrict for new user |
| 527 | synchronized (mRulesLock) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 528 | updateRulesForGlobalChangeLocked(true); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | }; |
| 532 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 533 | /** |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 534 | * Receiver that watches for {@link INetworkStatsService} updates, which we |
| 535 | * use to check against {@link NetworkPolicy#warningBytes}. |
| 536 | */ |
| 537 | private BroadcastReceiver mStatsReceiver = new BroadcastReceiver() { |
| 538 | @Override |
| 539 | public void onReceive(Context context, Intent intent) { |
| 540 | // on background handler thread, and verified |
| 541 | // READ_NETWORK_USAGE_HISTORY permission above. |
| 542 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 543 | maybeRefreshTrustedTime(); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 544 | synchronized (mRulesLock) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 545 | updateNetworkEnabledLocked(); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 546 | updateNotificationsLocked(); |
| 547 | } |
| 548 | } |
| 549 | }; |
| 550 | |
| 551 | /** |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 552 | * Receiver that watches for {@link Notification} control of |
| 553 | * {@link #mRestrictBackground}. |
| 554 | */ |
| 555 | private BroadcastReceiver mAllowReceiver = new BroadcastReceiver() { |
| 556 | @Override |
| 557 | public void onReceive(Context context, Intent intent) { |
| 558 | // on background handler thread, and verified MANAGE_NETWORK_POLICY |
| 559 | // permission above. |
| 560 | |
| 561 | setRestrictBackground(false); |
| 562 | } |
| 563 | }; |
| 564 | |
| 565 | /** |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 566 | * Receiver that watches for {@link Notification} control of |
| 567 | * {@link NetworkPolicy#lastWarningSnooze}. |
| 568 | */ |
| 569 | private BroadcastReceiver mSnoozeWarningReceiver = new BroadcastReceiver() { |
| 570 | @Override |
| 571 | public void onReceive(Context context, Intent intent) { |
| 572 | // on background handler thread, and verified MANAGE_NETWORK_POLICY |
| 573 | // permission above. |
| 574 | |
| 575 | final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE); |
| 576 | performSnooze(template, TYPE_WARNING); |
| 577 | } |
| 578 | }; |
| 579 | |
| 580 | /** |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 581 | * Receiver that watches for {@link WifiConfiguration} to be changed. |
| 582 | */ |
| 583 | private BroadcastReceiver mWifiConfigReceiver = new BroadcastReceiver() { |
| 584 | @Override |
| 585 | public void onReceive(Context context, Intent intent) { |
| 586 | // on background handler thread, and verified CONNECTIVITY_INTERNAL |
| 587 | // permission above. |
| 588 | |
| 589 | final int reason = intent.getIntExtra(EXTRA_CHANGE_REASON, CHANGE_REASON_ADDED); |
| 590 | if (reason == CHANGE_REASON_REMOVED) { |
| 591 | final WifiConfiguration config = intent.getParcelableExtra( |
| 592 | EXTRA_WIFI_CONFIGURATION); |
Irfan Sheriff | 00a10a1 | 2012-04-27 21:24:17 -0700 | [diff] [blame] | 593 | if (config.SSID != null) { |
Jeff Sharkey | 2e4dce0 | 2012-12-18 17:06:06 -0800 | [diff] [blame] | 594 | final NetworkTemplate template = NetworkTemplate.buildTemplateWifi(config.SSID); |
Irfan Sheriff | 00a10a1 | 2012-04-27 21:24:17 -0700 | [diff] [blame] | 595 | synchronized (mRulesLock) { |
| 596 | if (mNetworkPolicy.containsKey(template)) { |
| 597 | mNetworkPolicy.remove(template); |
| 598 | writePolicyLocked(); |
| 599 | } |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | }; |
| 605 | |
| 606 | /** |
| 607 | * Receiver that watches {@link WifiInfo} state changes to infer metered |
| 608 | * state. Ignores hints when policy is user-defined. |
| 609 | */ |
| 610 | private BroadcastReceiver mWifiStateReceiver = new BroadcastReceiver() { |
| 611 | @Override |
| 612 | public void onReceive(Context context, Intent intent) { |
| 613 | // on background handler thread, and verified CONNECTIVITY_INTERNAL |
| 614 | // permission above. |
| 615 | |
| 616 | // ignore when not connected |
| 617 | final NetworkInfo netInfo = intent.getParcelableExtra(EXTRA_NETWORK_INFO); |
| 618 | if (!netInfo.isConnected()) return; |
| 619 | |
| 620 | final WifiInfo info = intent.getParcelableExtra(EXTRA_WIFI_INFO); |
| 621 | final boolean meteredHint = info.getMeteredHint(); |
| 622 | |
Jeff Sharkey | 2e4dce0 | 2012-12-18 17:06:06 -0800 | [diff] [blame] | 623 | final NetworkTemplate template = NetworkTemplate.buildTemplateWifi(info.getSSID()); |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 624 | synchronized (mRulesLock) { |
| 625 | NetworkPolicy policy = mNetworkPolicy.get(template); |
| 626 | if (policy == null && meteredHint) { |
| 627 | // policy doesn't exist, and AP is hinting that it's |
| 628 | // metered: create an inferred policy. |
| 629 | policy = new NetworkPolicy(template, CYCLE_NONE, Time.TIMEZONE_UTC, |
| 630 | WARNING_DISABLED, LIMIT_DISABLED, SNOOZE_NEVER, SNOOZE_NEVER, |
| 631 | meteredHint, true); |
| 632 | addNetworkPolicyLocked(policy); |
| 633 | |
| 634 | } else if (policy != null && policy.inferred) { |
| 635 | // policy exists, and was inferred: update its current |
| 636 | // metered state. |
| 637 | policy.metered = meteredHint; |
| 638 | |
| 639 | // since this is inferred for each wifi session, just update |
| 640 | // rules without persisting. |
| 641 | updateNetworkRulesLocked(); |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | }; |
| 646 | |
| 647 | /** |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 648 | * Observer that watches for {@link INetworkManagementService} alerts. |
| 649 | */ |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 650 | private INetworkManagementEventObserver mAlertObserver = new BaseNetworkObserver() { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 651 | @Override |
| 652 | public void limitReached(String limitName, String iface) { |
| 653 | // only someone like NMS should be calling us |
| 654 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 655 | |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 656 | if (!LIMIT_GLOBAL_ALERT.equals(limitName)) { |
| 657 | mHandler.obtainMessage(MSG_LIMIT_REACHED, iface).sendToTarget(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | }; |
| 661 | |
| 662 | /** |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 663 | * Check {@link NetworkPolicy} against current {@link INetworkStatsService} |
| 664 | * to show visible notifications as needed. |
| 665 | */ |
| 666 | private void updateNotificationsLocked() { |
| 667 | if (LOGV) Slog.v(TAG, "updateNotificationsLocked()"); |
| 668 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 669 | // keep track of previously active notifications |
| 670 | final HashSet<String> beforeNotifs = Sets.newHashSet(); |
| 671 | beforeNotifs.addAll(mActiveNotifs); |
| 672 | mActiveNotifs.clear(); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 673 | |
| 674 | // TODO: when switching to kernel notifications, compute next future |
| 675 | // cycle boundary to recompute notifications. |
| 676 | |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 677 | // examine stats for each active policy |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 678 | final long currentTime = currentTimeMillis(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 679 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 680 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 681 | // ignore policies that aren't relevant to user |
| 682 | if (!isTemplateRelevant(policy.template)) continue; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 683 | if (!policy.hasCycle()) continue; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 684 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 685 | final long start = computeLastCycleBoundary(currentTime, policy); |
| 686 | final long end = currentTime; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 687 | final long totalBytes = getTotalBytes(policy.template, start, end); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 688 | |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 689 | if (policy.isOverLimit(totalBytes)) { |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 690 | if (policy.lastLimitSnooze >= start) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 691 | enqueueNotification(policy, TYPE_LIMIT_SNOOZED, totalBytes); |
| 692 | } else { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 693 | enqueueNotification(policy, TYPE_LIMIT, totalBytes); |
| 694 | notifyOverLimitLocked(policy.template); |
| 695 | } |
| 696 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 697 | } else { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 698 | notifyUnderLimitLocked(policy.template); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 699 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 700 | if (policy.isOverWarning(totalBytes) && policy.lastWarningSnooze < start) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 701 | enqueueNotification(policy, TYPE_WARNING, totalBytes); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 702 | } |
| 703 | } |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 706 | // ongoing notification when restricting background data |
| 707 | if (mRestrictBackground) { |
| 708 | enqueueRestrictedNotification(TAG_ALLOW_BACKGROUND); |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 709 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 710 | |
| 711 | // cancel stale notifications that we didn't renew above |
| 712 | for (String tag : beforeNotifs) { |
| 713 | if (!mActiveNotifs.contains(tag)) { |
| 714 | cancelNotification(tag); |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * Test if given {@link NetworkTemplate} is relevant to user based on |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 721 | * current device state, such as when |
| 722 | * {@link TelephonyManager#getSubscriberId()} matches. This is regardless of |
| 723 | * data connection status. |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 724 | */ |
| 725 | private boolean isTemplateRelevant(NetworkTemplate template) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 726 | final TelephonyManager tele = TelephonyManager.from(mContext); |
| 727 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 728 | switch (template.getMatchRule()) { |
| 729 | case MATCH_MOBILE_3G_LOWER: |
| 730 | case MATCH_MOBILE_4G: |
| 731 | case MATCH_MOBILE_ALL: |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 732 | // mobile templates are relevant when SIM is ready and |
| 733 | // subscriberId matches. |
| 734 | if (tele.getSimState() == SIM_STATE_READY) { |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 735 | return Objects.equals(tele.getSubscriberId(), template.getSubscriberId()); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 736 | } else { |
Jeff Sharkey | 3a66cf3 | 2012-03-20 17:00:01 -0700 | [diff] [blame] | 737 | return false; |
| 738 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 739 | } |
| 740 | return true; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | /** |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 744 | * Notify that given {@link NetworkTemplate} is over |
| 745 | * {@link NetworkPolicy#limitBytes}, potentially showing dialog to user. |
| 746 | */ |
| 747 | private void notifyOverLimitLocked(NetworkTemplate template) { |
| 748 | if (!mOverLimitNotified.contains(template)) { |
| 749 | mContext.startActivity(buildNetworkOverLimitIntent(template)); |
| 750 | mOverLimitNotified.add(template); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | private void notifyUnderLimitLocked(NetworkTemplate template) { |
| 755 | mOverLimitNotified.remove(template); |
| 756 | } |
| 757 | |
| 758 | /** |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 759 | * Build unique tag that identifies an active {@link NetworkPolicy} |
| 760 | * notification of a specific type, like {@link #TYPE_LIMIT}. |
| 761 | */ |
| 762 | private String buildNotificationTag(NetworkPolicy policy, int type) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 763 | return TAG + ":" + policy.template.hashCode() + ":" + type; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | /** |
| 767 | * Show notification for combined {@link NetworkPolicy} and specific type, |
| 768 | * like {@link #TYPE_LIMIT}. Okay to call multiple times. |
| 769 | */ |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 770 | private void enqueueNotification(NetworkPolicy policy, int type, long totalBytes) { |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 771 | final String tag = buildNotificationTag(policy, type); |
| 772 | final Notification.Builder builder = new Notification.Builder(mContext); |
| 773 | builder.setOnlyAlertOnce(true); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 774 | builder.setWhen(0L); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 775 | |
| 776 | final Resources res = mContext.getResources(); |
| 777 | switch (type) { |
| 778 | case TYPE_WARNING: { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 779 | final CharSequence title = res.getText(R.string.data_usage_warning_title); |
Jeff Sharkey | 8ca953d | 2011-09-14 19:56:11 -0700 | [diff] [blame] | 780 | final CharSequence body = res.getString(R.string.data_usage_warning_body); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 781 | |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 782 | builder.setSmallIcon(R.drawable.stat_notify_error); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 783 | builder.setTicker(title); |
| 784 | builder.setContentTitle(title); |
| 785 | builder.setContentText(body); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 786 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 787 | final Intent snoozeIntent = buildSnoozeWarningIntent(policy.template); |
| 788 | builder.setDeleteIntent(PendingIntent.getBroadcast( |
| 789 | mContext, 0, snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 790 | |
| 791 | final Intent viewIntent = buildViewDataUsageIntent(policy.template); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 792 | builder.setContentIntent(PendingIntent.getActivity( |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 793 | mContext, 0, viewIntent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 794 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 795 | break; |
| 796 | } |
| 797 | case TYPE_LIMIT: { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 798 | final CharSequence body = res.getText(R.string.data_usage_limit_body); |
| 799 | |
| 800 | final CharSequence title; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 801 | switch (policy.template.getMatchRule()) { |
| 802 | case MATCH_MOBILE_3G_LOWER: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 803 | title = res.getText(R.string.data_usage_3g_limit_title); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 804 | break; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 805 | case MATCH_MOBILE_4G: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 806 | title = res.getText(R.string.data_usage_4g_limit_title); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 807 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 808 | case MATCH_MOBILE_ALL: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 809 | title = res.getText(R.string.data_usage_mobile_limit_title); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 810 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 811 | case MATCH_WIFI: |
| 812 | title = res.getText(R.string.data_usage_wifi_limit_title); |
| 813 | break; |
| 814 | default: |
| 815 | title = null; |
| 816 | break; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 819 | builder.setOngoing(true); |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 820 | builder.setSmallIcon(R.drawable.stat_notify_disabled); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 821 | builder.setTicker(title); |
| 822 | builder.setContentTitle(title); |
| 823 | builder.setContentText(body); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 824 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 825 | final Intent intent = buildNetworkOverLimitIntent(policy.template); |
| 826 | builder.setContentIntent(PendingIntent.getActivity( |
| 827 | mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 828 | break; |
| 829 | } |
| 830 | case TYPE_LIMIT_SNOOZED: { |
| 831 | final long overBytes = totalBytes - policy.limitBytes; |
| 832 | final CharSequence body = res.getString(R.string.data_usage_limit_snoozed_body, |
| 833 | Formatter.formatFileSize(mContext, overBytes)); |
| 834 | |
| 835 | final CharSequence title; |
| 836 | switch (policy.template.getMatchRule()) { |
| 837 | case MATCH_MOBILE_3G_LOWER: |
| 838 | title = res.getText(R.string.data_usage_3g_limit_snoozed_title); |
| 839 | break; |
| 840 | case MATCH_MOBILE_4G: |
| 841 | title = res.getText(R.string.data_usage_4g_limit_snoozed_title); |
| 842 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 843 | case MATCH_MOBILE_ALL: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 844 | title = res.getText(R.string.data_usage_mobile_limit_snoozed_title); |
| 845 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 846 | case MATCH_WIFI: |
| 847 | title = res.getText(R.string.data_usage_wifi_limit_snoozed_title); |
| 848 | break; |
| 849 | default: |
| 850 | title = null; |
| 851 | break; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 854 | builder.setOngoing(true); |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 855 | builder.setSmallIcon(R.drawable.stat_notify_error); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 856 | builder.setTicker(title); |
| 857 | builder.setContentTitle(title); |
| 858 | builder.setContentText(body); |
| 859 | |
| 860 | final Intent intent = buildViewDataUsageIntent(policy.template); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 861 | builder.setContentIntent(PendingIntent.getActivity( |
| 862 | mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 863 | break; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | // TODO: move to NotificationManager once we can mock it |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 868 | // XXX what to do about multi-user? |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 869 | try { |
| 870 | final String packageName = mContext.getPackageName(); |
| 871 | final int[] idReceived = new int[1]; |
| 872 | mNotifManager.enqueueNotificationWithTag( |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 873 | packageName, packageName, tag, 0x0, builder.getNotification(), idReceived, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 874 | UserHandle.USER_OWNER); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 875 | mActiveNotifs.add(tag); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 876 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 877 | // ignored; service lives in system_server |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | |
| 881 | /** |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 882 | * Show ongoing notification to reflect that {@link #mRestrictBackground} |
| 883 | * has been enabled. |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 884 | */ |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 885 | private void enqueueRestrictedNotification(String tag) { |
| 886 | final Resources res = mContext.getResources(); |
| 887 | final Notification.Builder builder = new Notification.Builder(mContext); |
| 888 | |
| 889 | final CharSequence title = res.getText(R.string.data_usage_restricted_title); |
| 890 | final CharSequence body = res.getString(R.string.data_usage_restricted_body); |
| 891 | |
| 892 | builder.setOnlyAlertOnce(true); |
| 893 | builder.setOngoing(true); |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 894 | builder.setSmallIcon(R.drawable.stat_notify_error); |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 895 | builder.setTicker(title); |
| 896 | builder.setContentTitle(title); |
| 897 | builder.setContentText(body); |
| 898 | |
| 899 | final Intent intent = buildAllowBackgroundDataIntent(); |
| 900 | builder.setContentIntent( |
| 901 | PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 902 | |
| 903 | // TODO: move to NotificationManager once we can mock it |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 904 | // XXX what to do about multi-user? |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 905 | try { |
| 906 | final String packageName = mContext.getPackageName(); |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 907 | final int[] idReceived = new int[1]; |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 908 | mNotifManager.enqueueNotificationWithTag(packageName, packageName, tag, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 909 | 0x0, builder.getNotification(), idReceived, UserHandle.USER_OWNER); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 910 | mActiveNotifs.add(tag); |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 911 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 912 | // ignored; service lives in system_server |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 916 | private void cancelNotification(String tag) { |
| 917 | // TODO: move to NotificationManager once we can mock it |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 918 | // XXX what to do about multi-user? |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 919 | try { |
| 920 | final String packageName = mContext.getPackageName(); |
| 921 | mNotifManager.cancelNotificationWithTag( |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 922 | packageName, tag, 0x0, UserHandle.USER_OWNER); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 923 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 924 | // ignored; service lives in system_server |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
| 928 | /** |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 929 | * Receiver that watches for {@link IConnectivityManager} to claim network |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 930 | * interfaces. Used to apply {@link NetworkPolicy} to matching networks. |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 931 | */ |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 932 | private BroadcastReceiver mConnReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 933 | @Override |
| 934 | public void onReceive(Context context, Intent intent) { |
| 935 | // on background handler thread, and verified CONNECTIVITY_INTERNAL |
| 936 | // permission above. |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 937 | |
| 938 | maybeRefreshTrustedTime(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 939 | synchronized (mRulesLock) { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 940 | ensureActiveMobilePolicyLocked(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 941 | updateNetworkEnabledLocked(); |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 942 | updateNetworkRulesLocked(); |
| 943 | updateNotificationsLocked(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | }; |
| 947 | |
| 948 | /** |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 949 | * Proactively control network data connections when they exceed |
| 950 | * {@link NetworkPolicy#limitBytes}. |
| 951 | */ |
| 952 | private void updateNetworkEnabledLocked() { |
| 953 | if (LOGV) Slog.v(TAG, "updateNetworkEnabledLocked()"); |
| 954 | |
| 955 | // TODO: reset any policy-disabled networks when any policy is removed |
| 956 | // completely, which is currently rare case. |
| 957 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 958 | final long currentTime = currentTimeMillis(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 959 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 960 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 961 | // shortcut when policy has no limit |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 962 | if (policy.limitBytes == LIMIT_DISABLED || !policy.hasCycle()) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 963 | setNetworkTemplateEnabled(policy.template, true); |
| 964 | continue; |
| 965 | } |
| 966 | |
| 967 | final long start = computeLastCycleBoundary(currentTime, policy); |
| 968 | final long end = currentTime; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 969 | final long totalBytes = getTotalBytes(policy.template, start, end); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 970 | |
| 971 | // disable data connection when over limit and not snoozed |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 972 | final boolean overLimitWithoutSnooze = policy.isOverLimit(totalBytes) |
| 973 | && policy.lastLimitSnooze < start; |
| 974 | final boolean networkEnabled = !overLimitWithoutSnooze; |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 975 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 976 | setNetworkTemplateEnabled(policy.template, networkEnabled); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
| 980 | /** |
| 981 | * Control {@link IConnectivityManager#setPolicyDataEnable(int, boolean)} |
| 982 | * for the given {@link NetworkTemplate}. |
| 983 | */ |
| 984 | private void setNetworkTemplateEnabled(NetworkTemplate template, boolean enabled) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 985 | final TelephonyManager tele = TelephonyManager.from(mContext); |
| 986 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 987 | switch (template.getMatchRule()) { |
| 988 | case MATCH_MOBILE_3G_LOWER: |
| 989 | case MATCH_MOBILE_4G: |
| 990 | case MATCH_MOBILE_ALL: |
| 991 | // TODO: offer more granular control over radio states once |
| 992 | // 4965893 is available. |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 993 | if (tele.getSimState() == SIM_STATE_READY |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 994 | && Objects.equals(tele.getSubscriberId(), template.getSubscriberId())) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 995 | setPolicyDataEnable(TYPE_MOBILE, enabled); |
| 996 | setPolicyDataEnable(TYPE_WIMAX, enabled); |
| 997 | } |
| 998 | break; |
| 999 | case MATCH_WIFI: |
| 1000 | setPolicyDataEnable(TYPE_WIFI, enabled); |
| 1001 | break; |
| 1002 | case MATCH_ETHERNET: |
| 1003 | setPolicyDataEnable(TYPE_ETHERNET, enabled); |
| 1004 | break; |
| 1005 | default: |
| 1006 | throw new IllegalArgumentException("unexpected template"); |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | /** |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1011 | * Examine all connected {@link NetworkState}, looking for |
| 1012 | * {@link NetworkPolicy} that need to be enforced. When matches found, set |
| 1013 | * remaining quota based on usage cycle and historical stats. |
| 1014 | */ |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 1015 | private void updateNetworkRulesLocked() { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1016 | if (LOGV) Slog.v(TAG, "updateIfacesLocked()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1017 | |
| 1018 | final NetworkState[] states; |
| 1019 | try { |
| 1020 | states = mConnManager.getAllNetworkState(); |
| 1021 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1022 | // ignored; service lives in system_server |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1023 | return; |
| 1024 | } |
| 1025 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1026 | // If we are in restrict power mode, we want to treat all interfaces |
| 1027 | // as metered, to restrict access to the network by uid. However, we |
| 1028 | // will not have a bandwidth limit. Also only do this if restrict |
| 1029 | // background data use is *not* enabled, since that takes precendence |
| 1030 | // use over those networks can have a cost associated with it). |
| 1031 | final boolean powerSave = mRestrictPower && !mRestrictBackground; |
| 1032 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1033 | // first, derive identity for all connected networks, which can be used |
| 1034 | // to match against templates. |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1035 | final ArrayMap<NetworkIdentity, String> networks = new ArrayMap(); |
| 1036 | final ArraySet<String> connIfaces = new ArraySet<String>(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1037 | for (NetworkState state : states) { |
| 1038 | // stash identity and iface away for later use |
| 1039 | if (state.networkInfo.isConnected()) { |
| 1040 | final String iface = state.linkProperties.getInterfaceName(); |
| 1041 | final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state); |
| 1042 | networks.put(ident, iface); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1043 | if (powerSave) { |
| 1044 | connIfaces.add(iface); |
| 1045 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | // build list of rules and ifaces to enforce them against |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 1050 | mNetworkRules.clear(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1051 | final ArrayList<String> ifaceList = Lists.newArrayList(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1052 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 1053 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1054 | |
| 1055 | // collect all active ifaces that match this template |
| 1056 | ifaceList.clear(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1057 | for (int j = networks.size()-1; j >= 0; j--) { |
| 1058 | final NetworkIdentity ident = networks.keyAt(j); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1059 | if (policy.template.matches(ident)) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1060 | final String iface = networks.valueAt(j); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1061 | ifaceList.add(iface); |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | if (ifaceList.size() > 0) { |
| 1066 | final String[] ifaces = ifaceList.toArray(new String[ifaceList.size()]); |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 1067 | mNetworkRules.put(policy, ifaces); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1068 | } |
| 1069 | } |
| 1070 | |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1071 | long lowestRule = Long.MAX_VALUE; |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1072 | final HashSet<String> newMeteredIfaces = Sets.newHashSet(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1073 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1074 | // apply each policy that we found ifaces for; compute remaining data |
| 1075 | // based on current cycle and historical stats, and push to kernel. |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 1076 | final long currentTime = currentTimeMillis(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1077 | for (int i = mNetworkRules.size()-1; i >= 0; i--) { |
| 1078 | final NetworkPolicy policy = mNetworkRules.keyAt(i); |
| 1079 | final String[] ifaces = mNetworkRules.valueAt(i); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1080 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1081 | final long start; |
| 1082 | final long totalBytes; |
| 1083 | if (policy.hasCycle()) { |
| 1084 | start = computeLastCycleBoundary(currentTime, policy); |
| 1085 | totalBytes = getTotalBytes(policy.template, start, currentTime); |
| 1086 | } else { |
| 1087 | start = Long.MAX_VALUE; |
| 1088 | totalBytes = 0; |
| 1089 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1090 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1091 | if (LOGD) { |
| 1092 | Slog.d(TAG, "applying policy " + policy.toString() + " to ifaces " |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1093 | + Arrays.toString(ifaces)); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1096 | final boolean hasWarning = policy.warningBytes != LIMIT_DISABLED; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1097 | final boolean hasLimit = policy.limitBytes != LIMIT_DISABLED; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1098 | if (hasLimit || policy.metered) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1099 | final long quotaBytes; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1100 | if (!hasLimit) { |
| 1101 | // metered network, but no policy limit; we still need to |
| 1102 | // restrict apps, so push really high quota. |
| 1103 | quotaBytes = Long.MAX_VALUE; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1104 | } else if (policy.lastLimitSnooze >= start) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1105 | // snoozing past quota, but we still need to restrict apps, |
| 1106 | // so push really high quota. |
| 1107 | quotaBytes = Long.MAX_VALUE; |
| 1108 | } else { |
| 1109 | // remaining "quota" bytes are based on total usage in |
| 1110 | // current cycle. kernel doesn't like 0-byte rules, so we |
| 1111 | // set 1-byte quota and disable the radio later. |
| 1112 | quotaBytes = Math.max(1, policy.limitBytes - totalBytes); |
| 1113 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1114 | |
| 1115 | if (ifaces.length > 1) { |
| 1116 | // TODO: switch to shared quota once NMS supports |
| 1117 | Slog.w(TAG, "shared quota unsupported; generating rule for each iface"); |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1120 | for (String iface : ifaces) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1121 | removeInterfaceQuota(iface); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1122 | setInterfaceQuota(iface, quotaBytes); |
| 1123 | newMeteredIfaces.add(iface); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1124 | if (powerSave) { |
| 1125 | connIfaces.remove(iface); |
| 1126 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1127 | } |
| 1128 | } |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1129 | |
| 1130 | // keep track of lowest warning or limit of active policies |
| 1131 | if (hasWarning && policy.warningBytes < lowestRule) { |
| 1132 | lowestRule = policy.warningBytes; |
| 1133 | } |
| 1134 | if (hasLimit && policy.limitBytes < lowestRule) { |
| 1135 | lowestRule = policy.limitBytes; |
| 1136 | } |
| 1137 | } |
| 1138 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1139 | for (int i = connIfaces.size()-1; i >= 0; i--) { |
| 1140 | String iface = connIfaces.valueAt(i); |
| 1141 | removeInterfaceQuota(iface); |
| 1142 | setInterfaceQuota(iface, Long.MAX_VALUE); |
| 1143 | newMeteredIfaces.add(iface); |
| 1144 | } |
| 1145 | |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 1146 | mHandler.obtainMessage(MSG_ADVISE_PERSIST_THRESHOLD, lowestRule).sendToTarget(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1147 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1148 | // remove quota on any trailing interfaces |
| 1149 | for (String iface : mMeteredIfaces) { |
| 1150 | if (!newMeteredIfaces.contains(iface)) { |
| 1151 | removeInterfaceQuota(iface); |
| 1152 | } |
| 1153 | } |
| 1154 | mMeteredIfaces = newMeteredIfaces; |
| 1155 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1156 | final String[] meteredIfaces = mMeteredIfaces.toArray(new String[mMeteredIfaces.size()]); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1157 | mHandler.obtainMessage(MSG_METERED_IFACES_CHANGED, meteredIfaces).sendToTarget(); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | /** |
| 1161 | * Once any {@link #mNetworkPolicy} are loaded from disk, ensure that we |
| 1162 | * have at least a default mobile policy defined. |
| 1163 | */ |
| 1164 | private void ensureActiveMobilePolicyLocked() { |
| 1165 | if (LOGV) Slog.v(TAG, "ensureActiveMobilePolicyLocked()"); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1166 | if (mSuppressDefaultPolicy) return; |
| 1167 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1168 | final TelephonyManager tele = TelephonyManager.from(mContext); |
| 1169 | |
| 1170 | // avoid creating policy when SIM isn't ready |
| 1171 | if (tele.getSimState() != SIM_STATE_READY) return; |
| 1172 | |
| 1173 | final String subscriberId = tele.getSubscriberId(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1174 | final NetworkIdentity probeIdent = new NetworkIdentity( |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1175 | TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1176 | |
| 1177 | // examine to see if any policy is defined for active mobile |
| 1178 | boolean mobileDefined = false; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1179 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 1180 | if (mNetworkPolicy.valueAt(i).template.matches(probeIdent)) { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1181 | mobileDefined = true; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1182 | break; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | if (!mobileDefined) { |
| 1187 | Slog.i(TAG, "no policy for active mobile network; generating default policy"); |
| 1188 | |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 1189 | // build default mobile policy, and assume usage cycle starts today |
| 1190 | final long warningBytes = mContext.getResources().getInteger( |
| 1191 | com.android.internal.R.integer.config_networkPolicyDefaultWarning) |
| 1192 | * MB_IN_BYTES; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1193 | |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 1194 | final Time time = new Time(); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1195 | time.setToNow(); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 1196 | |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1197 | final int cycleDay = time.monthDay; |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 1198 | final String cycleTimezone = time.timezone; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1199 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 1200 | final NetworkTemplate template = buildTemplateMobileAll(subscriberId); |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 1201 | final NetworkPolicy policy = new NetworkPolicy(template, cycleDay, cycleTimezone, |
| 1202 | warningBytes, LIMIT_DISABLED, SNOOZE_NEVER, SNOOZE_NEVER, true, true); |
| 1203 | addNetworkPolicyLocked(policy); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1204 | } |
| 1205 | } |
| 1206 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1207 | private void readPolicyLocked() { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1208 | if (LOGV) Slog.v(TAG, "readPolicyLocked()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1209 | |
| 1210 | // clear any existing policy and read from disk |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1211 | mNetworkPolicy.clear(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1212 | mUidPolicy.clear(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1213 | |
| 1214 | FileInputStream fis = null; |
| 1215 | try { |
| 1216 | fis = mPolicyFile.openRead(); |
| 1217 | final XmlPullParser in = Xml.newPullParser(); |
| 1218 | in.setInput(fis, null); |
| 1219 | |
| 1220 | int type; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1221 | int version = VERSION_INIT; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1222 | while ((type = in.next()) != END_DOCUMENT) { |
| 1223 | final String tag = in.getName(); |
| 1224 | if (type == START_TAG) { |
| 1225 | if (TAG_POLICY_LIST.equals(tag)) { |
| 1226 | version = readIntAttribute(in, ATTR_VERSION); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1227 | if (version >= VERSION_ADDED_RESTRICT_BACKGROUND) { |
| 1228 | mRestrictBackground = readBooleanAttribute( |
| 1229 | in, ATTR_RESTRICT_BACKGROUND); |
| 1230 | } else { |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1231 | mRestrictBackground = false; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1232 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1233 | |
| 1234 | } else if (TAG_NETWORK_POLICY.equals(tag)) { |
| 1235 | final int networkTemplate = readIntAttribute(in, ATTR_NETWORK_TEMPLATE); |
| 1236 | final String subscriberId = in.getAttributeValue(null, ATTR_SUBSCRIBER_ID); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1237 | final String networkId; |
| 1238 | if (version >= VERSION_ADDED_NETWORK_ID) { |
| 1239 | networkId = in.getAttributeValue(null, ATTR_NETWORK_ID); |
| 1240 | } else { |
| 1241 | networkId = null; |
| 1242 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1243 | final int cycleDay = readIntAttribute(in, ATTR_CYCLE_DAY); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 1244 | final String cycleTimezone; |
| 1245 | if (version >= VERSION_ADDED_TIMEZONE) { |
| 1246 | cycleTimezone = in.getAttributeValue(null, ATTR_CYCLE_TIMEZONE); |
| 1247 | } else { |
| 1248 | cycleTimezone = Time.TIMEZONE_UTC; |
| 1249 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1250 | final long warningBytes = readLongAttribute(in, ATTR_WARNING_BYTES); |
| 1251 | final long limitBytes = readLongAttribute(in, ATTR_LIMIT_BYTES); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1252 | final long lastLimitSnooze; |
| 1253 | if (version >= VERSION_SPLIT_SNOOZE) { |
| 1254 | lastLimitSnooze = readLongAttribute(in, ATTR_LAST_LIMIT_SNOOZE); |
| 1255 | } else if (version >= VERSION_ADDED_SNOOZE) { |
| 1256 | lastLimitSnooze = readLongAttribute(in, ATTR_LAST_SNOOZE); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1257 | } else { |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1258 | lastLimitSnooze = SNOOZE_NEVER; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1259 | } |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1260 | final boolean metered; |
| 1261 | if (version >= VERSION_ADDED_METERED) { |
| 1262 | metered = readBooleanAttribute(in, ATTR_METERED); |
| 1263 | } else { |
| 1264 | switch (networkTemplate) { |
| 1265 | case MATCH_MOBILE_3G_LOWER: |
| 1266 | case MATCH_MOBILE_4G: |
| 1267 | case MATCH_MOBILE_ALL: |
| 1268 | metered = true; |
| 1269 | break; |
| 1270 | default: |
| 1271 | metered = false; |
| 1272 | } |
| 1273 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1274 | final long lastWarningSnooze; |
| 1275 | if (version >= VERSION_SPLIT_SNOOZE) { |
| 1276 | lastWarningSnooze = readLongAttribute(in, ATTR_LAST_WARNING_SNOOZE); |
| 1277 | } else { |
| 1278 | lastWarningSnooze = SNOOZE_NEVER; |
| 1279 | } |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 1280 | final boolean inferred; |
| 1281 | if (version >= VERSION_ADDED_INFERRED) { |
| 1282 | inferred = readBooleanAttribute(in, ATTR_INFERRED); |
| 1283 | } else { |
| 1284 | inferred = false; |
| 1285 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1286 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1287 | final NetworkTemplate template = new NetworkTemplate( |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1288 | networkTemplate, subscriberId, networkId); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1289 | mNetworkPolicy.put(template, new NetworkPolicy(template, cycleDay, |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 1290 | cycleTimezone, warningBytes, limitBytes, lastWarningSnooze, |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 1291 | lastLimitSnooze, metered, inferred)); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1292 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1293 | } else if (TAG_UID_POLICY.equals(tag)) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1294 | final int uid = readIntAttribute(in, ATTR_UID); |
| 1295 | final int policy = readIntAttribute(in, ATTR_POLICY); |
| 1296 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1297 | if (UserHandle.isApp(uid)) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1298 | setUidPolicyUncheckedLocked(uid, policy, false); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1299 | } else { |
| 1300 | Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring"); |
| 1301 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1302 | } else if (TAG_APP_POLICY.equals(tag)) { |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 1303 | final int appId = readIntAttribute(in, ATTR_APP_ID); |
| 1304 | final int policy = readIntAttribute(in, ATTR_POLICY); |
| 1305 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1306 | // TODO: set for other users during upgrade |
| 1307 | final int uid = UserHandle.getUid(UserHandle.USER_OWNER, appId); |
| 1308 | if (UserHandle.isApp(uid)) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1309 | setUidPolicyUncheckedLocked(uid, policy, false); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 1310 | } else { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1311 | Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring"); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 1312 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1313 | } |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | } catch (FileNotFoundException e) { |
| 1318 | // missing policy is okay, probably first boot |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1319 | upgradeLegacyBackgroundData(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1320 | } catch (IOException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1321 | Log.wtf(TAG, "problem reading network policy", e); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1322 | } catch (XmlPullParserException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1323 | Log.wtf(TAG, "problem reading network policy", e); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1324 | } finally { |
| 1325 | IoUtils.closeQuietly(fis); |
| 1326 | } |
| 1327 | } |
| 1328 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1329 | /** |
| 1330 | * Upgrade legacy background data flags, notifying listeners of one last |
| 1331 | * change to always-true. |
| 1332 | */ |
| 1333 | private void upgradeLegacyBackgroundData() { |
| 1334 | mRestrictBackground = Settings.Secure.getInt( |
| 1335 | mContext.getContentResolver(), Settings.Secure.BACKGROUND_DATA, 1) != 1; |
| 1336 | |
| 1337 | // kick off one last broadcast if restricted |
| 1338 | if (mRestrictBackground) { |
| 1339 | final Intent broadcast = new Intent( |
| 1340 | ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1341 | mContext.sendBroadcastAsUser(broadcast, UserHandle.ALL); |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1342 | } |
| 1343 | } |
| 1344 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1345 | private void writePolicyLocked() { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1346 | if (LOGV) Slog.v(TAG, "writePolicyLocked()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1347 | |
| 1348 | FileOutputStream fos = null; |
| 1349 | try { |
| 1350 | fos = mPolicyFile.startWrite(); |
| 1351 | |
| 1352 | XmlSerializer out = new FastXmlSerializer(); |
| 1353 | out.setOutput(fos, "utf-8"); |
| 1354 | out.startDocument(null, true); |
| 1355 | |
| 1356 | out.startTag(null, TAG_POLICY_LIST); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1357 | writeIntAttribute(out, ATTR_VERSION, VERSION_LATEST); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1358 | writeBooleanAttribute(out, ATTR_RESTRICT_BACKGROUND, mRestrictBackground); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1359 | |
| 1360 | // write all known network policies |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1361 | for (int i = 0; i < mNetworkPolicy.size(); i++) { |
| 1362 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1363 | final NetworkTemplate template = policy.template; |
| 1364 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1365 | out.startTag(null, TAG_NETWORK_POLICY); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1366 | writeIntAttribute(out, ATTR_NETWORK_TEMPLATE, template.getMatchRule()); |
| 1367 | final String subscriberId = template.getSubscriberId(); |
| 1368 | if (subscriberId != null) { |
| 1369 | out.attribute(null, ATTR_SUBSCRIBER_ID, subscriberId); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1370 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1371 | final String networkId = template.getNetworkId(); |
| 1372 | if (networkId != null) { |
| 1373 | out.attribute(null, ATTR_NETWORK_ID, networkId); |
| 1374 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1375 | writeIntAttribute(out, ATTR_CYCLE_DAY, policy.cycleDay); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 1376 | out.attribute(null, ATTR_CYCLE_TIMEZONE, policy.cycleTimezone); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1377 | writeLongAttribute(out, ATTR_WARNING_BYTES, policy.warningBytes); |
| 1378 | writeLongAttribute(out, ATTR_LIMIT_BYTES, policy.limitBytes); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1379 | writeLongAttribute(out, ATTR_LAST_WARNING_SNOOZE, policy.lastWarningSnooze); |
| 1380 | writeLongAttribute(out, ATTR_LAST_LIMIT_SNOOZE, policy.lastLimitSnooze); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1381 | writeBooleanAttribute(out, ATTR_METERED, policy.metered); |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 1382 | writeBooleanAttribute(out, ATTR_INFERRED, policy.inferred); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1383 | out.endTag(null, TAG_NETWORK_POLICY); |
| 1384 | } |
| 1385 | |
| 1386 | // write all known uid policies |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1387 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 1388 | final int uid = mUidPolicy.keyAt(i); |
| 1389 | final int policy = mUidPolicy.valueAt(i); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1390 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1391 | // skip writing empty policies |
| 1392 | if (policy == POLICY_NONE) continue; |
| 1393 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1394 | out.startTag(null, TAG_UID_POLICY); |
| 1395 | writeIntAttribute(out, ATTR_UID, uid); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1396 | writeIntAttribute(out, ATTR_POLICY, policy); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1397 | out.endTag(null, TAG_UID_POLICY); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | out.endTag(null, TAG_POLICY_LIST); |
| 1401 | out.endDocument(); |
| 1402 | |
| 1403 | mPolicyFile.finishWrite(fos); |
| 1404 | } catch (IOException e) { |
| 1405 | if (fos != null) { |
| 1406 | mPolicyFile.failWrite(fos); |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1411 | @Override |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1412 | public void setUidPolicy(int uid, int policy) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1413 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1414 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1415 | if (!UserHandle.isApp(uid)) { |
| 1416 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1417 | } |
| 1418 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1419 | synchronized (mRulesLock) { |
| 1420 | final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 1421 | if (oldPolicy != policy) { |
| 1422 | setUidPolicyUncheckedLocked(uid, policy, true); |
| 1423 | } |
| 1424 | } |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1425 | } |
| 1426 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1427 | @Override |
| 1428 | public void addUidPolicy(int uid, int policy) { |
| 1429 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1430 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1431 | if (!UserHandle.isApp(uid)) { |
| 1432 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
| 1433 | } |
| 1434 | |
| 1435 | synchronized (mRulesLock) { |
| 1436 | final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 1437 | policy |= oldPolicy; |
| 1438 | if (oldPolicy != policy) { |
| 1439 | setUidPolicyUncheckedLocked(uid, policy, true); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1440 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1441 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | @Override |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1445 | public void removeUidPolicy(int uid, int policy) { |
| 1446 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1447 | |
| 1448 | if (!UserHandle.isApp(uid)) { |
| 1449 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
| 1450 | } |
| 1451 | |
| 1452 | synchronized (mRulesLock) { |
| 1453 | final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 1454 | policy = oldPolicy & ~policy; |
| 1455 | if (oldPolicy != policy) { |
| 1456 | setUidPolicyUncheckedLocked(uid, policy, true); |
| 1457 | } |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | private void setUidPolicyUncheckedLocked(int uid, int policy, boolean persist) { |
| 1462 | mUidPolicy.put(uid, policy); |
| 1463 | |
| 1464 | // uid policy changed, recompute rules and persist policy. |
| 1465 | updateRulesForUidLocked(uid); |
| 1466 | if (persist) { |
| 1467 | writePolicyLocked(); |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | @Override |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1472 | public int getUidPolicy(int uid) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1473 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1474 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1475 | synchronized (mRulesLock) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1476 | return mUidPolicy.get(uid, POLICY_NONE); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1477 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1478 | } |
| 1479 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1480 | @Override |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1481 | public int[] getUidsWithPolicy(int policy) { |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 1482 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1483 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1484 | int[] uids = new int[0]; |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 1485 | synchronized (mRulesLock) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1486 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 1487 | final int uid = mUidPolicy.keyAt(i); |
| 1488 | final int uidPolicy = mUidPolicy.valueAt(i); |
| 1489 | if (uidPolicy == policy) { |
| 1490 | uids = appendInt(uids, uid); |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 1491 | } |
| 1492 | } |
| 1493 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1494 | return uids; |
| 1495 | } |
| 1496 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1497 | @Override |
| 1498 | public int[] getPowerSaveAppIdWhitelist() { |
| 1499 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1500 | |
| 1501 | synchronized (mRulesLock) { |
| 1502 | int size = mPowerSaveWhitelistAppIds.size(); |
| 1503 | int[] appids = new int[size]; |
| 1504 | for (int i = 0; i < size; i++) { |
| 1505 | appids[i] = mPowerSaveWhitelistAppIds.keyAt(i); |
| 1506 | } |
| 1507 | return appids; |
| 1508 | } |
| 1509 | } |
| 1510 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1511 | /** |
| 1512 | * Remove any policies associated with given {@link UserHandle}, persisting |
| 1513 | * if any changes are made. |
| 1514 | */ |
| 1515 | private void removePoliciesForUserLocked(int userId) { |
| 1516 | if (LOGV) Slog.v(TAG, "removePoliciesForUserLocked()"); |
| 1517 | |
| 1518 | int[] uids = new int[0]; |
| 1519 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 1520 | final int uid = mUidPolicy.keyAt(i); |
| 1521 | if (UserHandle.getUserId(uid) == userId) { |
| 1522 | uids = appendInt(uids, uid); |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | if (uids.length > 0) { |
| 1527 | for (int uid : uids) { |
| 1528 | mUidPolicy.delete(uid); |
| 1529 | updateRulesForUidLocked(uid); |
| 1530 | } |
| 1531 | writePolicyLocked(); |
| 1532 | } |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | @Override |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1536 | public void registerListener(INetworkPolicyListener listener) { |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 1537 | // TODO: create permission for observing network policy |
| 1538 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 1539 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1540 | mListeners.register(listener); |
| 1541 | |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1542 | // TODO: consider dispatching existing rules to new listeners |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | @Override |
| 1546 | public void unregisterListener(INetworkPolicyListener listener) { |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 1547 | // TODO: create permission for observing network policy |
| 1548 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 1549 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1550 | mListeners.unregister(listener); |
| 1551 | } |
| 1552 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 1553 | @Override |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1554 | public void setNetworkPolicies(NetworkPolicy[] policies) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1555 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1556 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 1557 | maybeRefreshTrustedTime(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1558 | synchronized (mRulesLock) { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1559 | mNetworkPolicy.clear(); |
| 1560 | for (NetworkPolicy policy : policies) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1561 | mNetworkPolicy.put(policy.template, policy); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1562 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1563 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1564 | updateNetworkEnabledLocked(); |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 1565 | updateNetworkRulesLocked(); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1566 | updateNotificationsLocked(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1567 | writePolicyLocked(); |
| 1568 | } |
| 1569 | } |
| 1570 | |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 1571 | private void addNetworkPolicyLocked(NetworkPolicy policy) { |
| 1572 | mNetworkPolicy.put(policy.template, policy); |
| 1573 | |
| 1574 | updateNetworkEnabledLocked(); |
| 1575 | updateNetworkRulesLocked(); |
| 1576 | updateNotificationsLocked(); |
| 1577 | writePolicyLocked(); |
| 1578 | } |
| 1579 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1580 | @Override |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1581 | public NetworkPolicy[] getNetworkPolicies() { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1582 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1583 | mContext.enforceCallingOrSelfPermission(READ_PHONE_STATE, TAG); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1584 | |
| 1585 | synchronized (mRulesLock) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1586 | return mNetworkPolicy.values().toArray(new NetworkPolicy[mNetworkPolicy.size()]); |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | @Override |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1591 | public void snoozeLimit(NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1592 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | 6c0b4f3 | 2012-06-12 21:06:30 -0700 | [diff] [blame] | 1593 | |
| 1594 | final long token = Binder.clearCallingIdentity(); |
| 1595 | try { |
| 1596 | performSnooze(template, TYPE_LIMIT); |
| 1597 | } finally { |
| 1598 | Binder.restoreCallingIdentity(token); |
| 1599 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1600 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1601 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1602 | private void performSnooze(NetworkTemplate template, int type) { |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 1603 | maybeRefreshTrustedTime(); |
| 1604 | final long currentTime = currentTimeMillis(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1605 | synchronized (mRulesLock) { |
| 1606 | // find and snooze local policy that matches |
| 1607 | final NetworkPolicy policy = mNetworkPolicy.get(template); |
| 1608 | if (policy == null) { |
| 1609 | throw new IllegalArgumentException("unable to find policy for " + template); |
| 1610 | } |
| 1611 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1612 | switch (type) { |
| 1613 | case TYPE_WARNING: |
| 1614 | policy.lastWarningSnooze = currentTime; |
| 1615 | break; |
| 1616 | case TYPE_LIMIT: |
| 1617 | policy.lastLimitSnooze = currentTime; |
| 1618 | break; |
| 1619 | default: |
| 1620 | throw new IllegalArgumentException("unexpected type"); |
| 1621 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1622 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1623 | updateNetworkEnabledLocked(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1624 | updateNetworkRulesLocked(); |
| 1625 | updateNotificationsLocked(); |
| 1626 | writePolicyLocked(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | @Override |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1631 | public void setRestrictBackground(boolean restrictBackground) { |
| 1632 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1633 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 1634 | maybeRefreshTrustedTime(); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1635 | synchronized (mRulesLock) { |
| 1636 | mRestrictBackground = restrictBackground; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1637 | updateRulesForGlobalChangeLocked(false); |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1638 | updateNotificationsLocked(); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1639 | writePolicyLocked(); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1640 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1641 | |
| 1642 | mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_CHANGED, restrictBackground ? 1 : 0, 0) |
| 1643 | .sendToTarget(); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | @Override |
| 1647 | public boolean getRestrictBackground() { |
| 1648 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1649 | |
| 1650 | synchronized (mRulesLock) { |
| 1651 | return mRestrictBackground; |
| 1652 | } |
| 1653 | } |
| 1654 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1655 | private NetworkPolicy findPolicyForNetworkLocked(NetworkIdentity ident) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1656 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 1657 | NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1658 | if (policy.template.matches(ident)) { |
| 1659 | return policy; |
| 1660 | } |
| 1661 | } |
| 1662 | return null; |
| 1663 | } |
| 1664 | |
| 1665 | @Override |
| 1666 | public NetworkQuotaInfo getNetworkQuotaInfo(NetworkState state) { |
| 1667 | mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG); |
| 1668 | |
| 1669 | // only returns usage summary, so we don't require caller to have |
| 1670 | // READ_NETWORK_USAGE_HISTORY. |
| 1671 | final long token = Binder.clearCallingIdentity(); |
| 1672 | try { |
| 1673 | return getNetworkQuotaInfoUnchecked(state); |
| 1674 | } finally { |
| 1675 | Binder.restoreCallingIdentity(token); |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | private NetworkQuotaInfo getNetworkQuotaInfoUnchecked(NetworkState state) { |
| 1680 | final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state); |
| 1681 | |
| 1682 | final NetworkPolicy policy; |
| 1683 | synchronized (mRulesLock) { |
| 1684 | policy = findPolicyForNetworkLocked(ident); |
| 1685 | } |
| 1686 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1687 | if (policy == null || !policy.hasCycle()) { |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1688 | // missing policy means we can't derive useful quota info |
| 1689 | return null; |
| 1690 | } |
| 1691 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 1692 | final long currentTime = currentTimeMillis(); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1693 | |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1694 | // find total bytes used under policy |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1695 | final long start = computeLastCycleBoundary(currentTime, policy); |
| 1696 | final long end = currentTime; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1697 | final long totalBytes = getTotalBytes(policy.template, start, end); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1698 | |
| 1699 | // report soft and hard limits under policy |
| 1700 | final long softLimitBytes = policy.warningBytes != WARNING_DISABLED ? policy.warningBytes |
| 1701 | : NetworkQuotaInfo.NO_LIMIT; |
| 1702 | final long hardLimitBytes = policy.limitBytes != LIMIT_DISABLED ? policy.limitBytes |
| 1703 | : NetworkQuotaInfo.NO_LIMIT; |
| 1704 | |
| 1705 | return new NetworkQuotaInfo(totalBytes, softLimitBytes, hardLimitBytes); |
| 1706 | } |
| 1707 | |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1708 | @Override |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1709 | public boolean isNetworkMetered(NetworkState state) { |
| 1710 | final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state); |
| 1711 | |
Jeff Sharkey | f166f48 | 2012-04-30 15:59:21 -0700 | [diff] [blame] | 1712 | // roaming networks are always considered metered |
| 1713 | if (ident.getRoaming()) { |
| 1714 | return true; |
| 1715 | } |
| 1716 | |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1717 | final NetworkPolicy policy; |
| 1718 | synchronized (mRulesLock) { |
| 1719 | policy = findPolicyForNetworkLocked(ident); |
| 1720 | } |
| 1721 | |
| 1722 | if (policy != null) { |
| 1723 | return policy.metered; |
| 1724 | } else { |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 1725 | final int type = state.networkInfo.getType(); |
| 1726 | if (isNetworkTypeMobile(type) || type == TYPE_WIMAX) { |
| 1727 | return true; |
| 1728 | } |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1729 | return false; |
| 1730 | } |
| 1731 | } |
| 1732 | |
| 1733 | @Override |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1734 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1735 | mContext.enforceCallingOrSelfPermission(DUMP, TAG); |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 1736 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1737 | final IndentingPrintWriter fout = new IndentingPrintWriter(writer, " "); |
| 1738 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1739 | final HashSet<String> argSet = new HashSet<String>(); |
| 1740 | for (String arg : args) { |
| 1741 | argSet.add(arg); |
| 1742 | } |
| 1743 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 1744 | synchronized (mRulesLock) { |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1745 | if (argSet.contains("--unsnooze")) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1746 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 1747 | mNetworkPolicy.valueAt(i).clearSnooze(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1748 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1749 | |
| 1750 | updateNetworkEnabledLocked(); |
| 1751 | updateNetworkRulesLocked(); |
| 1752 | updateNotificationsLocked(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1753 | writePolicyLocked(); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1754 | |
| 1755 | fout.println("Cleared snooze timestamps"); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1756 | return; |
| 1757 | } |
| 1758 | |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1759 | fout.print("Restrict background: "); fout.println(mRestrictBackground); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1760 | fout.print("Restrict power: "); fout.println(mRestrictPower); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1761 | fout.println("Network policies:"); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1762 | fout.increaseIndent(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1763 | for (int i = 0; i < mNetworkPolicy.size(); i++) { |
| 1764 | fout.println(mNetworkPolicy.valueAt(i).toString()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1765 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1766 | fout.decreaseIndent(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1767 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1768 | fout.println("Policy for UIDs:"); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1769 | fout.increaseIndent(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1770 | int size = mUidPolicy.size(); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 1771 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1772 | final int uid = mUidPolicy.keyAt(i); |
| 1773 | final int policy = mUidPolicy.valueAt(i); |
| 1774 | fout.print("UID="); |
| 1775 | fout.print(uid); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 1776 | fout.print(" policy="); |
| 1777 | dumpPolicy(fout, policy); |
| 1778 | fout.println(); |
| 1779 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1780 | fout.decreaseIndent(); |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 1781 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1782 | size = mPowerSaveWhitelistAppIds.size(); |
| 1783 | if (size > 0) { |
| 1784 | fout.println("Power save whitelist app ids:"); |
| 1785 | fout.increaseIndent(); |
| 1786 | for (int i = 0; i < size; i++) { |
| 1787 | fout.print("UID="); |
| 1788 | fout.print(mPowerSaveWhitelistAppIds.keyAt(i)); |
| 1789 | fout.print(": "); |
| 1790 | fout.print(mPowerSaveWhitelistAppIds.valueAt(i)); |
| 1791 | fout.println(); |
| 1792 | } |
| 1793 | fout.decreaseIndent(); |
| 1794 | } |
| 1795 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 1796 | final SparseBooleanArray knownUids = new SparseBooleanArray(); |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 1797 | collectKeys(mUidForeground, knownUids); |
| 1798 | collectKeys(mUidRules, knownUids); |
| 1799 | |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 1800 | fout.println("Status for known UIDs:"); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1801 | fout.increaseIndent(); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 1802 | size = knownUids.size(); |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 1803 | for (int i = 0; i < size; i++) { |
| 1804 | final int uid = knownUids.keyAt(i); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1805 | fout.print("UID="); |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 1806 | fout.print(uid); |
| 1807 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 1808 | fout.print(" foreground="); |
| 1809 | final int foregroundIndex = mUidPidForeground.indexOfKey(uid); |
| 1810 | if (foregroundIndex < 0) { |
| 1811 | fout.print("UNKNOWN"); |
| 1812 | } else { |
| 1813 | dumpSparseBooleanArray(fout, mUidPidForeground.valueAt(foregroundIndex)); |
| 1814 | } |
| 1815 | |
| 1816 | fout.print(" rules="); |
| 1817 | final int rulesIndex = mUidRules.indexOfKey(uid); |
| 1818 | if (rulesIndex < 0) { |
| 1819 | fout.print("UNKNOWN"); |
| 1820 | } else { |
| 1821 | dumpRules(fout, mUidRules.valueAt(rulesIndex)); |
| 1822 | } |
| 1823 | |
| 1824 | fout.println(); |
| 1825 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1826 | fout.decreaseIndent(); |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 1827 | } |
| 1828 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 1829 | |
| 1830 | @Override |
| 1831 | public boolean isUidForeground(int uid) { |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1832 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1833 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 1834 | synchronized (mRulesLock) { |
| 1835 | // only really in foreground when screen is also on |
| 1836 | return mUidForeground.get(uid, false) && mScreenOn; |
| 1837 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1838 | } |
| 1839 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1840 | /** |
| 1841 | * Foreground for PID changed; recompute foreground at UID level. If |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1842 | * changed, will trigger {@link #updateRulesForUidLocked(int)}. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1843 | */ |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1844 | private void computeUidForegroundLocked(int uid) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1845 | final SparseBooleanArray pidForeground = mUidPidForeground.get(uid); |
| 1846 | |
| 1847 | // current pid is dropping foreground; examine other pids |
| 1848 | boolean uidForeground = false; |
| 1849 | final int size = pidForeground.size(); |
| 1850 | for (int i = 0; i < size; i++) { |
| 1851 | if (pidForeground.valueAt(i)) { |
| 1852 | uidForeground = true; |
| 1853 | break; |
| 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | final boolean oldUidForeground = mUidForeground.get(uid, false); |
| 1858 | if (oldUidForeground != uidForeground) { |
| 1859 | // foreground changed, push updated rules |
| 1860 | mUidForeground.put(uid, uidForeground); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1861 | updateRulesForUidLocked(uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1862 | } |
| 1863 | } |
| 1864 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1865 | private void updateScreenOn() { |
| 1866 | synchronized (mRulesLock) { |
| 1867 | try { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1868 | mScreenOn = mPowerManager.isInteractive(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1869 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1870 | // ignored; service lives in system_server |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1871 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1872 | updateRulesForScreenLocked(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | /** |
| 1877 | * Update rules that might be changed by {@link #mScreenOn} value. |
| 1878 | */ |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1879 | private void updateRulesForScreenLocked() { |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1880 | // only update rules for anyone with foreground activities |
| 1881 | final int size = mUidForeground.size(); |
| 1882 | for (int i = 0; i < size; i++) { |
| 1883 | if (mUidForeground.valueAt(i)) { |
| 1884 | final int uid = mUidForeground.keyAt(i); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1885 | updateRulesForUidLocked(uid); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1886 | } |
| 1887 | } |
| 1888 | } |
| 1889 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1890 | /** |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1891 | * Update rules that might be changed by {@link #mRestrictBackground} |
| 1892 | * or {@link #mRestrictPower} value. |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1893 | */ |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1894 | private void updateRulesForGlobalChangeLocked(boolean restrictedNetworksChanged) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1895 | final PackageManager pm = mContext.getPackageManager(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1896 | final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 1897 | |
| 1898 | // update rules for all installed applications |
| 1899 | final List<UserInfo> users = um.getUsers(); |
| 1900 | final List<ApplicationInfo> apps = pm.getInstalledApplications( |
| 1901 | PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_DISABLED_COMPONENTS); |
| 1902 | |
| 1903 | for (UserInfo user : users) { |
| 1904 | for (ApplicationInfo app : apps) { |
| 1905 | final int uid = UserHandle.getUid(user.id, app.uid); |
| 1906 | updateRulesForUidLocked(uid); |
| 1907 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1908 | } |
| 1909 | |
Jeff Sharkey | 5294a2f | 2012-04-24 17:07:22 -0700 | [diff] [blame] | 1910 | // limit data usage for some internal system services |
| 1911 | updateRulesForUidLocked(android.os.Process.MEDIA_UID); |
| 1912 | updateRulesForUidLocked(android.os.Process.DRM_UID); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1913 | |
| 1914 | // If the set of restricted networks may have changed, re-evaluate those. |
| 1915 | if (restrictedNetworksChanged) { |
| 1916 | updateNetworkRulesLocked(); |
| 1917 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1918 | } |
| 1919 | |
Jeff Sharkey | 5294a2f | 2012-04-24 17:07:22 -0700 | [diff] [blame] | 1920 | private static boolean isUidValidForRules(int uid) { |
| 1921 | // allow rules on specific system services, and any apps |
| 1922 | if (uid == android.os.Process.MEDIA_UID || uid == android.os.Process.DRM_UID |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1923 | || UserHandle.isApp(uid)) { |
Jeff Sharkey | 5294a2f | 2012-04-24 17:07:22 -0700 | [diff] [blame] | 1924 | return true; |
| 1925 | } |
| 1926 | |
| 1927 | return false; |
| 1928 | } |
| 1929 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1930 | private void updateRulesForUidLocked(int uid) { |
Jeff Sharkey | 5294a2f | 2012-04-24 17:07:22 -0700 | [diff] [blame] | 1931 | if (!isUidValidForRules(uid)) return; |
| 1932 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1933 | final int uidPolicy = getUidPolicy(uid); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 1934 | final boolean uidForeground = isUidForeground(uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1935 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1936 | // derive active rules based on policy and active state |
| 1937 | int uidRules = RULE_ALLOW_ALL; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1938 | if (!uidForeground && (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0) { |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1939 | // uid in background, and policy says to block metered data |
| 1940 | uidRules = RULE_REJECT_METERED; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame^] | 1941 | } else if (mRestrictBackground) { |
| 1942 | if (!uidForeground) { |
| 1943 | // uid in background, and global background disabled |
| 1944 | uidRules = RULE_REJECT_METERED; |
| 1945 | } |
| 1946 | } else if (mRestrictPower) { |
| 1947 | final boolean whitelisted = mPowerSaveWhitelistAppIds.get(UserHandle.getAppId(uid)); |
| 1948 | if (!whitelisted && !uidForeground |
| 1949 | && (uidPolicy & POLICY_ALLOW_BACKGROUND_BATTERY_SAVE) == 0) { |
| 1950 | // uid is in background, restrict power use mode is on (so we want to |
| 1951 | // restrict all background network access), and this uid is not on the |
| 1952 | // white list of those allowed background access. |
| 1953 | uidRules = RULE_REJECT_METERED; |
| 1954 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1955 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1956 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1957 | // TODO: only dispatch when rules actually change |
| 1958 | |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 1959 | if (uidRules == RULE_ALLOW_ALL) { |
| 1960 | mUidRules.delete(uid); |
| 1961 | } else { |
| 1962 | mUidRules.put(uid, uidRules); |
| 1963 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1964 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1965 | final boolean rejectMetered = (uidRules & RULE_REJECT_METERED) != 0; |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 1966 | setUidNetworkRules(uid, rejectMetered); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1967 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1968 | // dispatch changed rule to existing listeners |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1969 | mHandler.obtainMessage(MSG_RULES_CHANGED, uid, uidRules).sendToTarget(); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 1970 | |
| 1971 | try { |
| 1972 | // adjust stats accounting based on foreground status |
| 1973 | mNetworkStats.setUidForeground(uid, uidForeground); |
| 1974 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1975 | // ignored; service lives in system_server |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 1976 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | private Handler.Callback mHandlerCallback = new Handler.Callback() { |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1980 | @Override |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1981 | public boolean handleMessage(Message msg) { |
| 1982 | switch (msg.what) { |
| 1983 | case MSG_RULES_CHANGED: { |
| 1984 | final int uid = msg.arg1; |
| 1985 | final int uidRules = msg.arg2; |
| 1986 | final int length = mListeners.beginBroadcast(); |
| 1987 | for (int i = 0; i < length; i++) { |
| 1988 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
| 1989 | if (listener != null) { |
| 1990 | try { |
| 1991 | listener.onUidRulesChanged(uid, uidRules); |
| 1992 | } catch (RemoteException e) { |
| 1993 | } |
| 1994 | } |
| 1995 | } |
| 1996 | mListeners.finishBroadcast(); |
| 1997 | return true; |
| 1998 | } |
| 1999 | case MSG_METERED_IFACES_CHANGED: { |
| 2000 | final String[] meteredIfaces = (String[]) msg.obj; |
| 2001 | final int length = mListeners.beginBroadcast(); |
| 2002 | for (int i = 0; i < length; i++) { |
| 2003 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
| 2004 | if (listener != null) { |
| 2005 | try { |
| 2006 | listener.onMeteredIfacesChanged(meteredIfaces); |
| 2007 | } catch (RemoteException e) { |
| 2008 | } |
| 2009 | } |
| 2010 | } |
| 2011 | mListeners.finishBroadcast(); |
| 2012 | return true; |
| 2013 | } |
Jeff Sharkey | 6f7af03 | 2011-11-01 18:25:15 -0700 | [diff] [blame] | 2014 | case MSG_FOREGROUND_ACTIVITIES_CHANGED: { |
| 2015 | final int pid = msg.arg1; |
| 2016 | final int uid = msg.arg2; |
| 2017 | final boolean foregroundActivities = (Boolean) msg.obj; |
| 2018 | |
| 2019 | synchronized (mRulesLock) { |
| 2020 | // because a uid can have multiple pids running inside, we need to |
| 2021 | // remember all pid states and summarize foreground at uid level. |
| 2022 | |
| 2023 | // record foreground for this specific pid |
| 2024 | SparseBooleanArray pidForeground = mUidPidForeground.get(uid); |
| 2025 | if (pidForeground == null) { |
| 2026 | pidForeground = new SparseBooleanArray(2); |
| 2027 | mUidPidForeground.put(uid, pidForeground); |
| 2028 | } |
| 2029 | pidForeground.put(pid, foregroundActivities); |
| 2030 | computeUidForegroundLocked(uid); |
| 2031 | } |
| 2032 | return true; |
| 2033 | } |
| 2034 | case MSG_PROCESS_DIED: { |
| 2035 | final int pid = msg.arg1; |
| 2036 | final int uid = msg.arg2; |
| 2037 | |
| 2038 | synchronized (mRulesLock) { |
| 2039 | // clear records and recompute, when they exist |
| 2040 | final SparseBooleanArray pidForeground = mUidPidForeground.get(uid); |
| 2041 | if (pidForeground != null) { |
| 2042 | pidForeground.delete(pid); |
| 2043 | computeUidForegroundLocked(uid); |
| 2044 | } |
| 2045 | } |
| 2046 | return true; |
| 2047 | } |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 2048 | case MSG_LIMIT_REACHED: { |
| 2049 | final String iface = (String) msg.obj; |
| 2050 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 2051 | maybeRefreshTrustedTime(); |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 2052 | synchronized (mRulesLock) { |
| 2053 | if (mMeteredIfaces.contains(iface)) { |
| 2054 | try { |
| 2055 | // force stats update to make sure we have |
| 2056 | // numbers that caused alert to trigger. |
| 2057 | mNetworkStats.forceUpdate(); |
| 2058 | } catch (RemoteException e) { |
| 2059 | // ignored; service lives in system_server |
| 2060 | } |
| 2061 | |
| 2062 | updateNetworkEnabledLocked(); |
| 2063 | updateNotificationsLocked(); |
| 2064 | } |
| 2065 | } |
| 2066 | return true; |
| 2067 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 2068 | case MSG_RESTRICT_BACKGROUND_CHANGED: { |
| 2069 | final boolean restrictBackground = msg.arg1 != 0; |
| 2070 | final int length = mListeners.beginBroadcast(); |
| 2071 | for (int i = 0; i < length; i++) { |
| 2072 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
| 2073 | if (listener != null) { |
| 2074 | try { |
| 2075 | listener.onRestrictBackgroundChanged(restrictBackground); |
| 2076 | } catch (RemoteException e) { |
| 2077 | } |
| 2078 | } |
| 2079 | } |
| 2080 | mListeners.finishBroadcast(); |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 2081 | return true; |
| 2082 | } |
| 2083 | case MSG_ADVISE_PERSIST_THRESHOLD: { |
| 2084 | final long lowestRule = (Long) msg.obj; |
| 2085 | try { |
| 2086 | // make sure stats are recorded frequently enough; we aim |
| 2087 | // for 2MB threshold for 2GB/month rules. |
| 2088 | final long persistThreshold = lowestRule / 1000; |
| 2089 | mNetworkStats.advisePersistThreshold(persistThreshold); |
| 2090 | } catch (RemoteException e) { |
| 2091 | // ignored; service lives in system_server |
| 2092 | } |
| 2093 | return true; |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 2094 | } |
Jeff Sharkey | 0abe556 | 2012-06-19 13:32:22 -0700 | [diff] [blame] | 2095 | case MSG_SCREEN_ON_CHANGED: { |
| 2096 | updateScreenOn(); |
| 2097 | return true; |
| 2098 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 2099 | default: { |
| 2100 | return false; |
Jeff Sharkey | af11d48 | 2011-06-13 00:14:31 -0700 | [diff] [blame] | 2101 | } |
| 2102 | } |
| 2103 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 2104 | }; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 2105 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2106 | private void setInterfaceQuota(String iface, long quotaBytes) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 2107 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2108 | mNetworkManager.setInterfaceQuota(iface, quotaBytes); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 2109 | } catch (IllegalStateException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 2110 | Log.wtf(TAG, "problem setting interface quota", e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 2111 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 2112 | // ignored; service lives in system_server |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | private void removeInterfaceQuota(String iface) { |
| 2117 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2118 | mNetworkManager.removeInterfaceQuota(iface); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 2119 | } catch (IllegalStateException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 2120 | Log.wtf(TAG, "problem removing interface quota", e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 2121 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 2122 | // ignored; service lives in system_server |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2123 | } |
| 2124 | } |
| 2125 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 2126 | private void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) { |
| 2127 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2128 | mNetworkManager.setUidNetworkRules(uid, rejectOnQuotaInterfaces); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 2129 | } catch (IllegalStateException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 2130 | Log.wtf(TAG, "problem setting uid rules", e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 2131 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 2132 | // ignored; service lives in system_server |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 2133 | } |
| 2134 | } |
| 2135 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 2136 | /** |
Jeff Sharkey | 3f3115b | 2011-11-08 16:30:37 -0800 | [diff] [blame] | 2137 | * Control {@link IConnectivityManager#setPolicyDataEnable(int, boolean)}. |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 2138 | */ |
| 2139 | private void setPolicyDataEnable(int networkType, boolean enabled) { |
Jeff Sharkey | 3f3115b | 2011-11-08 16:30:37 -0800 | [diff] [blame] | 2140 | try { |
| 2141 | mConnManager.setPolicyDataEnable(networkType, enabled); |
| 2142 | } catch (RemoteException e) { |
| 2143 | // ignored; service lives in system_server |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 2144 | } |
| 2145 | } |
| 2146 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 2147 | private long getTotalBytes(NetworkTemplate template, long start, long end) { |
| 2148 | try { |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 2149 | return mNetworkStats.getNetworkTotalBytes(template, start, end); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 2150 | } catch (RuntimeException e) { |
| 2151 | Slog.w(TAG, "problem reading network stats: " + e); |
| 2152 | return 0; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 2153 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 2154 | // ignored; service lives in system_server |
| 2155 | return 0; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 2156 | } |
| 2157 | } |
| 2158 | |
Jeff Sharkey | 8c1dc72 | 2012-05-04 14:49:37 -0700 | [diff] [blame] | 2159 | private boolean isBandwidthControlEnabled() { |
| 2160 | final long token = Binder.clearCallingIdentity(); |
| 2161 | try { |
| 2162 | return mNetworkManager.isBandwidthControlEnabled(); |
| 2163 | } catch (RemoteException e) { |
| 2164 | // ignored; service lives in system_server |
| 2165 | return false; |
| 2166 | } finally { |
| 2167 | Binder.restoreCallingIdentity(token); |
| 2168 | } |
| 2169 | } |
| 2170 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 2171 | /** |
| 2172 | * Try refreshing {@link #mTime} when stale. |
| 2173 | */ |
| 2174 | private void maybeRefreshTrustedTime() { |
| 2175 | if (mTime.getCacheAge() > TIME_CACHE_MAX_AGE) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 2176 | mTime.forceRefresh(); |
| 2177 | } |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 2178 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 2179 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 2180 | private long currentTimeMillis() { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 2181 | return mTime.hasCache() ? mTime.currentTimeMillis() : System.currentTimeMillis(); |
| 2182 | } |
| 2183 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2184 | private static Intent buildAllowBackgroundDataIntent() { |
| 2185 | return new Intent(ACTION_ALLOW_BACKGROUND); |
| 2186 | } |
| 2187 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2188 | private static Intent buildSnoozeWarningIntent(NetworkTemplate template) { |
| 2189 | final Intent intent = new Intent(ACTION_SNOOZE_WARNING); |
| 2190 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 2191 | return intent; |
| 2192 | } |
| 2193 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2194 | private static Intent buildNetworkOverLimitIntent(NetworkTemplate template) { |
| 2195 | final Intent intent = new Intent(); |
| 2196 | intent.setComponent(new ComponentName( |
| 2197 | "com.android.systemui", "com.android.systemui.net.NetworkOverLimitActivity")); |
| 2198 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2199 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 2200 | return intent; |
| 2201 | } |
| 2202 | |
| 2203 | private static Intent buildViewDataUsageIntent(NetworkTemplate template) { |
| 2204 | final Intent intent = new Intent(); |
| 2205 | intent.setComponent(new ComponentName( |
| 2206 | "com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity")); |
| 2207 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2208 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 2209 | return intent; |
| 2210 | } |
| 2211 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 2212 | @VisibleForTesting |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 2213 | public void addIdleHandler(IdleHandler handler) { |
| 2214 | mHandler.getLooper().getQueue().addIdleHandler(handler); |
| 2215 | } |
| 2216 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2217 | private static void collectKeys(SparseIntArray source, SparseBooleanArray target) { |
| 2218 | final int size = source.size(); |
| 2219 | for (int i = 0; i < size; i++) { |
| 2220 | target.put(source.keyAt(i), true); |
| 2221 | } |
| 2222 | } |
| 2223 | |
| 2224 | private static void collectKeys(SparseBooleanArray source, SparseBooleanArray target) { |
| 2225 | final int size = source.size(); |
| 2226 | for (int i = 0; i < size; i++) { |
| 2227 | target.put(source.keyAt(i), true); |
| 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | private static void dumpSparseBooleanArray(PrintWriter fout, SparseBooleanArray value) { |
| 2232 | fout.print("["); |
| 2233 | final int size = value.size(); |
| 2234 | for (int i = 0; i < size; i++) { |
| 2235 | fout.print(value.keyAt(i) + "=" + value.valueAt(i)); |
| 2236 | if (i < size - 1) fout.print(","); |
| 2237 | } |
| 2238 | fout.print("]"); |
| 2239 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2240 | } |