/*
 * Copyright (C) 2017 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 static android.app.slice.SliceItem.FORMAT_ACTION;
import static android.app.slice.SliceItem.FORMAT_IMAGE;
import static android.app.slice.SliceItem.FORMAT_INT;
import static android.app.slice.SliceItem.FORMAT_LONG;
import static android.app.slice.SliceItem.FORMAT_REMOTE_INPUT;
import static android.app.slice.SliceItem.FORMAT_SLICE;
import static android.app.slice.SliceItem.FORMAT_TEXT;

import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.collection.ArraySet;
import androidx.core.graphics.drawable.IconCompat;

import java.util.Set;

/**
 * Convert between {@link androidx.slice.Slice} and {@link android.app.slice.Slice}
 */
@RequiresApi(28)
public class SliceConvert {

    /**
     * Convert {@link androidx.slice.Slice} to {@link android.app.slice.Slice}
     */
    public static android.app.slice.Slice unwrap(androidx.slice.Slice slice) {
        android.app.slice.Slice.Builder builder = new android.app.slice.Slice.Builder(
                slice.getUri(), unwrap(slice.getSpec()));
        builder.addHints(slice.getHints());
        for (androidx.slice.SliceItem item : slice.getItems()) {
            switch (item.getFormat()) {
                case FORMAT_SLICE:
                    builder.addSubSlice(unwrap(item.getSlice()), item.getSubType());
                    break;
                case FORMAT_IMAGE:
                    builder.addIcon(item.getIcon().toIcon(), item.getSubType(), item.getHints());
                    break;
                case FORMAT_REMOTE_INPUT:
                    builder.addRemoteInput(item.getRemoteInput(), item.getSubType(),
                            item.getHints());
                    break;
                case FORMAT_ACTION:
                    builder.addAction(item.getAction(), unwrap(item.getSlice()), item.getSubType());
                    break;
                case FORMAT_TEXT:
                    builder.addText(item.getText(), item.getSubType(), item.getHints());
                    break;
                case FORMAT_INT:
                    builder.addInt(item.getInt(), item.getSubType(), item.getHints());
                    break;
                case FORMAT_LONG:
                    builder.addLong(item.getLong(), item.getSubType(), item.getHints());
                    break;
            }
        }
        return builder.build();
    }

    private static android.app.slice.SliceSpec unwrap(androidx.slice.SliceSpec spec) {
        if (spec == null) return null;
        return new android.app.slice.SliceSpec(spec.getType(), spec.getRevision());
    }

    static Set<android.app.slice.SliceSpec> unwrap(
            Set<androidx.slice.SliceSpec> supportedSpecs) {
        Set<android.app.slice.SliceSpec> ret = new ArraySet<>();
        for (androidx.slice.SliceSpec spec : supportedSpecs) {
            ret.add(unwrap(spec));
        }
        return ret;
    }

    /**
     * Convert {@link android.app.slice.Slice} to {@link androidx.slice.Slice}
     */
    public static androidx.slice.Slice wrap(android.app.slice.Slice slice) {
        androidx.slice.Slice.Builder builder = new androidx.slice.Slice.Builder(
                slice.getUri());
        builder.addHints(slice.getHints());
        builder.setSpec(wrap(slice.getSpec()));
        for (android.app.slice.SliceItem item : slice.getItems()) {
            switch (item.getFormat()) {
                case FORMAT_SLICE:
                    builder.addSubSlice(wrap(item.getSlice()), item.getSubType());
                    break;
                case FORMAT_IMAGE:
                    builder.addIcon(IconCompat.createFromIcon(item.getIcon()),
                            item.getSubType(), item.getHints());
                    break;
                case FORMAT_REMOTE_INPUT:
                    builder.addRemoteInput(item.getRemoteInput(), item.getSubType(),
                            item.getHints());
                    break;
                case FORMAT_ACTION:
                    builder.addAction(item.getAction(), wrap(item.getSlice()), item.getSubType());
                    break;
                case FORMAT_TEXT:
                    builder.addText(item.getText(), item.getSubType(), item.getHints());
                    break;
                case FORMAT_INT:
                    builder.addInt(item.getInt(), item.getSubType(), item.getHints());
                    break;
                case FORMAT_LONG:
                    builder.addLong(item.getLong(), item.getSubType(), item.getHints());
                    break;
            }
        }
        return builder.build();
    }

    private static androidx.slice.SliceSpec wrap(android.app.slice.SliceSpec spec) {
        if (spec == null) return null;
        return new androidx.slice.SliceSpec(spec.getType(), spec.getRevision());
    }

    /**
     * @hide
     */
    @RestrictTo(RestrictTo.Scope.LIBRARY)
    public static Set<androidx.slice.SliceSpec> wrap(
            Set<android.app.slice.SliceSpec> supportedSpecs) {
        Set<androidx.slice.SliceSpec> ret = new ArraySet<>();
        for (android.app.slice.SliceSpec spec : supportedSpecs) {
            ret.add(wrap(spec));
        }
        return ret;
    }

    private SliceConvert() {
    }
}

@@ -1531,32 +1531,98 @@
 .Slice slice) %7B%0A
+        if (slice == null %7C%7C slice.getUri() == null) return null;%0A
         android.
@@ -1684,32 +1684,32 @@
 .Slice.Builder(%0A
-
                 
@@ -3466,32 +3466,74 @@
 w ArraySet%3C%3E();%0A
+        if (supportedSpecs != null) %7B%0A    
         for (and
@@ -3583,32 +3583,36 @@
 ) %7B%0A            
+    
 ret.add(unwrap(s
@@ -3610,32 +3610,46 @@
 (unwrap(spec));%0A
+            %7D%0A
         %7D%0A      
@@ -3833,32 +3833,98 @@
 .Slice slice) %7B%0A
+        if (slice == null %7C%7C slice.getUri() == null) return null;%0A
         androidx
@@ -5898,32 +5898,74 @@
 w ArraySet%3C%3E();%0A
+        if (supportedSpecs != null) %7B%0A    
         for (and
@@ -6018,32 +6018,36 @@
 ) %7B%0A            
+    
 ret.add(wrap(spe
@@ -6043,32 +6043,46 @@
 dd(wrap(spec));%0A
+            %7D%0A
         %7D%0A      

Add additional null check in slice convert.