FileDocCategorySizeDatePackage
SdCardSettings.javaAPI DocAndroid 1.5 API8544Wed May 06 22:42:48 BST 2009com.android.settings

SdCardSettings

public class SdCardSettings extends android.app.Activity

Fields Summary
android.view.View.OnClickListener
mMassStorageListener
private final android.content.BroadcastReceiver
mReceiver
android.view.View.OnClickListener
mUnmountButtonHandler
android.view.View.OnClickListener
mFormatButtonHandler
private int
mStatus
private android.os.IMountService
mMountService
private android.widget.CheckBox
mMassStorage
private android.widget.TextView
mTotalSize
private android.widget.TextView
mUsedSize
private android.widget.TextView
mAvailableSize
private android.view.View
mRemovedLayout
private android.view.View
mMountedLayout
private android.view.View
mUnmountedLayout
private android.view.View
mScanningLayout
private android.view.View
mSharedLayout
private android.view.View
mBadRemovalLayout
private android.view.View
mReadOnlyStatus
Constructors Summary
Methods Summary
private java.lang.StringformatSize(long size)

        String suffix = null;

        // add K or M suffix if size is greater than 1K or 1M
        if (size >= 1024) {
            suffix = "K";
            size /= 1024;
            if (size >= 1024) {
                suffix = "M";
                size /= 1024;
            }
        }

        StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

        int commaOffset = resultBuffer.length() - 3;
        while (commaOffset > 0) {
            resultBuffer.insert(commaOffset, ',");
            commaOffset -= 3;
        }

        if (suffix != null)
            resultBuffer.append(suffix);
        return resultBuffer.toString();
    
public voidonCreate(android.os.Bundle icicle)

        super.onCreate(icicle);

        setContentView(R.layout.sdcard_settings_screen);

        mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount"));

        mRemovedLayout = findViewById(R.id.removed);
        mMountedLayout = findViewById(R.id.mounted);
        mUnmountedLayout = findViewById(R.id.unmounted);
        mScanningLayout = findViewById(R.id.scanning);
        mSharedLayout = findViewById(R.id.shared);
        mBadRemovalLayout = findViewById(R.id.bad_removal);
        mReadOnlyStatus = findViewById(R.id.read_only);

        mMassStorage = (CheckBox)findViewById(R.id.mass_storage);
        mMassStorage.setOnClickListener(mMassStorageListener);

        Button unmountButton = (Button)findViewById(R.id.sdcard_unmount);
        unmountButton.setOnClickListener(mUnmountButtonHandler);

        Button formatButton = (Button)findViewById(R.id.sdcard_format);
        formatButton.setOnClickListener(mFormatButtonHandler);

        mTotalSize = (TextView)findViewById(R.id.total);
        mUsedSize = (TextView)findViewById(R.id.used);
        mAvailableSize = (TextView)findViewById(R.id.available);

        // install an intent filter to receive SD card related events.
        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_CHECKING);
        intentFilter.addAction(Intent.ACTION_MEDIA_NOFS);
        intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
        intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
        intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
        intentFilter.addDataScheme("file");
        registerReceiver(mReceiver, intentFilter);
    
protected voidonDestroy()

        super.onDestroy();
    
public voidonResume()

        super.onResume();
        update();
    
private voidsetLayout(android.view.View layout)

        mRemovedLayout.setVisibility(layout == mRemovedLayout ? View.VISIBLE : View.GONE);
        mMountedLayout.setVisibility(layout == mMountedLayout ? View.VISIBLE : View.GONE);
        mUnmountedLayout.setVisibility(layout == mUnmountedLayout ? View.VISIBLE : View.GONE);
        mScanningLayout.setVisibility(layout == mScanningLayout ? View.VISIBLE : View.GONE);
        mSharedLayout.setVisibility(layout == mSharedLayout ? View.VISIBLE : View.GONE);
        mBadRemovalLayout.setVisibility(layout == mBadRemovalLayout ? View.VISIBLE : View.GONE);
    
private voidupdate()

        try {
            mMassStorage.setChecked(mMountService.getMassStorageEnabled());
        } catch (RemoteException ex) {
        }

        String scanVolume = null; // this no longer exists: SystemProperties.get(MediaScanner.CURRENT_VOLUME_PROPERTY, "");
        boolean scanning = "external".equals(scanVolume);

        if (scanning) {
            setLayout(mScanningLayout);
        } else {
            String status = Environment.getExternalStorageState();
            boolean readOnly = false;

            if (status.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
                status = Environment.MEDIA_MOUNTED;
                readOnly = true;
            }

            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();

                    mTotalSize.setText(formatSize(totalBlocks * blockSize));
                    mUsedSize.setText(formatSize((totalBlocks - availableBlocks) * blockSize));
                    mAvailableSize.setText(formatSize(availableBlocks * blockSize));
                } 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;
                }

                mReadOnlyStatus.setVisibility(readOnly ? View.VISIBLE : View.GONE);
                setLayout(mMountedLayout);
            } else if (status.equals(Environment.MEDIA_UNMOUNTED)) {
                setLayout(mUnmountedLayout);
            } else if (status.equals(Environment.MEDIA_REMOVED)) {
                setLayout(mRemovedLayout);
            } else if (status.equals(Environment.MEDIA_SHARED)) {
                setLayout(mSharedLayout);
            } else if (status.equals(Environment.MEDIA_BAD_REMOVAL)) {
                setLayout(mBadRemovalLayout);
            }
        }