FileDocCategorySizeDatePackage
ConnectivityController.javaAPI DocAndroid 5.1 API8311Thu Mar 12 22:22:42 GMT 2015com.android.server.job.controllers

ConnectivityController

public class ConnectivityController extends StateController implements ConnectivityManager.OnNetworkActiveListener
Handles changes in connectivity. We are only interested in metered vs. unmetered networks, and we're interested in them on a per-user basis.

Fields Summary
private static final String
TAG
private final List
mTrackedJobs
private final android.content.BroadcastReceiver
mConnectivityChangedReceiver
private static ConnectivityController
mSingleton
Singleton.
private static Object
sCreationLock
private boolean
mNetworkUnmetered
Track whether the latest active network is metered.
private boolean
mNetworkConnected
Track whether the latest active network is connected.
Constructors Summary
private ConnectivityController(com.android.server.job.StateChangedListener stateChangedListener, android.content.Context context)

        super(stateChangedListener, context);
        // Register connectivity changed BR.
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        mContext.registerReceiverAsUser(
                mConnectivityChangedReceiver, UserHandle.ALL, intentFilter, null, null);
        ConnectivityService cs =
                (ConnectivityService)ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
        if (cs != null) {
            if (cs.getActiveNetworkInfo() != null) {
                mNetworkConnected = cs.getActiveNetworkInfo().isConnected();
            }
            mNetworkUnmetered = mNetworkConnected && !cs.isActiveNetworkMetered();
        }
    
Methods Summary
public voiddumpControllerState(java.io.PrintWriter pw)

        pw.println("Conn.");
        pw.println("connected: " + mNetworkConnected + " unmetered: " + mNetworkUnmetered);
        for (JobStatus js: mTrackedJobs) {
            pw.println(String.valueOf(js.hashCode()).substring(0, 3) + ".."
                    + ": C=" + js.hasConnectivityConstraint()
                    + ", UM=" + js.hasUnmeteredConstraint());
        }
    
public static com.android.server.job.controllers.ConnectivityControllerget(com.android.server.job.JobSchedulerService jms)


         
        synchronized (sCreationLock) {
            if (mSingleton == null) {
                mSingleton = new ConnectivityController(jms, jms.getContext());
            }
            return mSingleton;
        }
    
public voidmaybeStartTrackingJob(JobStatus jobStatus)

        if (jobStatus.hasConnectivityConstraint() || jobStatus.hasUnmeteredConstraint()) {
            synchronized (mTrackedJobs) {
                jobStatus.connectivityConstraintSatisfied.set(mNetworkConnected);
                jobStatus.unmeteredConstraintSatisfied.set(mNetworkUnmetered);
                mTrackedJobs.add(jobStatus);
            }
        }
    
public voidmaybeStopTrackingJob(JobStatus jobStatus)

        if (jobStatus.hasConnectivityConstraint() || jobStatus.hasUnmeteredConstraint()) {
            synchronized (mTrackedJobs) {
                mTrackedJobs.remove(jobStatus);
            }
        }
    
public synchronized voidonNetworkActive()
We know the network has just come up. We want to run any jobs that are ready.

        synchronized (mTrackedJobs) {
            for (JobStatus js : mTrackedJobs) {
                if (js.isReady()) {
                    if (DEBUG) {
                        Slog.d(TAG, "Running " + js + " due to network activity.");
                    }
                    mStateChangedListener.onRunJobNow(js);
                }
            }
        }
    
private voidupdateTrackedJobs(int userId)

param
userId Id of the user for whom we are updating the connectivity state.

        synchronized (mTrackedJobs) {
            boolean changed = false;
            for (JobStatus js : mTrackedJobs) {
                if (js.getUserId() != userId) {
                    continue;
                }
                boolean prevIsConnected =
                        js.connectivityConstraintSatisfied.getAndSet(mNetworkConnected);
                boolean prevIsMetered = js.unmeteredConstraintSatisfied.getAndSet(mNetworkUnmetered);
                if (prevIsConnected != mNetworkConnected || prevIsMetered != mNetworkUnmetered) {
                    changed = true;
                }
            }
            if (changed) {
                mStateChangedListener.onControllerStateChanged();
            }
        }