blob: d10166a68d3e5f6f067bdb1d3b3b485de372b523 [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
63 mTotalSize = (TextView)findViewById(R.id.total);
64 mUsedSize = (TextView)findViewById(R.id.used);
65 mAvailableSize = (TextView)findViewById(R.id.available);
66
67 // install an intent filter to receive SD card related events.
68 IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_REMOVED);
69 intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
70 intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
71 intentFilter.addAction(Intent.ACTION_MEDIA_SHARED);
72 intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
73 intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
74 intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
75 intentFilter.addDataScheme("file");
76 registerReceiver(mReceiver, intentFilter);
77 }
78
79 @Override
80 protected void onDestroy() {
81 super.onDestroy();
82 }
83
84 @Override
85 public void onResume() {
86 super.onResume();
87 update();
88 }
89
90 private void setLayout(View layout) {
91 mRemovedLayout.setVisibility(layout == mRemovedLayout ? View.VISIBLE : View.GONE);
92 mMountedLayout.setVisibility(layout == mMountedLayout ? View.VISIBLE : View.GONE);
93 mUnmountedLayout.setVisibility(layout == mUnmountedLayout ? View.VISIBLE : View.GONE);
94 mScanningLayout.setVisibility(layout == mScanningLayout ? View.VISIBLE : View.GONE);
95 mSharedLayout.setVisibility(layout == mSharedLayout ? View.VISIBLE : View.GONE);
96 mBadRemovalLayout.setVisibility(layout == mBadRemovalLayout ? View.VISIBLE : View.GONE);
97 }
98
99 private void update() {
100 try {
101 mMassStorage.setChecked(mMountService.getMassStorageEnabled());
102 } catch (RemoteException ex) {
103 }
104
105 String scanVolume = null; // this no longer exists: SystemProperties.get(MediaScanner.CURRENT_VOLUME_PROPERTY, "");
106 boolean scanning = "external".equals(scanVolume);
107
108 if (scanning) {
109 setLayout(mScanningLayout);
110 } else {
111 String status = Environment.getExternalStorageState();
112 boolean readOnly = false;
113
114 if (status.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
115 status = Environment.MEDIA_MOUNTED;
116 readOnly = true;
117 }
118
119 if (status.equals(Environment.MEDIA_MOUNTED)) {
120 try {
121 File path = Environment.getExternalStorageDirectory();
122 StatFs stat = new StatFs(path.getPath());
123 long blockSize = stat.getBlockSize();
124 long totalBlocks = stat.getBlockCount();
125 long availableBlocks = stat.getAvailableBlocks();
126
127 mTotalSize.setText(formatSize(totalBlocks * blockSize));
128 mUsedSize.setText(formatSize((totalBlocks - availableBlocks) * blockSize));
129 mAvailableSize.setText(formatSize(availableBlocks * blockSize));
130 } catch (IllegalArgumentException e) {
131 // this can occur if the SD card is removed, but we haven't received the
132 // ACTION_MEDIA_REMOVED Intent yet.
133 status = Environment.MEDIA_REMOVED;
134 }
135
136 mReadOnlyStatus.setVisibility(readOnly ? View.VISIBLE : View.GONE);
137 setLayout(mMountedLayout);
138 } else if (status.equals(Environment.MEDIA_UNMOUNTED)) {
139 setLayout(mUnmountedLayout);
140 } else if (status.equals(Environment.MEDIA_REMOVED)) {
141 setLayout(mRemovedLayout);
142 } else if (status.equals(Environment.MEDIA_SHARED)) {
143 setLayout(mSharedLayout);
144 } else if (status.equals(Environment.MEDIA_BAD_REMOVAL)) {
145 setLayout(mBadRemovalLayout);
146 }
147 }
148 }
149
150 private String formatSize(long size) {
151 String suffix = null;
152
153 // add K or M suffix if size is greater than 1K or 1M
154 if (size >= 1024) {
155 suffix = "K";
156 size /= 1024;
157 if (size >= 1024) {
158 suffix = "M";
159 size /= 1024;
160 }
161 }
162
163 StringBuilder resultBuffer = new StringBuilder(Long.toString(size));
164
165 int commaOffset = resultBuffer.length() - 3;
166 while (commaOffset > 0) {
167 resultBuffer.insert(commaOffset, ',');
168 commaOffset -= 3;
169 }
170
171 if (suffix != null)
172 resultBuffer.append(suffix);
173 return resultBuffer.toString();
174 }
175
176 OnClickListener mMassStorageListener = new OnClickListener() {
177 public void onClick(View v) {
178 try {
179 mMountService.setMassStorageEnabled(mMassStorage.isChecked());
180 } catch (RemoteException ex) {
181 }
182 }
183 };
184
185 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
186 @Override
187 public void onReceive(Context context, Intent intent) {
188 update();
189 }
190 };
191
192 OnClickListener mUnmountButtonHandler = new OnClickListener() {
193 public void onClick(View v) {
194 try {
195 mMountService.unmountMedia(Environment.getExternalStorageDirectory().toString());
196 } catch (RemoteException ex) {
197 }
198 }
199 };
200
201
202 private int mStatus;
203 private IMountService mMountService;
204
205 private CheckBox mMassStorage;
206
207 private TextView mTotalSize;
208 private TextView mUsedSize;
209 private TextView mAvailableSize;
210
211 private View mRemovedLayout;
212 private View mMountedLayout;
213 private View mUnmountedLayout;
214 private View mScanningLayout;
215 private View mSharedLayout;
216 private View mBadRemovalLayout;
217 private View mReadOnlyStatus;
218}