summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/SdCardSettings.java
blob: 67f3550c06ea0e27ff89e803dee73fe836d10be5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.Environment;
import android.os.IMountService;
import android.os.ServiceManager;
import android.os.StatFs;
import android.text.format.Formatter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import java.io.File;


public class SdCardSettings extends Activity
{
    @Override
    public void onCreate(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);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        update();
    }

    private void setLayout(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 void update() {
        try {
            mMassStorage.setChecked(mMountService.getMassStorageEnabled());
        } catch (RemoteException ex) {
        }

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

    private String formatSize(long size) {
        return Formatter.formatFileSize(this, size);
    }

    OnClickListener mMassStorageListener = new OnClickListener() {
        public void onClick(View v) {
            try {
                mMountService.setMassStorageEnabled(mMassStorage.isChecked());
            } catch (RemoteException ex) {
            }
        }
    };

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            update();
        }
    };

    OnClickListener mUnmountButtonHandler = new OnClickListener() {
        public void onClick(View v) {
            try {
                mMountService.unmountMedia(Environment.getExternalStorageDirectory().toString());
            } catch (RemoteException ex) {
            }
        }
    };

    OnClickListener mFormatButtonHandler = new OnClickListener() {
        public void onClick(View v) {
            try {
                mMountService.formatMedia(Environment.getExternalStorageDirectory().toString());
            } catch (RemoteException ex) {
            }
        }
    };

    private IMountService   mMountService;

    private CheckBox    mMassStorage;

    private TextView    mTotalSize;
    private TextView    mUsedSize;
    private TextView    mAvailableSize;

    private View        mRemovedLayout;
    private View        mMountedLayout;
    private View        mUnmountedLayout;
    private View        mScanningLayout;
    private View        mSharedLayout;
    private View        mBadRemovalLayout;
    private View        mReadOnlyStatus;
}