package com.samples.hardware.osinfo;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class OsInfoItemActivity extends Activity {
    
	private TextView text;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item);
        
        text = (TextView)findViewById(R.id.text);
        
        String action = this.getIntent().getAction();
    	this.setTitle(action);
   	
        if (action.equals(
        		getResources().getString(R.string.cpu_info))) {
        	String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
        	PrintInfo(args);
        }   	
        else if (action.equals(
        		getResources().getString(R.string.disc_space_info))) {
        	String[] args = {"/system/bin/df"};
        	PrintInfo(args);
        }
        else if (action.equals(
        		getResources().getString(R.string.memory_info))) {
        	String[] args = {"/system/bin/cat", "/proc/meminfo"};
        	PrintInfo(args);
        }
        else if (action.equals(
        		getResources().getString(R.string.os_version_info))) {
        	String[] args = {"/system/bin/cat", "/proc/version"};
        	PrintInfo(args);
        }    
    }
    
    private void PrintInfo(String[] args)
    {    	
    	try{
    		ProcessBuilder builder = new ProcessBuilder(args);
    		
    		Process process = builder.start();
    		InputStream inputStream = process.getInputStream();
    		byte[] b = new byte[4096];
    		while(inputStream.read(b) != -1){
    			text.append(new String(b));
    		}
    		inputStream.close();
    	} 
    	catch(IOException e){
    		Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
    	}
    	
    }
}