blob: 75f02e77115443b4f0e0f7d27d49bc83c9e7930d [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() {
106 try {
107 mMassStorage.setChecked(mMountService.getMassStorageEnabled());
108 } catch (RemoteException ex) {
109 }
110
Michael Chan87620932009-05-14 17:47:02 -0700111 String status = Environment.getExternalStorageState();
112 boolean readOnly = false;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800113
Michael Chan87620932009-05-14 17:47:02 -0700114 if (status.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
115 status = Environment.MEDIA_MOUNTED;
116 readOnly = true;
117 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800118
Michael Chan87620932009-05-14 17:47:02 -0700119 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;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800134 }
135
Michael Chan87620932009-05-14 17:47:02 -0700136 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);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800146 }
147 }
148
149 private String formatSize(long size) {
Eric Fischer85f43572009-06-12 17:58:50 -0700150 return Formatter.formatFileSize(this, size);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800151 }
152
153 OnClickListener mMassStorageListener = new OnClickListener() {
154 public void onClick(View v) {
155 try {
156 mMountService.setMassStorageEnabled(mMassStorage.isChecked());
157 } catch (RemoteException ex) {
158 }
159 }
160 };
161
162 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
163 @Override
164 public void onReceive(Context context, Intent intent) {
165 update();
166 }
167 };
168
169 OnClickListener mUnmountButtonHandler = new OnClickListener() {
170 public void onClick(View v) {
171 try {
San Mehat33b02022010-01-12 12:22:37 -0800172 mMountService.unmountVolume(Environment.getExternalStorageDirectory().toString());
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800173 } catch (RemoteException ex) {
174 }
175 }
176 };
177
178 OnClickListener mFormatButtonHandler = new OnClickListener() {
179 public void onClick(View v) {
180 try {
San Mehat33b02022010-01-12 12:22:37 -0800181 mMountService.formatVolume(Environment.getExternalStorageDirectory().toString());
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800182 } catch (RemoteException ex) {
183 }
184 }
185 };
186
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800187 private IMountService mMountService;
188
189 private CheckBox mMassStorage;
190
191 private TextView mTotalSize;
192 private TextView mUsedSize;
193 private TextView mAvailableSize;
194
195 private View mRemovedLayout;
196 private View mMountedLayout;
197 private View mUnmountedLayout;
198 private View mScanningLayout;
199 private View mSharedLayout;
200 private View mBadRemovalLayout;
201 private View mReadOnlyStatus;
202}