Jason Monk | 901e2a6 | 2017-12-18 15:40:24 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package androidx.app.slice; |
| 18 | |
| 19 | import static android.app.slice.SliceItem.FORMAT_ACTION; |
| 20 | import static android.app.slice.SliceItem.FORMAT_IMAGE; |
| 21 | import static android.app.slice.SliceItem.FORMAT_REMOTE_INPUT; |
| 22 | |
| 23 | import android.content.Context; |
| 24 | import android.support.annotation.IntDef; |
Jason Monk | 811fa67 | 2017-12-20 09:33:01 -0500 | [diff] [blame] | 25 | import android.support.annotation.NonNull; |
Jason Monk | 901e2a6 | 2017-12-18 15:40:24 -0500 | [diff] [blame] | 26 | import android.support.annotation.RestrictTo; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.io.InputStream; |
| 30 | import java.io.OutputStream; |
| 31 | |
| 32 | /** |
Jason Monk | 811fa67 | 2017-12-20 09:33:01 -0500 | [diff] [blame] | 33 | * Utilities for dealing with slices. |
Jason Monk | 901e2a6 | 2017-12-18 15:40:24 -0500 | [diff] [blame] | 34 | */ |
| 35 | @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) |
| 36 | public class SliceUtils { |
| 37 | |
| 38 | private SliceUtils() { |
| 39 | } |
| 40 | |
| 41 | /** |
Jason Monk | 811fa67 | 2017-12-20 09:33:01 -0500 | [diff] [blame] | 42 | * Serialize a slice to an OutputStream. |
| 43 | * <p> |
| 44 | * The slice can later be read into slice form again with {@link #parseSlice}. |
| 45 | * Some slice types cannot be serialized, their handling is controlled by |
| 46 | * {@link SerializeOptions}. |
| 47 | * |
| 48 | * @param s The slice to serialize. |
| 49 | * @param context Context used to load any resources in the slice. |
| 50 | * @param output The output of the serialization. |
| 51 | * @param encoding The encoding to use for serialization. |
| 52 | * @param options Options defining how to handle non-serializable items. |
Jason Monk | 901e2a6 | 2017-12-18 15:40:24 -0500 | [diff] [blame] | 53 | */ |
Jason Monk | 811fa67 | 2017-12-20 09:33:01 -0500 | [diff] [blame] | 54 | public static void serializeSlice(@NonNull Slice s, @NonNull Context context, |
| 55 | @NonNull OutputStream output, @NonNull String encoding, |
| 56 | @NonNull SerializeOptions options) throws IOException { |
Jason Monk | 901e2a6 | 2017-12-18 15:40:24 -0500 | [diff] [blame] | 57 | SliceXml.serializeSlice(s, context, output, encoding, options); |
| 58 | } |
| 59 | |
| 60 | /** |
Jason Monk | 811fa67 | 2017-12-20 09:33:01 -0500 | [diff] [blame] | 61 | * Parse a slice that has been previously serialized. |
| 62 | * <p> |
| 63 | * Parses a slice that was serialized with {@link #serializeSlice}. |
| 64 | * |
| 65 | * @param input The input stream to read from. |
| 66 | * @param encoding The encoding to read as. |
Jason Monk | 901e2a6 | 2017-12-18 15:40:24 -0500 | [diff] [blame] | 67 | */ |
Jason Monk | 811fa67 | 2017-12-20 09:33:01 -0500 | [diff] [blame] | 68 | public static @NonNull Slice parseSlice(@NonNull InputStream input, @NonNull String encoding) |
Jason Monk | 901e2a6 | 2017-12-18 15:40:24 -0500 | [diff] [blame] | 69 | throws IOException { |
| 70 | return SliceXml.parseSlice(input, encoding); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Holds options for how to handle SliceItems that cannot be serialized. |
| 75 | */ |
| 76 | public static class SerializeOptions { |
| 77 | /** |
| 78 | * Constant indicating that the an {@link IllegalArgumentException} should be thrown |
| 79 | * when this format is encountered. |
| 80 | */ |
| 81 | public static final int MODE_THROW = 0; |
| 82 | /** |
| 83 | * Constant indicating that the SliceItem should be removed when this format is encountered. |
| 84 | */ |
| 85 | public static final int MODE_REMOVE = 1; |
| 86 | /** |
| 87 | * Constant indicating that the SliceItem should be serialized as much as possible. |
| 88 | * <p> |
| 89 | * For images this means it will be replaced with an empty image. For actions, the |
| 90 | * action will be removed but the content of the action will be serialized. |
| 91 | */ |
| 92 | public static final int MODE_DISABLE = 2; |
| 93 | |
| 94 | @IntDef({MODE_THROW, MODE_REMOVE, MODE_DISABLE}) |
| 95 | @interface FormatMode { |
| 96 | } |
| 97 | |
| 98 | private int mActionMode = MODE_THROW; |
| 99 | private int mImageMode = MODE_THROW; |
| 100 | |
| 101 | /** |
| 102 | * @hide |
| 103 | */ |
| 104 | @RestrictTo(RestrictTo.Scope.LIBRARY) |
| 105 | public void checkThrow(String format) { |
| 106 | switch (format) { |
| 107 | case FORMAT_ACTION: |
| 108 | case FORMAT_REMOTE_INPUT: |
| 109 | if (mActionMode != MODE_THROW) return; |
| 110 | break; |
| 111 | case FORMAT_IMAGE: |
| 112 | if (mImageMode != MODE_THROW) return; |
| 113 | break; |
| 114 | default: |
| 115 | return; |
| 116 | } |
| 117 | throw new IllegalArgumentException(format + " cannot be serialized"); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @hide |
| 122 | */ |
| 123 | @RestrictTo(RestrictTo.Scope.LIBRARY) |
| 124 | public @FormatMode int getActionMode() { |
| 125 | return mActionMode; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @hide |
| 130 | */ |
| 131 | @RestrictTo(RestrictTo.Scope.LIBRARY) |
| 132 | public @FormatMode int getImageMode() { |
| 133 | return mImageMode; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Sets how {@link android.app.slice.SliceItem#FORMAT_ACTION} items should be handled. |
| 138 | * |
| 139 | * The default mode is {@link #MODE_THROW}. |
| 140 | * @param mode The desired mode. |
| 141 | */ |
| 142 | public SerializeOptions setActionMode(@FormatMode int mode) { |
| 143 | mActionMode = mode; |
| 144 | return this; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Sets how {@link android.app.slice.SliceItem#FORMAT_IMAGE} items should be handled. |
| 149 | * |
| 150 | * The default mode is {@link #MODE_THROW}. |
| 151 | * @param mode The desired mode. |
| 152 | */ |
| 153 | public SerializeOptions setImageMode(@FormatMode int mode) { |
| 154 | mImageMode = mode; |
| 155 | return this; |
| 156 | } |
| 157 | } |
| 158 | } |