Android 格式化内部存储

 

1,在设置界面点击>存储->格式化内部存储将会执行

packages/apps/Settings/src/com/android/settings/deviceinfo/Memory.java

  1. if (preference == mSdFormat) { 
  2.             Intent intent = new Intent(Intent.ACTION_VIEW); 
  3.             intent.setClass(this, com.android.settings.MediaFormat.class); 
  4.             startActivity(intent); 
  5.             return true

  6.  

2,packages/apps/Settings/src/com/android/settings/MediaFormat.java

下面将会跳到onCreate方法的establishInitialState();

  1. protected void onCreate(Bundle savedState) { 
  2. @Override protected void onCreate(Bundle savedState) { super.onCreate(savedState); establishInitialState(); }
     


3,构造第一个含有一个按钮(格式化内部大容量存储设备)的页面,并为按钮添加事件

  1. private void establishInitialState() { 
  2.        if (mInitialView == null) { 
  3.            mInitialView = mInflater.inflate(R.layout.media_format_primary, null); 
  4.            mInitiateButton = 
  5.                    (Button) mInitialView.findViewById(R.id.initiate_media_format); 
  6.            mInitiateButton.setOnClickListener(mInitiateListener); 
  7.        } 
  8.     } 

4,当点击这个按钮时将会跳到确认页面,防止误操作

  1. private Button.OnClickListener mInitiateListener = new Button.OnClickListener() { 
  2.         public void onClick(View v) { 
  3.             if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) { 
  4.                 establishFinalConfirmationState(); 
  5.             } 
  6.         } 
  7.     }; 

5,确认格式化大容量存储设备的时候将会发送一个Intent,并将要格式化的路径传入

  1. Intent intent = new Intent(ExternalStorageFormatter.FORMAT_ONLY); 
  2. intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME); 
  3. String strPath = Environment.getExternalStorageDirectory().getPath(); 
  4. intent.putExtra("path", strPath); 
  5. startService(intent); 

6,frameworks/base/core/java/com/android/internal/os/storage/ExternalStorageFormatter.java
执行ExternalStorageFormatter类中的public int onStartCommand(Intent intent, int flags, int startId)方法

接收路径并执行updateProgressState()方法

7,在updateProgressState()方法中判断路径是否存在,将卸载存储卡,

  1. if (Environment.MEDIA_MOUNTED.equals(status) 
  2.                 || Environment.MEDIA_MOUNTED_READ_ONLY.equals(status)) { 
  3.             updateProgressDialog(mIsSdcard? R.string.progress_unmounting : R.string.progress_unmounting_nand); 
  4.             SystemProperties.set("sys.storage.state","formating"); 
  5.             IMountService mountService = getMountService(); 
  6.             try { 
  7.                 /* 
  8.                  * For mount point of external sdcard is: /mnt/sdcard/external_sd, 
  9.                  * unmount sdcard first. 
  10.                  */ 
  11.                 if (mPath.equals(Environment.getFlashStorageDirectory().toString())) { 
  12.                     updateProgressDialog(R.string.progress_unmounting_nand); 
  13.                     mountService.unmountVolume(Environment.getActExternalStorageDirectory().toString(),true); 
  14.                 } 
  15.                 mountService.unmountVolume(mPath, true); 
  16.             } 

8,成功卸载SD卡会激发StorageEventListener mStorageListener = new StorageEventListener()事件

mStorageListener会重新执行updateProgressState()方法

9,再次执行updateProgressState()方法的时候SD卡已经卸载将会执行格式化操作

  1. if (Environment.MEDIA_NOFS.equals(status) 
  2.                || Environment.MEDIA_UNMOUNTED.equals(status) 
  3.                || Environment.MEDIA_UNMOUNTABLE.equals(status)) { 
  4.            updateProgressDialog(mIsSdcard? R.string.progress_erasing : R.string.progress_erasing_nand); 
  5.            SystemProperties.set("sys.storage.state","ready"); 
  6.            final IMountService mountService = getMountService(); 
  7.            final String extStoragePath = mPath;//Environment.getExternalStorageDirectory().toString(); 
  8.            if (mountService != null) { 
  9.                new Thread() { 
  10.                    public void run() { 
  11.                        boolean success = false
  12.                        try { 
  13.                            mountService.formatVolume(extStoragePath); 
  14.                            success = true
  15.                        } catch (Exception e) { 
  16.                            Toast.makeText(ExternalStorageFormatter.this, mIsSdcard? 
  17.                                    R.string.format_error : R.string.format_error_nand, Toast.LENGTH_LONG).show(); 
  18.                        } 
  19.                        if (success) { 
  20.                            if (mFactoryReset) { 
  21.                                android.os.SystemProperties.set("ctl.start","bootanim"); 
  22.                                sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR")); 
  23.                                // Intent handling is asynchronous -- assume it will happen soon. 
  24.                                stopSelf(); 
  25.                                return
  26.                            } 
  27.                        } 
  28.                        // If we didn't succeed, or aren't doing a full factory 
  29.                        // reset, then it is time to remount the storage. 
  30.                        if (!success && mAlwaysReset) { 
  31.                            sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR")); 
  32.                        } 

10,success等于真的话会会清楚缓存,语句mountService.formatVolume(extStoragePath);是格式化操作

  1. public int formatVolume(String mountPoint) throws RemoteException { 
  2.     Parcel _data = Parcel.obtain(); 
  3.     Parcel _reply = Parcel.obtain(); 
  4.     int _result; 
  5.     try { 
  6.         _data.writeInterfaceToken(DESCRIPTOR); 
  7.         _data.writeString(mountPoint); 
  8.         mRemote.transact(Stub.TRANSACTION_formatVolume, _data, _reply, 0); 
  9.         _reply.readException(); 
  10.         _result = _reply.readInt(); 
  11.     } finally { 
  12.         _reply.recycle(); 
  13.         _data.recycle(); 
  14.     } 
  15.     return _result;