Merge "Send MODE_CHANGING broadcast before location mode change."
diff --git a/src/com/android/settings/deviceinfo/StorageWizardInit.java b/src/com/android/settings/deviceinfo/StorageWizardInit.java
index ffc07e5..05c7b15 100644
--- a/src/com/android/settings/deviceinfo/StorageWizardInit.java
+++ b/src/com/android/settings/deviceinfo/StorageWizardInit.java
@@ -16,18 +16,26 @@
 
 package com.android.settings.deviceinfo;
 
+import static com.android.settings.deviceinfo.StorageSettings.TAG;
+
 import android.app.ActivityManager;
 import android.content.Intent;
+import android.os.AsyncTask;
 import android.os.Bundle;
+import android.os.Environment;
 import android.os.UserManager;
 import android.os.storage.DiskInfo;
 import android.os.storage.VolumeInfo;
+import android.util.DebugUtils;
+import android.util.Log;
 import android.widget.CompoundButton;
 import android.widget.CompoundButton.OnCheckedChangeListener;
 import android.widget.RadioButton;
 
 import com.android.settings.R;
 
+import java.io.File;
+
 public class StorageWizardInit extends StorageWizardBase {
     private RadioButton mRadioExternal;
     private RadioButton mRadioInternal;
@@ -69,12 +77,15 @@
             mRadioExternal.setChecked(true);
             onNavigateNext();
             finish();
-        }
-
-        // TODO: Show a message about why this is disabled for guest and that only an admin user
-        // can adopt an sd card.
-        if (!mIsPermittedToAdopt) {
+        } else if (!mIsPermittedToAdopt) {
+            // TODO: Show a message about why this is disabled for guest and
+            // that only an admin user can adopt an sd card.
             mRadioInternal.setEnabled(false);
+        } else if (mVolume != null && mVolume.getType() == VolumeInfo.TYPE_PUBLIC
+                && mVolume.isMountedReadable()) {
+            // Device is mounted, so classify contents to possibly pick a
+            // recommended default operation.
+            new ClassifyTask().execute(mVolume.getPath());
         }
     }
 
@@ -121,4 +132,29 @@
             startActivity(intent);
         }
     }
+
+    /**
+     * Task that classifies the contents of a mounted storage device, and sets a
+     * recommended default operation based on result.
+     */
+    public class ClassifyTask extends AsyncTask<File, Void, Integer> {
+        @Override
+        protected Integer doInBackground(File... params) {
+            int classes = Environment.classifyExternalStorageDirectory(params[0]);
+            Log.v(TAG, "Classified " + params[0] + " as "
+                    + DebugUtils.flagsToString(Environment.class, "HAS_", classes));
+            return classes;
+        }
+
+        @Override
+        protected void onPostExecute(Integer classes) {
+            if (classes == 0) {
+                // Empty is strong signal for adopt
+                mRadioInternal.setChecked(true);
+            } else if ((classes & (Environment.HAS_PICTURES | Environment.HAS_DCIM)) != 0) {
+                // Photos is strong signal for portable
+                mRadioExternal.setChecked(true);
+            }
+        }
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeAutomaticRulesPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeAutomaticRulesPreferenceControllerTest.java
index 1b10e85..20f9e62 100644
--- a/tests/robotests/src/com/android/settings/notification/ZenModeAutomaticRulesPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/notification/ZenModeAutomaticRulesPreferenceControllerTest.java
@@ -82,7 +82,7 @@
                 mock(Lifecycle.class));
 
         ReflectionHelpers.setField(mController, "mBackend", mBackend);
-        ReflectionHelpers.setField(mController, "DEFAULT_RULE_IDS", mDefaultIds);
+        ReflectionHelpers.setField(mController, "mDefaultRuleIds", mDefaultIds);
 
         when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
                 mockPref);
@@ -188,4 +188,4 @@
 
         return ruleMap;
     }
-}
\ No newline at end of file
+}
diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeScheduleRuleSettingsTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeScheduleRuleSettingsTest.java
index f8cc767..070aa38 100644
--- a/tests/robotests/src/com/android/settings/notification/ZenModeScheduleRuleSettingsTest.java
+++ b/tests/robotests/src/com/android/settings/notification/ZenModeScheduleRuleSettingsTest.java
@@ -17,10 +17,10 @@
 package com.android.settings.notification;
 
 import android.app.Activity;
+import android.app.NotificationManager;
 import android.content.Context;
 import android.content.res.Resources;
 import android.content.Intent;
-import android.os.UserManager;
 
 import com.android.settings.R;
 import com.android.settings.TestConfig;
@@ -34,6 +34,8 @@
 import org.mockito.MockitoAnnotations;
 import org.robolectric.annotation.Config;
 import org.robolectric.shadows.ShadowToast;
+import org.robolectric.shadows.ShadowApplication;
+import org.robolectric.RuntimeEnvironment;
 
 import static com.google.common.truth.Truth.assertThat;
 import static org.mockito.Mockito.doReturn;
@@ -58,14 +60,19 @@
     private Intent mIntent;
 
     @Mock
-    private UserManager mUserManager;
+    private NotificationManager mNotificationManager;
 
     private TestFragment mFragment;
+    private Context mContext;
 
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
 
+        ShadowApplication shadowApplication = ShadowApplication.getInstance();
+        shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
+        mContext = shadowApplication.getApplicationContext();
+
         mFragment = spy(new TestFragment());
         mFragment.onAttach(application);
 
@@ -77,13 +84,13 @@
         when(mActivity.getTheme()).thenReturn(res.newTheme());
         when(mActivity.getIntent()).thenReturn(mIntent);
         when(mActivity.getResources()).thenReturn(res);
-        when(mFragment.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
+        when(mFragment.getContext()).thenReturn(mContext);
     }
 
     @Test
     public void onCreate_noRuleId_shouldToastAndFinishAndNoCrash() {
-        final Context ctx = application.getApplicationContext();
-        final String expected = ctx.getResources().getString(R.string.zen_mode_rule_not_found_text);
+        final String expected = mContext.getResources().getString(
+                R.string.zen_mode_rule_not_found_text);
 
         mFragment.onCreate(null);
 
@@ -93,7 +100,7 @@
         // verify the finish
         verify(mActivity).finish();
 
-        //shoud not crash
+        //should not crash
     }
 
     public static class TestFragment extends ZenModeScheduleRuleSettings {