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