blob: 87e8fb3608007e63790924737b24ee1515568de9 [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;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080027import android.os.ServiceManager;
28import android.os.StatFs;
San Mehat1e60c962010-02-05 11:35:45 -080029import android.os.storage.StorageManager;
30import android.os.storage.IMountService;
Eric Fischer85f43572009-06-12 17:58:50 -070031import android.text.format.Formatter;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080032import android.view.View;
33import android.view.View.OnClickListener;
34import android.widget.Button;
35import android.widget.CheckBox;
36import android.widget.TextView;
37
38import java.io.File;
39
40
41public class SdCardSettings extends Activity
42{
43 @Override
44 public void onCreate(Bundle icicle) {
45 super.onCreate(icicle);
46
47 setContentView(R.layout.sdcard_settings_screen);
48
San Mehat1e60c962010-02-05 11:35:45 -080049 mStorageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080050 mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount"));
51
52 mRemovedLayout = findViewById(R.id.removed);
53 mMountedLayout = findViewById(R.id.mounted);
54 mUnmountedLayout = findViewById(R.id.unmounted);
55 mScanningLayout = findViewById(R.id.scanning);
56 mSharedLayout = findViewById(R.id.shared);
57 mBadRemovalLayout = findViewById(R.id.bad_removal);
58 mReadOnlyStatus = findViewById(R.id.read_only);
59
60 mMassStorage = (CheckBox)findViewById(R.id.mass_storage);
61 mMassStorage.setOnClickListener(mMassStorageListener);
62
63 Button unmountButton = (Button)findViewById(R.id.sdcard_unmount);
64 unmountButton.setOnClickListener(mUnmountButtonHandler);
65
66 Button formatButton = (Button)findViewById(R.id.sdcard_format);
67 formatButton.setOnClickListener(mFormatButtonHandler);
68
69 mTotalSize = (TextView)findViewById(R.id.total);
70 mUsedSize = (TextView)findViewById(R.id.used);
71 mAvailableSize = (TextView)findViewById(R.id.available);
72
73 // install an intent filter to receive SD card related events.
74 IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_REMOVED);
75 intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
76 intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
77 intentFilter.addAction(Intent.ACTION_MEDIA_SHARED);
78 intentFilter.addAction(Intent.ACTION_MEDIA_CHECKING);
79 intentFilter.addAction(Intent.ACTION_MEDIA_NOFS);
80 intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
81 intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
82 intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
83 intentFilter.addDataScheme("file");
84 registerReceiver(mReceiver, intentFilter);
85 }
86
87 @Override
88 protected void onDestroy() {
89 super.onDestroy();
90 }
91
92 @Override
93 public void onResume() {
94 super.onResume();
95 update();
96 }
97
98 private void setLayout(View layout) {
99 mRemovedLayout.setVisibility(layout == mRemovedLayout ? View.VISIBLE : View.GONE);
100 mMountedLayout.setVisibility(layout == mMountedLayout ? View.VISIBLE : View.GONE);
101 mUnmountedLayout.setVisibility(layout == mUnmountedLayout ? View.VISIBLE : View.GONE);
102 mScanningLayout.setVisibility(layout == mScanningLayout ? View.VISIBLE : View.GONE);
103 mSharedLayout.setVisibility(layout == mSharedLayout ? View.VISIBLE : View.GONE);
104 mBadRemovalLayout.setVisibility(layout == mBadRemovalLayout ? View.VISIBLE : View.GONE);
105 }
106
107 private void update() {
San Mehat4a3d7132010-01-29 06:09:06 -0800108
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800109 try {
San Mehat1e60c962010-02-05 11:35:45 -0800110 mMassStorage.setChecked(mStorageManager.isUsbMassStorageEnabled());
San Mehat4a3d7132010-01-29 06:09:06 -0800111 } catch (Exception ex) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800112 }
113
Michael Chan87620932009-05-14 17:47:02 -0700114 String status = Environment.getExternalStorageState();
115 boolean readOnly = false;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800116
Michael Chan87620932009-05-14 17:47:02 -0700117 if (status.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
118 status = Environment.MEDIA_MOUNTED;
119 readOnly = true;
120 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800121
Michael Chan87620932009-05-14 17:47:02 -0700122 if (status.equals(Environment.MEDIA_MOUNTED)) {
123 try {
124 File path = Environment.getExternalStorageDirectory();
125 StatFs stat = new StatFs(path.getPath());
126 long blockSize = stat.getBlockSize();
127 long totalBlocks = stat.getBlockCount();
128 long availableBlocks = stat.getAvailableBlocks();
129
130 mTotalSize.setText(formatSize(totalBlocks * blockSize));
131 mUsedSize.setText(formatSize((totalBlocks - availableBlocks) * blockSize));
132 mAvailableSize.setText(formatSize(availableBlocks * blockSize));
133 } catch (IllegalArgumentException e) {
134 // this can occur if the SD card is removed, but we haven't received the
135 // ACTION_MEDIA_REMOVED Intent yet.
136 status = Environment.MEDIA_REMOVED;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800137 }
138
Michael Chan87620932009-05-14 17:47:02 -0700139 mReadOnlyStatus.setVisibility(readOnly ? View.VISIBLE : View.GONE);
140 setLayout(mMountedLayout);
141 } else if (status.equals(Environment.MEDIA_UNMOUNTED)) {
142 setLayout(mUnmountedLayout);
143 } else if (status.equals(Environment.MEDIA_REMOVED)) {
144 setLayout(mRemovedLayout);
145 } else if (status.equals(Environment.MEDIA_SHARED)) {
146 setLayout(mSharedLayout);
147 } else if (status.equals(Environment.MEDIA_BAD_REMOVAL)) {
148 setLayout(mBadRemovalLayout);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800149 }
150 }
151
152 private String formatSize(long size) {
Eric Fischer85f43572009-06-12 17:58:50 -0700153 return Formatter.formatFileSize(this, size);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800154 }
155
156 OnClickListener mMassStorageListener = new OnClickListener() {
157 public void onClick(View v) {
158 try {
San Mehat4a3d7132010-01-29 06:09:06 -0800159 if (mMassStorage.isChecked()) {
San Mehat1e60c962010-02-05 11:35:45 -0800160 mStorageManager.enableUsbMassStorage();
San Mehat4a3d7132010-01-29 06:09:06 -0800161 } else {
San Mehat1e60c962010-02-05 11:35:45 -0800162 mStorageManager.disableUsbMassStorage();
San Mehat4a3d7132010-01-29 06:09:06 -0800163 }
164 } catch (Exception ex) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800165 }
166 }
167 };
168
169 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
170 @Override
171 public void onReceive(Context context, Intent intent) {
172 update();
173 }
174 };
175
176 OnClickListener mUnmountButtonHandler = new OnClickListener() {
177 public void onClick(View v) {
178 try {
San Mehatefb26fd2010-02-18 11:44:21 -0800179 mMountService.unmountVolume(Environment.getExternalStorageDirectory().toString(), false);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800180 } catch (RemoteException ex) {
181 }
182 }
183 };
184
185 OnClickListener mFormatButtonHandler = new OnClickListener() {
186 public void onClick(View v) {
187 try {
San Mehat33b02022010-01-12 12:22:37 -0800188 mMountService.formatVolume(Environment.getExternalStorageDirectory().toString());
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800189 } catch (RemoteException ex) {
190 }
191 }
192 };
193
San Mehat1e60c962010-02-05 11:35:45 -0800194 private StorageManager mStorageManager;
195 private IMountService mMountService;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800196
197 private CheckBox mMassStorage;
198
199 private TextView mTotalSize;
200 private TextView mUsedSize;
201 private TextView mAvailableSize;
202
203 private View mRemovedLayout;
204 private View mMountedLayout;
205 private View mUnmountedLayout;
206 private View mScanningLayout;
207 private View mSharedLayout;
208 private View mBadRemovalLayout;
209 private View mReadOnlyStatus;
210}