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