diff options
| author | Alisher Alikhodjaev <alisher@google.com> | 2022-11-22 14:52:46 -0800 |
|---|---|---|
| committer | Alisher Alikhodjaev <alisher@google.com> | 2023-03-06 10:04:35 -0800 |
| commit | 8edf64d5f3f1c5500cdb083e01764e957776b27c (patch) | |
| tree | 17b344d3057efdfaa0a37af8332a6b718515fcc8 | |
| parent | bb4359638680eeca472a96fd90aefa3de4512338 (diff) | |
NFC ForegroundUtils cleanup for mainline
Use OnUidImportanceListener instead of IProcessObserver
Bug: 244264995
Bug: 235863754
Test: read a tag, nfc on/off
Merged-In: Ibe8515b153b54ad1780bda73fc4bb8196ca92bcc
Change-Id: Ibe8515b153b54ad1780bda73fc4bb8196ca92bcc
(cherry picked from commit 70a28509329c06465a7cc744fe9df9b184be8a63)
| -rw-r--r-- | src/com/android/nfc/ForegroundUtils.java | 110 | ||||
| -rw-r--r-- | src/com/android/nfc/NfcDispatcher.java | 14 | ||||
| -rw-r--r-- | src/com/android/nfc/NfcService.java | 10 | ||||
| -rwxr-xr-x | src/com/android/nfc/P2pLinkManager.java | 4 | ||||
| -rw-r--r-- | src/com/android/nfc/cardemulation/EnabledNfcFServices.java | 5 | ||||
| -rw-r--r-- | src/com/android/nfc/cardemulation/PreferredServices.java | 4 |
6 files changed, 71 insertions, 76 deletions
diff --git a/src/com/android/nfc/ForegroundUtils.java b/src/com/android/nfc/ForegroundUtils.java index 4ecec4b1..fb661181 100644 --- a/src/com/android/nfc/ForegroundUtils.java +++ b/src/com/android/nfc/ForegroundUtils.java @@ -16,9 +16,6 @@ package com.android.nfc; import android.app.ActivityManager; -import android.app.IActivityManager; -import android.app.IProcessObserver; -import android.os.RemoteException; import android.os.SystemProperties; import android.util.Log; import android.util.SparseArray; @@ -27,10 +24,10 @@ import android.util.SparseBooleanArray; import java.util.ArrayList; import java.util.List; -public class ForegroundUtils extends IProcessObserver.Stub { +public class ForegroundUtils implements ActivityManager.OnUidImportanceListener { static final boolean DBG = SystemProperties.getBoolean("persist.nfc.debug_enabled", false);; private final String TAG = "ForegroundUtils"; - private final IActivityManager mIActivityManager; + private final ActivityManager mActivityManager; private final Object mLock = new Object(); // We need to keep track of the individual PIDs per UID, @@ -41,17 +38,20 @@ public class ForegroundUtils extends IProcessObserver.Stub { private final SparseArray<List<Callback>> mBackgroundCallbacks = new SparseArray<List<Callback>>(); + private final SparseBooleanArray mForegroundUids = new SparseBooleanArray(); + private static class Singleton { - private static final ForegroundUtils INSTANCE = new ForegroundUtils(); + private static ForegroundUtils sInstance = null; } - private ForegroundUtils() { - mIActivityManager = ActivityManager.getService(); + private ForegroundUtils(ActivityManager am) { + mActivityManager = am; try { - mIActivityManager.registerProcessObserver(this); - } catch (RemoteException e) { + mActivityManager.addOnUidImportanceListener(this, + ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND); + } catch (Exception e) { // Should not happen! - Log.e(TAG, "ForegroundUtils: could not get IActivityManager"); + Log.e(TAG, "ForegroundUtils: could not register UidImportanceListener"); } } @@ -59,8 +59,17 @@ public class ForegroundUtils extends IProcessObserver.Stub { void onUidToBackground(int uid); } - public static ForegroundUtils getInstance() { - return Singleton.INSTANCE; + /** + * Get an instance of the ForegroundUtils sinleton + * + * @param am The ActivityManager instance for initialization + * @return the instance + */ + public static ForegroundUtils getInstance(ActivityManager am) { + if (Singleton.sInstance == null) { + Singleton.sInstance = new ForegroundUtils(am); + } + return Singleton.sInstance; } /** @@ -103,27 +112,26 @@ public class ForegroundUtils extends IProcessObserver.Stub { * if none are found. */ public List<Integer> getForegroundUids() { - ArrayList<Integer> uids = new ArrayList<Integer>(mForegroundUidPids.size()); + ArrayList<Integer> uids = new ArrayList<Integer>(mForegroundUids.size()); synchronized (mLock) { - for (int i = 0; i < mForegroundUidPids.size(); i++) { - uids.add(mForegroundUidPids.keyAt(i)); + for (int i = 0; i < mForegroundUids.size(); i++) { + if (mForegroundUids.valueAt(i)) { + uids.add(mForegroundUids.keyAt(i)); + } } } return uids; } private boolean isInForegroundLocked(int uid) { - if (mForegroundUidPids.get(uid) != null) + if (mForegroundUids.get(uid)) { return true; - if (DBG) Log.d(TAG, "Checking UID:" + Integer.toString(uid)); - try { - // If the onForegroundActivitiesChanged() has not yet been called, - // check whether the UID is in an active state to use the NFC. - return mIActivityManager.isUidActive(uid, NfcApplication.NFC_PROCESS); - } catch (RemoteException e) { - Log.e(TAG, "ForegroundUtils: could not get isUidActive"); } - return false; + if (DBG) Log.d(TAG, "Checking UID:" + Integer.toString(uid)); + // If the onForegroundActivitiesChanged() has not yet been called, + // check whether the UID is in an active state to use the NFC. + return (mActivityManager.getUidImportance(uid) + == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND); } private void handleUidToBackground(int uid) { @@ -145,55 +153,35 @@ public class ForegroundUtils extends IProcessObserver.Stub { } @Override - public void onForegroundActivitiesChanged(int pid, int uid, - boolean hasForegroundActivities) throws RemoteException { + public void onUidImportance(int uid, int importance) { boolean uidToBackground = false; synchronized (mLock) { - SparseBooleanArray foregroundPids = mForegroundUidPids.get(uid, - new SparseBooleanArray()); - if (hasForegroundActivities) { - foregroundPids.put(pid, true); - } else { - foregroundPids.delete(pid); + if (importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_GONE) { + mForegroundUids.delete(uid); + mBackgroundCallbacks.remove(uid); + if (DBG) Log.d(TAG, "UID: " + Integer.toString(uid) + " deleted."); + return; } - if (foregroundPids.size() == 0) { - mForegroundUidPids.remove(uid); - uidToBackground = true; + if (importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { + mForegroundUids.put(uid, true); } else { - mForegroundUidPids.put(uid, foregroundPids); + if (mForegroundUids.get(uid)) { + uidToBackground = true; + mForegroundUids.put(uid, false); + } } } if (uidToBackground) { handleUidToBackground(uid); } if (DBG) { - if (DBG) Log.d(TAG, "Foreground changed, PID: " + Integer.toString(pid) + " UID: " + - Integer.toString(uid) + " foreground: " + - hasForegroundActivities); + Log.d(TAG, "Foreground UID status:"); synchronized (mLock) { - Log.d(TAG, "Foreground UID/PID combinations:"); - for (int i = 0; i < mForegroundUidPids.size(); i++) { - int foregroundUid = mForegroundUidPids.keyAt(i); - SparseBooleanArray foregroundPids = mForegroundUidPids.get(foregroundUid); - if (foregroundPids.size() == 0) { - Log.e(TAG, "No PIDS associated with foreground UID!"); - } - for (int j = 0; j < foregroundPids.size(); j++) - Log.d(TAG, "UID: " + Integer.toString(foregroundUid) + " PID: " + - Integer.toString(foregroundPids.keyAt(j))); + for (int j = 0; j < mForegroundUids.size(); j++) { + Log.d(TAG, "UID: " + Integer.toString(mForegroundUids.keyAt(j)) + + " is in foreground: " + Boolean.toString(mForegroundUids.valueAt(j))); } } } } - - @Override - public void onForegroundServicesChanged(int pid, int uid, int fgServiceTypes) { - } - - @Override - public void onProcessDied(int pid, int uid) throws RemoteException { - if (DBG) Log.d(TAG, "Process died; UID " + Integer.toString(uid) + " PID " + - Integer.toString(pid)); - onForegroundActivitiesChanged(pid, uid, false); - } } diff --git a/src/com/android/nfc/NfcDispatcher.java b/src/com/android/nfc/NfcDispatcher.java index e079d6d6..e8f2c504 100644 --- a/src/com/android/nfc/NfcDispatcher.java +++ b/src/com/android/nfc/NfcDispatcher.java @@ -19,7 +19,6 @@ package com.android.nfc; import android.app.Activity; import android.app.ActivityManager; import android.app.AlertDialog; -import android.app.IActivityManager; import android.app.PendingIntent; import android.app.PendingIntent.CanceledException; import android.bluetooth.BluetoothAdapter; @@ -45,7 +44,6 @@ import android.os.Handler; import android.os.Message; import android.os.Messenger; import android.os.Process; -import android.os.RemoteException; import android.os.SystemProperties; import android.os.UserHandle; import android.os.UserManager; @@ -83,7 +81,6 @@ class NfcDispatcher { static final int DISPATCH_UNLOCK = 3; private final Context mContext; - private final IActivityManager mIActivityManager; private final RegisteredComponentCache mTechListFilters; private final ContentResolver mContentResolver; private final HandoverDataParser mHandoverDataParser; @@ -107,7 +104,6 @@ class NfcDispatcher { HandoverDataParser handoverDataParser, boolean provisionOnly) { mContext = context; - mIActivityManager = ActivityManager.getService(); mTechListFilters = new RegisteredComponentCache(mContext, NfcAdapter.ACTION_TECH_DISCOVERED, NfcAdapter.ACTION_TECH_DISCOVERED); mContentResolver = context.getContentResolver(); @@ -116,7 +112,8 @@ class NfcDispatcher { mNfcUnlockManager = NfcUnlockManager.getInstance(); mDeviceSupportsBluetooth = BluetoothAdapter.getDefaultAdapter() != null; mForegroundUid = Process.INVALID_UID; - mForegroundUtils = ForegroundUtils.getInstance(); + mForegroundUtils = ForegroundUtils.getInstance( + context.getSystemService(ActivityManager.class)); synchronized (this) { mProvisioningOnly = provisionOnly; } @@ -783,9 +780,10 @@ class NfcDispatcher { * resumeAppSwitches(). */ void resumeAppSwitches() { - try { - mIActivityManager.resumeAppSwitches(); - } catch (RemoteException e) { } + //// Should be auto resumed after S + // try { + // mIActivityManager.resumeAppSwitches(); + // } catch (RemoteException e) { } } /** Returns true if the tech list filter matches the techs on the tag */ diff --git a/src/com/android/nfc/NfcService.java b/src/com/android/nfc/NfcService.java index c5094be2..7e99b8b9 100644 --- a/src/com/android/nfc/NfcService.java +++ b/src/com/android/nfc/NfcService.java @@ -248,6 +248,7 @@ public class NfcService implements DeviceHostListener { VibrationAttributes.createForUsage(VibrationAttributes.USAGE_HARDWARE_FEEDBACK); private final UserManager mUserManager; + private final ActivityManager mActivityManager; private static int nci_version = NCI_VERSION_1_0; // NFC Execution Environment @@ -533,9 +534,10 @@ public class NfcService implements DeviceHostListener { | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "NfcService:mRequireUnlockWakeLock"); - mKeyguard = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); - mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); - mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); + mKeyguard = mContext.getSystemService(KeyguardManager.class); + mUserManager = mContext.getSystemService(UserManager.class); + mActivityManager = mContext.getSystemService(ActivityManager.class); + mVibrator = mContext.getSystemService(Vibrator.class); mVibrationEffect = VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE); mScreenState = mScreenStateHelper.checkScreenState(); @@ -600,7 +602,7 @@ public class NfcService implements DeviceHostListener { if (mIsHceCapable) { mCardEmulationManager = new CardEmulationManager(mContext); } - mForegroundUtils = ForegroundUtils.getInstance(); + mForegroundUtils = ForegroundUtils.getInstance(mActivityManager); mIsSecureNfcCapable = mNfcAdapter.deviceSupportsNfcSecure(); mIsSecureNfcEnabled = diff --git a/src/com/android/nfc/P2pLinkManager.java b/src/com/android/nfc/P2pLinkManager.java index fc39c0ac..b5edb209 100755 --- a/src/com/android/nfc/P2pLinkManager.java +++ b/src/com/android/nfc/P2pLinkManager.java @@ -16,6 +16,7 @@ package com.android.nfc; +import android.app.ActivityManager; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; @@ -282,7 +283,8 @@ class P2pLinkManager implements Handler.Callback, P2pEventListener.Callback { mDefaultRwSize = defaultRwSize; mLlcpServicesConnected = false; mNdefCallbackUid = -1; - mForegroundUtils = ForegroundUtils.getInstance(); + mForegroundUtils = ForegroundUtils.getInstance( + context.getSystemService(ActivityManager.class)); } /** diff --git a/src/com/android/nfc/cardemulation/EnabledNfcFServices.java b/src/com/android/nfc/cardemulation/EnabledNfcFServices.java index 734dd0d7..d6362988 100644 --- a/src/com/android/nfc/cardemulation/EnabledNfcFServices.java +++ b/src/com/android/nfc/cardemulation/EnabledNfcFServices.java @@ -15,6 +15,7 @@ */ package com.android.nfc.cardemulation; +import android.app.ActivityManager; import android.content.ComponentName; import android.content.Context; import android.nfc.cardemulation.NfcFServiceInfo; @@ -38,7 +39,7 @@ public class EnabledNfcFServices implements com.android.nfc.ForegroundUtils.Call final RegisteredNfcFServicesCache mNfcFServiceCache; final RegisteredT3tIdentifiersCache mT3tIdentifiersCache; final Callback mCallback; - final ForegroundUtils mForegroundUtils = ForegroundUtils.getInstance(); + final ForegroundUtils mForegroundUtils; final Handler mHandler = new Handler(Looper.getMainLooper()); final Object mLock = new Object(); @@ -62,6 +63,8 @@ public class EnabledNfcFServices implements com.android.nfc.ForegroundUtils.Call RegisteredT3tIdentifiersCache t3tIdentifiersCache, Callback callback) { if (DBG) Log.d(TAG, "EnabledNfcFServices"); mContext = context; + mForegroundUtils = ForegroundUtils.getInstance( + context.getSystemService(ActivityManager.class)); mNfcFServiceCache = nfcFServiceCache; mT3tIdentifiersCache = t3tIdentifiersCache; mCallback = callback; diff --git a/src/com/android/nfc/cardemulation/PreferredServices.java b/src/com/android/nfc/cardemulation/PreferredServices.java index 1f9b880a..d6e2fa69 100644 --- a/src/com/android/nfc/cardemulation/PreferredServices.java +++ b/src/com/android/nfc/cardemulation/PreferredServices.java @@ -67,7 +67,7 @@ public class PreferredServices implements com.android.nfc.ForegroundUtils.Callba final RegisteredServicesCache mServiceCache; final RegisteredAidCache mAidCache; final Callback mCallback; - final ForegroundUtils mForegroundUtils = ForegroundUtils.getInstance(); + final ForegroundUtils mForegroundUtils; final Handler mHandler = new Handler(Looper.getMainLooper()); final class PaymentDefaults { @@ -105,6 +105,8 @@ public class PreferredServices implements com.android.nfc.ForegroundUtils.Callba public PreferredServices(Context context, RegisteredServicesCache serviceCache, RegisteredAidCache aidCache, Callback callback) { mContext = context; + mForegroundUtils = ForegroundUtils.getInstance( + context.getSystemService(ActivityManager.class)); mServiceCache = serviceCache; mAidCache = aidCache; mCallback = callback; |
