zzzrrraamm

main

package com.example.grain.systemstatus;

import java.text.NumberFormat;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;

import android.os.Build;
import android.os.Bundle;
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();
    }





    private void recalculateTimerSchedule() {
        recalculateTimer = new Timer();
        recalculateTimer.schedule(
                new TimerTask() {

                    @Override
                    public void run() {
                        recalculateTimerMethod();
                    }
                },
                0, 5000);
    }

    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()
        );*/
    }

  }

Zram

package com.example.grain.systemstatus;
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();
    }


    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;
    }


    public int getDiskSize() throws Exception {
        if (_diskSize == 0) {
            _diskSize = _getDiskSize();
        }
        return _diskSize;
    }

    private int _getDiskSize() throws Exception {

        return Integer.parseInt(readFileAsString(ZRAMSTATFILE_DISKSIZE));
    }

    public int getCompressedDataSize() throws Exception {
        if (_compressedDataSize == 0) {
            _compressedDataSize = _getCompressedDataSize();
        }
        return _compressedDataSize;
    }

    private int _getCompressedDataSize() throws Exception {
        return Integer.parseInt(readFileAsString(ZRAMSTATFILE_COMPRESSED_DATA_SIZE));
    }

    public int getOriginalDataSize() throws Exception {
        if (_originalDataSize == 0) {
            _originalDataSize = _getOriginalDataSize();
        }
        return _originalDataSize;
    }

    private int _getOriginalDataSize() throws Exception {
        return Integer.parseInt(readFileAsString(ZRAMSTATFILE_ORIGINAL_DATA_SIZE));
    }


    public int getMemUsedTotal() throws Exception {
        if (_memUsedTotal == 0) {
            _memUsedTotal = _getMemUsedTotal();
        }
        return _memUsedTotal;
    }

    private int _getMemUsedTotal() throws Exception {
        return Integer.parseInt(readFileAsString(ZRAMSTATFILE_MEM_USED_TOTAL));
    }
    //---压缩比例---------(压缩数据比上原始数据)-------------使用比例----- --------(原始数据比上磁盘大小)---------------------------------
    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;
        }

    }
}

Acticity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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="30dp"
        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="30dp"
        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>

string.xml

<resources>
    <string name="app_name">SystemStatus</string>

    <string name="zram_disk_size">ZRAM Disk Size:</string>
    <string name="zram_compressed_data_size">ZRAM Compressed Data Size:</string>
    <string name="zram_original_data_size">ZRAM Original Data Size:</string>
    <string name="zram_mem_used_total">ZRAM Total Memory Used:</string>

    <string name="zram_compression_ratio">ZRAM Compression Ratio:</string>
    <string name="zram_used_ratio">ZRAM Used Ratio:</string>
</resources>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值