Home | History | Annotate | Download | only in loopback
      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 org.drrickorang.loopback;
     18 
     19 import android.content.Context;
     20 import android.util.Log;
     21 
     22 import java.io.File;
     23 import java.io.FileOutputStream;
     24 import java.io.IOException;
     25 import java.io.InputStream;
     26 import java.io.OutputStream;
     27 
     28 /**
     29  *  Places loopback_listener shell script on device storage
     30  */
     31 public class AtraceScriptsWriter {
     32 
     33     private static final String TAG = "AtraceScriptsWriter";
     34     private static final String LISTENER_SCRIPT_LOCATION =
     35             CaptureHolder.DIRECTORY + "/loopback_listener";
     36 
     37     /** Writes scripts to device storage, return true on successful write **/
     38     public static boolean writeScriptsToFile(Context ctx) {
     39         try {
     40             File file = new File(CaptureHolder.DIRECTORY);
     41 
     42             // Create a directory for script and signal file
     43             if (!file.exists()) {
     44                 if (file.mkdir()) {
     45                     Log.d(TAG, "writeScriptsToFile: Loopback folder created");
     46                 } else {
     47                     System.out.println("Failed to create folder!");
     48                     return false;
     49                 }
     50             }
     51             // Check for writable directory that already existed or after creating
     52             if (!file.isDirectory() || !file.canWrite()) {
     53                 Log.d(TAG, "writeScriptsToFile: " + CaptureHolder.DIRECTORY
     54                         + (!file.isDirectory() ? "is not a directory " : "")
     55                         + (!file.canWrite() ? "is not writable" : ""));
     56                 return false;
     57             }
     58             copyResToFile(ctx, R.raw.loopback_listener, LISTENER_SCRIPT_LOCATION);
     59         } catch (IOException e) {
     60             Log.e(TAG, "Unable to write script to file", e);
     61             return false;
     62         }
     63         return true;
     64     }
     65 
     66     private static void copyResToFile(Context ctx, int resId, String targetFile)
     67             throws IOException {
     68         InputStream inputStream = ctx.getResources().openRawResource(resId);
     69         OutputStream outputStream = new FileOutputStream(targetFile);
     70         copy(inputStream, outputStream);
     71         outputStream.close();
     72         inputStream.close();
     73     }
     74 
     75 
     76     private static int copy(InputStream input, OutputStream output) throws IOException {
     77         final int BYTES_TO_READ = 2048;
     78         byte[] buffer = new byte[BYTES_TO_READ];
     79         int total = 0;
     80         int n;
     81         while ((n = input.read(buffer)) != -1) {
     82             output.write(buffer, 0, n);
     83             total = total + n;
     84         }
     85         return total;
     86     }
     87 
     88 }
     89