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

Commit 3dd6c458 authored by Nick Pelly's avatar Nick Pelly
Browse files

Remove attemptDeadServiceRecovery() from TagTechnology's.

attemptDeadServiceRecovery() is a hack to recover from NfcService dying. It
should be a rare event, and is only needed in NfcAdapter which is a long-lived
object.

TagTechnology objects are transient, it is acceptable for them to go stale
when NFC service dies. Lets not complicate the code with recovery for a rare
event.

Change-Id: I101350e920b075c680eb4f250683f0a2bb878553
parent b4003bf7
Loading
Loading
Loading
Loading
+27 −2
Original line number Diff line number Diff line
@@ -195,6 +195,7 @@ public final class NfcAdapter {
    // attemptDeadServiceRecovery() when NFC crashes - we accept a best effort
    // recovery
    private static INfcAdapter sService;
    private static INfcTag sTagService;

    private final Context mContext;

@@ -233,6 +234,12 @@ public final class NfcAdapter {
                Log.e(TAG, "could not retrieve NFC service");
                return null;
            }
            try {
                sTagService = sService.getNfcTagInterface();
            } catch (RemoteException e) {
                Log.e(TAG, "could not retrieve NFC Tag service");
                return null;
            }
        }
        return sService;
    }
@@ -298,6 +305,14 @@ public final class NfcAdapter {
        return sService;
    }

    /**
     * Returns the binder interface to the tag service.
     * @hide
     */
    public INfcTag getTagService() {
        return sTagService;
    }

    /**
     * NFC service dead - attempt best effort recovery
     * @hide
@@ -307,11 +322,21 @@ public final class NfcAdapter {
        INfcAdapter service = getServiceInterface();
        if (service == null) {
            Log.e(TAG, "could not retrieve NFC service during service recovery");
            // nothing more can be done now, sService is still stale, we'll hit
            // this recovery path again later
            return;
        }
        /* assigning to sService is not thread-safe, but this is best-effort code
         * and on a well-behaved system should never happen */
        // assigning to sService is not thread-safe, but this is best-effort code
        // and on a well-behaved system should never happen
        sService = service;
        try {
            sTagService = service.getNfcTagInterface();
        } catch (RemoteException ee) {
            Log.e(TAG, "could not retrieve NFC tag service during service recovery");
            // nothing more can be done now, sService is still stale, we'll hit
            // this recovery path again later
        }

        return;
    }

+19 −41
Original line number Diff line number Diff line
@@ -30,19 +30,14 @@ import android.util.Log;
 * A base class for tag technologies that are built on top of transceive().
 */
