1 /* 2 * Copyright (C) 2018 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.server.wm.flicker.monitor; 18 19 import static com.android.compatibility.common.util.SystemUtil.runShellCommand; 20 21 import android.os.Environment; 22 import android.os.RemoteException; 23 24 import com.android.internal.annotations.VisibleForTesting; 25 26 import java.io.File; 27 import java.nio.file.Path; 28 import java.nio.file.Paths; 29 import java.util.Locale; 30 31 /** 32 * Base class for monitors containing common logic to read the trace 33 * as a byte array and save the trace to another location. 34 */ 35 public abstract class TraceMonitor { 36 public static final String TAG = "FLICKER"; 37 private static final String TRACE_DIR = "/data/misc/wmtrace/"; 38 private static final String OUTPUT_DIR = 39 Environment.getExternalStorageDirectory().getPath(); 40 41 String traceFileName; 42 43 abstract void start(); 44 45 abstract void stop(); 46 47 abstract boolean isEnabled() throws RemoteException; 48 49 /** 50 * Saves trace file to the external storage directory suffixing the name with the testtag 51 * and iteration. 52 * 53 * Moves the trace file from the default location via a shell command since the test app 54 * does not have security privileges to access /data/misc/wmtrace. 55 * @param testTag suffix added to trace name used to identify trace 56 * @param iteration suffix added to trace name used to identify trace 57 * @return Path to saved trace file 58 */ 59 public Path saveTraceFile(String testTag, int iteration) { 60 Path traceFileCopy = getOutputTraceFilePath(testTag, iteration); 61 String copyCommand = String.format(Locale.getDefault(), "mv %s%s %s", TRACE_DIR, 62 traceFileName, traceFileCopy.toString()); 63 runShellCommand(copyCommand); 64 return traceFileCopy; 65 } 66 67 @VisibleForTesting 68 Path getOutputTraceFilePath(String testTag, int iteration) { 69 return Paths.get(OUTPUT_DIR, traceFileName + "_" + testTag + "_" + iteration); 70 } 71 } 72