Android Open Source - cpustats Preview Activity






From Project

Back to project page cpustats.

License

The source code is released under:

Apache License

If you think the Android project cpustats listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package jp.takke.cpustats;
//from w w w  . j ava2s  .co m
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class PreviewActivity extends Activity {
    
    final Handler mHandler = new Handler();
    
    // ?????
    private boolean mIsForeground = false;
    
    // CPU???????(???????)???????????
    private int mMinFreq = 0;
    private String mMinFreqText = "";
    private int mMaxFreq = 0;
    private String mMaxFreqText = "";

    // ??????
    private final static int REQUEST_CONFIG_ACTIVITY = 0;
    
    // ?????????(?????????????????????????????????????????)
    private boolean mServiceMaybeRunning = false;
    
    // ???????IF
    private IUsageUpdateService mServiceIf = null;
    
    // ???????IF?????
    private IUsageUpdateCallback mCallback = new IUsageUpdateCallback.Stub() {
        
        /**
         * ?????????????????????
         */
        public void updateUsage(final int[] cpuUsages, final int currentFreq) throws RemoteException {
            
            mHandler.post(new Runnable() {
                
                public void run() {
                    
                    // ????????????????????????
                    mServiceMaybeRunning = true;
                    
                    if (mIsForeground) {
                        // CPU???????
                        updateCpuUsages(cpuUsages);
                        
                        // CPU???????????
                        updateCpuFrequency(currentFreq);
                        
                        setTitle("CPU Stats  Freq: " + MyUtil.formatFreq(currentFreq) + "");
                    }
                }
            });
        }
    };

    // ??????????????
    private ServiceConnection mServiceConnection = new ServiceConnection() {
        
        public void onServiceConnected(ComponentName name, IBinder service) {
            
            // ????????????????????????
            mServiceIf = IUsageUpdateService.Stub.asInterface(service);
            
            // ?????????
            try {
                mServiceIf.registerCallback(mCallback);
            } catch (RemoteException e) {
                MyLog.e(e);
            }
        }
        
        public void onServiceDisconnected(ComponentName name) {
            mServiceIf = null;
        }
    };

    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview);
        
        MyLog.debugMode = false;
        MyLog.d("PreviewActivity.onCreate");
        
        // ????????????????
        hideAllCoreFreqInfo();
        
        // CPU ??????
        mMinFreq = MyUtil.takeMinCpuFreq();
        mMinFreqText = MyUtil.formatFreq(mMinFreq);
        
        mMaxFreq = MyUtil.takeMaxCpuFreq();
        mMaxFreqText = MyUtil.formatFreq(mMaxFreq);
        updateCpuFrequency(MyUtil.takeCurrentCpuFreq());
        
        // ?????????????????
        setActionBarLogo(R.drawable.single000);
        
        // ?????????????????
        final Intent intent = new Intent(IUsageUpdateService.class.getName());
        bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
    }

    private void setActionBarLogo(int iconId) {
        
        if (Build.VERSION.SDK_INT >= 14) {
            // getActionBar().setLogo(R.drawable.single000);
            try {
                final Object actionBar = this.getClass().getMethod("getActionBar")
                    .invoke(this);
                actionBar.getClass().getMethod("setLogo", int.class)
                    .invoke(actionBar, iconId);
            } catch (Throwable th) {
                MyLog.e(th);
            }
        }
    }

    private void hideAllCoreFreqInfo() {
        
        // Core
        final int[] imageViews = new int[]{R.id.core1Image, R.id.core2Image, R.id.core3Image, R.id.core4Image};
        final int[] titleViews = new int[]{R.id.core1Title, R.id.core2Title, R.id.core3Title, R.id.core4Title};
        final int[] textViews = new int[]{R.id.core1Text, R.id.core2Text, R.id.core3Text, R.id.core4Text};
        
        for (int i=0; i<4; i++) {
            findViewById(imageViews[i]).setVisibility(View.GONE);
            findViewById(titleViews[i]).setVisibility(View.GONE);
            findViewById(textViews[i]).setVisibility(View.GONE);
        }
        
        // Freq
        findViewById(R.id.freqImage).setVisibility(View.GONE);
        findViewById(R.id.freqText1).setVisibility(View.GONE);
        findViewById(R.id.freqText2).setVisibility(View.GONE);
        findViewById(R.id.freqText3).setVisibility(View.GONE);
        findViewById(R.id.freqTitle).setVisibility(View.GONE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        getMenuInflater().inflate(R.menu.activity_preview, menu);
        
        return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        
        // ????????????????menu??
        {
            final MenuItem item = menu.findItem(R.id.menu_start_service);
            if (item != null) {
                item.setVisible(!mServiceMaybeRunning);
            }
        }
        {
            final MenuItem item = menu.findItem(R.id.menu_stop_service);
            if (item != null) {
                item.setVisible(mServiceMaybeRunning);
            }
        }
        
        return super.onPrepareOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        
        switch (item.getItemId()) {
        case R.id.menu_start_service:
            // ??????
            if (mServiceIf != null) {
                try {
                    // ?????
                    mServiceIf.startResident();
                } catch (RemoteException e) {
                    MyLog.e(e);
                }
            }
            break;
            
        case R.id.menu_stop_service:
            // ????????
            if (mServiceIf != null) {
                try {
                    // ???????
                    mServiceIf.stopResident();
                    
                    // ??????????????????????????
                    mServiceMaybeRunning = false;
                    
                    // ??????
                    hideAllCoreFreqInfo();
                    
                    // ????????
                    setTitle("CPU Stats");
                    
                    // ???????????????????
                    setActionBarLogo(R.drawable.single000);
                    
                } catch (RemoteException e) {
                    MyLog.e(e);
                }
            }
            break;
            
        case R.id.menu_settings:
            {
                final Intent intent = new Intent(this, ConfigActivity.class);
                startActivityForResult(intent, REQUEST_CONFIG_ACTIVITY);
            }
            break;
            
        case R.id.menu_exut:
            // ??
            finish();
            break;
        }
        
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        
        switch (requestCode) {
        case REQUEST_CONFIG_ACTIVITY:
            {
                // ????
                
                // ????????????????????????????
                if (mServiceIf != null) {
                    try {
                        // ?????????
                        mServiceIf.reloadSettings();
                    } catch (RemoteException e) {
                        MyLog.e(e);
                    }
                }
            }
            break;
        }
        
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        
        MyLog.d("PreviewActivity.onDestroy");
        
        cleanupServiceConnection();
        
        super.onDestroy();
    }

    private void cleanupServiceConnection() {
        
        // ?????????
        if (mServiceIf != null) {
            try {
                mServiceIf.unregisterCallback(mCallback);
            } catch (RemoteException e) {
                MyLog.e(e);
            }
        }
        
        // ???????bind??
        unbindService(mServiceConnection);
    }
    
    @Override
    protected void onPause() {
        
        MyLog.d("onPause");
        
        mIsForeground = false;
        
        super.onPause();
    }
    
    @Override
    protected void onResume() {
        
        MyLog.d("onResume");
        
        mIsForeground = true;
        
        super.onResume();
    }
    
    /**
     * CPU ?????????????????
     * 
     * @param cpuUsages
     */
    private void updateCpuUsages(int[] cpuUsages) {
        
        MyLog.d("updateCpuUsages");
        
        // ?????????????????
        if (cpuUsages != null && cpuUsages.length >= 1) {
            final int id = ResourceUtil.getIconIdForCpuUsageSingle(cpuUsages[0]);
            setActionBarLogo(id);
        }
        
        // ??????????????
        final int[] imageViews = new int[]{R.id.core1Image, R.id.core2Image, R.id.core3Image, R.id.core4Image};
        final int[] titleViews = new int[]{R.id.core1Title, R.id.core2Title, R.id.core3Title, R.id.core4Title};
        final int[] textViews = new int[]{R.id.core1Text, R.id.core2Text, R.id.core3Text, R.id.core4Text};
        
        final int coreCount = MyUtil.calcCpuCoreCount();
        for (int i=0; i<4; i++) {
            
            if (coreCount <= i) {
                // Core??????????????????
                findViewById(imageViews[i]).setVisibility(View.GONE);
                findViewById(titleViews[i]).setVisibility(View.GONE);
                findViewById(textViews[i]).setVisibility(View.GONE);
            } else {
                int cpuUsage = 0;
                if (cpuUsages != null && cpuUsages.length > i+1) {
                    cpuUsage = cpuUsages[i+1];
                }

                final TextView textView = (TextView) findViewById(textViews[i]);
                textView.setText("CPU Usage: " + cpuUsage + "%");
                textView.setVisibility(View.VISIBLE);
                
                findViewById(titleViews[i]).setVisibility(View.VISIBLE);
                
                final int id = ResourceUtil.getIconIdForCpuUsageSingle(cpuUsage);
                final ImageView imageView = (ImageView) findViewById(imageViews[i]);
                imageView.setImageResource(id);
                imageView.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * CPU ?????????????????????
     * 
     * @param currentFreq
     */
    private void updateCpuFrequency(final int currentFreq) {
        
        MyLog.d("updateCpuFrequency");
        
        final TextView textView1 = (TextView) findViewById(R.id.freqText1);
        textView1.setText("Freq: " + MyUtil.formatFreq(currentFreq));
        textView1.setVisibility(View.VISIBLE);
        
        final TextView textView2 = (TextView) findViewById(R.id.freqText2);
        textView2.setText("Max: " + mMaxFreqText);
        textView2.setVisibility(View.VISIBLE);
        
        final TextView textView3 = (TextView) findViewById(R.id.freqText3);
        textView3.setText("Min: " + mMinFreqText);
        textView3.setVisibility(View.VISIBLE);
        
        findViewById(R.id.freqTitle).setVisibility(View.VISIBLE);
        
//      final int clockPercent = mMaxFreq >= 0 ? ((currentFreq - mMinFreq) * 100 / (mMaxFreq - mMinFreq)) : 0;
//      MyLog.i(" clock[" + currentFreq + "] => " + clockPercent + "%");
        
        final int id = ResourceUtil.getIconIdForCpuFreq(currentFreq);
        final ImageView imageView = (ImageView) findViewById(R.id.freqImage);
        imageView.setImageResource(id);
        imageView.setVisibility(View.VISIBLE);
    }
    
}




Java Source Code List

jp.takke.cpustats.BootReceiver.java
jp.takke.cpustats.C.java
jp.takke.cpustats.ConfigActivity.java
jp.takke.cpustats.MyAsyncTask.java
jp.takke.cpustats.MyLog.java
jp.takke.cpustats.MyUtil.java
jp.takke.cpustats.PreviewActivity.java
jp.takke.cpustats.QuadResourceUtil.java
jp.takke.cpustats.QuadResourceUtil.java
jp.takke.cpustats.ResourceUtil.java
jp.takke.cpustats.UsageUpdateService.java