/* package */ abstract class BasicTagTechnology implements TagTechnology {
    private static final String TAG = "NFC";

    /*package*/ final Tag mTag;
    /*package*/ boolean mIsConnected;
    /*package*/ int mSelectedTechnology;
    private final NfcAdapter mAdapter;

    // Following fields are final after construction, except for
    // during attemptDeadServiceRecovery() when NFC crashes.
    // Not locked - we accept a best effort attempt when NFC crashes.
    /*package*/ INfcAdapter mService;
    /*package*/ INfcTag mTagService;

    private static final String TAG = "NFC";
    /*package*/ final INfcAdapter mService;
    /*package*/ final INfcTag mTagService;

    /**
     * @hide
@@ -64,11 +59,7 @@ import android.util.Log;

        mAdapter = adapter;
        mService = mAdapter.getService();
        try {
          mTagService = mService.getNfcTagInterface();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
        }
        mTagService = mAdapter.getTagService();
        mTag = tag;
        mSelectedTechnology = tech;
    }
@@ -80,19 +71,6 @@ import android.util.Log;
        this(adapter, tag, tag.getTechnologyList()[0]);
    }

    /** NFC service dead - attempt best effort recovery */
    /*package*/ void attemptDeadServiceRecovery(Exception e) {
        mAdapter.attemptDeadServiceRecovery(e);
        /* assigning to mService is not thread-safe, but this is best-effort code
         * and on a well-behaved system should never happen */
        mService = mAdapter.getService();
        try {
            mTagService = mService.getNfcTagInterface();
        } catch (RemoteException e2) {
            Log.e(TAG, "second RemoteException trying to recover from dead NFC service", e2);
        }
    }

    /**
     * Get the {@link Tag} this connection is associated with.
     * <p>Requires {@link android.Manifest.permission#NFC} permission.
@@ -135,7 +113,7 @@ import android.util.Log;
        try {
            return mTagService.isPresent(mTag.getServiceHandle());
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            Log.e(TAG, "NFC service dead", e);
            return false;
        }
    }
@@ -163,7 +141,7 @@ import android.util.Log;
                throw new IOException();
            }
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            Log.e(TAG, "NFC service dead", e);
            throw new IOException("NFC service died");
        }
    }
@@ -183,7 +161,8 @@ import android.util.Log;
    public void reconnect() throws IOException {
        if (!mIsConnected) {
            throw new IllegalStateException("Technology not connected yet");
        } else {
        }

        try {
            int errorCode = mTagService.reconnect(mTag.getServiceHandle());

@@ -195,11 +174,10 @@ import android.util.Log;
        } catch (RemoteException e) {
            mIsConnected = false;
            mTag.setTechnologyDisconnected();
                attemptDeadServiceRecovery(e);
            Log.e(TAG, "NFC service dead", e);
            throw new IOException("NFC service died");
        }
    }
    }

    /**
     * Close this connection.
@@ -219,7 +197,7 @@ import android.util.Log;
             */
            mTagService.reconnect(mTag.getServiceHandle());
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            Log.e(TAG, "NFC service dead", e);
        } finally {
            mIsConnected = false;
            mTag.setTechnologyDisconnected();
@@ -237,7 +215,7 @@ import android.util.Log;
            }
            return response;
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            Log.e(TAG, "NFC service dead", e);
            throw new IOException("NFC service died");
        }
    }
+6 −2
Original line number Diff line number Diff line
@@ -58,10 +58,14 @@ public final class IsoDep extends BasicTagTechnology {
    /**
     * 3A only
     */
    public byte[] getHistoricalBytes() { return mHistBytes; }
    public byte[] getHistoricalBytes() {
        return mHistBytes;
    }

    /**
     * 3B only
     */
    public byte[] getHiLayerResponse() { return mHiLayerResponse; }
    public byte[] getHiLayerResponse() {
        return mHiLayerResponse;
    }
}
+6 −3
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;

import java.io.IOException;

@@ -38,6 +39,8 @@ import java.io.IOException;
 * permission.
 */
public final class Ndef extends BasicTagTechnology {
    private static final String TAG = "NFC";

    /** @hide */
    public static final int NDEF_MODE_READ_ONLY = 1;
    /** @hide */
@@ -168,7 +171,7 @@ public final class Ndef extends BasicTagTechnology {
                return null;
            }
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            Log.e(TAG, "NFC service dead", e);
            return null;
        }
    }
@@ -200,7 +203,7 @@ public final class Ndef extends BasicTagTechnology {
                throw new IOException("Tag is not ndef");
            }
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            Log.e(TAG, "NFC service dead", e);
        }
    }

@@ -241,7 +244,7 @@ public final class Ndef extends BasicTagTechnology {
                    throw new IOException();
            }
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            Log.e(TAG, "NFC service dead", e);
            return false;
        }
    }
+4 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;

import java.io.IOException;

@@ -36,6 +37,8 @@ import java.io.IOException;
 * permission.
 */
public final class NdefFormatable extends BasicTagTechnology {
    private static final String TAG = "NFC";

    /**
     * Internal constructor, to be used by NfcAdapter
     * @hide
@@ -85,7 +88,7 @@ public final class NdefFormatable extends BasicTagTechnology {
                throw new IOException();
            }
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            Log.e(TAG, "NFC service dead", e);
        }
    }