• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
27 import android.os.IMountService;
28 import android.os.ServiceManager;
29 import android.os.StatFs;
30 import android.text.format.Formatter;
31 import android.view.View;
32 import android.view.View.OnClickListener;
33 import android.widget.Button;
34 import android.widget.CheckBox;
35 import android.widget.TextView;
36 
37 import java.io.File;
38 
39 
40 public class SdCardSettings extends Activity
41 {
42     @Override
onCreate(Bundle icicle)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
onDestroy()86     protected void onDestroy() {
87         super.onDestroy();
88     }
89 
90     @Override
onResume()91     public void onResume() {
92         super.onResume();
93         update();
94     }
95 
setLayout(View layout)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 
update()105     private void update() {
106         try {
107             mMassStorage.setChecked(mMountService.getMassStorageEnabled());
108         } catch (RemoteException ex) {
109         }
110 
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 
formatSize(long size)149     private String formatSize(long size) {
150         return Formatter.formatFileSize(this, size);
151     }
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 {
172                 mMountService.unmountMedia(Environment.getExternalStorageDirectory().toString());
173             } catch (RemoteException ex) {
174             }
175         }
176     };
177 
178     OnClickListener mFormatButtonHandler = new OnClickListener() {
179         public void onClick(View v) {
180             try {
181                 mMountService.formatMedia(Environment.getExternalStorageDirectory().toString());
182             } catch (RemoteException ex) {
183             }
184         }
185     };
186 
187     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 }
203