huangcm
2025-04-09 02d4ce54b909bd733f12e9f3fa4c1b03cf2d6f45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package com.DeviceTest;
import java.io.File;
 
import com.DeviceTest.helper.ControlButtonUtil;
import com.DeviceTest.helper.SystemInfoUtil;
 
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.StatFs;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.os.SystemProperties;
import android.os.storage.StorageEventListener;
import android.text.format.Formatter;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
 
public class InfomationActivity extends Activity {
   private final static String TAG = "InfomationActivity";
   
   private TextView mCPUInfoTV;
   private TextView mMemoryInfoTV;
   //private TextView mNandFlashTotalTV;
   //private TextView mNandFlashAvailTV;
   private TextView mInternalAvailTV;
   private TextView mInternalAvailSizeTV;
   
   public  String flash_path = null;
   public  String sdcard_path = null;
   public  String usb_path = null;
   private int flash_pit = 0; 
   private int sdcard_pit = 1; 
   private int usb_pit = 2; 
   private StorageVolume[] storageVolumes = null;
    private StorageManager mStorageManager = null;
    private final static int MSG_OK=0;
   
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.infomation);
       InitStorage();
       initRes();
       ControlButtonUtil.initControlButtonView(this);
       //Cancel automatically passing test.
       //handler.sendEmptyMessageDelayed(MSG_OK, 2500);
   }
   
   private Handler handler=new Handler(){
       @Override
       public void handleMessage(Message msg) {
           switch(msg.what){
           case MSG_OK:
               ((Button) findViewById(R.id.btn_Pass)).performClick();
           }
       }
   };
   
   @Override
   protected void onResume() {
       super.onResume();
       updateView();
   }
   
   private void initRes() {
       mCPUInfoTV = (TextView) findViewById(R.id.cpuinfo_tv);
       mMemoryInfoTV = (TextView) findViewById(R.id.meminfo_tv);
       //mNandFlashTotalTV = (TextView) findViewById(R.id.nand_total_tv);
       //mNandFlashAvailTV = (TextView) findViewById(R.id.nand_avail_tv);
       mInternalAvailTV  = (TextView) findViewById(R.id.internal_avail_tv);
       mInternalAvailSizeTV    = (TextView) findViewById(R.id.internal_avail_size_tv);
   }
   
   private String getCpuInfoString() {
       StringBuilder cpuInfoSB = new StringBuilder(); 
       /*cpuInfoSB.append("processor  :  ");*/
       /*cpuInfoSB.append(SystemInfoUtil.getCpuName()).append("  ").append("\n");*/
       cpuInfoSB.append(SystemInfoUtil.getNumCores()+"").append("").append(" * ").append(""+SystemInfoUtil.getMaxCpuFreq()+" Hz");
       return cpuInfoSB.toString();
   }
   
    private long getEMMC() {
        try {
 
        int blockNum = 0;
        for(int i=0;i<5;i++) {
                String path=String.format("/sys/block/mmcblk%dboot0", i);
                if(new File(path).exists()) {
                        blockNum = i;
                }
        }
 
        File targetFile = null;
 
        if ( blockNum == 0 ) {
            File File0 = new File("/sys/block/mmcblk0/size");
            if(File0.exists()) {
                targetFile = File0;
            }
        }
 
 
        if ( blockNum == 1 ) {
            File File1 = new File("/sys/block/mmcblk1/size");
            if(File1.exists()){
                targetFile = File1;
            }
 
        }
 
        if ( blockNum == 2) {
            File File2 = new File("/sys/block/mmcblk2/size");
            if(File2.exists()){
                targetFile = File2;
            }
        }
 
            //Log.d(TAG, "tanzh------" + targetFile.getPath());
 
 
                FileReader fr = new FileReader(targetFile.getPath());
                BufferedReader br = new BufferedReader(fr);
                String text = br.readLine();
                long result = Long.valueOf(text) / 2000000;
                int temp = 0;
                if (result > 54 && result < 66) {
                    temp = 64;
                } else if (result > 24 && result < 34) {
                    temp = 32;
                } else if (result > 12 && result < 18) {
                    temp = 16;
                } else if (result > 6 && result < 10) {
                    temp = 8;
                }
                return temp;
            } catch(FileNotFoundException e){
                e.printStackTrace();
            } catch(IOException e){
                e.printStackTrace();
            }
            return -1;
    }
   
   
   private void updateView() {
       mCPUInfoTV.setText(getCpuInfoString());
       mMemoryInfoTV.setText(SystemInfoUtil.GetMemInfo1(this));
       //updateMemoryStatus(flash_path);
          File dataPath = Environment.getDataDirectory();
        StatFs stat = new StatFs(dataPath.getPath());
        long blockSize = stat.getBlockSize();
       long blockCount = stat.getBlockCount();
       
       
       
       // long availableBlocks = stat.getAvailableBlocks();
        //mInternalAvailSizeTV.setText(formatSize(blockCount * blockSize));
       mInternalAvailSizeTV.setText(getEMMC() + "GB");
       
       //Log.d(TAG,"formatSize(availableBlocks * blockSize)="+formatSize(availableBlocks * blockSize));
   }
   
   
   private void updateMemoryStatus(String path) {
        String status = SystemProperties.get("EXTERNAL_STORAGE_STATE","unmounted");
        if (flash_path!=null&&path.equals(flash_path)) {
            status = mStorageManager.getVolumeState(path);
        }
        String readOnly = "";
        if (status.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
            status = Environment.MEDIA_MOUNTED;
            readOnly = getString(R.string.read_only);
        }
 
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            try {
                //File path = Environment.getExternalStorageDirectory();
                StatFs stat = new StatFs(path);
                long blockSize = stat.getBlockSize();
                long totalBlocks = stat.getBlockCount();
                long availableBlocks = stat.getAvailableBlocks();
 
                /*if (path.equals(flash_path)){
                    mNandFlashTotalTV.setText(getString(R.string.total_space)+":"+formatSize(totalBlocks * blockSize));
                    mNandFlashAvailTV.setText(getString(R.string.available_space)+":"+formatSize(availableBlocks * blockSize) + readOnly);
                }*/
            } catch (IllegalArgumentException e) {
                // this can occur if the SD card is removed, but we haven't received the 
                // ACTION_MEDIA_REMOVED Intent yet.
                status = Environment.MEDIA_REMOVED;
            }
            
        } else {
            if (flash_path!=null&&path.equals(flash_path)){
                /*mNandFlashTotalTV.setText(getString(R.string.nand_unavailable));
                mNandFlashAvailTV.setText(getString(R.string.nand_unavailable));*/
                if (status.equals(Environment.MEDIA_UNMOUNTED) ||
                    status.equals(Environment.MEDIA_NOFS) ||
                    status.equals(Environment.MEDIA_UNMOUNTABLE) ) {
                }
            }
        }
 
        File dataPath = Environment.getDataDirectory();
        StatFs stat = new StatFs(dataPath.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        mInternalAvailTV.setText(getString(R.string.available_space)+":"+formatSize(availableBlocks * blockSize));
       Log.d(TAG,"formatSize(availableBlocks * blockSize)="+formatSize(availableBlocks * blockSize));
    }
    
    private String formatSize(long size) {
        return Formatter.formatFileSize(this, size);
    }
 
    public void onCancel(DialogInterface dialog) {
        finish();
    }
    
   private void InitStorage(){
       if (mStorageManager == null) {
            mStorageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
            mStorageManager.registerListener(mStorageListener);
            storageVolumes = mStorageManager.getVolumeList();
            Log.e(TAG,"storageVolumes.length:"+storageVolumes.length);
            if(storageVolumes.length >= 3){
                flash_path = storageVolumes[flash_pit].getPath();
                sdcard_path = storageVolumes[sdcard_pit].getPath();
                usb_path = storageVolumes[usb_pit].getPath();
                Log.d(TAG, " _____ " + flash_path + "   " + sdcard_path + "   " + usb_path);
            }
        }
   }
   
    StorageEventListener mStorageListener = new StorageEventListener() {
 
        @Override
        public void onStorageStateChanged(String path, String oldState, String newState) {
            Log.d(TAG, "Received storage state changed notification that " +
                    path + " changed state from " + oldState +
                    " to " + newState);
            if (path.equals(sdcard_path) && !newState.equals(Environment.MEDIA_MOUNTED)) {
            } else {
                updateMemoryStatus(flash_path);
            }
        }
    };
   
   
}