The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
| 24 | import android.os.Bundle; |
| 25 | import android.os.RemoteException; |
| 26 | import android.os.Environment; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 27 | import android.os.ServiceManager; |
| 28 | import android.os.StatFs; |
San Mehat | 1e60c96 | 2010-02-05 11:35:45 -0800 | [diff] [blame] | 29 | import android.os.storage.StorageManager; |
| 30 | import android.os.storage.IMountService; |
Eric Fischer | 85f4357 | 2009-06-12 17:58:50 -0700 | [diff] [blame] | 31 | import android.text.format.Formatter; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 32 | import android.view.View; |
| 33 | import android.view.View.OnClickListener; |
| 34 | import android.widget.Button; |
| 35 | import android.widget.CheckBox; |
| 36 | import android.widget.TextView; |
| 37 | |
| 38 | import java.io.File; |
| 39 | |
| 40 | |
| 41 | public 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 Mehat | 1e60c96 | 2010-02-05 11:35:45 -0800 | [diff] [blame] | 49 | mStorageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 50 | 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 Mehat | 4a3d713 | 2010-01-29 06:09:06 -0800 | [diff] [blame] | 108 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 109 | try { |
San Mehat | 1e60c96 | 2010-02-05 11:35:45 -0800 | [diff] [blame] | 110 | mMassStorage.setChecked(mStorageManager.isUsbMassStorageEnabled()); |
San Mehat | 4a3d713 | 2010-01-29 06:09:06 -0800 | [diff] [blame] | 111 | } catch (Exception ex) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Michael Chan | 8762093 | 2009-05-14 17:47:02 -0700 | [diff] [blame] | 114 | String status = Environment.getExternalStorageState(); |
| 115 | boolean readOnly = false; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 116 | |
Michael Chan | 8762093 | 2009-05-14 17:47:02 -0700 | [diff] [blame] | 117 | if (status.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) { |
| 118 | status = Environment.MEDIA_MOUNTED; |
| 119 | readOnly = true; |
| 120 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 121 | |
Michael Chan | 8762093 | 2009-05-14 17:47:02 -0700 | [diff] [blame] | 122 | 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 Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Michael Chan | 8762093 | 2009-05-14 17:47:02 -0700 | [diff] [blame] | 139 | 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 Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | private String formatSize(long size) { |
Eric Fischer | 85f4357 | 2009-06-12 17:58:50 -0700 | [diff] [blame] | 153 | return Formatter.formatFileSize(this, size); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | OnClickListener mMassStorageListener = new OnClickListener() { |
| 157 | public void onClick(View v) { |
| 158 | try { |
San Mehat | 4a3d713 | 2010-01-29 06:09:06 -0800 | [diff] [blame] | 159 | if (mMassStorage.isChecked()) { |
San Mehat | 1e60c96 | 2010-02-05 11:35:45 -0800 | [diff] [blame] | 160 | mStorageManager.enableUsbMassStorage(); |
San Mehat | 4a3d713 | 2010-01-29 06:09:06 -0800 | [diff] [blame] | 161 | } else { |
San Mehat | 1e60c96 | 2010-02-05 11:35:45 -0800 | [diff] [blame] | 162 | mStorageManager.disableUsbMassStorage(); |
San Mehat | 4a3d713 | 2010-01-29 06:09:06 -0800 | [diff] [blame] | 163 | } |
| 164 | } catch (Exception ex) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 165 | } |
| 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 Mehat | efb26fd | 2010-02-18 11:44:21 -0800 | [diff] [blame] | 179 | mMountService.unmountVolume(Environment.getExternalStorageDirectory().toString(), false); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 180 | } catch (RemoteException ex) { |
| 181 | } |
| 182 | } |
| 183 | }; |
| 184 | |
| 185 | OnClickListener mFormatButtonHandler = new OnClickListener() { |
| 186 | public void onClick(View v) { |
| 187 | try { |
San Mehat | 33b0202 | 2010-01-12 12:22:37 -0800 | [diff] [blame] | 188 | mMountService.formatVolume(Environment.getExternalStorageDirectory().toString()); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 189 | } catch (RemoteException ex) { |
| 190 | } |
| 191 | } |
| 192 | }; |
| 193 | |
San Mehat | 1e60c96 | 2010-02-05 11:35:45 -0800 | [diff] [blame] | 194 | private StorageManager mStorageManager; |
| 195 | private IMountService mMountService; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 196 | |
| 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 | } |