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.
5 package org.xwalk.core.extension.api.device_capabilities;
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;
13 import java.io.RandomAccessFile;
14 import java.io.IOException;
16 import org.json.JSONException;
17 import org.json.JSONObject;
18 import org.xwalk.core.extension.XWalkExtensionContext;
20 class DeviceCapabilitiesMemory {
21 private static final String MEM_INFO_FILE = "/proc/meminfo";
22 private static final String TAG = "DeviceCapabilitiesMemory";
24 private DeviceCapabilities mDeviceCapabilities;
25 private Context mContext;
27 private long mAvailableCapacity = 0;
28 private long mCapacity = 0;
30 public DeviceCapabilitiesMemory(DeviceCapabilities instance,
31 XWalkExtensionContext context) {
32 mDeviceCapabilities = instance;
33 mContext = context.getContext();
36 public JSONObject getInfo() {
39 JSONObject out = new JSONObject();
41 out.put("capacity", mCapacity);
42 out.put("availCapacity", mAvailableCapacity);
43 } catch (JSONException e) {
44 return mDeviceCapabilities.setErrorMessage(e.toString());
50 private void readMemoryInfo() {
51 MemoryInfo mem_info = new MemoryInfo();
52 ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
53 activityManager.getMemoryInfo(mem_info);
55 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
56 mCapacity = mem_info.totalMem;
58 mCapacity = getTotalMemFromFile();
60 mAvailableCapacity = mem_info.availMem;
63 private long getTotalMemFromFile() {
65 RandomAccessFile file = null;
68 file = new RandomAccessFile(MEM_INFO_FILE, "r");
69 String line = file.readLine();
71 String[] arrs = line.split(":");
72 if (!arrs[0].equals("MemTotal")) {
75 String[] values = arrs[1].trim().split("\\s+");
76 capacity = Long.parseLong(values[0]) * 1024;
77 } catch (IOException e) {
79 Log.e(TAG, e.toString());
85 } catch (IOException e) {
86 Log.e(TAG, e.toString());