import java.io.BufferedReader;
import java.io.FileReader;
import android.os.Build;
public class Zram {
private final String ZRAMSTATFILE_DISKSIZE = "/sys/block/zram0/disksize";
private final String ZRAMSTATFILE_COMPRESSED_DATA_SIZE = "/sys/block/zram0/compr_data_size";
private final String ZRAMSTATFILE_ORIGINAL_DATA_SIZE = "/sys/block/zram0/orig_data_size";
private final String ZRAMSTATFILE_MEM_USED_TOTAL = "/sys/block/zram0/mem_used_total";
private int _diskSize = 0;
private int _compressedDataSize = 0;
private int _originalDataSize = 0;
private int _memUsedTotal = 0;
/**
* Zram constructor
*/
public Zram() {
super();
clearCache();
}
/**
* Read a file an return content of that file as a string
*
* Copied from http://stackoverflow.com/a/4867192
*
* @param filePath path of file
* @return file content as a string
* @throws java.io.IOException
*/
private static String readFileAsString(String filePath) throws java.io.IOException {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line, results = "";
while( ( line = reader.readLine() ) != null)
{
results += line;
}
reader.close();
return results;
}
public void clearCache() {
_compressedDataSize = 0;
_diskSize = 0;
_memUsedTotal = 0;
_originalDataSize = 0;
}
/**
* Get ZRAM disk size
* @return ZRAM disk size
* @throws Exception
*/
public int getDiskSize() throws Exception {
if (_diskSize == 0) {
_diskSize = _getDiskSize();
}
return _diskSize;
}
private int _getDiskSize() throws Exception {
String sizeInString = readFileAsString(ZRAMSTATFILE_DISKSIZE);
return Integer.parseInt(sizeInString);
}
public int getCompressedDataSize() throws Exception {
if (_compressedDataSize == 0) {
_compressedDataSize = _getCompressedDataSize();
}
return _compressedDataSize;
}
private int _getCompressedDataSize() throws Exception {
String sizeInString = readFileAsString(ZRAMSTATFILE_COMPRESSED_DATA_SIZE);
return Integer.parseInt(sizeInString);
}
public int getOriginalDataSize() throws Exception {
if (_originalDataSize == 0) {
_originalDataSize = _getOriginalDataSize();
}
return _originalDataSize;
}
private int _getOriginalDataSize() throws Exception {
String sizeInString = readFileAsString(ZRAMSTATFILE_ORIGINAL_DATA_SIZE);
return Integer.parseInt(sizeInString);
}
public int getMemUsedTotal() throws Exception {
if (_memUsedTotal == 0) {
_memUsedTotal = _getMemUsedTotal();
}
return _memUsedTotal;
}
private int _getMemUsedTotal() throws Exception {
String sizeInString = readFileAsString(ZRAMSTATFILE_MEM_USED_TOTAL);
return Integer.parseInt(sizeInString);
}
public float getCompressionRatio() throws Exception {
return (float) getCompressedDataSize() / (float) getOriginalDataSize();
}
public float getUsedRatio() throws Exception {
return (float) getOriginalDataSize() / (float) getDiskSize();
}
public String getKernelVersionFromSystemProperty() {
return System.getProperty("os.version");
}
public String getKernelVersion() {
return getKernelVersionFromSystemProperty();
}
public String getDeviceName() {
String manufacturer = Build.MANUFACTURER;
String model = Build.MODEL;
if (model.startsWith(manufacturer)) {
return model;
} else {
return manufacturer + " " + model;
}
}
}
---------------------------------------
package id.co.ptskp.android.zs;
import java.text.NumberFormat;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Zram zram;
private Timer recalculateTimer;
private final int recalculateTimerInterval = 5 * 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
zram = new Zram();
updateDeviceInfo();
recalculateTimerSchedule();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_reload:
recalculate();
return true;
case R.id.action_about:
Intent aboutIntent = new Intent(this, About.class);
startActivity(aboutIntent);
return true;
case R.id.action_share:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(
Intent.EXTRA_TEXT,
"Check out \""
+ getString(R.string.app_name)
+ "\" - https://play.google.com/store/apps/details?id=id.co.ptskp.android.zs"
);
shareIntent.setType("text/plain");
startActivity(shareIntent);
return true;
case R.id.action_exit:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void recalculateTimerSchedule() {
recalculateTimer = new Timer();
recalculateTimer.schedule(
new TimerTask() {
@Override
public void run() {
recalculateTimerMethod();
}
},
0, recalculateTimerInterval);
}
private void recalculateTimerMethod() {
runOnUiThread(RecalculateTimerTick);
}
private Runnable RecalculateTimerTick = new Runnable() {
@Override
public void run() {
recalculate();
}
};
private void recalculate() {
zram.clearCache();
try {
NumberFormat nf = NumberFormat.getNumberInstance();
TextView tvZramDiskSize = (TextView) findViewById(R.id.zram_disk_size);
tvZramDiskSize.setText(
getResources().getString(R.string.zram_disk_size)
+ " "
+ nf.format(zram.getDiskSize())
+ " bytes"
);
TextView tvZramCompressedDataSize = (TextView) findViewById(R.id.zram_compressed_data_size);
tvZramCompressedDataSize.setText(
getResources().getString(R.string.zram_compressed_data_size)
+ " "
+ nf.format(zram.getCompressedDataSize())
+ " bytes"
);
TextView tvZramOriginalDataSize = (TextView) findViewById(R.id.zram_original_data_size);
tvZramOriginalDataSize.setText(
getResources().getString(R.string.zram_original_data_size)
+ " "
+ nf.format(zram.getOriginalDataSize())
+ " bytes"
);
TextView tvZramMemUsedTotal = (TextView) findViewById(R.id.zram_mem_used_total);
tvZramMemUsedTotal.setText(
getResources().getString(R.string.zram_mem_used_total)
+ " "
+ nf.format(zram.getMemUsedTotal())
+ " bytes"
);
final int compressionRatio = Math.round(zram.getCompressionRatio() * 100);
ProgressBar pbZramCompressionRatio = (ProgressBar) findViewById(R.id.zram_compression_ratio_bar);
pbZramCompressionRatio.setProgress(compressionRatio);
TextView tvZramCompressionRatio = (TextView) findViewById(R.id.zram_compression_ratio);
tvZramCompressionRatio.setText(
getResources().getString(R.string.zram_compression_ratio)
+ " "
+ Integer.toString(compressionRatio)
+ "%"
);
final int usedRatio = Math.round(zram.getUsedRatio() * 100);
ProgressBar pbZramUsedRatio = (ProgressBar) findViewById(R.id.zram_used_ratio_bar);
pbZramUsedRatio.setProgress(usedRatio);
TextView tvZramUsedRatio = (TextView) findViewById(R.id.zram_used_ratio);
tvZramUsedRatio.setText(
getResources().getString(R.string.zram_used_ratio)
+ " "
+ Integer.toString(usedRatio)
+ "%"
);
MemoryInfo mi = new MemoryInfo();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
//long memoryTotal = mi.totalMem;
long memoryAvail = mi.availMem;
//long memoryUsed = memoryTotal - memoryAvail;
TextView tvRamAvailable = (TextView) findViewById(R.id.ram_available);
tvRamAvailable.setText(
getString(R.string.ram_available)
+ " " + nf.format(memoryAvail)
+ " bytes"
);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
finish();
}
}
private void updateDeviceInfo() {
TextView tvDeviceInfo = (TextView) findViewById(R.id.device_info);
tvDeviceInfo.setText(
zram.getDeviceName()
+ " - " + Build.DISPLAY
);
TextView tvKernelVersion = (TextView) findViewById(R.id.kernel_version);
tvKernelVersion.setText(
getString(R.string.text_kernel)
+ " " + zram.getKernelVersion()
);
}
}
-----------------------------------------
package id.co.ptskp.android.zs;
import java.util.Locale;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
public class About extends Activity {
@SuppressLint("DefaultLocale") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
TextView appVersion = (TextView) findViewById(R.id.app_version);
PackageManager manager = this.getPackageManager();
try {
PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
int versionCode = info.versionCode;
String versionName = info.versionName;
appVersion.setText(
getString(R.string.app_name)
+ " " + getString(R.string.text_version).toLowerCase(Locale.getDefault())
+ " " + versionName
+ "-" + Integer.toString(versionCode)
);
} catch (NameNotFoundException e) {
}
TextView appLicense = (TextView) findViewById(R.id.app_license);
appLicense.setText(Html.fromHtml(getString(R.string.app_license)));
}
}
----------------------------
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/zram_disk_size" android:text="@string/zram_disk_size"/><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/zram_compressed_data_size" android:text="@string/zram_compressed_data_size"/><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/zram_original_data_size" android:text="@string/zram_original_data_size"/><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/zram_mem_used_total" android:text="@string/zram_mem_used_total"/><ProgressBar android:id="@+id/zram_compression_ratio_bar" android:paddingTop="@dimen/activity_vertical_margin" android:layout_width="fill_parent" android:layout_height="@dimen/progressbar_height" android:progress="0" style="@android:style/Widget.ProgressBar.Horizontal"/><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/zram_compression_ratio" android:text="@string/zram_compression_ratio"/><ProgressBar android:id="@+id/zram_used_ratio_bar" android:paddingTop="@dimen/activity_vertical_margin" android:layout_width="fill_parent" android:layout_height="@dimen/progressbar_height" android:progress="0" style="@android:style/Widget.ProgressBar.Horizontal"/><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/zram_used_ratio" android:text="@string/zram_used_ratio"/><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="@dimen/activity_vertical_margin" android:id="@+id/device_info" android:textIsSelectable="true"/><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/kernel_version" android:textIsSelectable="true"/><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ram_available" android:textIsSelectable="true"/></LinearLayout>
<ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content"><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"><TextView android:id="@+id/app_version" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textIsSelectable="true"/><TextView android:id="@+id/app_description" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_description" android:autoLink="web" android:linksClickable="true" android:textIsSelectable="true"/><TextView android:id="@+id/app_license" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="16dp" android:textIsSelectable="true"/></LinearLayout></ScrollView>