Home | History | Annotate | Download | only in deviceinfo
      1 /*
      2  * Copyright (C) 2015 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 package com.android.compatibility.common.deviceinfo;
     17 
     18 import android.os.Environment;
     19 import android.util.Log;
     20 
     21 import com.android.compatibility.common.util.DeviceInfoStore;
     22 
     23 import java.io.IOException;
     24 import java.util.ArrayList;
     25 import java.util.List;
     26 import java.util.Scanner;
     27 
     28 /**
     29  * Storage device info collector.
     30  */
     31 public class StorageDeviceInfo extends DeviceInfo {
     32     private static final String TAG = "StorageDeviceInfo";
     33 
     34     @Override
     35     protected void collectDeviceInfo(DeviceInfoStore store) throws Exception {
     36         int total = 0;
     37         total = Math.max(total, getContext().getExternalCacheDirs().length);
     38         total = Math.max(total, getContext().getExternalFilesDirs(null).length);
     39         total = Math.max(
     40                 total, getContext().getExternalFilesDirs(Environment.DIRECTORY_PICTURES).length);
     41         total = Math.max(total, getContext().getObbDirs().length);
     42 
     43         int emulated = 0;
     44         int physical = 0;
     45         if (Environment.isExternalStorageEmulated()) {
     46             if (total == 1) {
     47                 emulated = 1;
     48             } else {
     49                 emulated = 1;
     50                 physical = total - 1;
     51             }
     52         } else {
     53             physical = total;
     54         }
     55 
     56         store.addResult("num_physical", physical);
     57         store.addResult("num_emulated", emulated);
     58 
     59         store.addListResult("raw_partition", scanPartitions());
     60     }
     61 
     62     private List<String> scanPartitions() {
     63         List<String> partitionList = new ArrayList<>();
     64         try {
     65             Process df = new ProcessBuilder("df -k").start();
     66             Scanner scanner = new Scanner(df.getInputStream());
     67             try {
     68                 while (scanner.hasNextLine()) {
     69                     partitionList.add(scanner.nextLine());
     70                 }
     71             } finally {
     72                 scanner.close();
     73             }
     74         } catch (Exception e) {
     75             Log.w(TAG, Log.getStackTraceString(e));
     76         }
     77         return partitionList;
     78     }
     79 
     80 }
     81