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 package android.content.cts; 17 18 import android.app.Activity; 19 import android.content.ClipData; 20 import android.content.ClipData.Item; 21 import android.content.Intent; 22 import android.net.Uri; 23 import android.os.Bundle; 24 import android.provider.MediaStore; 25 import android.util.Log; 26 27 import java.io.File; 28 import java.io.FileWriter; 29 import java.io.IOException; 30 import java.io.Writer; 31 32 public class ImageCaptureActivity extends Activity { 33 public static final String ACTION_FILE_READY = "android.content.cts.action.file_ready"; 34 private static final String TAG = ImageCaptureUriExtraToClipDataTest.TAG; 35 onCreate(Bundle savedInstanceState)36 protected void onCreate(Bundle savedInstanceState) { 37 super.onCreate(savedInstanceState); 38 Intent intent = getIntent(); 39 40 // Check action. 41 String action = intent.getAction(); 42 if ((MediaStore.ACTION_IMAGE_CAPTURE.equals(action) 43 || MediaStore.ACTION_IMAGE_CAPTURE_SECURE.equals(action) 44 || MediaStore.ACTION_VIDEO_CAPTURE.equals(action))) { 45 writeToClipDataUri(intent); 46 } 47 48 finish(); 49 } 50 51 // Sends ACTION_FILE_READY intent when write to clipdata uri is succesful. writeToClipDataUri(Intent intent)52 private void writeToClipDataUri(Intent intent) { 53 if ((intent.getFlags() & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0) { 54 55 // Note: since this activity is in the same package as the test we can write to the file 56 // regardless of this permission, but in general this permission is required. 57 Log.e(TAG, "Intent.FLAG_GRANT_WRITE_URI_PERMISSION was not granted."); 58 return; 59 } 60 61 File file = getFileFromIntent(intent); 62 if (file == null) { 63 Log.e(TAG, "Could not get file from clipdata."); 64 return; 65 } 66 try { 67 FileWriter writer = new FileWriter(file); 68 writer.write(ImageCaptureUriExtraToClipDataTest.TEST_INPUT); 69 writer.flush(); 70 writer.close(); 71 } catch (IOException e) { 72 Log.e(TAG, "File IO failure while writing."); 73 return; 74 } 75 Intent fileReady = new Intent(ACTION_FILE_READY); 76 sendBroadcast(fileReady); 77 } 78 getFileFromIntent(Intent intent)79 private File getFileFromIntent(Intent intent) { 80 ClipData clipData = intent.getClipData(); 81 if (clipData == null) { 82 Log.e(TAG, "ClipData missing."); 83 return null; 84 } 85 if (clipData.getItemCount() == 0) { 86 Log.e(TAG, "Uri missing in ClipData."); 87 return null; 88 } 89 90 Uri filePath = clipData.getItemAt(0).getUri(); 91 if (filePath == null) { 92 Log.e(TAG, "Uri missing in ClipData."); 93 return null; 94 } 95 96 try { 97 return new File(filePath.getPath()); 98 } catch (IllegalArgumentException e) { 99 Log.e(TAG, "Cannot get file at Uri."); 100 return null; 101 } 102 } 103 }