Upstream version 9.38.204.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core_internal / src / org / xwalk / core / internal / 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.internal.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
19 class DeviceCapabilitiesMemory {
20     private static final String MEM_INFO_FILE = "/proc/meminfo";
21     private static final String TAG = "DeviceCapabilitiesMemory";
22
23     private DeviceCapabilities mDeviceCapabilities;
24     private Context mContext;
25
26     private long mAvailableCapacity = 0;
27     private long mCapacity = 0;
28
29     public DeviceCapabilitiesMemory(DeviceCapabilities instance,
30                                     Context context) {
31         mDeviceCapabilities = instance;
32         mContext = context;
33     }
34
35     public JSONObject getInfo() {
36         readMemoryInfo();
37
38         JSONObject out = new JSONObject();
39         try {
40             out.put("capacity", mCapacity);
41             out.put("availCapacity", mAvailableCapacity);
42         } catch (JSONException e) {
43             return mDeviceCapabilities.setErrorMessage(e.toString());
44         }
45
46         return out;
47     }
48
49     private void readMemoryInfo() {
50         MemoryInfo mem_info = new MemoryInfo();
51         ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
52         activityManager.getMemoryInfo(mem_info);
53
54         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
55             mCapacity = mem_info.totalMem;
56         } else {
57             mCapacity = getTotalMemFromFile();
58         }
59         mAvailableCapacity = mem_info.availMem;
60     }
61
62     private long getTotalMemFromFile() {
63         long capacity = 0;
64         RandomAccessFile file = null;
65
66         try {
67             file = new RandomAccessFile(MEM_INFO_FILE, "r");
68             String line = file.readLine();
69
70             String[] arrs = line.split(":");
71             if (!arrs[0].equals("MemTotal")) {
72                 return 0;
73             }
74             String[] values = arrs[1].trim().split("\\s+");
75             capacity = Long.parseLong(values[0]) * 1024;
76         } catch (IOException e) {
77             capacity = 0;
78             Log.e(TAG, e.toString());
79         } finally {
80             try {
81                 if (file != null) {
82                     file.close();
83                 }
84             } catch (IOException e) {
85                 Log.e(TAG, e.toString());
86             }
87         }
88
89         return capacity; 
90     }
91 }