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