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

Commit 05ec3573 authored by Pinyao Ting's avatar Pinyao Ting
Browse files

Gracefully handle system error from AppSearch

In rare scenario, if AppSearchSession was invoked after user is locked,
the call will fail which currently resulted in a crash in system
process. This CL includes the logic to prevent such scenario from
crashing the system process.

Bug: 221110670
Test: manual
Change-Id: I668e34781f70ceb54bb87839412abc1bc04c0d3d
parent 8b50a568
Loading
Loading
Loading
Loading
+49 −19
Original line number Diff line number Diff line
@@ -19,9 +19,12 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.Person;
import android.app.appsearch.AppSearchBatchResult;
import android.app.appsearch.AppSearchManager;
import android.app.appsearch.AppSearchResult;
import android.app.appsearch.AppSearchSession;
import android.app.appsearch.BatchResultCallback;
import android.app.appsearch.GenericDocument;
import android.app.appsearch.GetByDocumentIdRequest;
import android.app.appsearch.PackageIdentifier;
import android.app.appsearch.PutDocumentsRequest;
@@ -2384,13 +2387,23 @@ class ShortcutPackage extends ShortcutPackageItem {
        }
        runAsSystem(() -> fromAppSearch().thenAccept(session -> {
            session.getByDocumentId(new GetByDocumentIdRequest.Builder(getPackageName())
                    .addIds(ids).build(), mShortcutUser.mExecutor, result -> {
                            .addIds(ids).build(), mShortcutUser.mExecutor,
                    new BatchResultCallback<String, GenericDocument>() {
                        @Override
                        public void onResult(
                                @NonNull AppSearchBatchResult<String, GenericDocument> result) {
                            final List<ShortcutInfo> ret = result.getSuccesses().values()
                                    .stream().map(doc ->
                                            ShortcutInfo.createFromGenericDocument(
                                                    mShortcutUser.getUserId(), doc))
                                    .collect(Collectors.toList());
                            cb.accept(ret);
                        }
                        @Override
                        public void onSystemError(
                                @Nullable Throwable throwable) {
                            Slog.d(TAG, "Error retrieving shortcuts", throwable);
                        }
                    });
        }));
    }
@@ -2407,7 +2420,11 @@ class ShortcutPackage extends ShortcutPackageItem {
        runAsSystem(() -> fromAppSearch().thenAccept(session ->
                session.remove(
                        new RemoveByDocumentIdRequest.Builder(getPackageName()).addIds(ids).build(),
                        mShortcutUser.mExecutor, result -> {
                        mShortcutUser.mExecutor,
                        new BatchResultCallback<String, Void>() {
                            @Override
                            public void onResult(
                                    @NonNull AppSearchBatchResult<String, Void> result) {
                                if (!result.isSuccess()) {
                                    final Map<String, AppSearchResult<Void>> failures =
                                            result.getFailures();
@@ -2416,6 +2433,11 @@ class ShortcutPackage extends ShortcutPackageItem {
                                                + failures.get(key).getErrorMessage());
                                    }
                                }
                            }
                            @Override
                            public void onSystemError(@Nullable Throwable throwable) {
                                Slog.e(TAG, "Error removing shortcuts", throwable);
                            }
                        })));
    }

@@ -2452,12 +2474,20 @@ class ShortcutPackage extends ShortcutPackageItem {
                                    AppSearchShortcutInfo.toGenericDocuments(shortcuts))
                            .build(),
                    mShortcutUser.mExecutor,
                    result -> {
                    new BatchResultCallback<String, Void>() {
                        @Override
                        public void onResult(
                                @NonNull AppSearchBatchResult<String, Void> result) {
                            if (!result.isSuccess()) {
                                for (AppSearchResult<Void> k : result.getFailures().values()) {
                                    Slog.e(TAG, k.getErrorMessage());
                                }
                            }
                        }
                        @Override
                        public void onSystemError(@Nullable Throwable throwable) {
                            Slog.d(TAG, "Error persisting shortcuts", throwable);
                        }
                    });
        }));
    }