Stop scan on device chooser activity backgrounded

This effectively treats chooser activity pause event as cancel.

Bug: 30932767
Test: Install two toy apps and call associate API from both.
  Ensure foreground app always end up showing fresh data.
Change-Id: I49943959c2b4f05f2e1d49603fd472b3e0c47213
This commit is contained in:
Eugene Susla
2017-03-28 15:40:44 -07:00
parent 6a2dd54502
commit 098a045057
4 changed files with 35 additions and 13 deletions

View File

@@ -19,4 +19,5 @@ package android.companion;
/** @hide */
interface ICompanionDeviceDiscoveryServiceCallback {
oneway void onDeviceSelected(String packageName, int userId, String deviceAddress);
oneway void onDeviceSelectionCancel();
}

View File

@@ -79,15 +79,25 @@ public class DeviceChooserActivity extends Activity {
}
mPairButton = findViewById(R.id.button_pair);
mPairButton.setOnClickListener((view) ->
onPairTapped(getService().mSelectedDevice));
mPairButton.setOnClickListener(v -> onPairTapped(getService().mSelectedDevice));
updatePairButtonEnabled();
mCancelButton = findViewById(R.id.button_cancel);
mCancelButton.setOnClickListener((view) -> {
setResult(RESULT_CANCELED);
finish();
});
mCancelButton.setOnClickListener(v -> cancel());
}
private void cancel() {
getService().onCancel();
setResult(RESULT_CANCELED);
finish();
}
@Override
protected void onPause() {
super.onPause();
if (!isFinishing()) {
cancel();
}
}
private CharSequence getCallingAppName() {

View File

@@ -202,8 +202,6 @@ public class DeviceDiscoveryService extends Service {
reset();
} else if (DEBUG) Log.i(LOG_TAG, "startDiscovery: duplicate request: " + request);
if (!ArrayUtils.isEmpty(mDevicesFound)) {
onReadyToShowUI();
}
@@ -307,6 +305,14 @@ public class DeviceDiscoveryService extends Service {
}
}
void onCancel() {
try {
mServiceCallback.onDeviceSelectionCancel();
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
class DevicesAdapter extends ArrayAdapter<DeviceFilterPair> {
//TODO wifi icon
private Drawable BLUETOOTH_ICON = icon(android.R.drawable.stat_sys_data_bluetooth);

View File

@@ -135,10 +135,10 @@ public class CompanionDeviceManagerService extends SystemService implements Bind
@Override
public void binderDied() {
Handler.getMain().post(this::handleBinderDied);
Handler.getMain().post(this::cleanup);
}
private void handleBinderDied() {
private void cleanup() {
mServiceConnection = unbind(mServiceConnection);
mFindDeviceCallback = unlinkToDeath(mFindDeviceCallback, this, 0);
}
@@ -201,7 +201,6 @@ public class CompanionDeviceManagerService extends SystemService implements Bind
}
}
@Override
public List<String> getAssociations(String callingPackage) {
return CollectionUtils.map(
@@ -237,7 +236,7 @@ public class CompanionDeviceManagerService extends SystemService implements Bind
mFindDeviceCallback.asBinder().linkToDeath(
CompanionDeviceManagerService.this, 0);
} catch (RemoteException e) {
handleBinderDied();
cleanup();
return;
}
try {
@@ -266,10 +265,16 @@ public class CompanionDeviceManagerService extends SystemService implements Bind
@Override
public void onDeviceSelected(String packageName, int userId, String deviceAddress) {
//TODO unbind
updateSpecialAccessPermissionForAssociatedPackage(packageName, userId);
recordAssociation(packageName, deviceAddress);
cleanup();
}
@Override
public void onDeviceSelectionCancel() {
cleanup();
}
};
}