• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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.cts.util;
18 
19 import android.app.ActivityManager;
20 import android.app.ActivityManager.MemoryInfo;
21 import android.app.Instrumentation;
22 import android.content.Context;
23 import android.os.ParcelFileDescriptor;
24 import android.os.StatFs;
25 
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 
29 public class SystemUtil {
getFreeDiskSize(Context context)30     public static long getFreeDiskSize(Context context) {
31         StatFs statFs = new StatFs(context.getFilesDir().getAbsolutePath());
32         return (long)statFs.getAvailableBlocks() * statFs.getBlockSize();
33     }
34 
getFreeMemory(Context context)35     public static long getFreeMemory(Context context) {
36         MemoryInfo info = new MemoryInfo();
37         ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
38         activityManager.getMemoryInfo(info);
39         return info.availMem;
40     }
41 
getTotalMemory(Context context)42     public static long getTotalMemory(Context context) {
43         MemoryInfo info = new MemoryInfo();
44         ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
45         activityManager.getMemoryInfo(info);
46         return info.totalMem; // TODO totalMem N/A in ICS.
47     }
48 
49     /**
50      * Executes a shell command using shell user identity, and return the standard output in string
51      * <p>Note: calling this function requires API level 21 or above
52      * @param instrumentation {@link Instrumentation} instance, obtained from a test running in
53      * instrumentation framework
54      * @param cmd the command to run
55      * @return the standard output of the command
56      * @throws Exception
57      */
runShellCommand(Instrumentation instrumentation, String cmd)58     public static String runShellCommand(Instrumentation instrumentation, String cmd)
59             throws IOException {
60         ParcelFileDescriptor pfd = instrumentation.getUiAutomation().executeShellCommand(cmd);
61         byte[] buf = new byte[512];
62         int bytesRead;
63         FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(pfd);
64         StringBuffer stdout = new StringBuffer();
65         while ((bytesRead = fis.read(buf)) != -1) {
66             stdout.append(new String(buf, 0, bytesRead));
67         }
68         fis.close();
69         return stdout.toString();
70     }
71 
72 }
73