Android Direct Boot issue while switching from credential to device storage
<p>I have made an app to test direct boot. One activity which only store five preferences and boot receiver.</p>

<pre><code>MainActivity.java
mSharedPreferences = getSharedPreferences("myapp_preferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString("pref1", "someStringPref1");
editor.putString("pref2", "someStringPref2");
editor.putString("pref3", "someStringPref3");
editor.putString("pref4", "someStringPref4");
editor.putString("pref5", "someStringPref5");
editor.commit();

final Map&lt;String, ?&gt; map = mSharedPreferences.getAll();
Log.d("BootReceiver", "map size: " + map.size());
</code></pre>

<p>Than a boot receiver code:</p>

<pre><code>Context storageContext;
final Context deviceContext = context.createDeviceProtectedStorageContext();
if (!unlocked) {
    if (!deviceContext.moveSharedPreferencesFrom(context, "myapp_preferences")) {
        Log.w(TAG, "Failed to migrate shared preferences.");
    }
    storageContext = deviceContext;
} else {
    if (!context.moveSharedPreferencesFrom(deviceContext, "myapp_preferences")) {
        Log.w(TAG, "Failed to migrate shared preferences.");
    }
    storageContext = context;
}

mSharedPreferences = storageContext.getSharedPreferences("myapp_preferences", Context.MODE_PRIVATE);
final Map&lt;String, ?&gt; map = mSharedPreferences.getAll();

Log.d(TAG, "intent action: " + intent.getAction());
Log.d(TAG, "map size: " + map.size());
</code></pre>

<p>My log</p>

<pre><code>// activity started, five prefs successfully added
map size: 5
// device restarted, still unlocked -&gt; direct boot mode
intent action: android.intent.action.LOCKED_BOOT_COMPLETED
map size: 0
// device unlocked (PIN screen lock used) -&gt; credential encrypted storage
intent action: android.intent.action.BOOT_COMPLETED
map size: 5
</code></pre>

<p>Why I cannot move prefs to device context (map size: 0)? An API bug? My device is buggy (file encryption is turned on, Nexus 5x, with patch from this October)? Or most likely, am I missing something?</p>

<p>PS Missing android-directboot tag.</p>

<p><strong>Update</strong>
Same behaviour with Nexus 6p, for both 7.0 (app compiled with <code>compileSdkVersion 24</code>) and 7.1.1 (<code>compileSdkVersion 24&amp;25</code>) OS version.</p>

<p><strong>Update #2</strong></p>

<pre><code>if (Intent.ACTION_LOCKED_BOOT_COMPLETED.equals(action)) {
    unlocked = false;
} else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
    unlocked = true;
}
</code></pre>