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; 18 19 import static android.health.connect.Constants.DEFAULT_INT; 20 21 import static com.android.server.healthconnect.HealthConnectDailyService.EXTRA_JOB_NAME_KEY; 22 import static com.android.server.healthconnect.HealthConnectDailyService.EXTRA_USER_ID; 23 24 import android.annotation.NonNull; 25 import android.annotation.UserIdInt; 26 import android.app.job.JobInfo; 27 import android.app.job.JobParameters; 28 import android.app.job.JobScheduler; 29 import android.content.ComponentName; 30 import android.content.Context; 31 import android.os.PersistableBundle; 32 import android.os.UserHandle; 33 34 import com.android.server.healthconnect.logging.DailyLoggingService; 35 import com.android.server.healthconnect.storage.AutoDeleteService; 36 37 import java.util.Objects; 38 import java.util.concurrent.TimeUnit; 39 40 /** @hide */ 41 public class HealthConnectDailyJobs { 42 public static final String HC_DAILY_JOB = "hc_daily_job"; 43 private static final int MIN_JOB_ID = HealthConnectDailyJobs.class.hashCode(); 44 private static final long JOB_RUN_INTERVAL = TimeUnit.DAYS.toMillis(1); 45 private static final String HEALTH_CONNECT_NAMESPACE = "HEALTH_CONNECT_DAILY_JOB"; 46 schedule(@onNull Context context, @UserIdInt int userId)47 public static void schedule(@NonNull Context context, @UserIdInt int userId) { 48 ComponentName componentName = new ComponentName(context, HealthConnectDailyService.class); 49 final PersistableBundle extras = new PersistableBundle(); 50 extras.putInt(EXTRA_USER_ID, userId); 51 extras.putString(EXTRA_JOB_NAME_KEY, HC_DAILY_JOB); 52 JobInfo.Builder builder = 53 new JobInfo.Builder(MIN_JOB_ID + userId, componentName) 54 .setExtras(extras) 55 .setRequiresCharging(true) 56 .setRequiresDeviceIdle(true) 57 .setPeriodic(JOB_RUN_INTERVAL, JOB_RUN_INTERVAL / 2); 58 59 HealthConnectDailyService.schedule( 60 Objects.requireNonNull(context.getSystemService(JobScheduler.class)) 61 .forNamespace(HEALTH_CONNECT_NAMESPACE), 62 userId, 63 builder.build()); 64 } 65 cancelAllJobs(Context context)66 public static void cancelAllJobs(Context context) { 67 Objects.requireNonNull(context.getSystemService(JobScheduler.class)) 68 .forNamespace(HEALTH_CONNECT_NAMESPACE) 69 .cancelAll(); 70 } 71 execute(@onNull Context context, JobParameters params)72 public static void execute(@NonNull Context context, JobParameters params) { 73 int userId = params.getExtras().getInt(EXTRA_USER_ID, /* defaultValue= */ DEFAULT_INT); 74 AutoDeleteService.startAutoDelete(context); 75 DailyLoggingService.logDailyMetrics(context, UserHandle.getUserHandleForUid(userId)); 76 } 77 } 78