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 | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 19 | import android.app.job.JobInfo; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
| 24 | import android.net.ConnectivityManager; |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 25 | import android.net.INetworkPolicyListener; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 26 | import android.net.NetworkInfo; |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 27 | import android.net.NetworkPolicyManager; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 28 | import android.os.UserHandle; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 29 | import android.util.Slog; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 30 | |
wangshujie | b49420a7 | 2016-03-16 15:20:04 +0800 | [diff] [blame] | 31 | import com.android.internal.os.BackgroundThread; |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 32 | import com.android.internal.annotations.GuardedBy; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 33 | import com.android.server.job.JobSchedulerService; |
| 34 | import com.android.server.job.StateChangedListener; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 35 | |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 36 | import java.io.PrintWriter; |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 37 | import java.util.ArrayList; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 40 | * Handles changes in connectivity. |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 41 | * <p> |
| 42 | * Each app can have a different default networks or different connectivity |
| 43 | * status due to user-requested network policies, so we need to check |
| 44 | * constraints on a per-UID basis. |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 45 | */ |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 46 | public class ConnectivityController extends StateController implements |
| 47 | ConnectivityManager.OnNetworkActiveListener { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 48 | private static final String TAG = "JobScheduler.Conn"; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 49 | |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 50 | private final ConnectivityManager mConnManager; |
| 51 | private final NetworkPolicyManager mNetPolicyManager; |
| 52 | |
| 53 | @GuardedBy("mLock") |
| 54 | private final ArrayList<JobStatus> mTrackedJobs = new ArrayList<JobStatus>(); |
| 55 | |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 56 | /** Singleton. */ |
| 57 | private static ConnectivityController mSingleton; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 58 | private static Object sCreationLock = new Object(); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 59 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 60 | public static ConnectivityController get(JobSchedulerService jms) { |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 61 | synchronized (sCreationLock) { |
| 62 | if (mSingleton == null) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 63 | mSingleton = new ConnectivityController(jms, jms.getContext(), jms.getLock()); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 64 | } |
| 65 | return mSingleton; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 66 | } |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 69 | private ConnectivityController(StateChangedListener stateChangedListener, Context context, |
| 70 | Object lock) { |
| 71 | super(stateChangedListener, context, lock); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 72 | |
| 73 | mConnManager = mContext.getSystemService(ConnectivityManager.class); |
| 74 | mNetPolicyManager = mContext.getSystemService(NetworkPolicyManager.class); |
| 75 | |
| 76 | final IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 77 | mContext.registerReceiverAsUser( |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 78 | mConnectivityReceiver, UserHandle.SYSTEM, intentFilter, null, null); |
| 79 | |
| 80 | mNetPolicyManager.registerListener(mNetPolicyListener); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 84 | public void maybeStartTrackingJobLocked(JobStatus jobStatus, JobStatus lastJob) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 85 | if (jobStatus.hasConnectivityConstraint() || jobStatus.hasUnmeteredConstraint() |
| 86 | || jobStatus.hasNotRoamingConstraint()) { |
| 87 | updateConstraintsSatisfied(jobStatus); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 88 | mTrackedJobs.add(jobStatus); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | @Override |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 93 | public void maybeStopTrackingJobLocked(JobStatus jobStatus, JobStatus incomingJob, |
| 94 | boolean forUpdate) { |
| 95 | if (jobStatus.hasConnectivityConstraint() || jobStatus.hasUnmeteredConstraint() |
| 96 | || jobStatus.hasNotRoamingConstraint()) { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 97 | mTrackedJobs.remove(jobStatus); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 98 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 101 | private boolean updateConstraintsSatisfied(JobStatus jobStatus) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 102 | final boolean ignoreBlocked = (jobStatus.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) != 0; |
| 103 | final NetworkInfo info = mConnManager.getActiveNetworkInfoForUid(jobStatus.getSourceUid(), |
| 104 | ignoreBlocked); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 105 | final boolean connected = (info != null) && info.isConnected(); |
| 106 | final boolean unmetered = connected && !info.isMetered(); |
| 107 | final boolean notRoaming = connected && !info.isRoaming(); |
| 108 | |
| 109 | boolean changed = false; |
| 110 | changed |= jobStatus.setConnectivityConstraintSatisfied(connected); |
| 111 | changed |= jobStatus.setUnmeteredConstraintSatisfied(unmetered); |
| 112 | changed |= jobStatus.setNotRoamingConstraintSatisfied(notRoaming); |
| 113 | return changed; |
| 114 | } |
| 115 | |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 116 | /** |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 117 | * Update all jobs tracked by this controller. |
| 118 | * |
| 119 | * @param uid only update jobs belonging to this UID, or {@code -1} to |
| 120 | * update all tracked jobs. |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 121 | */ |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 122 | private void updateTrackedJobs(int uid) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 123 | synchronized (mLock) { |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 124 | boolean changed = false; |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 125 | for (int i = 0; i < mTrackedJobs.size(); i++) { |
| 126 | final JobStatus js = mTrackedJobs.get(i); |
| 127 | if (uid == -1 || uid == js.getSourceUid()) { |
| 128 | changed |= updateConstraintsSatisfied(js); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 129 | } |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 130 | } |
| 131 | if (changed) { |
| 132 | mStateChangedListener.onControllerStateChanged(); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 138 | * 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] | 139 | */ |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 140 | @Override |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 141 | public synchronized void onNetworkActive() { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 142 | synchronized (mLock) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 143 | for (int i = 0; i < mTrackedJobs.size(); i++) { |
| 144 | final JobStatus js = mTrackedJobs.get(i); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 145 | if (js.isReady()) { |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 146 | if (DEBUG) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 147 | Slog.d(TAG, "Running " + js + " due to network activity."); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 148 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 149 | mStateChangedListener.onRunJobNow(js); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 152 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 155 | private BroadcastReceiver mConnectivityReceiver = new BroadcastReceiver() { |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 156 | @Override |
| 157 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 158 | updateTrackedJobs(-1); |
| 159 | } |
| 160 | }; |
| 161 | |
| 162 | private INetworkPolicyListener mNetPolicyListener = new INetworkPolicyListener.Stub() { |
| 163 | @Override |
| 164 | public void onUidRulesChanged(int uid, int uidRules) { |
| 165 | updateTrackedJobs(uid); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public void onMeteredIfacesChanged(String[] meteredIfaces) { |
| 170 | updateTrackedJobs(-1); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void onRestrictBackgroundChanged(boolean restrictBackground) { |
| 175 | updateTrackedJobs(-1); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void onRestrictBackgroundWhitelistChanged(int uid, boolean whitelisted) { |
| 180 | updateTrackedJobs(uid); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 181 | } |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 182 | |
| 183 | @Override |
| 184 | public void onRestrictBackgroundBlacklistChanged(int uid, boolean blacklisted) { |
| 185 | updateTrackedJobs(uid); |
| 186 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 187 | }; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 188 | |
| 189 | @Override |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 190 | public void dumpControllerStateLocked(PrintWriter pw, int filterUid) { |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 191 | pw.println("Conn."); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 192 | for (int i = 0; i < mTrackedJobs.size(); i++) { |
| 193 | final JobStatus js = mTrackedJobs.get(i); |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 194 | if (js.shouldDump(filterUid)) { |
| 195 | pw.println(String.valueOf(js.getJobId() + "," + js.getUid()) |
| 196 | + ": C=" + js.hasConnectivityConstraint() |
| 197 | + ", UM=" + js.hasUnmeteredConstraint() |
| 198 | + ", NR=" + js.hasNotRoamingConstraint()); |
| 199 | } |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 202 | } |