Initial Contribution
This commit is contained in:
178
src/com/android/settings/deviceinfo/Memory.java
Normal file
178
src/com/android/settings/deviceinfo/Memory.java
Normal file
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
* Copyright (C) 2008 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.deviceinfo;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Bundle;
|
||||
import android.os.RemoteException;
|
||||
import android.os.Environment;
|
||||
import android.os.IMountService;
|
||||
import android.os.ServiceManager;
|
||||
import android.os.StatFs;
|
||||
import android.preference.Preference;
|
||||
import android.preference.PreferenceActivity;
|
||||
import android.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
public class Memory extends PreferenceActivity {
|
||||
|
||||
private static final String MEMORY_SD_SIZE = "memory_sd_size";
|
||||
|
||||
private static final String MEMORY_SD_AVAIL = "memory_sd_avail";
|
||||
|
||||
private static final String MEMORY_SD_UNMOUNT = "memory_sd_unmount";
|
||||
|
||||
private Resources mRes;
|
||||
|
||||
private Preference mSdSize;
|
||||
private Preference mSdAvail;
|
||||
private Preference mSdUnmount;
|
||||
|
||||
private IMountService mMountService;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
|
||||
addPreferencesFromResource(R.xml.device_info_memory);
|
||||
|
||||
mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount"));
|
||||
|
||||
mRes = getResources();
|
||||
mSdSize = findPreference(MEMORY_SD_SIZE);
|
||||
mSdAvail = findPreference(MEMORY_SD_AVAIL);
|
||||
mSdUnmount = findPreference(MEMORY_SD_UNMOUNT);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
||||
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_REMOVED);
|
||||
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
|
||||
intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
|
||||
intentFilter.addAction(Intent.ACTION_MEDIA_SHARED);
|
||||
intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
|
||||
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
|
||||
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
|
||||
intentFilter.addDataScheme("file");
|
||||
registerReceiver(mReceiver, intentFilter);
|
||||
|
||||
updateMemoryStatus();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
unregisterReceiver(mReceiver);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
|
||||
if (preference == mSdUnmount) {
|
||||
unmount();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
updateMemoryStatus();
|
||||
}
|
||||
};
|
||||
|
||||
private void unmount() {
|
||||
try {
|
||||
mMountService.unmountMedia(Environment.getExternalStorageDirectory().toString());
|
||||
} catch (RemoteException ex) {
|
||||
// Failed for some reason, try to update UI to actual state
|
||||
updateMemoryStatus();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateMemoryStatus() {
|
||||
String status = Environment.getExternalStorageState();
|
||||
String readOnly = "";
|
||||
if (status.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
|
||||
status = Environment.MEDIA_MOUNTED;
|
||||
readOnly = mRes.getString(R.string.read_only);
|
||||
}
|
||||
|
||||
if (status.equals(Environment.MEDIA_MOUNTED)) {
|
||||
try {
|
||||
File path = Environment.getExternalStorageDirectory();
|
||||
StatFs stat = new StatFs(path.getPath());
|
||||
long blockSize = stat.getBlockSize();
|
||||
long totalBlocks = stat.getBlockCount();
|
||||
long availableBlocks = stat.getAvailableBlocks();
|
||||
|
||||
mSdSize.setSummary(formatSize(totalBlocks * blockSize));
|
||||
mSdAvail.setSummary(formatSize(availableBlocks * blockSize) + readOnly);
|
||||
mSdUnmount.setEnabled(true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// this can occur if the SD card is removed, but we haven't received the
|
||||
// ACTION_MEDIA_REMOVED Intent yet.
|
||||
status = Environment.MEDIA_REMOVED;
|
||||
}
|
||||
|
||||
} else {
|
||||
mSdSize.setSummary(mRes.getString(R.string.sd_unavailable));
|
||||
mSdAvail.setSummary(mRes.getString(R.string.sd_unavailable));
|
||||
mSdUnmount.setEnabled(false);
|
||||
}
|
||||
|
||||
File path = Environment.getDataDirectory();
|
||||
StatFs stat = new StatFs(path.getPath());
|
||||
long blockSize = stat.getBlockSize();
|
||||
long availableBlocks = stat.getAvailableBlocks();
|
||||
findPreference("memory_internal_avail").setSummary(formatSize(availableBlocks * blockSize));
|
||||
}
|
||||
|
||||
private String formatSize(long size) {
|
||||
String suffix = null;
|
||||
|
||||
// add KB or MB suffix if size is greater than 1K or 1M
|
||||
if (size >= 1024) {
|
||||
suffix = " KB";
|
||||
size /= 1024;
|
||||
if (size >= 1024) {
|
||||
suffix = " MB";
|
||||
size /= 1024;
|
||||
}
|
||||
}
|
||||
|
||||
DecimalFormat formatter = new DecimalFormat();
|
||||
formatter.setGroupingSize(3);
|
||||
String result = formatter.format(size);
|
||||
|
||||
if (suffix != null)
|
||||
result = result + suffix;
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user