FileDocCategorySizeDatePackage
Memory.javaAPI DocAndroid 1.5 API7540Wed May 06 22:42:48 BST 2009com.android.settings.deviceinfo

Memory

public class Memory extends android.preference.PreferenceActivity

Fields Summary
private static final String
TAG
private static final String
MEMORY_SD_SIZE
private static final String
MEMORY_SD_AVAIL
private static final String
MEMORY_SD_UNMOUNT
private static final String
MEMORY_SD_FORMAT
private android.content.res.Resources
mRes
private android.preference.Preference
mSdSize
private android.preference.Preference
mSdAvail
private android.preference.Preference
mSdUnmount
private android.preference.Preference
mSdFormat
private android.os.IMountService
mMountService
private final android.content.BroadcastReceiver
mReceiver
Constructors Summary
Methods Summary
private java.lang.StringformatSize(long size)

        String suffix = null;
        
        // add KB or MB suffix if size is greater than 1K or 1M
        if (size >= 1024) {
            suffix = " KB";
            size /= 1024;
            if (size >= 1024) {
                suffix = " MB";
                size /= 1024;
            }
        }
        
        DecimalFormat formatter = new DecimalFormat();
        formatter.setGroupingSize(3);
        String result = formatter.format(size);
                
        if (suffix != null)
            result = result + suffix;
        return result;
    
private synchronized android.os.IMountServicegetMountService()

       if (mMountService == null) {
           IBinder service = ServiceManager.getService("mount");
           if (service != null) {
               mMountService = IMountService.Stub.asInterface(service);
           } else {
               Log.e(TAG, "Can't get mount service");
           }
       }
       return mMountService;
    
protected voidonCreate(android.os.Bundle icicle)


    
        
        super.onCreate(icicle);
        
        addPreferencesFromResource(R.xml.device_info_memory);
        
        mRes = getResources();
        mSdSize = findPreference(MEMORY_SD_SIZE);
        mSdAvail = findPreference(MEMORY_SD_AVAIL);
        mSdUnmount = findPreference(MEMORY_SD_UNMOUNT);
        mSdFormat = findPreference(MEMORY_SD_FORMAT);
    
protected voidonPause()

        super.onPause();
        unregisterReceiver(mReceiver);
    
public booleanonPreferenceTreeClick(android.preference.PreferenceScreen preferenceScreen, android.preference.Preference preference)

        if (preference == mSdUnmount) {
            unmount();
            return true;
        } else if (preference == mSdFormat) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setClass(this, com.android.settings.MediaFormat.class);
            startActivity(intent);
            return true;
        }
        
        return false;
    
protected voidonResume()

        super.onResume();
        
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_REMOVED);
        intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
        intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
        intentFilter.addAction(Intent.ACTION_MEDIA_SHARED);
        intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
        intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTABLE);
        intentFilter.addAction(Intent.ACTION_MEDIA_NOFS);
        intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
        intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
        intentFilter.addDataScheme("file");
        registerReceiver(mReceiver, intentFilter);

        updateMemoryStatus();
    
private voidunmount()


       
        IMountService mountService = getMountService();
        try {
            if (mountService != null) {
                mountService.unmountMedia(Environment.getExternalStorageDirectory().toString());
            } else {
                Log.e(TAG, "Mount service is null, can't unmount");
            }
        } catch (RemoteException ex) {
            // Failed for some reason, try to update UI to actual state
            updateMemoryStatus();
        }
    
private voidupdateMemoryStatus()

        String status = Environment.getExternalStorageState();
        String readOnly = "";
        if (status.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
            status = Environment.MEDIA_MOUNTED;
            readOnly = mRes.getString(R.string.read_only);
        }
 
        mSdFormat.setEnabled(false);

        if (status.equals(Environment.MEDIA_MOUNTED)) {
            try {
                File path = Environment.getExternalStorageDirectory();
                StatFs stat = new StatFs(path.getPath());
                long blockSize = stat.getBlockSize();
                long totalBlocks = stat.getBlockCount();
                long availableBlocks = stat.getAvailableBlocks();
                
                mSdSize.setSummary(formatSize(totalBlocks * blockSize));
                mSdAvail.setSummary(formatSize(availableBlocks * blockSize) + readOnly);
                mSdUnmount.setEnabled(true);
            } catch (IllegalArgumentException e) {
                // this can occur if the SD card is removed, but we haven't received the 
                // ACTION_MEDIA_REMOVED Intent yet.
                status = Environment.MEDIA_REMOVED;
            }
            
        } else {
            mSdSize.setSummary(mRes.getString(R.string.sd_unavailable));
            mSdAvail.setSummary(mRes.getString(R.string.sd_unavailable));
            mSdUnmount.setEnabled(false);

            if (status.equals(Environment.MEDIA_UNMOUNTED) ||
                status.equals(Environment.MEDIA_NOFS) ||
                status.equals(Environment.MEDIA_UNMOUNTABLE) ) {
                mSdFormat.setEnabled(true);
            }

            
        }

        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        findPreference("memory_internal_avail").setSummary(formatSize(availableBlocks * blockSize));