blob: 6033c429660eb8e4f9f463eae70f3127851aed8f [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/*
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
63 Button formatButton = (Button)findViewById(R.id.sdcard_format);
64 formatButton.setOnClickListener(mFormatButtonHandler);
65
66 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);
75 intentFilter.addAction(Intent.ACTION_MEDIA_CHECKING);
76 intentFilter.addAction(Intent.ACTION_MEDIA_NOFS);
77 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
Michael Chan87620932009-05-14 17:47:02 -0700110 String status = Environment.getExternalStorageState();
111 boolean readOnly = false;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800112
Michael Chan87620932009-05-14 17:47:02 -0700113 if (status.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
114 status = Environment.MEDIA_MOUNTED;
115 readOnly = true;
116 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800117
Michael Chan87620932009-05-14 17:47:02 -0700118 if (status.equals(Environment.MEDIA_MOUNTED)) {
119 try {
120 File path = Environment.getExternalStorageDirectory();
121 StatFs stat = new StatFs(path.getPath());
122 long blockSize = stat.getBlockSize();
123 long totalBlocks = stat.getBlockCount();
124 long availableBlocks = stat.getAvailableBlocks();
125
126 mTotalSize.setText(formatSize(totalBlocks * blockSize));
127 mUsedSize.setText(formatSize((totalBlocks - availableBlocks) * blockSize));
128 mAvailableSize.setText(formatSize(availableBlocks * blockSize));
129 } catch (IllegalArgumentException e) {
130 // this can occur if the SD card is removed, but we haven't received the
131 // ACTION_MEDIA_REMOVED Intent yet.
132 status = Environment.MEDIA_REMOVED;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800133 }
134
Michael Chan87620932009-05-14 17:47:02 -0700135 mReadOnlyStatus.setVisibility(readOnly ? View.VISIBLE : View.GONE);
136 setLayout(mMountedLayout);
137 } else if (status.equals(Environment.MEDIA_UNMOUNTED)) {
138 setLayout(mUnmountedLayout);
139 } else if (status.equals(Environment.MEDIA_REMOVED)) {
140 setLayout(mRemovedLayout);
141 } else if (status.equals(Environment.MEDIA_SHARED)) {
142 setLayout(mSharedLayout);
143 } else if (status.equals(Environment.MEDIA_BAD_REMOVAL)) {
144 setLayout(mBadRemovalLayout);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800145 }
146 }
147
148 private String formatSize(long size) {
149 String suffix = null;
150
151 // add K or M suffix if size is greater than 1K or 1M
152 if (size >= 1024) {
153 suffix = "K";
154 size /= 1024;
155 if (size >= 1024) {
156 suffix = "M";
157 size /= 1024;
158 }
159 }
160
161 StringBuilder resultBuffer = new StringBuilder(Long.toString(size));
162
163 int commaOffset = resultBuffer.length() - 3;
164 while (commaOffset > 0) {
165 resultBuffer.insert(commaOffset, ',');
166 commaOffset -= 3;
167 }
168
169 if (suffix != null)
170 resultBuffer.append(suffix);
171 return resultBuffer.toString();
172 }
173
174 OnClickListener mMassStorageListener = new OnClickListener() {
175 public void onClick(View v) {
176 try {
177 mMountService.setMassStorageEnabled(mMassStorage.isChecked());
178 } catch (RemoteException ex) {
179 }
180 }
181 };
182
183 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
184 @Override
185 public void onReceive(Context context, Intent intent) {
186 update();
187 }
188 };
189
190 OnClickListener mUnmountButtonHandler = new OnClickListener() {
191 public void onClick(View v) {
192 try {
193 mMountService.unmountMedia(Environment.getExternalStorageDirectory().toString());
194 } catch (RemoteException ex) {
195 }
196 }
197 };
198
199 OnClickListener mFormatButtonHandler = new OnClickListener() {
200 public void onClick(View v) {
201 try {
202 mMountService.formatMedia(Environment.getExternalStorageDirectory().toString());
203 } catch (RemoteException ex) {
204 }
205 }
206 };
207
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800208 private IMountService mMountService;
209
210 private CheckBox mMassStorage;
211
212 private TextView mTotalSize;
213 private TextView mUsedSize;
214 private TextView mAvailableSize;
215
216 private View mRemovedLayout;
217 private View mMountedLayout;
218 private View mUnmountedLayout;
219 private View mScanningLayout;
220 private View mSharedLayout;
221 private View mBadRemovalLayout;
222 private View mReadOnlyStatus;
223}