1 /* 2 * Copyright (C) 2014 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 com.android.cts.verifier.sample; 18 19 import com.android.cts.verifier.PassFailButtons; 20 import com.android.cts.verifier.R; 21 22 import android.content.Intent; 23 import android.net.Uri; 24 import android.os.Bundle; 25 import android.view.View; 26 import android.widget.Button; 27 28 import java.io.File; 29 import java.io.FileOutputStream; 30 31 /** 32 * A sample CTS Verifier test case for testing file transfers using bluetooth sharing. 33 * 34 * This test assumes bluetooth is turned on and the device is already paired with a second device. 35 * Note: the second device need not be an Android device; it could be a laptop or desktop. 36 */ 37 public class SampleTestActivity extends PassFailButtons.Activity { 38 39 /** 40 * The name of the test file being transferred. 41 */ 42 private static final String FILE_NAME = "test.txt"; 43 44 /** 45 * The content of the test file being transferred. 46 */ 47 private static final String TEST_STRING = "Sample Test String"; 48 49 @Override onCreate(Bundle savedInstanceState)50 protected void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 53 // Setup the UI. 54 setContentView(R.layout.pass_fail_sample); 55 setPassFailButtonClickListeners(); 56 setInfoResources(R.string.sample_test, R.string.sample_test_info, -1); 57 // Get the share button and attach the listener. 58 Button shareBtn = (Button) findViewById(R.id.sample_share_btn); 59 shareBtn.setOnClickListener(new View.OnClickListener() { 60 @Override 61 public void onClick(View v) { 62 try { 63 createFileAndShare(); 64 } catch (Exception e) { 65 e.printStackTrace(); 66 } 67 } 68 }); 69 } 70 71 /** 72 * Creates a temporary file containing the test string and then issues the intent to share it. 73 * 74 * @throws Exception 75 */ createFileAndShare()76 private void createFileAndShare() throws Exception { 77 // Use the external cache directory so the file will be deleted when the app is uninstalled 78 // and the file can be accessed by other apps, such as the sharing app. 79 File dir = getExternalCacheDir (); 80 // Create the file with the given name. 81 File file = new File(dir, FILE_NAME); 82 FileOutputStream outputStream = null; 83 try { 84 // Write the test string to the test file. 85 outputStream = new FileOutputStream(file); 86 outputStream.write(TEST_STRING.getBytes()); 87 88 // Create the share intent. 89 Intent intent = new Intent(); 90 intent.setAction(Intent.ACTION_SEND); 91 intent.setType("text/plain"); 92 intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); 93 startActivity(intent); 94 } finally { 95 // Clean up. 96 if (outputStream != null) { 97 outputStream.close(); 98 } 99 } 100 101 } 102 } 103