blob: 4a838430038cf14b3ede592a93fe4063682125b3 [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;
Eric Fischer85f43572009-06-12 17:58:50 -070030import android.text.format.Formatter;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080031import android.view.View;
32import android.view.View.OnClickListener;
33import android.widget.Button;
34import android.widget.CheckBox;
35import android.widget.TextView;
36
37import java.io.File;
38
39
40public class SdCardSettings extends Activity
41{
42 @Override
43 public void onCreate(Bundle icicle) {
44 super.onCreate(icicle);
45
46 setContentView(R.layout.sdcard_settings_screen);
47
48 mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount"));
49
50 mRemovedLayout = findViewById(R.id.removed);
51 mMountedLayout = findViewById(R.id.mounted);
52 mUnmountedLayout = findViewById(R.id.unmounted);
53 mScanningLayout = findViewById(R.id.scanning);
54 mSharedLayout = findViewById(R.id.shared);
55 mBadRemovalLayout = findViewById(R.id.bad_removal);
56 mReadOnlyStatus = findViewById(R.id.read_only);
57
58 mMassStorage = (CheckBox)findViewById(R.id.mass_storage);
59 mMassStorage.setOnClickListener(mMassStorageListener);
60
61 Button unmountButton = (Button)findViewById(R.id.sdcard_unmount);
62 unmountButton.setOnClickListener(mUnmountButtonHandler);
63
64 Button formatButton = (Button)findViewById(R.id.sdcard_format);
65 formatButton.setOnClickListener(mFormatButtonHandler);
66
67 mTotalSize = (TextView)findViewById(R.id.total);
68 mUsedSize = (TextView)findViewById(R.id.used);
69 mAvailableSize = (TextView)findViewById(R.id.available);
70
71 // install an intent filter to receive SD card related events.
72 IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_REMOVED);
73 intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
74 intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
75 intentFilter.addAction(Intent.ACTION_MEDIA_SHARED);
76 intentFilter.addAction(Intent.ACTION_MEDIA_CHECKING);
77 intentFilter.addAction(Intent.ACTION_MEDIA_NOFS);
78 intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
79 intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
80 intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
81 intentFilter.addDataScheme("file");
82 registerReceiver(mReceiver, intentFilter);
83 }
84
85 @Override
86 protected void onDestroy() {
87 super.onDestroy();
88 }
89
90 @Override
91 public void onResume() {
92 super.onResume();
93 update();
94 }
95
96 private void setLayout(View layout) {
97 mRemovedLayout.setVisibility(layout == mRemovedLayout ? View.VISIBLE : View.GONE);
98 mMountedLayout.setVisibility(layout == mMountedLayout ? View.VISIBLE : View.GONE);
99 mUnmountedLayout.setVisibility(layout == mUnmountedLayout ? View.VISIBLE : View.GONE);
100 mScanningLayout.setVisibility(layout == mScanningLayout ? View.VISIBLE : View.GONE);
101 mSharedLayout.setVisibility(layout == mSharedLayout ? View.VISIBLE : View.GONE);
102 mBadRemovalLayout.setVisibility(layout == mBadRemovalLayout ? View.VISIBLE : View.GONE);
103 }
104
105 private void update() {
San Mehat4a3d7132010-01-29 06:09:06 -0800106
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800107 try {
San Mehat4a3d7132010-01-29 06:09:06 -0800108 String path = Environment.getExternalStorageDirectory().getPath();
109 mMassStorage.setChecked(
110 mMountService.getVolumeShared(
111 Environment.getExternalStorageDirectory().getPath(), "ums"));
112 } catch (Exception ex) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800113 }
114
Michael Chan87620932009-05-14 17:47:02 -0700115 String status = Environment.getExternalStorageState();
116 boolean readOnly = false;
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_READ_ONLY)) {
119 status = Environment.MEDIA_MOUNTED;
120 readOnly = true;
121 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800122
Michael Chan87620932009-05-14 17:47:02 -0700123 if (status.equals(Environment.MEDIA_MOUNTED)) {
124 try {
125 File path = Environment.getExternalStorageDirectory();
126 StatFs stat = new StatFs(path.getPath());
127 long blockSize = stat.getBlockSize();
128 long totalBlocks = stat.getBlockCount();
129 long availableBlocks = stat.getAvailableBlocks();
130
131 mTotalSize.setText(formatSize(totalBlocks * blockSize));
132 mUsedSize.setText(formatSize((totalBlocks - availableBlocks) * blockSize));
133 mAvailableSize.setText(formatSize(availableBlocks * blockSize));
134 } catch (IllegalArgumentException e) {
135 // this can occur if the SD card is removed, but we haven't received the
136 // ACTION_MEDIA_REMOVED Intent yet.
137 status = Environment.MEDIA_REMOVED;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800138 }
139
Michael Chan87620932009-05-14 17:47:02 -0700140 mReadOnlyStatus.setVisibility(readOnly ? View.VISIBLE : View.GONE);
141 setLayout(mMountedLayout);
142 } else if (status.equals(Environment.MEDIA_UNMOUNTED)) {
143 setLayout(mUnmountedLayout);
144 } else if (status.equals(Environment.MEDIA_REMOVED)) {
145 setLayout(mRemovedLayout);
146 } else if (status.equals(Environment.MEDIA_SHARED)) {
147 setLayout(mSharedLayout);
148 } else if (status.equals(Environment.MEDIA_BAD_REMOVAL)) {
149 setLayout(mBadRemovalLayout);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800150 }
151 }
152
153 private String formatSize(long size) {
Eric Fischer85f43572009-06-12 17:58:50 -0700154 return Formatter.formatFileSize(this, size);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800155 }
156
157 OnClickListener mMassStorageListener = new OnClickListener() {
158 public void onClick(View v) {
159 try {
San Mehat4a3d7132010-01-29 06:09:06 -0800160 if (mMassStorage.isChecked()) {
161 mMountService.shareVolume(
162 Environment.getExternalStorageDirectory().getPath(), "ums");
163 } else {
164 mMountService.unshareVolume(
165 Environment.getExternalStorageDirectory().getPath(), "ums");
166 }
167 } catch (Exception ex) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800168 }
169 }
170 };
171
172 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
173 @Override
174 public void onReceive(Context context, Intent intent) {
175 update();
176 }
177 };
178
179 OnClickListener mUnmountButtonHandler = new OnClickListener() {
180 public void onClick(View v) {
181 try {
San Mehat33b02022010-01-12 12:22:37 -0800182 mMountService.unmountVolume(Environment.getExternalStorageDirectory().toString());
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800183 } catch (RemoteException ex) {
184 }
185 }
186 };
187
188 OnClickListener mFormatButtonHandler = new OnClickListener() {
189 public void onClick(View v) {
190 try {
San Mehat33b02022010-01-12 12:22:37 -0800191 mMountService.formatVolume(Environment.getExternalStorageDirectory().toString());
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800192 } catch (RemoteException ex) {
193 }
194 }
195 };
196
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800197 private IMountService mMountService;
198
199 private CheckBox mMassStorage;
200
201 private TextView mTotalSize;
202 private TextView mUsedSize;
203 private TextView mAvailableSize;
204
205 private View mRemovedLayout;
206 private View mMountedLayout;
207 private View mUnmountedLayout;
208 private View mScanningLayout;
209 private View mSharedLayout;
210 private View mBadRemovalLayout;
211 private View mReadOnlyStatus;
212}