From 55186d05e371371e9918367119b76280e5828a25 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Mon, 9 Sep 2019 00:07:04 +0200 Subject: [PATCH] SystemUI: Enable NFC tile Author: Thecrazyskull Date: Tue Jan 17 10:56:39 2017 -0600 SystemUI: Enable NFC tile Change-Id: Ib21f927d336fcb5e460552786828b79551b235fa Author: Luca Stefani Date: Tue Sep 4 10:07:17 2018 +0200 SystemUI: Update NFCTile to match P style Change-Id: I8309eda6b2dbb28156dd463e667ff60545b37b7d Author: inthewaves Date: Sun Sep 20 14:59:10 2020 -0700 have NfcTile get an NfcAdapter directly This changes the NfcTile to not be dependent on an NfcManager giving the NfcTile an NfcAdapter. The problem with NfcTile is that the NfcService doesn't start until the device is unlocked after a reboot, but NfcTile can be created and have its state updated before the device is unlocked. The state of NfcTile depends on an NfcAdapter. The tile gets an NfcAdapter from a call to NfcAdapter#getDefaultAdapter, which gets an adapter from an NfcManager via Context#getSystemService, and NfcManager tries to get an adapter in its constructor via NfcAdapter#getNfcAdapter. If this is done before unlock, NfcService isn't started, so the constructor of NfcManager fails to get an adapter, opting to just store null. This means that the NfcManager that's _cached_ by the NfcTile's application context holds a null NfcAdapter, so subsequent calls in NfcTile to get the NfcAdapter will keep returning null. We can just have NfcTile get the NfcAdapter directly via NfcAdapter#getNfcAdapter instead of relying on an NfcManager to call the same method for us to get its default adapter. We just have to make sure we use the application context for NfcAdapter#getNfcAdapter, as per the doc comments for getNfcAdapter. This means that there's no longer an NfcManager associated with the NfcTile's application context. It doesn't look like NfcManager does anything special with the NfcAdapter anyway. It seems to just be some middle man for NfcAdapters: * The NfcAdapter field in NfcManager is final, and it doesn't do anything else with it besides have a getter method for returning it. The NfcManager seems to be a way to force third-party apps that want to get an NfcAdapter to use the application context. * NfcAdapter#getNfcAdapter manages the caching of NfcAdapters by application context in a static HashMap. NfcManager doesn't manage caching; it just calls NfcAdapter#getNfcAdapter. Change-Id: Ifafc666568a001a60baf7c8c16782dca1ee4d011 Change-Id: Ia06b923ee15e91a9c84a806d4c69755478df2063 Signed-off-by: SahilSonar Signed-off-by: HeroBuxx Signed-off-by: HeroBuxx --- .../{ic_qs_nfc_enabled.xml => ic_qs_nfc.xml} | 4 +-- .../res/drawable/ic_qs_nfc_disabled.xml | 31 ------------------- packages/SystemUI/res/values/config.xml | 2 +- .../android/systemui/qs/tiles/NfcTile.java | 7 +++-- 4 files changed, 7 insertions(+), 37 deletions(-) rename packages/SystemUI/res/drawable/{ic_qs_nfc_enabled.xml => ic_qs_nfc.xml} (95%) delete mode 100644 packages/SystemUI/res/drawable/ic_qs_nfc_disabled.xml diff --git a/packages/SystemUI/res/drawable/ic_qs_nfc_enabled.xml b/packages/SystemUI/res/drawable/ic_qs_nfc.xml similarity index 95% rename from packages/SystemUI/res/drawable/ic_qs_nfc_enabled.xml rename to packages/SystemUI/res/drawable/ic_qs_nfc.xml index becb18ad8ba2..2c080964b48e 100644 --- a/packages/SystemUI/res/drawable/ic_qs_nfc_enabled.xml +++ b/packages/SystemUI/res/drawable/ic_qs_nfc.xml @@ -14,8 +14,8 @@ limitations under the License. --> diff --git a/packages/SystemUI/res/drawable/ic_qs_nfc_disabled.xml b/packages/SystemUI/res/drawable/ic_qs_nfc_disabled.xml deleted file mode 100644 index 558f3d083f42..000000000000 --- a/packages/SystemUI/res/drawable/ic_qs_nfc_disabled.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - diff --git a/packages/SystemUI/res/values/config.xml b/packages/SystemUI/res/values/config.xml index e4f2dbb299ae..83282de00ca0 100644 --- a/packages/SystemUI/res/values/config.xml +++ b/packages/SystemUI/res/values/config.xml @@ -114,7 +114,7 @@ - wifi,cell,battery,dnd,flashlight,rotation,bt,airplane,location,hotspot,inversion,saver,dark,work,cast,night,screenrecord,reverse,heads_up,dataswitch,sync,volume_panel,reboot,caffeine + wifi,cell,battery,dnd,flashlight,rotation,bt,airplane,nfc,location,hotspot,inversion,saver,dark,work,cast,night,screenrecord,reverse,heads_up,dataswitch,sync,volume_panel,reboot,caffeine diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/NfcTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/NfcTile.java index 336f6c9ec6c3..407c9500f2ff 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/tiles/NfcTile.java +++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/NfcTile.java @@ -39,6 +39,8 @@ /** Quick settings tile: Enable/Disable NFC **/ public class NfcTile extends QSTileImpl { + private final Icon mIcon = ResourceIcon.get(R.drawable.ic_qs_nfc); + private NfcAdapter mAdapter; private BroadcastDispatcher mBroadcastDispatcher; @@ -109,8 +111,7 @@ protected void handleUpdateState(BooleanState state, Object arg) { state.state = getAdapter() == null ? Tile.STATE_UNAVAILABLE : state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE; - state.icon = ResourceIcon.get( - state.value ? R.drawable.ic_qs_nfc_enabled : R.drawable.ic_qs_nfc_disabled); + state.icon = mIcon; state.label = mContext.getString(R.string.quick_settings_nfc_label); state.expandedAccessibilityClassName = Switch.class.getName(); state.contentDescription = state.label; @@ -133,7 +134,7 @@ protected String composeChangeAnnouncement() { private NfcAdapter getAdapter() { if (mAdapter == null) { try { - mAdapter = NfcAdapter.getDefaultAdapter(mContext); + mAdapter = NfcAdapter.getNfcAdapter(mContext.getApplicationContext()); } catch (UnsupportedOperationException e) { mAdapter = null; }