blob: b6935a2fe4a2ad836e807b01c1847f58d38389e8 [file] [log] [blame]
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
19import android.app.Activity;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.os.Bundle;
25import android.os.RemoteException;
26import android.os.Environment;
27import android.os.IMountService;
28import android.os.ServiceManager;
29import android.os.StatFs;
30import android.view.View;
31import android.view.View.OnClickListener;
32import android.widget.Button;
33import android.widget.CheckBox;
34import android.widget.TextView;
35
36import java.io.File;
37
38
39public class SdCardSettings extends Activity
40{
41 @Override
42 public void onCreate(Bundle icicle) {
43 super.onCreate(icicle);
44
45 setContentView(R.layout.sdcard_settings_screen);
46
47 mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount"));
48
49 mRemovedLayout = findViewById(R.id.removed);
50 mMountedLayout = findViewById(R.id.mounted);
51 mUnmountedLayout = findViewById(R.id.unmounted);
52 mScanningLayout = findViewById(R.id.scanning);
53 mSharedLayout = findViewById(R.id.shared);
54 mBadRemovalLayout = findViewById(R.id.bad_removal);
55 mReadOnlyStatus = findViewById(R.id.read_only);
56
57 mMassStorage = (CheckBox)findViewById(R.id.mass_storage);
58 mMassStorage.setOnClickListener(mMassStorageListener);
59
60 Button unmountButton = (Button)findViewById(R.id.sdcard_unmount);
61 unmountButton.setOnClickListener(mUnmountButtonHandler);
62
The Android Open Source Project1feaa852009-02-10 15:44:05 -080063 Button formatButton = (Button)findViewById(R.id.sdcard_format);
64 formatButton.setOnClickListener(mFormatButtonHandler);
65
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070066 mTotalSize = (TextView)findViewById(R.id.total);
67 mUsedSize = (TextView)findViewById(R.id.used);
68 mAvailableSize = (TextView)findViewById(R.id.available);
69
70 // install an intent filter to receive SD card related events.
71 IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_REMOVED);
72 intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
73 intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
74 intentFilter.addAction(Intent.ACTION_MEDIA_SHARED);
The Android Open Source Project1feaa852009-02-10 15:44:05 -080075 intentFilter.addAction(Intent.ACTION_MEDIA_CHECKING);
76 intentFilter.addAction(Intent.ACTION_MEDIA_NOFS);
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070077 intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
78 intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
79 intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
80 intentFilter.addDataScheme("file");
81 registerReceiver(mReceiver, intentFilter);
82 }
83
84 @Override
85 protected void onDestroy() {
86 super.onDestroy();
87 }
88
89 @Override
90 public void onResume() {
91 super.onResume();
92 update();
93 }
94
95 private void setLayout(View layout) {
96 mRemovedLayout.setVisibility(layout == mRemovedLayout ? View.VISIBLE : View.GONE);
97 mMountedLayout.setVisibility(layout == mMountedLayout ? View.VISIBLE : View.GONE);
98 mUnmountedLayout.setVisibility(layout == mUnmountedLayout ? View.VISIBLE : View.GONE);
99 mScanningLayout.setVisibility(layout == mScanningLayout ? View.VISIBLE : View.GONE);
100 mSharedLayout.setVisibility(layout == mSharedLayout ? View.VISIBLE : View.GONE);
101 mBadRemovalLayout.setVisibility(layout == mBadRemovalLayout ? View.VISIBLE : View.GONE);
102 }
103
104 private void update() {
105 try {
106 mMassStorage.setChecked(mMountService.getMassStorageEnabled());
107 } catch (RemoteException ex) {
108 }
109
110 String scanVolume = null; // this no longer exists: SystemProperties.get(MediaScanner.CURRENT_VOLUME_PROPERTY, "");
111 boolean scanning = "external".equals(scanVolume);
112
113 if (scanning) {
114 setLayout(mScanningLayout);
115 } else {
116 String status = Environment.getExternalStorageState();
117 boolean readOnly = false;
118
119 if (status.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
120 status = Environment.MEDIA_MOUNTED;
121 readOnly = true;
122 }
123
124 if (status.equals(Environment.MEDIA_MOUNTED)) {
125 try {
126 File path = Environment.getExternalStorageDirectory();
127 StatFs stat = new StatFs(path.getPath());
128 long blockSize = stat.getBlockSize();
129 long totalBlocks = stat.getBlockCount();
130 long availableBlocks = stat.getAvailableBlocks();
131
132 mTotalSize.setText(formatSize(totalBlocks * blockSize));
133 mUsedSize.setText(formatSize((totalBlocks - availableBlocks) * blockSize));
134 mAvailableSize.setText(formatSize(availableBlocks * blockSize));
135 } catch (IllegalArgumentException e) {
136 // this can occur if the SD card is removed, but we haven't received the
137 // ACTION_MEDIA_REMOVED Intent yet.
138 status = Environment.MEDIA_REMOVED;
139 }
140
141 mReadOnlyStatus.setVisibility(readOnly ? View.VISIBLE : View.GONE);
142 setLayout(mMountedLayout);
143 } else if (status.equals(Environment.MEDIA_UNMOUNTED)) {
144 setLayout(mUnmountedLayout);
145 } else if (status.equals(Environment.MEDIA_REMOVED)) {
146 setLayout(mRemovedLayout);
147 } else if (status.equals(Environment.MEDIA_SHARED)) {
148 setLayout(mSharedLayout);
149 } else if (status.equals(Environment.MEDIA_BAD_REMOVAL)) {
150 setLayout(mBadRemovalLayout);
151 }
152 }
153 }
154
155 private String formatSize(long size) {
156 String suffix = null;
157
158 // add K or M suffix if size is greater than 1K or 1M
159 if (size >= 1024) {
160 suffix = "K";
161 size /= 1024;
162 if (size >= 1024) {
163 suffix = "M";
164 size /= 1024;
165 }
166 }
167
168 StringBuilder resultBuffer = new StringBuilder(Long.toString(size));
169
170 int commaOffset = resultBuffer.length() - 3;
171 while (commaOffset > 0) {
172 resultBuffer.insert(commaOffset, ',');
173 commaOffset -= 3;
174 }
175
176 if (suffix != null)
177 resultBuffer.append(suffix);
178 return resultBuffer.toString();
179 }
180
181 OnClickListener mMassStorageListener = new OnClickListener() {
182 public void onClick(View v) {
183 try {
184 mMountService.setMassStorageEnabled(mMassStorage.isChecked());
185 } catch (RemoteException ex) {
186 }
187 }
188 };
189
190 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
191 @Override
192 public void onReceive(Context context, Intent intent) {
193 update();
194 }
195 };
196
197 OnClickListener mUnmountButtonHandler = new OnClickListener() {
198 public void onClick(View v) {
199 try {
200 mMountService.unmountMedia(Environment.getExternalStorageDirectory().toString());
201 } catch (RemoteException ex) {
202 }
203 }
204 };
205
The Android Open Source Project1feaa852009-02-10 15:44:05 -0800206 OnClickListener mFormatButtonHandler = new OnClickListener() {
207 public void onClick(View v) {
208 try {
209 mMountService.formatMedia(Environment.getExternalStorageDirectory().toString());
210 } catch (RemoteException ex) {
211 }
212 }
213 };
214
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -0700215
216 private int mStatus;
217 private IMountService mMountService;
218
219 private CheckBox mMassStorage;
220
221 private TextView mTotalSize;
222 private TextView mUsedSize;
223 private TextView mAvailableSize;
224
225 private View mRemovedLayout;
226 private View mMountedLayout;
227 private View mUnmountedLayout;
228 private View mScanningLayout;
229 private View mSharedLayout;
230 private View mBadRemovalLayout;
231 private View mReadOnlyStatus;
232}