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

Commit ac428bae authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Eliminate boilder plate code for recovering service in NfcAdapter" into main

parents 22a9d4eb 00bec5f8
Loading
Loading
Loading
Loading
+84 −390
Original line number Diff line number Diff line
@@ -963,22 +963,9 @@ public final class NfcAdapter {
            throw new UnsupportedOperationException("You need a context on NfcAdapter to use the "
                    + " NFC extras APIs");
        }
        try {
            return sService.getNfcDtaInterface(mContext.getPackageName());
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return null;
            }
            try {
                return sService.getNfcDtaInterface(mContext.getPackageName());
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return null;
        }
        return callServiceReturn(() ->  sService.getNfcDtaInterface(mContext.getPackageName()),
                null);

    }

    /**
@@ -1095,22 +1082,8 @@ public final class NfcAdapter {
    @SystemApi
    @FlaggedApi(Flags.FLAG_ENABLE_NFC_MAINLINE)
    public @AdapterState int getAdapterState() {
        try {
            return sService.getState();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return NfcAdapter.STATE_OFF;
            }
            try {
                return sService.getState();
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return NfcAdapter.STATE_OFF;
        }
        return callServiceReturn(() ->  sService.getState(), NfcAdapter.STATE_OFF);

    }

    /**
@@ -1134,22 +1107,8 @@ public final class NfcAdapter {
    @FlaggedApi(Flags.FLAG_NFC_STATE_CHANGE)
    @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
    public boolean enable() {
        try {
            return sService.enable(mContext.getPackageName());
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return false;
            }
            try {
                return sService.enable(mContext.getPackageName());
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return false;
        }
        return callServiceReturn(() ->  sService.enable(mContext.getPackageName()), false);

    }

    /**
@@ -1175,22 +1134,9 @@ public final class NfcAdapter {
    @FlaggedApi(Flags.FLAG_NFC_STATE_CHANGE)
    @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
    public boolean disable() {
        try {
            return sService.disable(true, mContext.getPackageName());
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return false;
            }
            try {
                return sService.disable(true, mContext.getPackageName());
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return false;
        }
        return callServiceReturn(() ->  sService.disable(true, mContext.getPackageName()),
                false);

    }

    /**
@@ -1200,22 +1146,9 @@ public final class NfcAdapter {
    @SystemApi
    @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
    public boolean disable(boolean persist) {
        try {
            return sService.disable(persist, mContext.getPackageName());
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return false;
            }
            try {
                return sService.disable(persist, mContext.getPackageName());
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return false;
        }
        return callServiceReturn(() ->  sService.disable(persist, mContext.getPackageName()),
                false);

    }

    /**
@@ -1241,12 +1174,7 @@ public final class NfcAdapter {
     */
    @FlaggedApi(Flags.FLAG_NFC_OBSERVE_MODE)
    public boolean isObserveModeSupported() {
        try {
            return sService.isObserveModeSupported();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            return false;
        }
        return callServiceReturn(() ->  sService.isObserveModeSupported(), false);
    }

    /**
@@ -1257,12 +1185,7 @@ public final class NfcAdapter {

    @FlaggedApi(Flags.FLAG_NFC_OBSERVE_MODE)
    public boolean isObserveModeEnabled() {
        try {
            return sService.isObserveModeEnabled();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            return false;
        }
        return callServiceReturn(() ->  sService.isObserveModeEnabled(), false);
    }

    /**
@@ -1286,12 +1209,8 @@ public final class NfcAdapter {
            throw new UnsupportedOperationException("You need a context on NfcAdapter to use the "
                    + " observe mode APIs");
        }
        try {
            return sService.setObserveMode(enabled, mContext.getPackageName());
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            return false;
        }
        return callServiceReturn(() ->  sService.setObserveMode(enabled, mContext.getPackageName()),
                false);
    }

    /**
@@ -2057,22 +1976,8 @@ public final class NfcAdapter {
        if (!sHasNfcFeature && !sHasCeFeature) {
            throw new UnsupportedOperationException();
        }
        try {
            return sService.setNfcSecure(enable);
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return false;
            }
            try {
                return sService.setNfcSecure(enable);
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return false;
        }
        return callServiceReturn(() ->  sService.setNfcSecure(enable), false);

    }

    /**
@@ -2088,22 +1993,8 @@ public final class NfcAdapter {
        if (!sHasNfcFeature && !sHasCeFeature) {
            throw new UnsupportedOperationException();
        }
        try {
            return sService.deviceSupportsNfcSecure();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return false;
            }
            try {
                return sService.deviceSupportsNfcSecure();
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return false;
        }
        return callServiceReturn(() ->  sService.deviceSupportsNfcSecure(), false);

    }

    /**
@@ -2121,22 +2012,8 @@ public final class NfcAdapter {
        if (!sHasNfcFeature && !sHasCeFeature) {
            throw new UnsupportedOperationException();
        }
        try {
            return sService.getNfcAntennaInfo();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return null;
            }
            try {
                return sService.getNfcAntennaInfo();
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return null;
        }
        return callServiceReturn(() ->  sService.getNfcAntennaInfo(), null);

    }

    /**
@@ -2154,22 +2031,8 @@ public final class NfcAdapter {
        if (!sHasNfcFeature && !sHasCeFeature) {
            throw new UnsupportedOperationException();
        }
        try {
            return sService.isNfcSecureEnabled();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return false;
            }
            try {
                return sService.isNfcSecureEnabled();
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return false;
        }
        return callServiceReturn(() ->  sService.isNfcSecureEnabled(), false);

    }

    /**
@@ -2185,22 +2048,8 @@ public final class NfcAdapter {
        if (!sHasNfcFeature) {
            throw new UnsupportedOperationException();
        }
        try {
            return sService.enableReaderOption(enable);
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return false;
            }
            try {
                return sService.enableReaderOption(enable);
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return false;
        }
        return callServiceReturn(() ->  sService.enableReaderOption(enable), false);

    }

    /**
@@ -2214,22 +2063,8 @@ public final class NfcAdapter {
        if (!sHasNfcFeature) {
            throw new UnsupportedOperationException();
        }
        try {
            return sService.isReaderOptionSupported();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return false;
            }
            try {
                return sService.isReaderOptionSupported();
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return false;
        }
        return callServiceReturn(() ->  sService.isReaderOptionSupported(), false);

    }

    /**
@@ -2245,22 +2080,8 @@ public final class NfcAdapter {
        if (!sHasNfcFeature) {
            throw new UnsupportedOperationException();
        }
        try {
            return sService.isReaderOptionEnabled();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return false;
            }
            try {
                return sService.isReaderOptionEnabled();
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return false;
        }
        return callServiceReturn(() ->  sService.isReaderOptionEnabled(), false);

    }

    /**
@@ -2388,11 +2209,9 @@ public final class NfcAdapter {
        synchronized (mLock) {
            mTagRemovedListener = iListener;
        }
        try {
            return sService.ignore(tag.getServiceHandle(), debounceMs, iListener);
        } catch (RemoteException e) {
            return false;
        }
        final ITagRemovedCallback.Stub passedListener = iListener;
        return callServiceReturn(() ->
                sService.ignore(tag.getServiceHandle(), debounceMs, passedListener), false);
    }

    /**
@@ -2509,22 +2328,9 @@ public final class NfcAdapter {
            throw new UnsupportedOperationException("You need a context on NfcAdapter to use the "
                    + " NFC extras APIs");
        }
        try {
            return sService.getNfcAdapterExtrasInterface(mContext.getPackageName());
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return null;
            }
            try {
                return sService.getNfcAdapterExtrasInterface(mContext.getPackageName());
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return null;
        }
        return callServiceReturn(() ->
                sService.getNfcAdapterExtrasInterface(mContext.getPackageName()), null);

    }

    void enforceResumed(Activity activity) {
@@ -2569,22 +2375,8 @@ public final class NfcAdapter {
        if (!sHasNfcFeature && !sHasCeFeature) {
            throw new UnsupportedOperationException();
        }
        try {
            return sService.setControllerAlwaysOn(value);
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return false;
            }
            try {
                return sService.setControllerAlwaysOn(value);
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return false;
        }
        return callServiceReturn(() ->  sService.setControllerAlwaysOn(value), false);

    }

    /**
@@ -2600,22 +2392,8 @@ public final class NfcAdapter {
    @SystemApi
    @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON)
    public boolean isControllerAlwaysOn() {
        try {
            return sService.isControllerAlwaysOn();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return false;
            }
            try {
                return sService.isControllerAlwaysOn();
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return false;
        }
        return callServiceReturn(() ->  sService.isControllerAlwaysOn(), false);

    }

    /**
@@ -2634,22 +2412,8 @@ public final class NfcAdapter {
        if (!sHasNfcFeature && !sHasCeFeature) {
            throw new UnsupportedOperationException();
        }
        try {
            return sService.isControllerAlwaysOnSupported();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return false;
            }
            try {
                return sService.isControllerAlwaysOnSupported();
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return false;
        }
        return callServiceReturn(() ->  sService.isControllerAlwaysOnSupported(), false);

    }

    /**
@@ -2719,21 +2483,9 @@ public final class NfcAdapter {
            Log.e(TAG, "TagIntentAppPreference is not supported");
            throw new UnsupportedOperationException();
        }
        try {
            return sService.setTagIntentAppPreferenceForUser(userId, pkg, allow);
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            try {
                return sService.setTagIntentAppPreferenceForUser(userId, pkg, allow);
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return TAG_INTENT_APP_PREF_RESULT_UNAVAILABLE;
        }
        return callServiceReturn(() ->
                sService.setTagIntentAppPreferenceForUser(userId, pkg, allow),
                        TAG_INTENT_APP_PREF_RESULT_UNAVAILABLE);
    }


@@ -2808,22 +2560,8 @@ public final class NfcAdapter {
        if (!sHasNfcFeature) {
            throw new UnsupportedOperationException();
        }
        try {
            return sService.isTagIntentAppPreferenceSupported();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return false;
            }
            try {
                return sService.isTagIntentAppPreferenceSupported();
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return false;
        }
        return callServiceReturn(() ->  sService.isTagIntentAppPreferenceSupported(), false);

    }

   /**
@@ -2836,11 +2574,30 @@ public final class NfcAdapter {
    @TestApi
    @FlaggedApi(Flags.FLAG_NFC_READ_POLLING_LOOP)
    public void notifyPollingLoop(@NonNull PollingFrame pollingFrame) {
        callService(() ->  sService.notifyPollingLoop(pollingFrame));
    }


   /**
     * Notifies the system of new HCE data for tests.
     *
     * @hide
     */
    @FlaggedApi(Flags.FLAG_NFC_READ_POLLING_LOOP)
    public void notifyTestHceData(int technology, byte[] data) {
        callService(() ->  sService.notifyTestHceData(technology, data));
    }

    interface ServiceCall {
        void call() throws RemoteException;
    }

    void callService(ServiceCall call) {
        try {
            if (sService == null) {
                attemptDeadServiceRecovery(null);
            }
            sService.notifyPollingLoop(pollingFrame);
            call.call();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
@@ -2849,38 +2606,35 @@ public final class NfcAdapter {
                return;
            }
            try {
                sService.notifyPollingLoop(pollingFrame);
                call.call();
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
        }
    }

   /**
     * Notifies the system of new HCE data for tests.
     *
     * @hide
     */
    @FlaggedApi(Flags.FLAG_NFC_READ_POLLING_LOOP)
    public void notifyTestHceData(int technology, byte[] data) {
    interface ServiceCallReturn<T> {
        T call() throws RemoteException;
    }
    <T> T callServiceReturn(ServiceCallReturn<T> call, T defaultReturn) {
        try {
            if (sService == null) {
                attemptDeadServiceRecovery(null);
            }
            sService.notifyTestHceData(technology, data);
            return call.call();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return;
                return defaultReturn;
            }
            try {
                sService.notifyTestHceData(technology, data);
            } catch (RemoteException e2) {
                return call.call();
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
        }
        return defaultReturn;
    }

   /**
@@ -2891,24 +2645,7 @@ public final class NfcAdapter {
    @TestApi
    @FlaggedApi(Flags.FLAG_NFC_READ_POLLING_LOOP)
    public void notifyHceDeactivated() {
        try {
            if (sService == null) {
                attemptDeadServiceRecovery(null);
            }
            sService.notifyHceDeactivated();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return;
            }
            try {
                sService.notifyHceDeactivated();
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
        }
        callService(() ->  sService.notifyHceDeactivated());
    }

    /**
@@ -2924,22 +2661,7 @@ public final class NfcAdapter {
        if (!sHasNfcWlcFeature) {
            throw new UnsupportedOperationException();
        }
        try {
            return sService.setWlcEnabled(enable);
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return false;
            }
            try {
                return sService.setWlcEnabled(enable);
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return false;
        }
        return callServiceReturn(() ->  sService.setWlcEnabled(enable), false);
    }

    /**
@@ -2954,22 +2676,8 @@ public final class NfcAdapter {
        if (!sHasNfcWlcFeature) {
            throw new UnsupportedOperationException();
        }
        try {
            return sService.isWlcEnabled();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return false;
            }
            try {
                return sService.isWlcEnabled();
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return false;
        }
        return callServiceReturn(() ->  sService.isWlcEnabled(), false);

    }

    /**
@@ -3048,22 +2756,8 @@ public final class NfcAdapter {
        if (!sHasNfcWlcFeature) {
            throw new UnsupportedOperationException();
        }
        try {
            return sService.getWlcListenerDeviceInfo();
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
            // Try one more time
            if (sService == null) {
                Log.e(TAG, "Failed to recover NFC Service.");
                return null;
            }
            try {
                return sService.getWlcListenerDeviceInfo();
            } catch (RemoteException ee) {
                Log.e(TAG, "Failed to recover NFC Service.");
            }
            return null;
        }
        return callServiceReturn(() ->  sService.getWlcListenerDeviceInfo(), null);

    }

    /**