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