Add dialog when ejecting sdcard
Remove some dead code
This commit is contained in:
@@ -1,70 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2007 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;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.util.Config;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class SdCardIntentReceiver extends BroadcastReceiver {
|
||||
|
||||
private static final int SDCARD_STATUS = 1;
|
||||
private static final String TAG = "SdCardIntentReceiver";
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
NotificationManager nm = (NotificationManager) context
|
||||
.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
String action = intent.getAction();
|
||||
if (Config.LOGD) Log.d(TAG, "onReceiveIntent " + action);
|
||||
|
||||
if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
|
||||
nm.cancel(SDCARD_STATUS);
|
||||
|
||||
Intent statusIntent = new Intent(Intent.ACTION_MAIN, null);
|
||||
statusIntent.setClass(context, SdCardSettings.class);
|
||||
nm.notify(SDCARD_STATUS, new Notification(context,
|
||||
android.R.drawable.stat_notify_sdcard,
|
||||
null,
|
||||
System.currentTimeMillis(),
|
||||
context.getText(R.string.sdcard_setting),
|
||||
null,
|
||||
statusIntent));
|
||||
} else if (action.equals(Intent.ACTION_MEDIA_REMOVED)) {
|
||||
nm.cancel(SDCARD_STATUS);
|
||||
} else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
|
||||
nm.cancel(SDCARD_STATUS);
|
||||
|
||||
Intent statusIntent = new Intent(Intent.ACTION_MAIN, null);
|
||||
statusIntent.setClass(context, SdCardSettings.class);
|
||||
nm.notify(SDCARD_STATUS, new Notification(context,
|
||||
android.R.drawable.stat_notify_sdcard_usb,
|
||||
null,
|
||||
System.currentTimeMillis(),
|
||||
"SD Card",
|
||||
null,
|
||||
statusIntent));
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,210 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2007 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;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Bundle;
|
||||
import android.os.RemoteException;
|
||||
import android.os.Environment;
|
||||
import android.os.ServiceManager;
|
||||
import android.os.StatFs;
|
||||
import android.os.storage.StorageManager;
|
||||
import android.os.storage.IMountService;
|
||||
import android.text.format.Formatter;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.Button;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
|
||||
public class SdCardSettings extends Activity
|
||||
{
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
|
||||
setContentView(R.layout.sdcard_settings_screen);
|
||||
|
||||
mStorageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
|
||||
mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount"));
|
||||
|
||||
mRemovedLayout = findViewById(R.id.removed);
|
||||
mMountedLayout = findViewById(R.id.mounted);
|
||||
mUnmountedLayout = findViewById(R.id.unmounted);
|
||||
mScanningLayout = findViewById(R.id.scanning);
|
||||
mSharedLayout = findViewById(R.id.shared);
|
||||
mBadRemovalLayout = findViewById(R.id.bad_removal);
|
||||
mReadOnlyStatus = findViewById(R.id.read_only);
|
||||
|
||||
mMassStorage = (CheckBox)findViewById(R.id.mass_storage);
|
||||
mMassStorage.setOnClickListener(mMassStorageListener);
|
||||
|
||||
Button unmountButton = (Button)findViewById(R.id.sdcard_unmount);
|
||||
unmountButton.setOnClickListener(mUnmountButtonHandler);
|
||||
|
||||
Button formatButton = (Button)findViewById(R.id.sdcard_format);
|
||||
formatButton.setOnClickListener(mFormatButtonHandler);
|
||||
|
||||
mTotalSize = (TextView)findViewById(R.id.total);
|
||||
mUsedSize = (TextView)findViewById(R.id.used);
|
||||
mAvailableSize = (TextView)findViewById(R.id.available);
|
||||
|
||||
// install an intent filter to receive SD card related events.
|
||||
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_CHECKING);
|
||||
intentFilter.addAction(Intent.ACTION_MEDIA_NOFS);
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
update();
|
||||
}
|
||||
|
||||
private void setLayout(View layout) {
|
||||
mRemovedLayout.setVisibility(layout == mRemovedLayout ? View.VISIBLE : View.GONE);
|
||||
mMountedLayout.setVisibility(layout == mMountedLayout ? View.VISIBLE : View.GONE);
|
||||
mUnmountedLayout.setVisibility(layout == mUnmountedLayout ? View.VISIBLE : View.GONE);
|
||||
mScanningLayout.setVisibility(layout == mScanningLayout ? View.VISIBLE : View.GONE);
|
||||
mSharedLayout.setVisibility(layout == mSharedLayout ? View.VISIBLE : View.GONE);
|
||||
mBadRemovalLayout.setVisibility(layout == mBadRemovalLayout ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
|
||||
private void update() {
|
||||
|
||||
try {
|
||||
mMassStorage.setChecked(mStorageManager.isUsbMassStorageEnabled());
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
|
||||
String status = Environment.getExternalStorageState();
|
||||
boolean readOnly = false;
|
||||
|
||||
if (status.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
|
||||
status = Environment.MEDIA_MOUNTED;
|
||||
readOnly = true;
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
mTotalSize.setText(formatSize(totalBlocks * blockSize));
|
||||
mUsedSize.setText(formatSize((totalBlocks - availableBlocks) * blockSize));
|
||||
mAvailableSize.setText(formatSize(availableBlocks * blockSize));
|
||||
} 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;
|
||||
}
|
||||
|
||||
mReadOnlyStatus.setVisibility(readOnly ? View.VISIBLE : View.GONE);
|
||||
setLayout(mMountedLayout);
|
||||
} else if (status.equals(Environment.MEDIA_UNMOUNTED)) {
|
||||
setLayout(mUnmountedLayout);
|
||||
} else if (status.equals(Environment.MEDIA_REMOVED)) {
|
||||
setLayout(mRemovedLayout);
|
||||
} else if (status.equals(Environment.MEDIA_SHARED)) {
|
||||
setLayout(mSharedLayout);
|
||||
} else if (status.equals(Environment.MEDIA_BAD_REMOVAL)) {
|
||||
setLayout(mBadRemovalLayout);
|
||||
}
|
||||
}
|
||||
|
||||
private String formatSize(long size) {
|
||||
return Formatter.formatFileSize(this, size);
|
||||
}
|
||||
|
||||
OnClickListener mMassStorageListener = new OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
try {
|
||||
if (mMassStorage.isChecked()) {
|
||||
mStorageManager.enableUsbMassStorage();
|
||||
} else {
|
||||
mStorageManager.disableUsbMassStorage();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
update();
|
||||
}
|
||||
};
|
||||
|
||||
OnClickListener mUnmountButtonHandler = new OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
try {
|
||||
mMountService.unmountVolume(Environment.getExternalStorageDirectory().toString(), false);
|
||||
} catch (RemoteException ex) {
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
OnClickListener mFormatButtonHandler = new OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
try {
|
||||
mMountService.formatVolume(Environment.getExternalStorageDirectory().toString());
|
||||
} catch (RemoteException ex) {
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private StorageManager mStorageManager;
|
||||
private IMountService mMountService;
|
||||
|
||||
private CheckBox mMassStorage;
|
||||
|
||||
private TextView mTotalSize;
|
||||
private TextView mUsedSize;
|
||||
private TextView mAvailableSize;
|
||||
|
||||
private View mRemovedLayout;
|
||||
private View mMountedLayout;
|
||||
private View mUnmountedLayout;
|
||||
private View mScanningLayout;
|
||||
private View mSharedLayout;
|
||||
private View mBadRemovalLayout;
|
||||
private View mReadOnlyStatus;
|
||||
}
|
@@ -16,13 +16,19 @@
|
||||
|
||||
package com.android.settings.deviceinfo;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.DialogInterface.OnCancelListener;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.os.Message;
|
||||
import android.os.RemoteException;
|
||||
import android.os.Environment;
|
||||
import android.os.storage.IMountService;
|
||||
@@ -35,15 +41,15 @@ import android.preference.PreferenceActivity;
|
||||
import android.preference.PreferenceScreen;
|
||||
import android.text.format.Formatter;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
public class Memory extends PreferenceActivity {
|
||||
|
||||
public class Memory extends PreferenceActivity implements OnCancelListener {
|
||||
private static final String TAG = "Memory";
|
||||
private static final boolean localLOGV = false;
|
||||
|
||||
private static final String MEMORY_SD_SIZE = "memory_sd_size";
|
||||
|
||||
@@ -52,6 +58,10 @@ public class Memory extends PreferenceActivity {
|
||||
private static final String MEMORY_SD_MOUNT_TOGGLE = "memory_sd_mount_toggle";
|
||||
|
||||
private static final String MEMORY_SD_FORMAT = "memory_sd_format";
|
||||
|
||||
private static final int DLG_CONFIRM_UNMOUNT = 1;
|
||||
private static final int DLG_ERROR_UNMOUNT = 2;
|
||||
|
||||
private Resources mRes;
|
||||
|
||||
private Preference mSdSize;
|
||||
@@ -98,6 +108,9 @@ public class Memory extends PreferenceActivity {
|
||||
|
||||
@Override
|
||||
public void onStorageStateChanged(String path, String oldState, String newState) {
|
||||
Log.i(TAG, "Received storage state changed notification that " +
|
||||
path + " changed state from " + oldState +
|
||||
" to " + newState);
|
||||
updateMemoryStatus();
|
||||
}
|
||||
};
|
||||
@@ -107,7 +120,15 @@ public class Memory extends PreferenceActivity {
|
||||
super.onPause();
|
||||
unregisterReceiver(mReceiver);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
if (mStorageManager != null && mStorageListener != null) {
|
||||
mStorageManager.unregisterListener(mStorageListener);
|
||||
}
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
private synchronized IMountService getMountService() {
|
||||
if (mMountService == null) {
|
||||
IBinder service = ServiceManager.getService("mount");
|
||||
@@ -147,19 +168,80 @@ public class Memory extends PreferenceActivity {
|
||||
}
|
||||
};
|
||||
|
||||
private void unmount() {
|
||||
IMountService mountService = getMountService();
|
||||
try {
|
||||
if (mountService != null) {
|
||||
mountService.unmountVolume(Environment.getExternalStorageDirectory().toString(), false);
|
||||
} else {
|
||||
Log.e(TAG, "Mount service is null, can't unmount");
|
||||
}
|
||||
} catch (RemoteException ex) {
|
||||
@Override
|
||||
public Dialog onCreateDialog(int id, Bundle args) {
|
||||
switch (id) {
|
||||
case DLG_CONFIRM_UNMOUNT:
|
||||
return new AlertDialog.Builder(this)
|
||||
.setTitle(R.string.dlg_confirm_unmount_title)
|
||||
.setPositiveButton(R.string.dlg_ok, new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
doUnmount(true);
|
||||
}})
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.setMessage(R.string.dlg_confirm_unmount_text)
|
||||
.setOnCancelListener(this)
|
||||
.create();
|
||||
case DLG_ERROR_UNMOUNT:
|
||||
return new AlertDialog.Builder(this )
|
||||
.setTitle(R.string.dlg_error_unmount_title)
|
||||
.setNeutralButton(R.string.dlg_ok, null)
|
||||
.setMessage(R.string.dlg_error_unmount_text)
|
||||
.setOnCancelListener(this)
|
||||
.create();
|
||||
}
|
||||
updateMemoryStatus();
|
||||
return null;
|
||||
}
|
||||
|
||||
private void doUnmount(boolean force) {
|
||||
// Present a toast here
|
||||
Toast.makeText(this, R.string.unmount_inform_text, Toast.LENGTH_SHORT).show();
|
||||
IMountService mountService = getMountService();
|
||||
String extStoragePath = Environment.getExternalStorageDirectory().toString();
|
||||
try {
|
||||
mSdMountToggle.setEnabled(false);
|
||||
mSdMountToggle.setTitle(mRes.getString(R.string.sd_ejecting_title));
|
||||
mSdMountToggle.setSummary(mRes.getString(R.string.sd_ejecting_summary));
|
||||
mountService.unmountVolume(extStoragePath, force);
|
||||
} catch (RemoteException e) {
|
||||
// Informative dialog to user that
|
||||
// unmount failed.
|
||||
showDialogInner(DLG_ERROR_UNMOUNT);
|
||||
}
|
||||
}
|
||||
|
||||
private void showDialogInner(int id) {
|
||||
removeDialog(id);
|
||||
showDialog(id);
|
||||
}
|
||||
|
||||
private void unmount() {
|
||||
// Check if the sdcard is being accessed by other processes
|
||||
// and let the user decide if the sdcard should be ejected.
|
||||
String extStoragePath = Environment.
|
||||
getExternalStorageDirectory().toString();
|
||||
IMountService mountService = getMountService();
|
||||
int stUsers[] = null;
|
||||
try {
|
||||
stUsers = mountService.getStorageUsers(extStoragePath);
|
||||
} catch (RemoteException e) {
|
||||
// Very unlikely. But present an error dialog anyway
|
||||
Log.e(TAG, "Is MountService running?");
|
||||
showDialogInner(DLG_ERROR_UNMOUNT);
|
||||
}
|
||||
if (stUsers != null && stUsers.length > 0) {
|
||||
if (localLOGV) Log.i(TAG, "Do have storage users accessing "
|
||||
+ extStoragePath);
|
||||
for (int pid : stUsers) {
|
||||
if (localLOGV) Log.i(TAG, pid + " accessing file on sdcard");
|
||||
}
|
||||
// Present dialog to user
|
||||
showDialogInner(DLG_CONFIRM_UNMOUNT);
|
||||
} else {
|
||||
doUnmount(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void mount() {
|
||||
IMountService mountService = getMountService();
|
||||
try {
|
||||
@@ -170,7 +252,6 @@ public class Memory extends PreferenceActivity {
|
||||
}
|
||||
} catch (RemoteException ex) {
|
||||
}
|
||||
updateMemoryStatus();
|
||||
}
|
||||
|
||||
private void updateMemoryStatus() {
|
||||
@@ -233,5 +314,9 @@ public class Memory extends PreferenceActivity {
|
||||
private String formatSize(long size) {
|
||||
return Formatter.formatFileSize(this, size);
|
||||
}
|
||||
|
||||
public void onCancel(DialogInterface dialog) {
|
||||
finish();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user