Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 17 | package com.android.server.job.controllers; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 18 | |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 19 | import static android.net.NetworkCapabilities.LINK_BANDWIDTH_UNSPECIFIED; |
| 20 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_CONGESTED; |
| 21 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED; |
| 22 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 23 | import android.app.job.JobInfo; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 25 | import android.net.ConnectivityManager; |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 26 | import android.net.ConnectivityManager.NetworkCallback; |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 27 | import android.net.INetworkPolicyListener; |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 28 | import android.net.Network; |
| 29 | import android.net.NetworkCapabilities; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 30 | import android.net.NetworkInfo; |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 31 | import android.net.NetworkPolicyManager; |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 32 | import android.net.NetworkRequest; |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 33 | import android.net.TrafficStats; |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 34 | import android.os.Process; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 35 | import android.os.UserHandle; |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 36 | import android.text.format.DateUtils; |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 37 | import android.util.ArraySet; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 38 | import android.util.Slog; |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 39 | import android.util.proto.ProtoOutputStream; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 40 | |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 41 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 42 | import com.android.internal.annotations.VisibleForTesting; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 43 | import com.android.server.job.JobSchedulerService; |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 44 | import com.android.server.job.JobServiceContext; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 45 | import com.android.server.job.StateChangedListener; |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 46 | import com.android.server.job.StateControllerProto; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 47 | |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 48 | import java.io.PrintWriter; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 51 | * Handles changes in connectivity. |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 52 | * <p> |
| 53 | * Each app can have a different default networks or different connectivity |
| 54 | * status due to user-requested network policies, so we need to check |
| 55 | * constraints on a per-UID basis. |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 56 | */ |
Dianne Hackborn | 6466c1c | 2017-06-13 10:33:19 -0700 | [diff] [blame] | 57 | public final class ConnectivityController extends StateController implements |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 58 | ConnectivityManager.OnNetworkActiveListener { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 59 | private static final String TAG = "JobScheduler.Conn"; |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 60 | private static final boolean DEBUG = false; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 61 | |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 62 | private final ConnectivityManager mConnManager; |
| 63 | private final NetworkPolicyManager mNetPolicyManager; |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 64 | private boolean mConnected; |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 65 | |
| 66 | @GuardedBy("mLock") |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 67 | private final ArraySet<JobStatus> mTrackedJobs = new ArraySet<>(); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 68 | |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 69 | /** Singleton. */ |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 70 | private static ConnectivityController sSingleton; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 71 | private static Object sCreationLock = new Object(); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 72 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 73 | public static ConnectivityController get(JobSchedulerService jms) { |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 74 | synchronized (sCreationLock) { |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 75 | if (sSingleton == null) { |
| 76 | sSingleton = new ConnectivityController(jms, jms.getContext(), jms.getLock()); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 77 | } |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 78 | return sSingleton; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 79 | } |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 82 | private ConnectivityController(StateChangedListener stateChangedListener, Context context, |
| 83 | Object lock) { |
| 84 | super(stateChangedListener, context, lock); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 85 | |
| 86 | mConnManager = mContext.getSystemService(ConnectivityManager.class); |
| 87 | mNetPolicyManager = mContext.getSystemService(NetworkPolicyManager.class); |
| 88 | |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 89 | mConnected = false; |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 90 | |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 91 | mConnManager.registerDefaultNetworkCallback(mNetworkCallback); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 92 | mNetPolicyManager.registerListener(mNetPolicyListener); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 95 | @GuardedBy("mLock") |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 96 | @Override |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 97 | public void maybeStartTrackingJobLocked(JobStatus jobStatus, JobStatus lastJob) { |
Christopher Tate | 60977f4 | 2017-04-13 13:48:46 -0700 | [diff] [blame] | 98 | if (jobStatus.hasConnectivityConstraint()) { |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 99 | updateConstraintsSatisfied(jobStatus); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 100 | mTrackedJobs.add(jobStatus); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 101 | jobStatus.setTrackingController(JobStatus.TRACKING_CONNECTIVITY); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 105 | @GuardedBy("mLock") |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 106 | @Override |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 107 | public void maybeStopTrackingJobLocked(JobStatus jobStatus, JobStatus incomingJob, |
| 108 | boolean forUpdate) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 109 | if (jobStatus.clearTrackingController(JobStatus.TRACKING_CONNECTIVITY)) { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 110 | mTrackedJobs.remove(jobStatus); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 111 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 114 | /** |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 115 | * Test to see if running the given job on the given network is insane. |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 116 | * <p> |
| 117 | * For example, if a job is trying to send 10MB over a 128Kbps EDGE |
| 118 | * connection, it would take 10.4 minutes, and has no chance of succeeding |
| 119 | * before the job times out, so we'd be insane to try running it. |
| 120 | */ |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 121 | @SuppressWarnings("unused") |
| 122 | private static boolean isInsane(JobStatus jobStatus, Network network, |
| 123 | NetworkCapabilities capabilities) { |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 124 | final long estimatedBytes = jobStatus.getEstimatedNetworkBytes(); |
| 125 | if (estimatedBytes == JobInfo.NETWORK_BYTES_UNKNOWN) { |
| 126 | // We don't know how large the job is; cross our fingers! |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 127 | return false; |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // We don't ask developers to differentiate between upstream/downstream |
| 131 | // in their size estimates, so test against the slowest link direction. |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 132 | final long slowest = NetworkCapabilities.minBandwidth( |
| 133 | capabilities.getLinkDownstreamBandwidthKbps(), |
| 134 | capabilities.getLinkUpstreamBandwidthKbps()); |
| 135 | if (slowest == LINK_BANDWIDTH_UNSPECIFIED) { |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 136 | // We don't know what the network is like; cross our fingers! |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 137 | return false; |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | final long estimatedMillis = ((estimatedBytes * DateUtils.SECOND_IN_MILLIS) |
| 141 | / (slowest * TrafficStats.KB_IN_BYTES / 8)); |
| 142 | if (estimatedMillis > JobServiceContext.EXECUTING_TIMESLICE_MILLIS) { |
| 143 | // If we'd never finish before the timeout, we'd be insane! |
| 144 | Slog.w(TAG, "Estimated " + estimatedBytes + " bytes over " + slowest |
| 145 | + " kbps network would take " + estimatedMillis + "ms; that's insane!"); |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 146 | return true; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 147 | } else { |
| 148 | return false; |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 152 | @SuppressWarnings("unused") |
| 153 | private static boolean isCongestionDelayed(JobStatus jobStatus, Network network, |
| 154 | NetworkCapabilities capabilities) { |
| 155 | // If network is congested, and job is less than 50% through the |
| 156 | // developer-requested window, then we're okay delaying the job. |
| 157 | if (!capabilities.hasCapability(NET_CAPABILITY_NOT_CONGESTED)) { |
| 158 | return jobStatus.getFractionRunTime() < 0.5; |
| 159 | } else { |
| 160 | return false; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | @SuppressWarnings("unused") |
| 165 | private static boolean isStrictSatisfied(JobStatus jobStatus, Network network, |
| 166 | NetworkCapabilities capabilities) { |
| 167 | return jobStatus.getJob().getRequiredNetwork().networkCapabilities |
| 168 | .satisfiedByNetworkCapabilities(capabilities); |
| 169 | } |
| 170 | |
| 171 | @SuppressWarnings("unused") |
| 172 | private static boolean isRelaxedSatisfied(JobStatus jobStatus, Network network, |
| 173 | NetworkCapabilities capabilities) { |
| 174 | // Only consider doing this for prefetching jobs |
| 175 | if ((jobStatus.getJob().getFlags() & JobInfo.FLAG_IS_PREFETCH) == 0) { |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | // See if we match after relaxing any unmetered request |
| 180 | final NetworkCapabilities relaxed = new NetworkCapabilities( |
| 181 | jobStatus.getJob().getRequiredNetwork().networkCapabilities) |
| 182 | .removeCapability(NET_CAPABILITY_NOT_METERED); |
| 183 | if (relaxed.satisfiedByNetworkCapabilities(capabilities)) { |
| 184 | // TODO: treat this as "maybe" response; need to check quotas |
| 185 | return jobStatus.getFractionRunTime() > 0.5; |
| 186 | } else { |
| 187 | return false; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | @VisibleForTesting |
| 192 | static boolean isSatisfied(JobStatus jobStatus, Network network, |
| 193 | NetworkCapabilities capabilities) { |
| 194 | // Zeroth, we gotta have a network to think about being satisfied |
| 195 | if (network == null || capabilities == null) return false; |
| 196 | |
| 197 | // First, are we insane? |
| 198 | if (isInsane(jobStatus, network, capabilities)) return false; |
| 199 | |
| 200 | // Second, is the network congested? |
| 201 | if (isCongestionDelayed(jobStatus, network, capabilities)) return false; |
| 202 | |
| 203 | // Third, is the network a strict match? |
| 204 | if (isStrictSatisfied(jobStatus, network, capabilities)) return true; |
| 205 | |
| 206 | // Third, is the network a relaxed match? |
| 207 | if (isRelaxedSatisfied(jobStatus, network, capabilities)) return true; |
| 208 | |
| 209 | return false; |
| 210 | } |
| 211 | |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 212 | private boolean updateConstraintsSatisfied(JobStatus jobStatus) { |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 213 | // TODO: consider matching against non-active networks |
| 214 | |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 215 | final int jobUid = jobStatus.getSourceUid(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 216 | final boolean ignoreBlocked = (jobStatus.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) != 0; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 217 | |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 218 | final Network network = mConnManager.getActiveNetworkForUid(jobUid, ignoreBlocked); |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 219 | final NetworkInfo info = mConnManager.getNetworkInfoForUid(network, jobUid, ignoreBlocked); |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 220 | final NetworkCapabilities capabilities = mConnManager.getNetworkCapabilities(network); |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 221 | |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 222 | final boolean connected = (info != null) && info.isConnected(); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 223 | final boolean satisfied = isSatisfied(jobStatus, network, capabilities); |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 224 | |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 225 | final boolean changed = jobStatus |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 226 | .setConnectivityConstraintSatisfied(connected && satisfied); |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 227 | |
Jeff Sharkey | 76a0241 | 2017-10-24 16:55:04 -0600 | [diff] [blame] | 228 | // Pass along the evaluated network for job to use; prevents race |
| 229 | // conditions as default routes change over time, and opens the door to |
| 230 | // using non-default routes. |
| 231 | jobStatus.network = network; |
| 232 | |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 233 | // Track system-uid connected/validated as a general reportable proxy for the |
| 234 | // overall state of connectivity constraint satisfiability. |
| 235 | if (jobUid == Process.SYSTEM_UID) { |
| 236 | mConnected = connected; |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | if (DEBUG) { |
| 240 | Slog.i(TAG, "Connectivity " + (changed ? "CHANGED" : "unchanged") |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 241 | + " for " + jobStatus + ": connected=" + connected |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 242 | + " satisfied=" + satisfied); |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 243 | } |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 244 | return changed; |
| 245 | } |
| 246 | |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 247 | /** |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 248 | * Update all jobs tracked by this controller. |
| 249 | * |
| 250 | * @param uid only update jobs belonging to this UID, or {@code -1} to |
| 251 | * update all tracked jobs. |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 252 | */ |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 253 | private void updateTrackedJobs(int uid) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 254 | synchronized (mLock) { |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 255 | boolean changed = false; |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 256 | for (int i = mTrackedJobs.size()-1; i >= 0; i--) { |
| 257 | final JobStatus js = mTrackedJobs.valueAt(i); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 258 | if (uid == -1 || uid == js.getSourceUid()) { |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 259 | changed |= updateConstraintsSatisfied(js); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 260 | } |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 261 | } |
| 262 | if (changed) { |
| 263 | mStateChangedListener.onControllerStateChanged(); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 269 | * We know the network has just come up. We want to run any jobs that are ready. |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 270 | */ |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 271 | @Override |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 272 | public void onNetworkActive() { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 273 | synchronized (mLock) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 274 | for (int i = mTrackedJobs.size()-1; i >= 0; i--) { |
| 275 | final JobStatus js = mTrackedJobs.valueAt(i); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 276 | if (js.isReady()) { |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 277 | if (DEBUG) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 278 | Slog.d(TAG, "Running " + js + " due to network activity."); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 279 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 280 | mStateChangedListener.onRunJobNow(js); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 283 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 286 | private final NetworkCallback mNetworkCallback = new NetworkCallback() { |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 287 | @Override |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 288 | public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) { |
| 289 | if (DEBUG) { |
| 290 | Slog.v(TAG, "onCapabilitiesChanged() : " + networkCapabilities); |
| 291 | } |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 292 | updateTrackedJobs(-1); |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public void onLost(Network network) { |
| 297 | if (DEBUG) { |
| 298 | Slog.v(TAG, "Network lost"); |
| 299 | } |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 300 | updateTrackedJobs(-1); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 301 | } |
| 302 | }; |
| 303 | |
Jeff Sharkey | 75d3189 | 2018-01-18 22:01:59 +0900 | [diff] [blame] | 304 | private final INetworkPolicyListener mNetPolicyListener = new NetworkPolicyManager.Listener() { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 305 | @Override |
| 306 | public void onUidRulesChanged(int uid, int uidRules) { |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 307 | if (DEBUG) { |
| 308 | Slog.v(TAG, "Uid rules changed for " + uid); |
| 309 | } |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 310 | updateTrackedJobs(uid); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | @Override |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 314 | public void onRestrictBackgroundChanged(boolean restrictBackground) { |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 315 | if (DEBUG) { |
| 316 | Slog.v(TAG, "Background restriction change to " + restrictBackground); |
| 317 | } |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 318 | updateTrackedJobs(-1); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Override |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 322 | public void onUidPoliciesChanged(int uid, int uidPolicies) { |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 323 | if (DEBUG) { |
| 324 | Slog.v(TAG, "Uid policy changed for " + uid); |
| 325 | } |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 326 | updateTrackedJobs(uid); |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 327 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 328 | }; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 329 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 330 | @GuardedBy("mLock") |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 331 | @Override |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 332 | public void dumpControllerStateLocked(PrintWriter pw, int filterUid) { |
Christopher Tate | 8bbe704 | 2017-03-15 11:07:46 -0700 | [diff] [blame] | 333 | pw.print("Connectivity: connected="); |
Makoto Onuki | de10e24 | 2018-02-05 12:56:20 -0800 | [diff] [blame] | 334 | pw.println(mConnected); |
| 335 | |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 336 | pw.print("Tracking "); |
| 337 | pw.print(mTrackedJobs.size()); |
Makoto Onuki | de10e24 | 2018-02-05 12:56:20 -0800 | [diff] [blame] | 338 | pw.println(" jobs"); |
| 339 | |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 340 | for (int i = 0; i < mTrackedJobs.size(); i++) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 341 | final JobStatus js = mTrackedJobs.valueAt(i); |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 342 | if (js.shouldDump(filterUid)) { |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 343 | pw.print(" #"); |
| 344 | js.printUniqueId(pw); |
| 345 | pw.print(" from "); |
| 346 | UserHandle.formatUid(pw, js.getSourceUid()); |
Makoto Onuki | de10e24 | 2018-02-05 12:56:20 -0800 | [diff] [blame] | 347 | pw.print(": "); |
| 348 | pw.print(js.getJob().getRequiredNetwork()); |
| 349 | pw.println(); |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 350 | } |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 351 | } |
| 352 | } |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 353 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 354 | @GuardedBy("mLock") |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 355 | @Override |
| 356 | public void dumpControllerStateLocked(ProtoOutputStream proto, long fieldId, int filterUid) { |
| 357 | final long token = proto.start(fieldId); |
| 358 | final long mToken = proto.start(StateControllerProto.CONNECTIVITY); |
| 359 | |
| 360 | proto.write(StateControllerProto.ConnectivityController.IS_CONNECTED, mConnected); |
| 361 | |
| 362 | for (int i = 0; i < mTrackedJobs.size(); i++) { |
| 363 | final JobStatus js = mTrackedJobs.valueAt(i); |
| 364 | if (!js.shouldDump(filterUid)) { |
| 365 | continue; |
| 366 | } |
| 367 | final long jsToken = proto.start(StateControllerProto.ConnectivityController.TRACKED_JOBS); |
| 368 | js.writeToShortProto(proto, StateControllerProto.ConnectivityController.TrackedJob.INFO); |
| 369 | proto.write(StateControllerProto.ConnectivityController.TrackedJob.SOURCE_UID, |
| 370 | js.getSourceUid()); |
| 371 | NetworkRequest rn = js.getJob().getRequiredNetwork(); |
| 372 | if (rn != null) { |
| 373 | rn.writeToProto(proto, |
| 374 | StateControllerProto.ConnectivityController.TrackedJob.REQUIRED_NETWORK); |
| 375 | } |
| 376 | proto.end(jsToken); |
| 377 | } |
| 378 | |
| 379 | proto.end(mToken); |
| 380 | proto.end(token); |
| 381 | } |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 382 | } |