1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package android.slice.cts; 16 17 import static java.util.stream.Collectors.toList; 18 19 import android.app.PendingIntent; 20 import android.app.slice.Slice; 21 import android.app.slice.Slice.Builder; 22 import android.app.slice.SliceSpec; 23 import android.content.ContentResolver; 24 import android.content.Intent; 25 import android.graphics.drawable.Icon; 26 import android.net.Uri; 27 import android.os.Bundle; 28 import android.os.Parcel; 29 import android.os.Parcelable; 30 31 import java.util.Arrays; 32 import java.util.Collection; 33 import java.util.Collections; 34 import java.util.Set; 35 36 public class SliceProvider extends android.app.slice.SliceProvider { 37 38 static final String[] PATHS = new String[]{ 39 "/set_flag", 40 "/subslice", 41 "/text", 42 "/icon", 43 "/action", 44 "/int", 45 "/timestamp", 46 "/hints", 47 "/bundle", 48 "/spec", 49 }; 50 51 public static final String SPEC_TYPE = "android.cts.SliceType"; 52 public static final int SPEC_REV = 4; 53 54 public static final SliceSpec SPEC = new SliceSpec(SPEC_TYPE, SPEC_REV); 55 56 @Override 57 public boolean onCreate() { 58 return true; 59 } 60 61 @Override 62 public Collection<Uri> onGetSliceDescendants(Uri uri) { 63 if (uri.getPath().equals("/")) { 64 Uri.Builder builder = new Uri.Builder() 65 .scheme(ContentResolver.SCHEME_CONTENT) 66 .authority("android.slice.cts"); 67 return Arrays.asList(PATHS).stream().map(s -> 68 builder.path(s).build()).collect(toList()); 69 } 70 return Collections.emptyList(); 71 } 72 73 @Override 74 public Slice onBindSlice(Uri sliceUri, Set<SliceSpec> supportedSpecs) { 75 switch (sliceUri.getPath()) { 76 case "/set_flag": 77 SliceBindingTest.sFlag = true; 78 break; 79 case "/subslice": 80 Builder b = new Builder(sliceUri, SPEC); 81 return b.addSubSlice(new Slice.Builder(b).build(), "subslice").build(); 82 case "/text": 83 return new Slice.Builder(sliceUri, SPEC).addText("Expected text", "text", 84 Collections.emptyList()).build(); 85 case "/icon": 86 return new Slice.Builder(sliceUri, SPEC).addIcon( 87 Icon.createWithResource(getContext(), R.drawable.size_48x48), "icon", 88 Collections.emptyList()).build(); 89 case "/action": 90 Builder builder = new Builder(sliceUri, SPEC); 91 Slice subSlice = new Slice.Builder(builder).build(); 92 PendingIntent broadcast = PendingIntent.getBroadcast(getContext(), 0, 93 new Intent(getContext().getPackageName() + ".action"), 0); 94 return builder.addAction(broadcast, subSlice, "action").build(); 95 case "/int": 96 return new Slice.Builder(sliceUri, SPEC).addInt(0xff121212, "int", 97 Collections.emptyList()).build(); 98 case "/timestamp": 99 return new Slice.Builder(sliceUri, SPEC).addLong(43, "timestamp", 100 Collections.emptyList()).build(); 101 case "/hints": 102 return new Slice.Builder(sliceUri, SPEC) 103 .addHints(Arrays.asList(Slice.HINT_LIST)) 104 .addText("Text", null, Arrays.asList(Slice.HINT_TITLE)) 105 .addIcon(Icon.createWithResource(getContext(), R.drawable.size_48x48), 106 null, Arrays.asList(Slice.HINT_NO_TINT, Slice.HINT_LARGE)) 107 .build(); 108 case "/bundle": 109 Bundle b1 = new Bundle(); 110 b1.putParcelable("a", new TestParcel()); 111 return new Slice.Builder(sliceUri, SPEC).addBundle(b1, "bundle", 112 Collections.emptyList()).build(); 113 case "/spec": 114 return new Slice.Builder(sliceUri, SPEC) 115 .build(); 116 } 117 return new Slice.Builder(sliceUri, SPEC).build(); 118 } 119 120 public static class TestParcel implements Parcelable { 121 122 private final int mValue; 123 124 public TestParcel() { 125 mValue = 42; 126 } 127 128 protected TestParcel(Parcel in) { 129 mValue = in.readInt(); 130 } 131 132 @Override 133 public void writeToParcel(Parcel dest, int flags) { 134 dest.writeInt(mValue); 135 } 136 137 @Override 138 public int describeContents() { 139 return 0; 140 } 141 142 @Override 143 public boolean equals(Object obj) { 144 try { 145 TestParcel p = (TestParcel) obj; 146 return p.mValue == mValue; 147 } catch (ClassCastException e) { 148 return false; 149 } 150 } 151 152 public static final Creator<TestParcel> CREATOR = new Creator<TestParcel>() { 153 @Override 154 public TestParcel createFromParcel(Parcel in) { 155 return new TestParcel(in); 156 } 157 158 @Override 159 public TestParcel[] newArray(int size) { 160 return new TestParcel[size]; 161 } 162 }; 163 } 164 } 165