Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b04cce0e authored by Nick Pelly's avatar Nick Pelly
Browse files

Throw a nicer error message when using an invalid context to create Nfc

This can happen when using getContext() instead of getTargetContext()
in an InstrumentationTestCase.

Bug: 5644274
Change-Id: I020a637c271e25bcf25ae2927fd878001d5fea0a
parent 5cf2a139
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -357,8 +357,11 @@ public final class NfcAdapter {
            throw new IllegalArgumentException("context cannot be null");
        }
        context = context.getApplicationContext();
        /* use getSystemService() instead of just instantiating to take
         * advantage of the context's cached NfcManager & NfcAdapter */
        if (context == null) {
            throw new IllegalArgumentException(
                    "context not associated with any application (using a mock context?)");
        }
        /* use getSystemService() for consistency */
        NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
        return manager.getDefaultAdapter();
    }
+4 −0
Original line number Diff line number Diff line
@@ -40,6 +40,10 @@ public final class NfcManager {
    public NfcManager(Context context) {
        NfcAdapter adapter;
        context = context.getApplicationContext();
        if (context == null) {
            throw new IllegalArgumentException(
                    "context not associated with any application (using a mock context?)");
        }
        try {
            adapter = NfcAdapter.getNfcAdapter(context);
        } catch (UnsupportedOperationException e) {