/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.slice;

import android.content.Context;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.core.content.PermissionChecker;

import java.util.List;
import java.util.Set;

/**
 * @hide
 */
@RestrictTo(RestrictTo.Scope.LIBRARY)
@RequiresApi(api = 28)
class SliceManagerWrapper extends SliceManager {

    private final android.app.slice.SliceManager mManager;
    private final Context mContext;

    SliceManagerWrapper(Context context) {
        this(context, context.getSystemService(android.app.slice.SliceManager.class));
    }

    SliceManagerWrapper(Context context, android.app.slice.SliceManager manager) {
        mContext = context;
        mManager = manager;
    }

    @Override
    public @NonNull Set<androidx.slice.SliceSpec> getPinnedSpecs(@NonNull Uri uri) {
        return SliceConvert.wrap(mManager.getPinnedSpecs(uri));
    }

    @Override
    @PermissionChecker.PermissionResult
    public int checkSlicePermission(@NonNull Uri uri, int pid, int uid) {
        return mManager.checkSlicePermission(uri, pid, uid);
    }

    @Override
    public void grantSlicePermission(@NonNull String toPackage, @NonNull Uri uri) {
        mManager.grantSlicePermission(toPackage, uri);
    }

    @Override
    public void revokeSlicePermission(@NonNull String toPackage, @NonNull Uri uri) {
        mManager.revokeSlicePermission(toPackage, uri);
    }

    @Override
    public List<Uri> getPinnedSlices() {
        return mManager.getPinnedSlices();
    }
}

@@ -690,16 +690,98 @@
 net.Uri;
+%0Aimport android.os.Build;%0Aimport android.os.Process;%0Aimport android.os.UserHandle;
 %0A%0Aimport
@@ -938,16 +938,68 @@
 ecker;%0A%0A
+import java.lang.reflect.InvocationTargetException;%0A
 import j
@@ -1634,32 +1634,131 @@
 Null Uri uri) %7B%0A
+        if (Build.VERSION.SDK_INT == 28) %7B%0A            uri = maybeAddCurrentUserId(uri);%0A        %7D%0A
         return S
@@ -2381,24 +2381,24 @@
 dSlices() %7B%0A
-
         retu
@@ -2433,11 +2433,732 @@
 );%0A    %7D
+%0A%0A    private Uri maybeAddCurrentUserId(Uri uri) %7B%0A        if (uri == null %7C%7C uri.getAuthority().contains(%22@%22)) %7B%0A            return uri;%0A        %7D%0A        String auth = uri.getAuthority();%0A        return uri.buildUpon()%0A                .encodedAuthority(getCurrentUserId() + %22@%22 + auth)%0A                .build();%0A    %7D%0A%0A    private int getCurrentUserId() %7B%0A        UserHandle h = Process.myUserHandle();%0A        try %7B%0A            return (int) h.getClass().getDeclaredMethod(%22getIdentifier%22).invoke(h);%0A        %7D catch (IllegalAccessException e) %7B%0A            return 0;%0A        %7D catch (InvocationTargetException e) %7B%0A            return 0;%0A        %7D catch (NoSuchMethodException e) %7B%0A            return 0;%0A        %7D%0A    %7D
 %0A%7D%0A

Fix broken getPinnedSpecs APIs on 28