1 /* 2 * Copyright (C) 2016 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 android.backup.app; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.util.Log; 22 23 import java.io.BufferedOutputStream; 24 import java.io.File; 25 import java.io.FileOutputStream; 26 import java.io.IOException; 27 import java.util.Random; 28 29 /** 30 * The activity could be invoked to create a test file. 31 * 32 * Here is an example of a call: 33 * 34 * am start -a android.intent.action.MAIN \ 35 * -c android.intent.category.LAUNCHER \ 36 * -n android.backup.app/android.backup.app.MainActivity \ 37 * -e file_size 10000000 38 * 39 * "File created!" string is printed in logcat when file is created. 40 */ 41 public class MainActivity extends Activity { 42 public static final String TAG = "BackupCTSApp"; 43 public static final String FILE_NAME = "file_name"; 44 45 private static final String FILE_SIZE_EXTRA = "file_size"; 46 private static final int DATA_CHUNK_SIZE = 1024 * 1024; 47 48 @Override onCreate(Bundle savedInstanceActivity)49 protected void onCreate(Bundle savedInstanceActivity) { 50 super.onCreate(savedInstanceActivity); 51 if (getIntent().hasExtra(FILE_SIZE_EXTRA)) { 52 int fileSize = Integer.parseInt(getIntent().getStringExtra(FILE_SIZE_EXTRA)); 53 Log.i(TAG, "Creating file of size: " + fileSize); 54 try { 55 createFile(fileSize); 56 Log.d(TAG, "File created!"); 57 } catch (IOException e) { 58 Log.e(TAG, "IOException: " + e); 59 } 60 } else { 61 Log.d(TAG, "No file size was provided"); 62 } 63 finish(); 64 } 65 createFile(int size)66 private void createFile(int size) throws IOException { 67 byte[] bytes = new byte[DATA_CHUNK_SIZE]; 68 new Random().nextBytes(bytes); 69 File f = new File(getFilesDir().getAbsolutePath(), FILE_NAME); 70 f.getParentFile().mkdirs(); 71 try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f))) { 72 while (size > 0) { 73 int bytesToWrite = Math.min(size, DATA_CHUNK_SIZE); 74 bos.write(bytes, 0, bytesToWrite); 75 size -= bytesToWrite; 76 } 77 } 78 } 79 } 80