1 /* 2 * Copyright (C) 2020 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 package android.controls.cts; 17 18 import android.service.controls.Control; 19 20 import java.util.ArrayList; 21 import java.util.List; 22 import java.util.concurrent.Flow.Publisher; 23 import java.util.concurrent.Flow.Subscriber; 24 import java.util.concurrent.Flow.Subscription; 25 26 /** 27 * Simplified Publisher for use with CTS testing only. Assumes all Controls are added 28 * to the Publisher ahead of a subscribe request. Assumes only one request() call. 29 */ 30 public class CtsControlsPublisher implements Publisher<Control> { 31 32 private final List<Control> mControls = new ArrayList<>(); 33 private Subscriber<? super Control> mSubscriber; 34 CtsControlsPublisher(List<Control> controls)35 public CtsControlsPublisher(List<Control> controls) { 36 if (controls != null) { 37 mControls.addAll(controls); 38 } 39 } 40 subscribe�(Subscriber<? super Control> subscriber)41 public void subscribe(Subscriber<? super Control> subscriber) { 42 mSubscriber = subscriber; 43 mSubscriber.onSubscribe(new Subscription() { 44 public void request(long n) { 45 int i = 0; 46 while (i < n && i < mControls.size()) { 47 subscriber.onNext(mControls.get(i)); 48 i++; 49 } 50 51 if (i == mControls.size()) { 52 subscriber.onComplete(); 53 } 54 } 55 56 public void cancel() { 57 58 } 59 }); 60 } 61 onNext(Control c)62 public void onNext(Control c) { 63 if (mSubscriber == null) { 64 mControls.add(c); 65 } else { 66 mSubscriber.onNext(c); 67 } 68 } 69 } 70