package com.samples.os.activitymanager;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.ProcessErrorStateInfo;
import android.app.ActivityManager.RecentTaskInfo;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.pm.ConfigurationInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.TextView;

public class SystemInfoItemActivity 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);
    	
        ActivityManager manager = 
        		(ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
        
        if (action.equals(
        		getResources().getString(R.string.itemDeviceConfigInfo))) {
        	ConfigurationInfo config = manager.getDeviceConfigurationInfo();
        	text.append("GlEs Version:\t" + config.getGlEsVersion());
        	
        	int keyType = config.reqKeyboardType;
        	text.append("\nKeyboard Type:\t");
        	switch (keyType) {
        	case Configuration.KEYBOARD_UNDEFINED:
        		text.append("Undefined");
        		break;
        	case Configuration.KEYBOARD_NOKEYS:
        		text.append("No keys");
        		break;
        	case Configuration.KEYBOARD_QWERTY:
        		text.append("QWERTY");
        		break;
        	case Configuration.KEYBOARD_12KEY:
        		text.append("12 Key");
        		break;
        	}
        	
        	int nav = config.reqNavigation;
        	text.append("\nNavigation:\t");
        	switch (nav) {
        	case Configuration.NAVIGATION_DPAD:
        		text.append("D pad");
        		break;
        	case Configuration.NAVIGATION_TRACKBALL:
        		text.append("Trackball");
        		break;
        	case Configuration.NAVIGATION_WHEEL:
        		text.append("Wheel");
        		break;
        	case Configuration.NAVIGATION_UNDEFINED:
        		text.append("Undefined");
        		break;
       		
        	}
        	
        	int touch = config.reqTouchScreen;
        	text.append("\nTouch Screen:\t");
        	switch (touch) {
        	case Configuration.TOUCHSCREEN_FINGER:
        		text.append("Finger");
        		break;
        	case Configuration.TOUCHSCREEN_NOTOUCH:
        		text.append("Notouch");
        		break;
        	case Configuration.TOUCHSCREEN_STYLUS:
        		text.append("stylus");
        		break;
        	case Configuration.TOUCHSCREEN_UNDEFINED:
        		text.append("Undefined");
        		break;
        	}
        }
        else if (action.equals(
        		getResources().getString(R.string.itemMemoryInfo))) {
            ActivityManager.MemoryInfo memInfo = 
            	new ActivityManager.MemoryInfo();  	
            	
            manager.getMemoryInfo(memInfo);
            text.append("Available Memory:\t" + memInfo.availMem + " B");	
            text.append("\nTreshold Memory:\t" + memInfo.threshold + " B");  
        }
        else if (action.equals(
        		getResources().getString(R.string.itemRunningAppProcesses))) {
        	List<RunningAppProcessInfo> runningAppProcesses = 
        		manager.getRunningAppProcesses();
        	text.append("Running Processes:");
        	
        	for (int i = 0; i < runningAppProcesses.size(); i++) {
        		text.append("\n\t" + runningAppProcesses.get(i).processName);
        	} 
        }
        else if (action.equals(
        		getResources().getString(R.string.itemRunningServices))) {
        	List<RunningServiceInfo> runningServices = 
        		manager.getRunningServices(20);
        	text.append("Running Services:");
        	
        	for (int i = 0; i < runningServices.size(); i++) {
        		text.append("\n\t" + runningServices.get(i).process);
        	}
        }
        else if (action.equals(
        		getResources().getString(R.string.itemRunningTasks))) {
        	List<RunningTaskInfo> runTasks = manager.getRunningTasks(10);
        	text.append("Running Tasks:");
        		
        	for (int i = 0; i < runTasks.size(); i++) {
        		RunningTaskInfo runTask = runTasks.get(i);	
        		text.append("\nTask ID:\t" + runTask.id);
        		text.append("\n\tBase Activity:\n\t\t" + 
        				runTask.baseActivity.getShortClassName());
        		text.append("\n\tNum Activities: " + runTask.numActivities);
        		text.append("\n\tNum Running: " + runTask.numRunning);
        	}
        }
        else if (action.equals(
        		getResources().getString(R.string.itemRecentTasks))) {
        	List<RecentTaskInfo> recTasks = manager.getRecentTasks(10, 0);
        	text.append("Recent Tasks:");
        	
        	for (int i = 0; i < recTasks.size(); i++) {
        		RecentTaskInfo recTask = recTasks.get(i);
        		text.append("\n\tTask ID:\t" + recTask.id);
        		text.append("\n\tBase Intent:\t" + 
        				recTask.baseIntent.getAction());
        		if (recTask.origActivity != null) {
            		text.append("Orig Activity:\t" + 
            			recTask.origActivity.getShortClassName());
        		}  			
        	}
        }
        else if (action.equals(
        		getResources().getString(R.string.itemProcessesInErrorState))) {
        	List<ProcessErrorStateInfo> procError = 
        			manager.getProcessesInErrorState();
        	text.append("ProcessesInErrorState:");
        	
        	if (procError != null)
        	{
	        	for (int i = 0; i < procError.size(); i++) {
	        		text.append("\n\t" + procError.get(i).processName);
	        	}
        	}
        	else {
        		text.append("\tNone");
        	}
        }       
    }
}