Merge "Reduce extra connections made to app" into tm-qpr-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
a88f73834f
@@ -239,6 +239,8 @@ constructor(
|
|||||||
data.playbackLocation != MediaData.PLAYBACK_CAST_REMOTE)
|
data.playbackLocation != MediaData.PLAYBACK_CAST_REMOTE)
|
||||||
if (data.resumeAction == null && !data.hasCheckedForResume && isEligibleForResume) {
|
if (data.resumeAction == null && !data.hasCheckedForResume && isEligibleForResume) {
|
||||||
// TODO also check for a media button receiver intended for restarting (b/154127084)
|
// TODO also check for a media button receiver intended for restarting (b/154127084)
|
||||||
|
// Set null action to prevent additional attempts to connect
|
||||||
|
mediaDataManager.setResumeAction(key, null)
|
||||||
Log.d(TAG, "Checking for service component for " + data.packageName)
|
Log.d(TAG, "Checking for service component for " + data.packageName)
|
||||||
val pm = context.packageManager
|
val pm = context.packageManager
|
||||||
val serviceIntent = Intent(MediaBrowserService.SERVICE_INTERFACE)
|
val serviceIntent = Intent(MediaBrowserService.SERVICE_INTERFACE)
|
||||||
@@ -249,9 +251,6 @@ constructor(
|
|||||||
backgroundExecutor.execute {
|
backgroundExecutor.execute {
|
||||||
tryUpdateResumptionList(key, inf!!.get(0).componentInfo.componentName)
|
tryUpdateResumptionList(key, inf!!.get(0).componentInfo.componentName)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// No service found
|
|
||||||
mediaDataManager.setResumeAction(key, null)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -263,8 +262,6 @@ constructor(
|
|||||||
*/
|
*/
|
||||||
private fun tryUpdateResumptionList(key: String, componentName: ComponentName) {
|
private fun tryUpdateResumptionList(key: String, componentName: ComponentName) {
|
||||||
Log.d(TAG, "Testing if we can connect to $componentName")
|
Log.d(TAG, "Testing if we can connect to $componentName")
|
||||||
// Set null action to prevent additional attempts to connect
|
|
||||||
mediaDataManager.setResumeAction(key, null)
|
|
||||||
mediaBrowser =
|
mediaBrowser =
|
||||||
mediaBrowserFactory.create(
|
mediaBrowserFactory.create(
|
||||||
object : ResumeMediaBrowser.Callback() {
|
object : ResumeMediaBrowser.Callback() {
|
||||||
|
|||||||
@@ -85,16 +85,13 @@ public class ResumeMediaBrowser {
|
|||||||
* ResumeMediaBrowser#disconnect will be called automatically with this function.
|
* ResumeMediaBrowser#disconnect will be called automatically with this function.
|
||||||
*/
|
*/
|
||||||
public void findRecentMedia() {
|
public void findRecentMedia() {
|
||||||
disconnect();
|
|
||||||
Bundle rootHints = new Bundle();
|
Bundle rootHints = new Bundle();
|
||||||
rootHints.putBoolean(MediaBrowserService.BrowserRoot.EXTRA_RECENT, true);
|
rootHints.putBoolean(MediaBrowserService.BrowserRoot.EXTRA_RECENT, true);
|
||||||
mMediaBrowser = mBrowserFactory.create(
|
MediaBrowser browser = mBrowserFactory.create(
|
||||||
mComponentName,
|
mComponentName,
|
||||||
mConnectionCallback,
|
mConnectionCallback,
|
||||||
rootHints);
|
rootHints);
|
||||||
updateMediaController();
|
connectBrowser(browser, "findRecentMedia");
|
||||||
mLogger.logConnection(mComponentName, "findRecentMedia");
|
|
||||||
mMediaBrowser.connect();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final MediaBrowser.SubscriptionCallback mSubscriptionCallback =
|
private final MediaBrowser.SubscriptionCallback mSubscriptionCallback =
|
||||||
@@ -201,6 +198,21 @@ public class ResumeMediaBrowser {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connect using a new media browser. Disconnects the existing browser first, if it exists.
|
||||||
|
* @param browser media browser to connect
|
||||||
|
* @param reason Reason to log for connection
|
||||||
|
*/
|
||||||
|
private void connectBrowser(MediaBrowser browser, String reason) {
|
||||||
|
mLogger.logConnection(mComponentName, reason);
|
||||||
|
disconnect();
|
||||||
|
mMediaBrowser = browser;
|
||||||
|
if (browser != null) {
|
||||||
|
browser.connect();
|
||||||
|
}
|
||||||
|
updateMediaController();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disconnect the media browser. This should be done after callbacks have completed to
|
* Disconnect the media browser. This should be done after callbacks have completed to
|
||||||
* disconnect from the media browser service.
|
* disconnect from the media browser service.
|
||||||
@@ -222,10 +234,9 @@ public class ResumeMediaBrowser {
|
|||||||
* getting a media update from the app
|
* getting a media update from the app
|
||||||
*/
|
*/
|
||||||
public void restart() {
|
public void restart() {
|
||||||
disconnect();
|
|
||||||
Bundle rootHints = new Bundle();
|
Bundle rootHints = new Bundle();
|
||||||
rootHints.putBoolean(MediaBrowserService.BrowserRoot.EXTRA_RECENT, true);
|
rootHints.putBoolean(MediaBrowserService.BrowserRoot.EXTRA_RECENT, true);
|
||||||
mMediaBrowser = mBrowserFactory.create(mComponentName,
|
MediaBrowser browser = mBrowserFactory.create(mComponentName,
|
||||||
new MediaBrowser.ConnectionCallback() {
|
new MediaBrowser.ConnectionCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onConnected() {
|
public void onConnected() {
|
||||||
@@ -265,9 +276,7 @@ public class ResumeMediaBrowser {
|
|||||||
disconnect();
|
disconnect();
|
||||||
}
|
}
|
||||||
}, rootHints);
|
}, rootHints);
|
||||||
updateMediaController();
|
connectBrowser(browser, "restart");
|
||||||
mLogger.logConnection(mComponentName, "restart");
|
|
||||||
mMediaBrowser.connect();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
@@ -305,16 +314,13 @@ public class ResumeMediaBrowser {
|
|||||||
* ResumeMediaBrowser#disconnect should be called after this to ensure the connection is closed.
|
* ResumeMediaBrowser#disconnect should be called after this to ensure the connection is closed.
|
||||||
*/
|
*/
|
||||||
public void testConnection() {
|
public void testConnection() {
|
||||||
disconnect();
|
|
||||||
Bundle rootHints = new Bundle();
|
Bundle rootHints = new Bundle();
|
||||||
rootHints.putBoolean(MediaBrowserService.BrowserRoot.EXTRA_RECENT, true);
|
rootHints.putBoolean(MediaBrowserService.BrowserRoot.EXTRA_RECENT, true);
|
||||||
mMediaBrowser = mBrowserFactory.create(
|
MediaBrowser browser = mBrowserFactory.create(
|
||||||
mComponentName,
|
mComponentName,
|
||||||
mConnectionCallback,
|
mConnectionCallback,
|
||||||
rootHints);
|
rootHints);
|
||||||
updateMediaController();
|
connectBrowser(browser, "testConnection");
|
||||||
mLogger.logConnection(mComponentName, "testConnection");
|
|
||||||
mMediaBrowser.connect();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Updates mMediaController based on our current browser values. */
|
/** Updates mMediaController based on our current browser values. */
|
||||||
|
|||||||
@@ -312,6 +312,25 @@ class MediaResumeListenerTest : SysuiTestCase() {
|
|||||||
verify(mediaDataManager, never()).setResumeAction(KEY, null)
|
verify(mediaDataManager, never()).setResumeAction(KEY, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testOnLoadTwice_onlyChecksOnce() {
|
||||||
|
// When data is first loaded,
|
||||||
|
setUpMbsWithValidResolveInfo()
|
||||||
|
resumeListener.onMediaDataLoaded(KEY, null, data)
|
||||||
|
|
||||||
|
// We notify the manager to set a null action
|
||||||
|
verify(mediaDataManager).setResumeAction(KEY, null)
|
||||||
|
|
||||||
|
// If we then get another update from the app before the first check completes
|
||||||
|
assertThat(executor.numPending()).isEqualTo(1)
|
||||||
|
var dataWithCheck = data.copy(hasCheckedForResume = true)
|
||||||
|
resumeListener.onMediaDataLoaded(KEY, null, dataWithCheck)
|
||||||
|
|
||||||
|
// We do not try to start another check
|
||||||
|
assertThat(executor.numPending()).isEqualTo(1)
|
||||||
|
verify(mediaDataManager).setResumeAction(KEY, null)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testOnUserUnlock_loadsTracks() {
|
fun testOnUserUnlock_loadsTracks() {
|
||||||
// Set up mock service to successfully find valid media
|
// Set up mock service to successfully find valid media
|
||||||
@@ -392,7 +411,7 @@ class MediaResumeListenerTest : SysuiTestCase() {
|
|||||||
assertThat(result.size).isEqualTo(3)
|
assertThat(result.size).isEqualTo(3)
|
||||||
assertThat(result[2].toLong()).isEqualTo(currentTime)
|
assertThat(result[2].toLong()).isEqualTo(currentTime)
|
||||||
}
|
}
|
||||||
verify(sharedPrefsEditor, times(1)).apply()
|
verify(sharedPrefsEditor).apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -432,8 +451,8 @@ class MediaResumeListenerTest : SysuiTestCase() {
|
|||||||
resumeListener.userUnlockReceiver.onReceive(mockContext, intent)
|
resumeListener.userUnlockReceiver.onReceive(mockContext, intent)
|
||||||
|
|
||||||
// We add its resume controls
|
// We add its resume controls
|
||||||
verify(resumeBrowser, times(1)).findRecentMedia()
|
verify(resumeBrowser).findRecentMedia()
|
||||||
verify(mediaDataManager, times(1))
|
verify(mediaDataManager)
|
||||||
.addResumptionControls(anyInt(), any(), any(), any(), any(), any(), eq(PACKAGE_NAME))
|
.addResumptionControls(anyInt(), any(), any(), any(), any(), any(), eq(PACKAGE_NAME))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -516,7 +535,7 @@ class MediaResumeListenerTest : SysuiTestCase() {
|
|||||||
assertThat(result.size).isEqualTo(3)
|
assertThat(result.size).isEqualTo(3)
|
||||||
assertThat(result[2].toLong()).isEqualTo(currentTime)
|
assertThat(result[2].toLong()).isEqualTo(currentTime)
|
||||||
}
|
}
|
||||||
verify(sharedPrefsEditor, times(1)).apply()
|
verify(sharedPrefsEditor).apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user