Added the following in codova_plugins.js
code
{
"file": "plugins\com.ankamagames.plugins.sysinfo\www\SysInfo.js",
"id": "com.ankamagames.plugins.sysinfo",
"clobbers": [
"window.Sysinfo"
]
}
/code
Added the following in config.xml
code
<feature name="Sysinfo" >
<param name="android-package" value="com.ankamagames.plugins.sysinfo"/>
</feature>
/code
Here is Sysinfo.java
code
package com.ankamagames.plugins.sysinfo;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.os.Build;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import java.lang.Process;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import android.util.*;
public class Sysinfo extends CordovaPlugin {
private MemoryInfo memoryInfo;
@Override
public boolean execute(String action, JSONArray args, CallbackContext callback) {
Activity activity = this.cordova.getActivity();
ActivityManager m = (ActivityManager) activity.getSystemService(Activity.ACTIVITY_SERVICE);
this.memoryInfo = new MemoryInfo();
m.getMemoryInfo(this.memoryInfo);
if (action.equals("getInfo")) {
try {
JSONObject r = new JSONObject();
r.put("cpu", this.getCpuInfo());
r.put("memory", this.getMemoryInfo());
Log.d("OUTPUT", r.toString());
callback.success(r);
} catch (final Exception e) {
callback.error(e.getMessage());
}
}
return false;
}
public JSONObject getCpuInfo() {
JSONObject cpu = new JSONObject();
try {
// Get CPU Core count
String output = readSystemFile("/sys/devices/system/cpu/present");
String[] parts = output.split("-");
Integer cpuCount = Integer.parseInt(parts[1]) + 1;
Code: Select all
cpu.put("count", cpuCount);
// Get CPU Core frequency
JSONArray cpuCores = new JSONArray();
for(int i = 0; i < cpuCount; i++) {
Integer cpuMaxFreq = getCPUFrequencyMax(i);
cpuCores.put(cpuMaxFreq == 0 ? null : cpuMaxFreq);
}
cpu.put("cores", cpuCores);
} catch (final Exception e) { }
return cpu;
}
public JSONObject getMemoryInfo() {
JSONObject memory = new JSONObject();
try {
memory.put("available", this.memoryInfo.availMem);
memory.put("total", this.getTotalMemory());
memory.put("threshold", this.memoryInfo.threshold);
memory.put("low", this.memoryInfo.lowMemory);
} catch (final Exception e) {
}
return memory;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public Object getTotalMemory() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
return this.memoryInfo.totalMem;
}
else {
return null;
}
}
/**
@return in kiloHertz.
@throws SystemUtilsException
*/
public int getCPUFrequencyMax(int index) throws Exception {
return readSystemFileAsInt("/sys/devices/system/cpu/cpu" + index + "/cpufreq/cpuinfo_max_freq");
}
private String readSystemFile(final String pSystemFile) {
String content = ""
InputStream in = null;
try {
final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start();
in = process.getInputStream();
content = readFully(in);
} catch (final Exception e) { }
return content;
}
private int readSystemFileAsInt(final String pSystemFile) throws Exception {
String content = readSystemFile(pSystemFile);
if (content == "") {
return 0;
}
return Integer.parseInt( content );
}
private String readFully(final InputStream pInputStream) throws IOException {
final StringBuilder sb = new StringBuilder();
final Scanner sc = new Scanner(pInputStream);
while(sc.hasNextLine()) {
sb.append(sc.nextLine());
}
return sb.toString();
}
}
/code
and Sysinfo.js file
code
/*
*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
*
http://www.apache.org/licenses/LICENSE-2.0
*
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*
*/
var argscheck = require('cordova/argscheck'),
channel = require('cordova/channel'),
utils = require('cordova/utils'),
exec = require('cordova/exec'),
cordova = require('cordova');
function Sysinfo() {
this.available = false;
this.cpu = null;
this.memory = null;
var self = this;
channel.onCordovaReady.subscribe(function() {
self.getInfo(function(info) {
self.available = true;
self.cpu = info.cpu;
self.memory = info.memory;
},function(e) {
self.available = false;
//utils.alert("[ERROR] Error initializing Cordova: " + e);
});
});
}
/**
Get system info
*
@param {Function} successCallback The function to call when the heading data is available
@param {Function} errorCallback The function to call when there is an error getting the heading data. (OPTIONAL)
*/
Sysinfo.prototype.getInfo = function(successCallback, errorCallback) {
exec(successCallback, errorCallback, "Sysinfo", "getInfo", []);
};
module.exports = new Sysinfo();
/code