Add memorizing work tile position when it's restored to use it when
adding the tile The problem is that we don't save tiles along with workprofile. It's saved with the user. So we remove the work-profile tile when restoring the backup for the user, because there is no workprofile yet. And then we add it to the end. Test: Manual + auto Bug: 254931931 Change-Id: I9223e112b019c84d9bacfb6406c0fd178b4c072d
This commit is contained in:
@@ -43,6 +43,7 @@ import java.util.concurrent.Executor
|
||||
import javax.inject.Inject
|
||||
|
||||
private const val TAG = "AutoAddTracker"
|
||||
private const val DELIMITER = ","
|
||||
|
||||
/**
|
||||
* Class to track tiles that have been auto-added
|
||||
@@ -67,7 +68,7 @@ class AutoAddTracker @VisibleForTesting constructor(
|
||||
|
||||
@GuardedBy("autoAdded")
|
||||
private val autoAdded = ArraySet<String>()
|
||||
private var restoredTiles: Set<String>? = null
|
||||
private var restoredTiles: Map<String, AutoTile>? = null
|
||||
|
||||
override val currentUserId: Int
|
||||
get() = userId
|
||||
@@ -98,25 +99,26 @@ class AutoAddTracker @VisibleForTesting constructor(
|
||||
when (intent.getStringExtra(Intent.EXTRA_SETTING_NAME)) {
|
||||
Settings.Secure.QS_TILES -> {
|
||||
restoredTiles = intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE)
|
||||
?.split(",")
|
||||
?.toSet()
|
||||
?.split(DELIMITER)
|
||||
?.mapIndexed(::AutoTile)
|
||||
?.associateBy(AutoTile::tileType)
|
||||
?: run {
|
||||
Log.w(TAG, "Null restored tiles for user $userId")
|
||||
emptySet()
|
||||
emptyMap()
|
||||
}
|
||||
}
|
||||
Settings.Secure.QS_AUTO_ADDED_TILES -> {
|
||||
restoredTiles?.let { tiles ->
|
||||
restoredTiles?.let { restoredTiles ->
|
||||
val restoredAutoAdded = intent
|
||||
.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE)
|
||||
?.split(",")
|
||||
?.split(DELIMITER)
|
||||
?: emptyList()
|
||||
val autoAddedBeforeRestore = intent
|
||||
.getStringExtra(Intent.EXTRA_SETTING_PREVIOUS_VALUE)
|
||||
?.split(",")
|
||||
?.split(DELIMITER)
|
||||
?: emptyList()
|
||||
|
||||
val tilesToRemove = restoredAutoAdded.filter { it !in tiles }
|
||||
val tilesToRemove = restoredAutoAdded.filter { it !in restoredTiles }
|
||||
if (tilesToRemove.isNotEmpty()) {
|
||||
qsHost.removeTiles(tilesToRemove)
|
||||
}
|
||||
@@ -180,6 +182,9 @@ class AutoAddTracker @VisibleForTesting constructor(
|
||||
registerBroadcastReceiver()
|
||||
}
|
||||
|
||||
fun getRestoredTilePosition(tile: String): Int =
|
||||
restoredTiles?.get(tile)?.index ?: QSTileHost.POSITION_AT_END
|
||||
|
||||
/**
|
||||
* Returns `true` if the tile has been auto-added before
|
||||
*/
|
||||
@@ -196,12 +201,12 @@ class AutoAddTracker @VisibleForTesting constructor(
|
||||
*/
|
||||
fun setTileAdded(tile: String) {
|
||||
val tiles = synchronized(autoAdded) {
|
||||
if (autoAdded.add(tile)) {
|
||||
getTilesFromListLocked()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
if (autoAdded.add(tile)) {
|
||||
getTilesFromListLocked()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
tiles?.let { saveTiles(it) }
|
||||
}
|
||||
|
||||
@@ -222,7 +227,7 @@ class AutoAddTracker @VisibleForTesting constructor(
|
||||
}
|
||||
|
||||
private fun getTilesFromListLocked(): String {
|
||||
return TextUtils.join(",", autoAdded)
|
||||
return TextUtils.join(DELIMITER, autoAdded)
|
||||
}
|
||||
|
||||
private fun saveTiles(tiles: String) {
|
||||
@@ -245,7 +250,7 @@ class AutoAddTracker @VisibleForTesting constructor(
|
||||
|
||||
private fun getAdded(): Collection<String> {
|
||||
val current = secureSettings.getStringForUser(Settings.Secure.QS_AUTO_ADDED_TILES, userId)
|
||||
return current?.split(",") ?: emptySet()
|
||||
return current?.split(DELIMITER) ?: emptySet()
|
||||
}
|
||||
|
||||
override fun dump(pw: PrintWriter, args: Array<out String>) {
|
||||
@@ -281,4 +286,6 @@ class AutoAddTracker @VisibleForTesting constructor(
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private data class AutoTile(val index: Int, val tileType: String)
|
||||
}
|
||||
@@ -279,7 +279,8 @@ public class AutoTileManager implements UserAwareController {
|
||||
public void onManagedProfileChanged() {
|
||||
if (mManagedProfileController.hasActiveProfile()) {
|
||||
if (mAutoTracker.isAdded(WORK)) return;
|
||||
mHost.addTile(WORK);
|
||||
final int position = mAutoTracker.getRestoredTilePosition(WORK);
|
||||
mHost.addTile(WORK, position);
|
||||
mAutoTracker.setTileAdded(WORK);
|
||||
} else {
|
||||
if (!mAutoTracker.isAdded(WORK)) return;
|
||||
|
||||
@@ -59,6 +59,7 @@ import java.util.concurrent.Executor;
|
||||
@SmallTest
|
||||
public class AutoAddTrackerTest extends SysuiTestCase {
|
||||
|
||||
private static final int END_POSITION = -1;
|
||||
private static final int USER = 0;
|
||||
|
||||
@Mock
|
||||
@@ -141,6 +142,29 @@ public class AutoAddTrackerTest extends SysuiTestCase {
|
||||
assertTrue(mAutoTracker.isAdded(SAVER));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRestoredTilePositionPreserved() {
|
||||
verify(mBroadcastDispatcher).registerReceiver(
|
||||
mBroadcastReceiverArgumentCaptor.capture(), any(), any(), any(), anyInt(), any());
|
||||
String restoredTiles = "saver,internet,work,cast";
|
||||
Intent restoreTilesIntent = makeRestoreIntent(Secure.QS_TILES, null, restoredTiles);
|
||||
|
||||
mBroadcastReceiverArgumentCaptor.getValue().onReceive(mContext, restoreTilesIntent);
|
||||
|
||||
assertEquals(2, mAutoTracker.getRestoredTilePosition("work"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoRestoredTileReturnsEndPosition() {
|
||||
verify(mBroadcastDispatcher).registerReceiver(
|
||||
mBroadcastReceiverArgumentCaptor.capture(), any(), any(), any(), anyInt(), any());
|
||||
Intent restoreTilesIntent = makeRestoreIntent(Secure.QS_TILES, null, null);
|
||||
|
||||
mBroadcastReceiverArgumentCaptor.getValue().onReceive(mContext, restoreTilesIntent);
|
||||
|
||||
assertEquals(END_POSITION, mAutoTracker.getRestoredTilePosition("work"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBroadcastReceiverRegistered() {
|
||||
verify(mBroadcastDispatcher).registerReceiver(
|
||||
|
||||
@@ -510,6 +510,7 @@ public class AutoTileManagerTest extends SysuiTestCase {
|
||||
@Test
|
||||
public void managedProfileAdded_tileAdded() {
|
||||
when(mAutoAddTracker.isAdded(eq("work"))).thenReturn(false);
|
||||
when(mAutoAddTracker.getRestoredTilePosition(eq("work"))).thenReturn(2);
|
||||
mAutoTileManager = createAutoTileManager(mContext);
|
||||
Mockito.doAnswer((Answer<Object>) invocation -> {
|
||||
mManagedProfileCallback = invocation.getArgument(0);
|
||||
@@ -520,7 +521,7 @@ public class AutoTileManagerTest extends SysuiTestCase {
|
||||
|
||||
mManagedProfileCallback.onManagedProfileChanged();
|
||||
|
||||
verify(mQsTileHost, times(1)).addTile(eq("work"));
|
||||
verify(mQsTileHost, times(1)).addTile(eq("work"), eq(2));
|
||||
verify(mAutoAddTracker, times(1)).setTileAdded(eq("work"));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user