Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / extension / api / device_capabilities / DeviceCapabilitiesMemory.java
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.xwalk.core.extension.api.device_capabilities;
6
7 import android.app.ActivityManager;
8 import android.app.ActivityManager.MemoryInfo;
9 import android.content.Context;
10 import android.util.Log;
11 import android.os.Build;
12
13 import java.io.RandomAccessFile;
14 import java.io.IOException;
15
16 import org.json.JSONException;
17 import org.json.JSONObject;
18 import org.xwalk.core.extension.XWalkExtensionContext;
19
20 class DeviceCapabilitiesMemory {
21     private static final String MEM_INFO_FILE = "/proc/meminfo";
22     private static final String TAG = "DeviceCapabilitiesMemory";
23
24     private DeviceCapabilities mDeviceCapabilities;
25     private Context mContext;
26
27     private long mAvailableCapacity = 0;
28     private long mCapacity = 0;
29
30     public DeviceCapabilitiesMemory(DeviceCapabilities instance,
31                                     XWalkExtensionContext context) {
32         mDeviceCapabilities = instance;
33         mContext = context.getContext();
34     }
35
36     public JSONObject getInfo() {
37         readMemoryInfo();
38
39         JSONObject out = new JSONObject();
40         try {
41             out.put("capacity", mCapacity);
42             out.put("availCapacity", mAvailableCapacity);
43         } catch (JSONException e) {
44             return mDeviceCapabilities.setErrorMessage(e.toString());
45         }
46
47         return out;
48     }
49
50     private void readMemoryInfo() {
51         MemoryInfo mem_info = new MemoryInfo();
52         ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
53         activityManager.getMemoryInfo(mem_info);
54
55         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
56             mCapacity = mem_info.totalMem;
57         } else {
58             mCapacity = getTotalMemFromFile();
59         }
60         mAvailableCapacity = mem_info.availMem;
61     }
62
63     private long getTotalMemFromFile() {
64         long capacity = 0;
65         RandomAccessFile file = null;
66
67         try {
68             file = new RandomAccessFile(MEM_INFO_FILE, "r");
69             String line = file.readLine();
70
71             String[] arrs = line.split(":");
72             if (!arrs[0].equals("MemTotal")) {
73                 return 0;
74             }
75             String[] values = arrs[1].trim().split("\\s+");
76             capacity = Long.parseLong(values[0]) * 1024;
77         } catch (IOException e) {
78             capacity = 0;
79             Log.e(TAG, e.toString());
80         } finally {
81             try {
82                 if (file != null) {
83                     file.close();
84                 }
85             } catch (IOException e) {
86                 Log.e(TAG, e.toString());
87             }
88         }
89
90         return capacity; 
91     }
92 }