diff options
author | Zhizhi Liu <zhizhiliu@google.com> | 2022-04-05 01:47:20 +0000 |
---|---|---|
committer | Zhizhi Liu <zhizhiliu@google.com> | 2022-04-05 05:54:45 +0000 |
commit | 4b0a0140fc90549dd03dfcfa019fa0c3e409e364 (patch) | |
tree | 15ce87167a2ed9122bc0e9458d3ec47354c53731 | |
parent | 4a81888cc988324c5262d650155ee39c53b63723 (diff) | |
download | atv-4b0a0140fc90549dd03dfcfa019fa0c3e409e364.tar.gz |
Ensure remotes & accessories updates correctly.
This refactoring CL fix the current remotes&accessores issue in two
ways:
a) Previous Cl modify the PendingIntent to be immutable which disables
filling intent from TvSettings. Modify the logic to allow pendingintent
to be immutable.
b) Update code to trigger notify slice provider change each time remotes
& accesories updates.
Bug: 226557527
Test: make -j, check TvSettings "remotes & accesories" screen.
Change-Id: I5b269c14e597136f915116709a0e134ea83be38d
3 files changed, 93 insertions, 61 deletions
diff --git a/libraries/BluetoothServices/src/com/google/android/tv/btservices/settings/ConnectedDevicesSliceProvider.java b/libraries/BluetoothServices/src/com/google/android/tv/btservices/settings/ConnectedDevicesSliceProvider.java index d3cd708..08f7e5f 100644 --- a/libraries/BluetoothServices/src/com/google/android/tv/btservices/settings/ConnectedDevicesSliceProvider.java +++ b/libraries/BluetoothServices/src/com/google/android/tv/btservices/settings/ConnectedDevicesSliceProvider.java @@ -16,6 +16,8 @@ package com.google.android.tv.btservices.settings; +import static com.android.tv.twopanelsettings.slices.SlicesConstants.EXTRA_SLICE_FOLLOWUP; + import static com.google.android.tv.btservices.settings.BluetoothDevicePreferenceFragment.CONT_CANCEL_ARGS; import static com.google.android.tv.btservices.settings.BluetoothDevicePreferenceFragment.KEY_CONNECT; import static com.google.android.tv.btservices.settings.BluetoothDevicePreferenceFragment.KEY_DISCONNECT; @@ -32,7 +34,9 @@ import static com.google.android.tv.btservices.settings.ConnectedDevicesPreferen import static com.google.android.tv.btservices.settings.SliceBroadcastReceiver.CEC; import static com.google.android.tv.btservices.settings.SliceBroadcastReceiver.TOGGLE_STATE; import static com.google.android.tv.btservices.settings.SliceBroadcastReceiver.TOGGLE_TYPE; -import static com.google.android.tv.btservices.settings.SlicesUtil.EXTRAS_SLICE_URI; +import static com.google.android.tv.btservices.settings.SliceBroadcastReceiver.backAndUpdateSliceIntent; +import static com.google.android.tv.btservices.settings.SliceBroadcastReceiver.updateSliceIntent; +import static com.google.android.tv.btservices.settings.SlicesUtil.GENERAL_SLICE_URI; import android.app.PendingIntent; import android.app.admin.DevicePolicyManager; @@ -43,7 +47,6 @@ import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.net.Uri; - import android.os.Bundle; import android.os.Handler; import android.os.IBinder; @@ -73,6 +76,7 @@ import com.google.android.tv.btservices.SettingsUtils; import com.google.android.tv.btservices.remote.Version; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -91,7 +95,7 @@ public class ConnectedDevicesSliceProvider extends SliceProvider implements private static final String TAG = "Atv.ConDevsSliceProvider"; private static final boolean DEBUG = false; private boolean mBtDeviceServiceBound; - private Map<String, Version> mVersionsMap = new ConcurrentHashMap<>(); + private final Map<String, Version> mVersionsMap = new ConcurrentHashMap<>(); private BluetoothDeviceService.LocalBinder mBtDeviceServiceBinder; private final Map<Uri, Integer> pinnedUris = new ArrayMap<>(); @@ -165,8 +169,8 @@ public class ConnectedDevicesSliceProvider extends SliceProvider implements } Context context = getContext(); if (!mBtDeviceServiceBound && context.bindService( - new Intent(context, BluetoothUtils.getBluetoothDeviceServiceClass(context)), - mBtDeviceServiceConnection, Context.BIND_AUTO_CREATE)) { + new Intent(context, BluetoothUtils.getBluetoothDeviceServiceClass(context)), + mBtDeviceServiceConnection, Context.BIND_AUTO_CREATE)) { mBtDeviceServiceBound = true; } if (!pinnedUris.containsKey(sliceUri)) { @@ -243,15 +247,20 @@ public class ConnectedDevicesSliceProvider extends SliceProvider implements RestrictedLockUtilsInternal.checkIfRestrictionEnforced(getContext(), UserManager.DISALLOW_CONFIG_BLUETOOTH, UserHandle.myUserId()); PendingIntent pendingIntent; + List<String> updatedUris = Arrays.asList(GENERAL_SLICE_URI.toString()); + PendingIntent updateGeneralSliceIntent = updateSliceIntent(getContext(), 0, + new ArrayList<>(updatedUris)); if (admin == null) { Intent i = SettingsUtils.getPairingIntent(); + i.putExtra(EXTRA_SLICE_FOLLOWUP, updateGeneralSliceIntent); pendingIntent = PendingIntent.getActivity(getContext(), 0, i, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); } else { Intent intent = RestrictedLockUtils.getShowAdminSupportDetailsIntent(getContext(), admin); intent.putExtra(DevicePolicyManager.EXTRA_RESTRICTION, - UserManager.DISALLOW_CONFIG_BLUETOOTH); + UserManager.DISALLOW_CONFIG_BLUETOOTH) + .putExtra(EXTRA_SLICE_FOLLOWUP, updateGeneralSliceIntent); pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, PendingIntent.FLAG_IMMUTABLE); } @@ -425,8 +434,12 @@ public class ConnectedDevicesSliceProvider extends SliceProvider implements null, ResponseFragment.DEFAULT_CHOICE_UNDEFINED ); - i.putExtras(extras); - i.putExtra(KEY_EXTRAS_DEVICE, device); + i.putExtras(extras).putExtra(KEY_EXTRAS_DEVICE, device); + List<String> updatedUris = Arrays.asList(GENERAL_SLICE_URI.toString(), + sliceUri.toString()); + PendingIntent updateSliceIntent = updateSliceIntent(getContext(), 0, + new ArrayList<>(updatedUris)); + i.putExtra(EXTRA_SLICE_FOLLOWUP, updateSliceIntent); PendingIntent updatePendingIntent = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); @@ -477,18 +490,17 @@ public class ConnectedDevicesSliceProvider extends SliceProvider implements deviceName, isConnected ? 1 /* default to NO (index 1) */ : 0 /* default to YES */ ); - i.putExtras(extras); - i.putExtra(KEY_EXTRAS_DEVICE, device); + i.putExtras(extras) + .putExtra(KEY_EXTRAS_DEVICE, device); + List<String> updatedUris = Arrays.asList(GENERAL_SLICE_URI.toString(), + sliceUri.toString()); + PendingIntent updateSliceIntent = backAndUpdateSliceIntent(getContext(), 1, + new ArrayList<>(updatedUris), sliceUri.toString()); + i.putExtra(EXTRA_SLICE_FOLLOWUP, updateSliceIntent); PendingIntent pendingIntent = PendingIntent - .getActivity(context, 2, i, - PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); - Intent followUpIntent = new Intent(context, SliceBroadcastReceiver.class); - followUpIntent.putExtra(EXTRAS_SLICE_URI, sliceUri.toString()); - PendingIntent followupIntent = PendingIntent - .getBroadcast(context, 2, followUpIntent, + .getActivity(context, 1, i, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); disconnectPref.setPendingIntent(pendingIntent); - disconnectPref.setFollowupPendingIntent(followupIntent); psb.addPreference(disconnectPref); } @@ -508,19 +520,16 @@ public class ConnectedDevicesSliceProvider extends SliceProvider implements deviceName, ResponseFragment.DEFAULT_CHOICE_UNDEFINED ); - i = new Intent(context, ResponseActivity.class); - i.putExtra(KEY_EXTRAS_DEVICE, device); - i.putExtras(extras); + i = new Intent(context, ResponseActivity.class) + .putExtra(KEY_EXTRAS_DEVICE, device) + .putExtras(extras); + List<String> updatedUris = Arrays.asList(GENERAL_SLICE_URI.toString(), sliceUri.toString()); + PendingIntent updateSliceIntent = updateSliceIntent(getContext(), 2, + new ArrayList<>(updatedUris)); + i.putExtra(EXTRA_SLICE_FOLLOWUP, updateSliceIntent); PendingIntent renamePendingIntent = PendingIntent - .getActivity(context, 1, i, - PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); - - Intent followUpIntent = new Intent(context, SliceBroadcastReceiver.class); - followUpIntent.putExtra(EXTRAS_SLICE_URI, sliceUri.toString()); - PendingIntent renameFollowupIntent = PendingIntent - .getBroadcast(context, 1, followUpIntent, + .getActivity(context, 2, i, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); - renamePref.setFollowupPendingIntent(renameFollowupIntent); renamePref.setPendingIntent(renamePendingIntent); psb.addPreference(renamePref); @@ -541,18 +550,15 @@ public class ConnectedDevicesSliceProvider extends SliceProvider implements deviceName, 1 /* default to NO (index 1) */ ); - i.putExtras(extras); - i.putExtra(KEY_EXTRAS_DEVICE, device); + i.putExtras(extras).putExtra(KEY_EXTRAS_DEVICE, device); + updatedUris = Arrays.asList(GENERAL_SLICE_URI.toString(), sliceUri.toString()); + updateSliceIntent = backAndUpdateSliceIntent(getContext(), 3, + new ArrayList<>(updatedUris), sliceUri.toString()); + i.putExtra(EXTRA_SLICE_FOLLOWUP, updateSliceIntent); PendingIntent disconnectPendingIntent = PendingIntent .getActivity(context, 3, i, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); - followUpIntent = new Intent(context, SliceBroadcastReceiver.class); - followUpIntent.putExtra(EXTRAS_SLICE_URI, sliceUri.toString()); - PendingIntent forgetFollowupIntent = PendingIntent - .getBroadcast(context, 3, followUpIntent, - PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); forgetPref.setPendingIntent(disconnectPendingIntent); - forgetPref.setFollowupPendingIntent(forgetFollowupIntent); psb.addPreference(forgetPref); // Update "bluetooth device info preference". @@ -687,7 +693,7 @@ public class ConnectedDevicesSliceProvider extends SliceProvider implements pref.setIconNeedsToBeProcessed(true); } - private BluetoothDeviceProvider mLocalBluetoothDeviceProvider = + private final BluetoothDeviceProvider mLocalBluetoothDeviceProvider = new LocalBluetoothDeviceProvider() { final BluetoothDeviceProvider getHostBluetoothDeviceProvider() { return getBluetoothDeviceProvider(); diff --git a/libraries/BluetoothServices/src/com/google/android/tv/btservices/settings/ResponseActivity.java b/libraries/BluetoothServices/src/com/google/android/tv/btservices/settings/ResponseActivity.java index b9deb59..6bb7d64 100644 --- a/libraries/BluetoothServices/src/com/google/android/tv/btservices/settings/ResponseActivity.java +++ b/libraries/BluetoothServices/src/com/google/android/tv/btservices/settings/ResponseActivity.java @@ -24,8 +24,6 @@ import static com.google.android.tv.btservices.settings.BluetoothDevicePreferenc import static com.google.android.tv.btservices.settings.BluetoothDevicePreferenceFragment.KEY_UPDATE; import static com.google.android.tv.btservices.settings.BluetoothDevicePreferenceFragment.YES; import static com.google.android.tv.btservices.settings.ConnectedDevicesSliceProvider.KEY_EXTRAS_DEVICE; -import static com.google.android.tv.btservices.settings.SlicesUtil.DIRECTION_BACK; -import static com.google.android.tv.btservices.settings.SlicesUtil.EXTRAS_DIRECTION; import android.app.Activity; import android.bluetooth.BluetoothDevice; @@ -91,8 +89,7 @@ public class ResponseActivity extends Activity implements @Override public void onChoice(String key, int choice) { BluetoothDeviceProvider provider = getBluetoothDeviceProvider(); - Intent i = new Intent(); - + Intent i = new Intent().putExtras(getIntent()); if (provider == null) { return; } @@ -104,19 +101,19 @@ public class ResponseActivity extends Activity implements case KEY_CONNECT: if (choice == YES) { provider.connectDevice(mDevice); - i.putExtra(EXTRAS_DIRECTION, DIRECTION_BACK); + setResult(RESULT_OK, i); } break; case KEY_DISCONNECT: if (choice == YES) { provider.disconnectDevice(mDevice); - i.putExtra(EXTRAS_DIRECTION, DIRECTION_BACK); + setResult(RESULT_OK, i); } break; case KEY_FORGET: if (choice == YES) { provider.forgetDevice(mDevice); - i.putExtra(EXTRAS_DIRECTION, DIRECTION_BACK); + setResult(RESULT_OK, i); } break; case KEY_UPDATE: @@ -125,24 +122,23 @@ public class ResponseActivity extends Activity implements intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra(RemoteDfuActivity.EXTRA_BT_ADDRESS, mDevice.getAddress()); startActivity(intent); + setResult(RESULT_OK, i); } break; } - setResult(RESULT_OK, i); finish(); } @Override public void onText(String key, String text) { BluetoothDeviceProvider provider = getBluetoothDeviceProvider(); + Intent i = new Intent().putExtras(getIntent()); if (KEY_RENAME.equals(key)) { if (mDevice != null) { provider.renameDevice(mDevice, text); + setResult(RESULT_OK, i); } } - Intent i = new Intent(); - i.putExtra(EXTRAS_DIRECTION, DIRECTION_BACK); - setResult(RESULT_OK, i); finish(); } diff --git a/libraries/BluetoothServices/src/com/google/android/tv/btservices/settings/SliceBroadcastReceiver.java b/libraries/BluetoothServices/src/com/google/android/tv/btservices/settings/SliceBroadcastReceiver.java index 968ac3a..063a757 100644 --- a/libraries/BluetoothServices/src/com/google/android/tv/btservices/settings/SliceBroadcastReceiver.java +++ b/libraries/BluetoothServices/src/com/google/android/tv/btservices/settings/SliceBroadcastReceiver.java @@ -18,12 +18,11 @@ package com.google.android.tv.btservices.settings; import static com.google.android.tv.btservices.settings.ConnectedDevicesSliceProvider.ACTION_TOGGLE_CHANGED; import static com.google.android.tv.btservices.settings.SlicesUtil.CEC_SLICE_URI; -import static com.google.android.tv.btservices.settings.SlicesUtil.DIRECTION_BACK; -import static com.google.android.tv.btservices.settings.SlicesUtil.EXTRAS_DIRECTION; import static com.google.android.tv.btservices.settings.SlicesUtil.EXTRAS_SLICE_URI; import static com.google.android.tv.btservices.settings.SlicesUtil.GENERAL_SLICE_URI; import static com.google.android.tv.btservices.settings.SlicesUtil.notifyToGoBack; +import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -31,6 +30,8 @@ import android.net.Uri; import com.google.android.tv.btservices.PowerUtils; +import java.util.ArrayList; + /** * This broadcast receiver handles two cases: * (a) CEC control toggle. @@ -42,24 +43,53 @@ public class SliceBroadcastReceiver extends BroadcastReceiver { static final String CEC = "CEC"; static final String TOGGLE_TYPE = "TOGGLE_TYPE"; static final String TOGGLE_STATE = "TOGGLE_STATE"; + private static final String ACTION_UPDATE_SLICE = "UPDATE_SLICE"; + private static final String ACTION_BACK_AND_UPDATE_SLICE = "BACK_AND_UPDATE_SLICE"; + private static final String PARAM_URIS = "URIS"; @Override public void onReceive(Context context, Intent intent) { - // Handle CEC control toggle. final String action = intent.getAction(); final boolean isChecked = intent.getBooleanExtra(TOGGLE_STATE, false); - if (ACTION_TOGGLE_CHANGED.equals(action) - && CEC.equals(intent.getStringExtra(TOGGLE_TYPE))) { - PowerUtils.enableCecControl(context, isChecked); - context.getContentResolver().notifyChange(CEC_SLICE_URI, null); - context.getContentResolver().notifyChange(GENERAL_SLICE_URI, null); + if (action == null) { return; } - - // Notify TvSettings to go back to the previous level. - String direction = intent.getStringExtra(EXTRAS_DIRECTION); - if (DIRECTION_BACK.equals(direction)) { - notifyToGoBack(context, Uri.parse(intent.getStringExtra(EXTRAS_SLICE_URI))); + switch (action) { + case ACTION_TOGGLE_CHANGED: + if (CEC.equals(intent.getStringExtra(TOGGLE_TYPE))) { + PowerUtils.enableCecControl(context, isChecked); + context.getContentResolver().notifyChange(CEC_SLICE_URI, null); + context.getContentResolver().notifyChange(GENERAL_SLICE_URI, null); + } + break; + case ACTION_BACK_AND_UPDATE_SLICE: + notifyToGoBack(context, Uri.parse(intent.getStringExtra(EXTRAS_SLICE_URI))); + case ACTION_UPDATE_SLICE: + ArrayList<String> uris = intent.getStringArrayListExtra(PARAM_URIS); + uris.forEach(uri -> { + context.getContentResolver().notifyChange(Uri.parse(uri), null); + }); + default: + // no-op } } + + public static PendingIntent updateSliceIntent( + Context context, int requestCode, ArrayList<String> uris) { + Intent i = new Intent(context, SliceBroadcastReceiver.class) + .setAction(ACTION_UPDATE_SLICE) + .putStringArrayListExtra(PARAM_URIS, uris); + return PendingIntent.getBroadcast(context, requestCode, i, + PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); + } + + public static PendingIntent backAndUpdateSliceIntent( + Context context, int requestCode, ArrayList<String> uris, String navigatingBackUri) { + Intent i = new Intent(context, SliceBroadcastReceiver.class) + .setAction(ACTION_BACK_AND_UPDATE_SLICE) + .putStringArrayListExtra(PARAM_URIS, uris) + .putExtra(EXTRAS_SLICE_URI, navigatingBackUri); + return PendingIntent.getBroadcast(context, requestCode, i, + PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); + } } |