|
| 1 | +/* |
| 2 | + * Copyright 2025 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.mobileer.oboetester; |
| 18 | + |
| 19 | +import android.app.AlarmManager; |
| 20 | +import android.app.PendingIntent; |
| 21 | +import android.content.Context; |
| 22 | +import android.content.Intent; |
| 23 | +import android.os.Build; |
| 24 | +import android.os.SystemClock; |
| 25 | +import android.provider.Settings; |
| 26 | +import android.util.Log; |
| 27 | +import android.widget.Toast; |
| 28 | + |
| 29 | +public class TestTimeoutScheduler { |
| 30 | + |
| 31 | + private static final int REQUEST_CODE = 54321; |
| 32 | + private static final String TAG = "TestTimeoutScheduler"; |
| 33 | + |
| 34 | + public void scheduleTestTimeout(Context context, int durationSeconds) { |
| 35 | + Log.d(TAG, "Attempting to schedule test timeout."); |
| 36 | + AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 37 | + if (alarmManager == null) { |
| 38 | + Log.e(TAG, "AlarmManager is null."); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + // On Android 12 (S) and higher, we need to check for the permission. |
| 43 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { |
| 44 | + if (!alarmManager.canScheduleExactAlarms()) { |
| 45 | + Log.w(TAG, "Missing SCHEDULE_EXACT_ALARM permission."); |
| 46 | + Toast.makeText(context, "Permission needed to schedule test timeout", Toast.LENGTH_LONG).show(); |
| 47 | + // Guide the user to the settings screen to grant the permission. |
| 48 | + Intent intent = new Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM); |
| 49 | + context.startActivity(intent); |
| 50 | + return; // Stop execution until the permission is granted. |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + Intent intent = new Intent(context, TestTimeoutReceiver.class); |
| 55 | + PendingIntent pendingIntent = PendingIntent.getBroadcast( |
| 56 | + context, |
| 57 | + REQUEST_CODE, |
| 58 | + intent, |
| 59 | + PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE |
| 60 | + ); |
| 61 | + |
| 62 | + long triggerAtMillis = SystemClock.elapsedRealtime() + durationSeconds * 1000L; |
| 63 | + |
| 64 | + try { |
| 65 | + alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtMillis, pendingIntent); |
| 66 | + Log.i(TAG, "Exact alarm scheduled successfully for " + durationSeconds + " seconds."); |
| 67 | + } catch (SecurityException se) { |
| 68 | + // This catch is now a fallback, as the check above should prevent this. |
| 69 | + Log.e(TAG, "SecurityException while setting alarm. This should not happen after the check.", se); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public void cancelTestTimeout(Context context) { |
| 74 | + AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 75 | + if (alarmManager == null) { |
| 76 | + Log.e(TAG, "AlarmManager is null when trying to cancel."); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + Intent intent = new Intent(context, TestTimeoutReceiver.class); |
| 81 | + PendingIntent pendingIntent = PendingIntent.getBroadcast( |
| 82 | + context, |
| 83 | + REQUEST_CODE, |
| 84 | + intent, |
| 85 | + PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE |
| 86 | + ); |
| 87 | + |
| 88 | + alarmManager.cancel(pendingIntent); |
| 89 | + Log.i(TAG, "Canceled test timeout alarm."); |
| 90 | + } |
| 91 | +} |
0 commit comments