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