1 /*
2  * Copyright (C) 2017 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 
17 package com.android.carrierdefaultapp;
18 
19 import android.app.job.JobInfo;
20 import android.app.job.JobParameters;
21 import android.app.job.JobScheduler;
22 import android.app.job.JobService;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.net.ConnectivityManager;
27 import android.net.Network;
28 import android.net.NetworkCapabilities;
29 import android.provider.Settings;
30 import android.telephony.TelephonyManager;
31 import android.util.Log;
32 
33 /**
34  * Service to run {@link android.app.job.JobScheduler} job.
35  * Service to monitor when there is a change to conent URI
36  * {@link android.provider.Settings.Global#DEVICE_PROVISIONED DEVICE_PROVISIONED}
37  */
38 public class ProvisionObserver extends JobService {
39 
40     private static final String TAG = ProvisionObserver.class.getSimpleName();
41     public static final int PROVISION_OBSERVER_REEVALUATION_JOB_ID = 1;
42     // minimum & maximum update delay TBD
43     private static final int CONTENT_UPDATE_DELAY_MS = 100;
44     private static final int CONTENT_MAX_DELAY_MS = 200;
45 
46     @Override
onStartJob(JobParameters jobParameters)47     public boolean onStartJob(JobParameters jobParameters) {
48         switch (jobParameters.getJobId()) {
49             case PROVISION_OBSERVER_REEVALUATION_JOB_ID:
50                 if (isProvisioned(this)) {
51                     Log.d(TAG, "device provisioned, force network re-evaluation");
52                     final ConnectivityManager connMgr = ConnectivityManager.from(this);
53                     Network[] info = connMgr.getAllNetworks();
54                     for (Network nw : info) {
55                         final NetworkCapabilities nc = connMgr.getNetworkCapabilities(nw);
56                         if (nc.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
57                                 && nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
58                             // force connectivity re-evaluation to assume skipped carrier actions.
59                             // one of the following calls will match the last evaluation.
60                             connMgr.reportNetworkConnectivity(nw, true);
61                             connMgr.reportNetworkConnectivity(nw, false);
62                             break;
63                         }
64                     }
65                 }
66             default:
67                 break;
68         }
69         return false;
70     }
71 
72     @Override
onStopJob(JobParameters jobParameters)73     public boolean onStopJob(JobParameters jobParameters) {
74         return false;
75     }
76 
77     // Returns true if the device is not provisioned yet (in setup wizard), false otherwise
isProvisioned(Context context)78     private static boolean isProvisioned(Context context) {
79         return Settings.Global.getInt(context.getContentResolver(),
80                 Settings.Global.DEVICE_PROVISIONED, 0) == 1;
81     }
82 
83     /**
84      * Static utility function to schedule a job to execute upon the change of content URI
85      * {@link android.provider.Settings.Global#DEVICE_PROVISIONED DEVICE_PROVISIONED}.
86      * @param context The context used to retrieve the {@link ComponentName} and system services
87      * @return true carrier actions are deferred due to phone provisioning process, false otherwise
88      */
isDeferredForProvision(Context context, Intent intent)89     public static boolean isDeferredForProvision(Context context, Intent intent) {
90         if (isProvisioned(context)) {
91             return false;
92         }
93         int jobId;
94         switch(intent.getAction()) {
95             case TelephonyManager.ACTION_CARRIER_SIGNAL_REDIRECTED:
96                 jobId = PROVISION_OBSERVER_REEVALUATION_JOB_ID;
97                 break;
98             default:
99                 return false;
100         }
101         final JobScheduler jobScheduler =  (JobScheduler) context.getSystemService(
102                 Context.JOB_SCHEDULER_SERVICE);
103         final JobInfo job = new JobInfo.Builder(jobId,
104                 new ComponentName(context, ProvisionObserver.class))
105                 .addTriggerContentUri(new JobInfo.TriggerContentUri(
106                         Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED), 0))
107                 .setTriggerContentUpdateDelay(CONTENT_UPDATE_DELAY_MS)
108                 .setTriggerContentMaxDelay(CONTENT_MAX_DELAY_MS)
109                 .build();
110         jobScheduler.schedule(job);
111         return true;
112     }
113 }
114