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