1 /*
2  * Copyright (C) 2024 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 package com.android.nfc.emulator;
17 
18 import android.content.BroadcastReceiver;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.os.Bundle;
24 import android.util.Log;
25 
26 import com.android.nfc.service.ScreenOffPaymentService;
27 import com.android.nfc.utils.HceUtils;
28 
29 public class ScreenOffPaymentEmulatorActivity extends BaseEmulatorActivity {
30     private static final String TAG = "ScreenOffPaymentEm";
31     private static final int STATE_SCREEN_ON = 0;
32     private static final int STATE_SCREEN_OFF = 1;
33 
34     private int mState = STATE_SCREEN_ON;
35     private ScreenOnOffReceiver mScreenOnOffReceiver;
36 
37     @Override
onCreate(Bundle savedInstanceState)38     protected void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40         mState = STATE_SCREEN_ON;
41         setupServices(ScreenOffPaymentService.COMPONENT);
42         makeDefaultWalletRoleHolder();
43 
44         if (mAdapter.isSecureNfcSupported() && mAdapter.isSecureNfcEnabled()) {
45             boolean res = HceUtils.disableSecureNfc(mAdapter);
46             if (!res) {
47                 Log.e(TAG, "Problem while attempting to disable secure NFC");
48             }
49         }
50 
51         mScreenOnOffReceiver = new ScreenOnOffReceiver();
52         IntentFilter filter = new IntentFilter();
53         filter.addAction(Intent.ACTION_SCREEN_OFF);
54         filter.addAction(Intent.ACTION_SCREEN_ON);
55         registerReceiver(mScreenOnOffReceiver, filter, RECEIVER_EXPORTED);
56     }
57 
58     @Override
onResume()59     protected void onResume() {
60         super.onResume();
61     }
62 
63     @Override
onDestroy()64     protected void onDestroy() {
65         super.onDestroy();
66         unregisterReceiver(mScreenOnOffReceiver);
67     }
68 
69     @Override
onApduSequenceComplete(ComponentName component, long duration)70     public void onApduSequenceComplete(ComponentName component, long duration) {
71         if (component.equals(ScreenOffPaymentService.COMPONENT)
72                 && mState == STATE_SCREEN_OFF) {
73             setTestPassed();
74         }
75     }
76 
77     @Override
getPreferredServiceComponent()78     public ComponentName getPreferredServiceComponent() {
79         return ScreenOffPaymentService.COMPONENT;
80     }
81 
82     private class ScreenOnOffReceiver extends BroadcastReceiver {
83         @Override
onReceive(Context context, Intent intent)84         public void onReceive(Context context, Intent intent) {
85             String action = intent.getAction();
86             Log.d(TAG, "Received intent " + action);
87             if (action.equals(Intent.ACTION_SCREEN_OFF)) {
88                 mState = STATE_SCREEN_OFF;
89             } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
90                 mState = STATE_SCREEN_ON;
91             }
92         }
93     }
94 }
95