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.contentcaptureservice.cts; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import android.content.LocusId; 21 import android.contentcaptureservice.cts.CtsContentCaptureService.Session; 22 import android.os.ParcelFileDescriptor; 23 import android.view.contentcapture.ContentCaptureManager; 24 import android.view.contentcapture.DataShareRequest; 25 import android.view.contentcapture.DataShareWriteAdapter; 26 27 import androidx.annotation.NonNull; 28 29 import java.io.IOException; 30 import java.io.OutputStream; 31 import java.util.Random; 32 import java.util.concurrent.Executor; 33 import java.util.concurrent.Executors; 34 35 public class DataSharingActivity extends AbstractContentCaptureActivity { 36 37 private static final Executor sExecutor = Executors.newCachedThreadPool(); 38 private static final Random sRandom = new Random(); 39 private static final String sLocusId = "DataShare_CTSTest"; 40 private static final String sMimeType = "application/octet-stream"; 41 42 boolean mSessionFinished = false; 43 boolean mSessionSucceeded = false; 44 int mSessionErrorCode = 0; 45 boolean mSessionRejected = false; 46 byte[] mDataWritten = new byte[10_000]; 47 48 @Override onStart()49 public void onStart() { 50 super.onStart(); 51 52 ContentCaptureManager manager = 53 getApplicationContext().getSystemService(ContentCaptureManager.class); 54 55 assertThat(manager).isNotNull(); 56 assertThat(manager.isContentCaptureEnabled()).isTrue(); 57 58 manager.shareData( 59 new DataShareRequest(new LocusId(sLocusId), sMimeType), 60 sExecutor, new DataShareWriteAdapter() { 61 @Override 62 public void onWrite(ParcelFileDescriptor destination) { 63 sRandom.nextBytes(mDataWritten); 64 65 try (OutputStream outputStream = 66 new ParcelFileDescriptor.AutoCloseOutputStream(destination)) { 67 outputStream.write(mDataWritten); 68 } catch (IOException e) { 69 // fall through, sessionSucceeded will stay false. 70 } 71 72 mSessionSucceeded = true; 73 mSessionFinished = true; 74 } 75 76 @Override 77 public void onRejected() { 78 mSessionRejected = true; 79 mSessionSucceeded = false; 80 mSessionFinished = true; 81 } 82 83 @Override 84 public void onError(int errorCode) { 85 mSessionErrorCode = errorCode; 86 mSessionSucceeded = false; 87 mSessionFinished = true; 88 } 89 }); 90 } 91 92 @Override assertDefaultEvents(@onNull Session session)93 public void assertDefaultEvents(@NonNull Session session) { 94 // Do nothing - this test operates with file sharing. 95 } 96 } 97