1 /*
2  * Copyright (C) 2022 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 android.alarmmanager.alarmtestapp.cts.common;
18 
19 import android.app.AlarmManager;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.util.Log;
24 
25 /**
26  * Receives ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED, and starts and FGS.
27  * Send whether it was able to start the FGS to the main test app using a broadcast.
28  */
29 public class PermissionStateChangedReceiver extends BroadcastReceiver {
30     private static final String TAG = PermissionStateChangedReceiver.class.getSimpleName();
31     private static final String PACKAGE_NAME = "android.alarmmanager.alarmtestapp.cts.common";
32     private static final String MAIN_CTS_PACKAGE = "android.alarmmanager.cts";
33 
34     public static final String ACTION_FGS_START_RESULT = PACKAGE_NAME + ".action.FGS_START_RESULT";
35 
36     @Override
onReceive(Context context, Intent intent)37     public void onReceive(Context context, Intent intent) {
38         Log.d(TAG, "Broadcast received: " + intent);
39         if (AlarmManager.ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED
40                 .equals(intent.getAction())) {
41 
42             final String result = FgsTester.tryStartingFgs(context);
43 
44             // Send the result broadcast to the main CTS package.
45             final Intent response = new Intent(ACTION_FGS_START_RESULT);
46             response.setPackage(MAIN_CTS_PACKAGE);
47             response.putExtra(FgsTester.EXTRA_FGS_START_RESULT, result);
48 
49             Log.d(TAG, "Sending response: " + result);
50             context.sendBroadcast(response);
51         }
52     }
53 
54 }
55