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 17 package com.android.nfc; 18 19 import android.annotation.NonNull; 20 import android.app.AlarmManager; 21 import android.app.PendingIntent; 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.os.Process; 27 import android.os.SystemClock; 28 import android.util.Log; 29 30 import java.util.concurrent.CountDownLatch; 31 import java.util.concurrent.TimeUnit; 32 33 /** @hide */ 34 public class NfcWatchdog extends BroadcastReceiver { 35 static final String TAG = "NfcWatchdog"; 36 static final long NFC_SERVICE_TIMEOUT_MS = 1000; 37 static final long NFC_MONITOR_INTERVAL = 60_000; 38 static final String ACTION_WATCHDOG = "android.nfc.intent.action.WATCHDOG"; 39 40 CountDownLatch mCountDownLatch; 41 Context mContext; 42 NfcWatchdog(Context context)43 NfcWatchdog(Context context) { 44 mContext = context; 45 if (android.nfc.Flags.nfcWatchdog()) { 46 context.registerReceiver(this, new IntentFilter(ACTION_WATCHDOG), 47 Context.RECEIVER_EXPORTED); 48 } 49 } 50 notifyHasReturned()51 synchronized void notifyHasReturned() { 52 if (mCountDownLatch != null) { 53 mCountDownLatch.countDown(); 54 } 55 } 56 57 @Override onReceive(Context context, Intent intent)58 public void onReceive(Context context, Intent intent) { 59 if (ACTION_WATCHDOG.equals(intent.getAction())) { 60 monitor(); 61 } 62 } 63 monitor()64 public synchronized void monitor() { 65 if (mCountDownLatch != null) { 66 return; 67 } 68 69 mCountDownLatch = new CountDownLatch(1); 70 71 Thread testThread = new TestThread(); 72 new MonitorThread(testThread).start(); 73 testThread.start(); 74 } 75 killNfcProcess()76 void killNfcProcess() { 77 Log.wtf(TAG, "killNfcProcess"); 78 Process.killProcess(Process.myPid()); 79 } 80 81 class TestThread extends Thread { 82 83 @Override run()84 public void run() { 85 final NfcService nfcService = NfcService.getInstance(); 86 if (nfcService != null) { 87 synchronized (nfcService) { 88 nfcService.sendMessage(NfcService.MSG_WATCHDOG_PING, NfcWatchdog.this); 89 } 90 } 91 } 92 } 93 94 class MonitorThread extends Thread { 95 Thread mTestThread; 96 MonitorThread(@onNull Thread testThread)97 MonitorThread(@NonNull Thread testThread) { 98 mTestThread = testThread; 99 } 100 101 @Override run()102 public void run() { 103 try { 104 if (!mCountDownLatch.await(NFC_SERVICE_TIMEOUT_MS, 105 TimeUnit.MILLISECONDS)) { 106 killNfcProcess(); 107 } 108 synchronized (NfcWatchdog.this) { 109 mCountDownLatch = null; 110 } 111 } catch (InterruptedException e) { 112 Log.wtf(TAG, e); 113 } 114 } 115 } 116 ensureWatchdogMonitoring()117 void ensureWatchdogMonitoring() { 118 if (android.nfc.Flags.nfcWatchdog()) { 119 Intent watchdogIntent = new Intent(ACTION_WATCHDOG); 120 AlarmManager alarmManager = mContext.getSystemService(AlarmManager.class); 121 PendingIntent pendingIntent = PendingIntent.getBroadcast( 122 mContext, 0, watchdogIntent, PendingIntent.FLAG_IMMUTABLE); 123 if (alarmManager != null && alarmManager.getNextAlarmClock() == null) { 124 alarmManager.setInexactRepeating( 125 AlarmManager.ELAPSED_REALTIME, 126 SystemClock.elapsedRealtime() + NFC_MONITOR_INTERVAL, 127 NFC_MONITOR_INTERVAL, 128 pendingIntent); 129 } 130 } 131 } 132 stopMonitoring()133 void stopMonitoring() { 134 AlarmManager alarmManager = mContext.getSystemService(AlarmManager.class); 135 if (alarmManager != null) { 136 alarmManager.cancelAll(); 137 } 138 } 139 } 140