Java tutorial
/** * This file was auto-generated by the Titanium Module SDK helper for Android * Appcelerator Titanium Mobile * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved. * Licensed under the terms of the Apache Public License * Please see the LICENSE included with this distribution for details. * */ package be.k0suke.tistats; import org.appcelerator.kroll.KrollModule; import org.appcelerator.kroll.annotations.Kroll; import org.appcelerator.titanium.TiApplication; import org.appcelerator.kroll.common.Log; import org.appcelerator.kroll.common.TiConfig; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import android.app.Activity; import android.app.ActivityManager; import android.app.ActivityManager.RunningServiceInfo; import android.os.Bundle; import org.json.JSONException; import org.json.JSONObject; @Kroll.module(name = "TiStats", id = "be.k0suke.tistats") public class TiStatsModule extends KrollModule { // Standard Debugging variables private static final String LCAT = "TiStatsModule"; private static final boolean DBG = TiConfig.LOGD; // You can define constants with @Kroll.constant, for example: // @Kroll.constant public static final String EXTERNAL_NAME = value; public TiStatsModule() { super(); } @Kroll.onAppCreate public static void onAppCreate(TiApplication app) { Log.d(LCAT, "inside onAppCreate"); // put module init code that needs to run when the application is created // StatsTimer statsTimer = new StatsTimer(); // statsTimer.start(); } // Methods @Kroll.method public String example() { Log.d(LCAT, "example called"); return "hello world"; } // Properties @Kroll.getProperty public String getExampleProp() { Log.d(LCAT, "get example property"); return "hello world"; } @Kroll.setProperty public void setExampleProp(String value) { Log.d(LCAT, "set example property: " + value); } @Kroll.method public String stats() { Log.d(LCAT, "stats called"); TiApplication tiApp = TiApplication.getInstance(); ActivityManager activityManager = (ActivityManager) tiApp.getSystemService(Activity.ACTIVITY_SERVICE); JSONObject json = new JSONObject(); try { JSONObject memoryInfoJson = new JSONObject(); ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo(); activityManager.getMemoryInfo(memoryInfo); memoryInfoJson.put("availMem", (int) (memoryInfo.availMem)); JSONObject processMemoryInfoJson = new JSONObject(); int[] pids = new int[1]; pids[0] = android.os.Process.myPid(); android.os.Debug.MemoryInfo[] processMemoryInfo = activityManager.getProcessMemoryInfo(pids); processMemoryInfoJson.put("totalPrivateDirty", processMemoryInfo[0].getTotalPrivateDirty()); processMemoryInfoJson.put("totalSharedDirty", processMemoryInfo[0].getTotalSharedDirty()); processMemoryInfoJson.put("totalPss", processMemoryInfo[0].getTotalPss()); json.put("memoryInfo", memoryInfoJson); json.put("processMemoryInfo", processMemoryInfoJson); } catch (JSONException e) { } return json.toString(); } public static class StatsTimer { private ScheduledExecutorService scheduler; public StatsTimer() { scheduler = Executors.newScheduledThreadPool(1); } public void start() { scheduler.scheduleAtFixedRate(new Runnable() { @Override public void run() { TiApplication tiApp = TiApplication.getInstance(); if (tiApp.isDebuggerEnabled()) { ActivityManager activityManager = (ActivityManager) tiApp .getSystemService(Activity.ACTIVITY_SERVICE); ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo(); activityManager.getMemoryInfo(memoryInfo); Log.d(LCAT, "memoryInfo.availMem[MB] = " + (int) (memoryInfo.availMem / 1024 / 1024)); } } }, 0, 1000, TimeUnit.MILLISECONDS); } } }