1 /*
2  * Copyright (C) 2023 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.server.healthconnect.migration;
18 
19 import static com.android.server.healthconnect.migration.MigrationConstants.EXTRA_USER_ID;
20 
21 import android.app.job.JobParameters;
22 import android.app.job.JobService;
23 import android.content.Context;
24 import android.os.PersistableBundle;
25 import android.os.UserHandle;
26 import android.util.Slog;
27 
28 import com.android.server.healthconnect.HealthConnectThreadScheduler;
29 
30 /**
31  * A service that sends migration broadcast to migration aware apps.
32  *
33  * @hide
34  */
35 public class MigrationBroadcastJobService extends JobService {
36 
37     private static final String TAG = "MigrationBroadcastJobService";
38 
39     /** Called every time the operation corresponding to this service is to be performed. */
40     @Override
onStartJob(JobParameters params)41     public boolean onStartJob(JobParameters params) {
42         try {
43             Context context = getApplicationContext();
44 
45             PersistableBundle extras = params.getExtras();
46             int userId = extras.getInt(EXTRA_USER_ID);
47 
48             HealthConnectThreadScheduler.scheduleInternalTask(
49                     () -> {
50                         try {
51                             MigrationBroadcast migrationBroadcast =
52                                     new MigrationBroadcast(context, UserHandle.of(userId));
53                             migrationBroadcast.sendInvocationBroadcast();
54                         } catch (Exception e) {
55                             Slog.e(TAG, "Exception while executing job : ", e);
56                         }
57                     });
58         } catch (Exception e) {
59             // Don't rethrow as that will crash system_server
60             Slog.e(TAG, "Scheduled job failed to run", e);
61         }
62         return false;
63     }
64 
65     /** Called when job needs to be stopped. Don't do anything here and let the job be killed. */
66     @Override
onStopJob(JobParameters params)67     public boolean onStopJob(JobParameters params) {
68         return false;
69     }
70 }
71