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

Commit 3e30289c authored by Charles Chen's avatar Charles Chen Committed by Android (Google) Code Review
Browse files

Merge "Fix NPE when pfd is not present" into main

parents cec6037b a17110e2
Loading
Loading
Loading
Loading
+11 −3
Original line number Diff line number Diff line
@@ -338,16 +338,24 @@ public abstract class VisualQueryDetectionService extends Service

    /**
     * Overrides {@link Context#openFileInput} to read files with the given file names under the
     * internal app storage of the {@link VoiceInteractionService}, i.e., only files stored in
     * {@link Context#getFilesDir()} can be opened.
     * internal app storage of the {@link VoiceInteractionService}, i.e., the input file path would
     * be added with {@link Context#getFilesDir()} as prefix.
     *
     * @param filename Relative path of a file under {@link Context#getFilesDir()}.
     * @throws FileNotFoundException if the file does not exist or cannot be open.
     */
    @Override
    public @Nullable FileInputStream openFileInput(@NonNull String filename) throws
    public @NonNull FileInputStream openFileInput(@NonNull String filename) throws
            FileNotFoundException {
        try {
            AndroidFuture<ParcelFileDescriptor> future = new AndroidFuture<>();
            assert mDetectorSessionStorageService != null;
            mDetectorSessionStorageService.openFile(filename, future);
            ParcelFileDescriptor pfd = future.get();
            if (pfd == null) {
                throw new FileNotFoundException(
                        "File does not exist. Unable to open " + filename + ".");
            }
            return new FileInputStream(pfd.getFileDescriptor());
        } catch (RemoteException | ExecutionException | InterruptedException e) {
            Log.w(TAG, "Cannot open file due to remote service failure");
+2 −2
Original line number Diff line number Diff line
@@ -447,12 +447,12 @@ public class VisualQueryDetector {
        public void onOpenFile(String filename, AndroidFuture future) throws RemoteException {
            Slog.v(TAG, "BinderCallback#onOpenFile " + filename);
            Binder.withCleanCallingIdentity(() -> mExecutor.execute(() -> {
                Slog.v(TAG, "onOpenFile: " + filename);
                Slog.v(TAG, "onOpenFile: " + filename + "under internal app storage.");
                File f = new File(mContext.getFilesDir(), filename);
                ParcelFileDescriptor pfd = null;
                try {
                    Slog.d(TAG, "opened a file with ParcelFileDescriptor.");
                    pfd = ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
                    Slog.d(TAG, "Successfully opened a file with ParcelFileDescriptor.");
                } catch (FileNotFoundException e) {
                    Slog.e(TAG, "Cannot open file. No ParcelFileDescriptor returned.");
                } finally {