MTK(Android N)设置SDCard为默认存储

思路:当系统将所有存储挂载完毕,然后调用StorageManagerEx.setDefaultPath(path)设置默认存储,当用户手动改变以后就跟随用户设置。
diff --git a/packages/apps/Settings/src/com/mediatek/settings/deviceinfo/StorageSettingsExts.java b/packages/apps/Settings/src/com/mediatek/settings/deviceinfo/StorageSettingsExts.java
old mode 100644
new mode 100755
index 445c7d5..0944468
--- a/packages/apps/Settings/src/com/mediatek/settings/deviceinfo/StorageSettingsExts.java
+++ b/packages/apps/Settings/src/com/mediatek/settings/deviceinfo/StorageSettingsExts.java
@@ -20,6 +20,9 @@ import com.mediatek.storage.StorageManagerEx;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+/*gxk@Revo modify SDCard to default storage 20170818*/
+import android.provider.Settings;
+/*End of by gxk*/

 public class StorageSettingsExts {
     private static final String TAG = "StorageSettings";
@@ -114,6 +117,11 @@ public class StorageSettingsExts {
                 }
                 StorageManagerEx.setDefaultPath(preference.getKey());
                 Log.d(TAG, "Set default path : " + preference.getKey());
+/*gxk@Revo modify SDCard to default storage 20170818*/
+                if (com.android.featureoption.FeatureOption.PRJ_FEATURE_REVO_B_SET_DEFAULT_STORAGE){
+                    Settings.System.putInt(mActivity.getContentResolver(), "isUserSet", 1);
+                }
+/*End of by gxk*/
                 mDeafultWritePathPref = (RadioButtonPreference) preference;
                 return true;
             }
diff --git a/packages/providers/MediaProvider/src/com/android/providers/media/MediaScannerReceiver.java b/packages/providers/MediaProvider/src/com/android/providers/media/MediaScannerReceiver.java
old mode 100644
new mode 100755
index 9c9df73..15b2d93
--- a/packages/providers/MediaProvider/src/com/android/providers/media/MediaScannerReceiver.java
+++ b/packages/providers/MediaProvider/src/com/android/providers/media/MediaScannerReceiver.java
@@ -40,6 +40,13 @@ import android.os.storage.StorageVolume;
 import android.os.SystemProperties;
 import android.preference.PreferenceManager;
 import android.provider.MediaStore;
+/*gxk@Revo modify SDCard to default storage 20170818*/
+import com.mediatek.storage.StorageManagerEx;
+import java.util.ArrayList;
+import java.util.List;
+import android.provider.Settings;
+import com.android.featureoption.FeatureOption;
+/*End of by gxk*/

 public class MediaScannerReceiver extends BroadcastReceiver
 {
@@ -58,10 +65,19 @@ public class MediaScannerReceiver extends BroadcastReceiver
     static boolean sIsShutdown = !"def_value".equals(SystemProperties.get(
             "sys.shutdown.requested", "def_value"));
     private static final String DEVICE_MOUNT_BEFORE_BOOT = "dev.mount_before_boot";
+/*gxk@Revo modify SDCard to default storage 20170818*/
+    private StorageManager storageManager;
+    private int isUserSet = 0;
+/*End of by gxk*/

     @Override
     public void onReceive(Context context, Intent intent) {
         final String action = intent.getAction();
+/*gxk@Revo modify SDCard to default storage 20170818*/
+        storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
+        if (FeatureOption.PRJ_FEATURE_REVO_B_SET_DEFAULT_STORAGE)
+            isUserSet = Settings.System.getInt(context.getContentResolver(), "isUserSet", 0);
+/*End of by gxk*/
         MtkLog.v(TAG, "onReceive action = " + action);
         if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
             MtkLog.v(TAG, "onReceive BOOT_COMPLETED, begin to scan internal and external storage.");
@@ -192,7 +208,7 @@ public class MediaScannerReceiver extends BroadcastReceiver
      * @return
      */
     private boolean isAllStorageMounted(Context context) {
-        StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
+        // StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
         StorageVolume[] ExternalVolumeList = storageManager.getVolumeList();
         for (StorageVolume storageVolume : ExternalVolumeList) {
             String path = storageVolume.getPath();
@@ -204,6 +220,18 @@ public class MediaScannerReceiver extends BroadcastReceiver
         }
         return true;
     }
+/*gxk@Revo modify SDCard to default storage 20170818*/
+    private StorageVolume[] getDefaultWriteDiskList() {
+        List storageVolumes = new ArrayList();
+        StorageVolume[] volumes = storageManager.getVolumeList();
+        for (StorageVolume volume : volumes) {
+            if (Environment.MEDIA_MOUNTED.equals(storageManager.getVolumeState(volume.getPath()))) {
+                storageVolumes.add(volume);
+            }
+        }
+        return storageVolumes.toArray(new StorageVolume[storageVolumes.size()]);
+    }
+/*End of by gxk*/

     /// M: get main handler to do check whether all storage have been mounted every CHECK_INTERVAL time.
     private Handler getHandler() {
@@ -221,6 +249,20 @@ public class MediaScannerReceiver extends BroadcastReceiver
                             scan(context, MediaProvider.EXTERNAL_VOLUME);
                             removeCallbacksAndMessages(null);
                             sHandler = null;
+/*gxk@Revo modify SDCard to default storage 20170818*/
+                            if (FeatureOption.PRJ_FEATURE_REVO_B_SET_DEFAULT_STORAGE &&
+                                 isUserSet == 0 && isAllStorageMounted(context)) {
+                                StorageVolume[] availableVolumes = getDefaultWriteDiskList();
+                                for (StorageVolume volume : availableVolumes) {
+                                    String path = volume.getPath();
+                                    MtkLog.v(TAG, " setDefaultPath = " + path);
+                                    if (path != null && !"/storage/emulated/0".equals(path)) {
+                                        StorageManagerEx.setDefaultPath(path);
+                                        break;
+                                    }
+                                }
+                            }
+/*End of by gxk*/
                         } else {
                             MtkLog.v(TAG, "Some storage has not been mounted, wait it mounted until time out.");
                             Message next = obtainMessage(msg.what, waitTime + CHECK_INTERVAL, -1, msg.obj);

你可能感兴趣的:(Android)