1 /*
2  * Copyright (C) 2020 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.settings.sim;
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.os.PersistableBundle;
26 import android.util.Log;
27 
28 import com.android.settings.R;
29 
30 /** A JobService sends SIM notifications. */
31 public class SimNotificationService extends JobService {
32 
33     private static final String TAG = "SimNotificationService";
34     private static final String EXTRA_NOTIFICATION_TYPE = "notification_type";
35 
36     /**
37      * Schedules a service to send SIM push notifications.
38      *
39      * @param context
40      * @param notificationType indicates which SIM notification to send.
41      */
scheduleSimNotification( Context context, @SimActivationNotifier.NotificationType int notificationType)42     public static void scheduleSimNotification(
43             Context context, @SimActivationNotifier.NotificationType int notificationType) {
44         final JobScheduler jobScheduler =
45                 context.getApplicationContext().getSystemService(JobScheduler.class);
46         final ComponentName component =
47                 new ComponentName(context.getApplicationContext(), SimNotificationService.class);
48         PersistableBundle extra = new PersistableBundle();
49         extra.putInt(EXTRA_NOTIFICATION_TYPE, notificationType);
50 
51         jobScheduler.schedule(
52                 new JobInfo.Builder(R.integer.sim_notification_send, component)
53                         .setExtras(extra)
54                         .build());
55     }
56 
57     @Override
onStartJob(JobParameters params)58     public boolean onStartJob(JobParameters params) {
59         PersistableBundle extra = params.getExtras();
60         if (extra == null) {
61             Log.e(TAG, "Failed to get notification type.");
62             return false;
63         }
64         int notificationType = extra.getInt(EXTRA_NOTIFICATION_TYPE);
65         switch (notificationType) {
66             case SimActivationNotifier.NotificationType.NETWORK_CONFIG:
67                 Log.i(TAG, "Sending SIM config notification.");
68                 SimActivationNotifier.setShowSimSettingsNotification(this, false);
69                 new SimActivationNotifier(this).sendNetworkConfigNotification();
70                 break;
71             case SimActivationNotifier.NotificationType.SWITCH_TO_REMOVABLE_SLOT:
72                 new SimActivationNotifier(this).sendSwitchedToRemovableSlotNotification();
73                 break;
74             case SimActivationNotifier.NotificationType.ENABLE_DSDS:
75                 new SimActivationNotifier(this).sendEnableDsdsNotification();
76                 break;
77             default:
78                 Log.e(TAG, "Invalid notification type: " + notificationType);
79                 break;
80         }
81         return false;
82     }
83 
84     @Override
onStopJob(JobParameters params)85     public boolean onStopJob(JobParameters params) {
86         return false;
87     }
88 }
89