/* Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License") ; * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by appli Cable law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. */package Android.cts.util;import Android.app.activitymanager;import Android.app.ActivityManager.MemoryInfo; Import Android.app.instrumentation;import Android.content.context;import Android.os.parcelfiledescriptor;import Android.os.statfs;import Java.io.fileinputstream;import Java.io.ioexception;public class SystemUtil {public static lo Ng Getfreedisksize (Context context) {StatFs statFs = new StatFs (Context.getfilesdir (). GetAbsolutePath ()); Return (long) statfs.getavailableblocks () * statfs.getblocksize (); } public static long Getfreememory (context context) {Memoryinfo info = new Memoryinfo (); Activitymanager Activitymanager = (activitymanager) context.getsystemservice (Context.activity_service); Activitymanager.getmemoryinfo (info); return info.availmem; } public static long Gettotalmemory (context context) {Memoryinfo info = new Memoryinfo (); Activitymanager Activitymanager = (activitymanager) context.getsystemservice (Context.activity_service); Activitymanager.getmemoryinfo (info); return info.totalmem; TODO totalmem N/A in ICS. }/** * Executes a shell command using shell user identity, and return the standard output in String * <p> ; Note:calling This function requires API level or above * @param instrumentation {@link instrumentation} instance, Obtained from a TEST running in * instrumentation framework * @param cmd the command to run * @return the standard output of th e command * @throws Exception */public static string Runshellcommand (instrumentation instrumentation, string C MD) throws IOException {Parcelfiledescriptor PFD = Instrumentation.getuiautomation (). Executeshellcomman D (CMD); byte[] buf = new byte[512]; int bytesread; FileInputStream fis = new Parcelfiledescriptor.autocloseinputstream (PFD); StringBuffer stdout = new StringBuffer (); while ((Bytesread = Fis.read (BUF))! =-1) {Stdout.append (new String (buf, 0, bytesread)); } fis.close (); return stdout.tostring (); }}
Common tool classes, file and memory size acquisition, shell script execution