diff options
author | Vladimir Komsiyski <vladokom@google.com> | 2023-12-12 11:19:12 +0100 |
---|---|---|
committer | Vladimir Komsiyski <vladokom@google.com> | 2023-12-12 14:11:06 +0100 |
commit | 86612a3c1120531d2575479f7a1f7ebcdc7972c0 (patch) | |
tree | 389b8ef423f4d397dd09c2d4ed327db193b3227d | |
parent | 97eda2dcf48de6cb9d9817160f6a40caaeffe18e (diff) | |
download | development-86612a3c1120531d2575479f7a1f7ebcdc7972c0.tar.gz |
Fix a bunch of warnings in VDM Demo apps.
- Reduce ImmutableSet/List usage as it's not supported
well in Android.
- Method invocation ... may produce NPE
- Missing nullability annotations
Fix: 314478048
Test: manual
Change-Id: I7920471b0320144467054ee540d246ebe8c6c4cc
18 files changed, 97 insertions, 105 deletions
diff --git a/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/DisplayAdapter.java b/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/DisplayAdapter.java index e3376cdcf..730e73d83 100644 --- a/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/DisplayAdapter.java +++ b/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/DisplayAdapter.java @@ -41,6 +41,7 @@ import com.example.android.vdmdemo.common.RemoteIo; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; final class DisplayAdapter extends RecyclerView.Adapter<DisplayHolder> { @@ -161,7 +162,7 @@ final class DisplayAdapter extends RecyclerView.Adapter<DisplayHolder> { mRotateButton.setEnabled(rotationDegrees == 0 || resize); // Make sure the rotation is visible. - View strut = itemView.findViewById(R.id.strut); + View strut = itemView.requireViewById(R.id.strut); ViewGroup.LayoutParams layoutParams = strut.getLayoutParams(); layoutParams.width = Math.max(mTextureView.getWidth(), mTextureView.getHeight()); strut.setLayoutParams(layoutParams); @@ -220,16 +221,16 @@ final class DisplayAdapter extends RecyclerView.Adapter<DisplayHolder> { Log.v(TAG, "Binding DisplayHolder for display " + mDisplayId + " to position " + position); - mDisplayTitle = itemView.findViewById(R.id.display_title); - mTextureView = itemView.findViewById(R.id.remote_display_view); + mDisplayTitle = itemView.requireViewById(R.id.display_title); + mTextureView = itemView.requireViewById(R.id.remote_display_view); + final View displayHeader = itemView.requireViewById(R.id.display_header); mFocusListener = focusedDisplayId -> { - View displayFocusIndicator = itemView.findViewById(R.id.display_header); if (focusedDisplayId == mDisplayId) { - displayFocusIndicator.setBackgroundResource(R.drawable.focus_frame); + displayHeader.setBackgroundResource(R.drawable.focus_frame); } else { - displayFocusIndicator.setBackground(null); + displayHeader.setBackground(null); } }; mInputManager.addFocusListener(mFocusListener); @@ -239,14 +240,15 @@ final class DisplayAdapter extends RecyclerView.Adapter<DisplayHolder> { setDisplayTitle(""); - View closeButton = itemView.findViewById(R.id.display_close); + View closeButton = itemView.requireViewById(R.id.display_close); closeButton.setOnClickListener( - v -> ((DisplayAdapter) getBindingAdapter()).removeDisplay(mDisplayId)); + v -> ((DisplayAdapter) Objects.requireNonNull(getBindingAdapter())) + .removeDisplay(mDisplayId)); - View backButton = itemView.findViewById(R.id.display_back); + View backButton = itemView.requireViewById(R.id.display_back); backButton.setOnClickListener(v -> mInputManager.sendBack(mDisplayId)); - View homeButton = itemView.findViewById(R.id.display_home); + View homeButton = itemView.requireViewById(R.id.display_home); if (remoteDisplay.isHomeSupported()) { homeButton.setVisibility(View.VISIBLE); homeButton.setOnClickListener(v -> mInputManager.sendHome(mDisplayId)); @@ -254,27 +256,23 @@ final class DisplayAdapter extends RecyclerView.Adapter<DisplayHolder> { homeButton.setVisibility(View.GONE); } - mRotateButton = itemView.findViewById(R.id.display_rotate); - mRotateButton.setOnClickListener( - v -> { - // This rotation is simply resizing the display with width with height - // swapped. - mDisplayController.setSurface( - mSurface, - /* width= */ mTextureView.getHeight(), - /* height= */ mTextureView.getWidth()); - rotateDisplay( - mTextureView.getWidth() > mTextureView.getHeight() ? 90 : -90, - true); - }); + mRotateButton = itemView.requireViewById(R.id.display_rotate); + mRotateButton.setOnClickListener(v -> { + // This rotation is simply resizing the display with width with height swapped. + mDisplayController.setSurface( + mSurface, + /* width= */ mTextureView.getHeight(), + /* height= */ mTextureView.getWidth()); + rotateDisplay(mTextureView.getWidth() > mTextureView.getHeight() ? 90 : -90, true); + }); - View resizeButton = itemView.findViewById(R.id.display_resize); + View resizeButton = itemView.requireViewById(R.id.display_resize); resizeButton.setOnTouchListener((v, event) -> { if (event.getAction() != MotionEvent.ACTION_DOWN) { return false; } - int maxSize = itemView.getHeight() - - itemView.findViewById(R.id.display_header).getHeight(); + int maxSize = itemView.getHeight() - displayHeader.getHeight() + - itemView.getPaddingTop() - itemView.getPaddingBottom(); mRecyclerView.startResizing( mTextureView, event, maxSize, DisplayHolder.this::resizeDisplay); return true; diff --git a/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/DisplayController.java b/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/DisplayController.java index 542bf1332..cf4011f79 100644 --- a/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/DisplayController.java +++ b/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/DisplayController.java @@ -44,10 +44,6 @@ final class DisplayController { mDpi = dpi; } - int getDpi() { - return mDpi; - } - void close() { mRemoteIo.sendMessage( RemoteEvent.newBuilder() diff --git a/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/DpadFragment.java b/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/DpadFragment.java index 878d750ff..867bd15cd 100644 --- a/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/DpadFragment.java +++ b/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/DpadFragment.java @@ -54,7 +54,7 @@ public final class DpadFragment extends Hilt_DpadFragment { // Set the callback for all the buttons // Note: the onClick XML attribute cannot be used with fragments, only activities. for (int buttonId : BUTTONS) { - ImageButton button = view.findViewById(buttonId); + ImageButton button = view.requireViewById(buttonId); button.setOnTouchListener(this::onDpadButtonClick); } diff --git a/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/ImmersiveActivity.java b/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/ImmersiveActivity.java index 8841c0515..6da311e4b 100644 --- a/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/ImmersiveActivity.java +++ b/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/ImmersiveActivity.java @@ -97,7 +97,7 @@ public class ImmersiveActivity extends Hilt_ImmersiveActivity { mDisplayController = new DisplayController(DEFAULT_DISPLAY, mRemoteIo); mDisplayController.setDpi((int) (getResources().getDisplayMetrics().density * DIP_TO_DPI)); - TextureView textureView = findViewById(R.id.immersive_surface_view); + TextureView textureView = requireViewById(R.id.immersive_surface_view); textureView.setOnTouchListener( (v, event) -> { if (event.getDevice().supportsSource(InputDevice.SOURCE_TOUCHSCREEN)) { diff --git a/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/MainActivity.java b/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/MainActivity.java index 9e6246410..6354ef9d8 100644 --- a/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/MainActivity.java +++ b/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/MainActivity.java @@ -86,11 +86,11 @@ public class MainActivity extends Hilt_MainActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - Toolbar toolbar = findViewById(R.id.main_tool_bar); + Toolbar toolbar = requireViewById(R.id.main_tool_bar); toolbar.setOverflowIcon(getDrawable(R.drawable.settings)); setSupportActionBar(toolbar); - ClientView displaysView = findViewById(R.id.displays); + ClientView displaysView = requireViewById(R.id.displays); displaysView.setLayoutManager( new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)); displaysView.setItemAnimator(null); @@ -194,12 +194,12 @@ public class MainActivity extends Hilt_MainActivity { } private void onDisplayFocusChange(int displayId) { - findViewById(R.id.dpad_fragment_container) + requireViewById(R.id.dpad_fragment_container) .setVisibility( mSettings.dpadEnabled && displayId != Display.INVALID_DISPLAY ? View.VISIBLE : View.GONE); - findViewById(R.id.nav_touchpad_fragment_container) + requireViewById(R.id.nav_touchpad_fragment_container) .setVisibility( mSettings.navTouchpadEnabled && displayId != Display.INVALID_DISPLAY ? View.VISIBLE diff --git a/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/NavTouchpadFragment.java b/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/NavTouchpadFragment.java index 427d01719..b78b9675a 100644 --- a/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/NavTouchpadFragment.java +++ b/samples/VirtualDeviceManager/client/src/com/example/android/vdmdemo/client/NavTouchpadFragment.java @@ -43,7 +43,7 @@ public final class NavTouchpadFragment extends Hilt_NavTouchpadFragment { LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_nav_touchpad, container, false); - TextView navTouchpad = view.findViewById(R.id.nav_touchpad); + TextView navTouchpad = view.requireViewById(R.id.nav_touchpad); navTouchpad.setOnTouchListener( (v, event) -> { mInputManager.sendInputEventToFocusedDisplay( diff --git a/samples/VirtualDeviceManager/common/res/values/strings.xml b/samples/VirtualDeviceManager/common/res/values/strings.xml index 92eaebd7b..a7278d355 100644 --- a/samples/VirtualDeviceManager/common/res/values/strings.xml +++ b/samples/VirtualDeviceManager/common/res/values/strings.xml @@ -3,4 +3,5 @@ <string name="disconnected" translatable="false">Disconnected</string> <string name="connecting" translatable="false">Connecting to %s...</string> <string name="connected" translatable="false">Connected to %s</string> + <string name="error" translatable="false">Error: %s</string> </resources> diff --git a/samples/VirtualDeviceManager/common/src/com/example/android/vdmdemo/common/ConnectivityFragment.java b/samples/VirtualDeviceManager/common/src/com/example/android/vdmdemo/common/ConnectivityFragment.java index a28abfb43..f0660c592 100644 --- a/samples/VirtualDeviceManager/common/src/com/example/android/vdmdemo/common/ConnectivityFragment.java +++ b/samples/VirtualDeviceManager/common/src/com/example/android/vdmdemo/common/ConnectivityFragment.java @@ -16,16 +16,20 @@ package com.example.android.vdmdemo.common; +import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.util.TypedValue; import android.view.View; import android.widget.TextView; +import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import dagger.hilt.android.AndroidEntryPoint; +import java.util.Objects; + import javax.inject.Inject; /** Fragment that holds the connectivity status UI. */ @@ -41,27 +45,22 @@ public final class ConnectivityFragment extends Hilt_ConnectivityFragment { new ConnectionManager.ConnectionCallback() { @Override public void onConnecting(String remoteDeviceName) { - updateStatus( - getContext().getString(R.string.connecting, remoteDeviceName), - mDefaultBackgroundColor); + updateStatus(mDefaultBackgroundColor, R.string.connecting, remoteDeviceName); } @Override public void onConnected(String remoteDeviceName) { - updateStatus( - getContext().getString(R.string.connected, remoteDeviceName), - Color.GREEN); + updateStatus(Color.GREEN, R.string.connected, remoteDeviceName); } @Override public void onDisconnected() { - updateStatus( - getContext().getString(R.string.disconnected), mDefaultBackgroundColor); + updateStatus(mDefaultBackgroundColor, R.string.disconnected); } @Override public void onError(String message) { - updateStatus(message, Color.RED); + updateStatus(Color.RED, R.string.error, message); } }; @@ -70,10 +69,10 @@ public final class ConnectivityFragment extends Hilt_ConnectivityFragment { } @Override - public void onViewCreated(View view, Bundle bundle) { + public void onViewCreated(@NonNull View view, Bundle bundle) { super.onViewCreated(view, bundle); - mStatus = getActivity().findViewById(R.id.connection_status); + mStatus = Objects.requireNonNull(getActivity()).requireViewById(R.id.connection_status); TypedValue background = new TypedValue(); getActivity() @@ -105,9 +104,10 @@ public final class ConnectivityFragment extends Hilt_ConnectivityFragment { mConnectionManager.removeConnectionCallback(mConnectionCallback); } - private void updateStatus(String message, int backgroundColor) { - getActivity().runOnUiThread(() -> { - mStatus.setText(message); + private void updateStatus(int backgroundColor, int resId, Object... formatArgs) { + Activity activity = Objects.requireNonNull(getActivity()); + activity.runOnUiThread(() -> { + mStatus.setText(activity.getString(resId, formatArgs)); mStatus.setBackgroundColor(backgroundColor); }); } diff --git a/samples/VirtualDeviceManager/common/src/com/example/android/vdmdemo/common/VideoManager.java b/samples/VirtualDeviceManager/common/src/com/example/android/vdmdemo/common/VideoManager.java index 2bfa60e92..3df356655 100644 --- a/samples/VirtualDeviceManager/common/src/com/example/android/vdmdemo/common/VideoManager.java +++ b/samples/VirtualDeviceManager/common/src/com/example/android/vdmdemo/common/VideoManager.java @@ -28,6 +28,7 @@ import android.util.Log; import android.view.Surface; import androidx.annotation.GuardedBy; +import androidx.annotation.NonNull; import com.example.android.vdmdemo.common.RemoteEventProto.DisplayFrame; import com.example.android.vdmdemo.common.RemoteEventProto.RemoteEvent; @@ -41,6 +42,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.ByteBuffer; +import java.util.Objects; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.atomic.AtomicBoolean; @@ -187,12 +189,13 @@ public class VideoManager { private final class MediaCodecCallback extends MediaCodec.Callback { @Override - public void onInputBufferAvailable(MediaCodec codec, int i) { + public void onInputBufferAvailable(@NonNull MediaCodec codec, int i) { mFreeInputBuffers.add(i); } @Override - public void onOutputBufferAvailable(MediaCodec codec, int i, BufferInfo bufferInfo) { + public void onOutputBufferAvailable( + @NonNull MediaCodec codec, int i, @NonNull BufferInfo bufferInfo) { synchronized (mCodecLock) { if (mMediaCodec == null) { return; @@ -200,7 +203,7 @@ public class VideoManager { if (mMediaCodec.getCodecInfo().isEncoder()) { ByteBuffer buffer = mMediaCodec.getOutputBuffer(i); byte[] data = new byte[bufferInfo.size]; - buffer.get(data, bufferInfo.offset, bufferInfo.size); + Objects.requireNonNull(buffer).get(data, bufferInfo.offset, bufferInfo.size); mMediaCodec.releaseOutputBuffer(i, false); if (mRecordEncoderOutput) { mStorageFile.writeOutputFile(data); @@ -216,10 +219,11 @@ public class VideoManager { } @Override - public void onError(MediaCodec mediaCodec, CodecException e) {} + public void onError(@NonNull MediaCodec mediaCodec, @NonNull CodecException e) {} @Override - public void onOutputFormatChanged(MediaCodec mediaCodec, MediaFormat mediaFormat) {} + public void onOutputFormatChanged( + @NonNull MediaCodec mediaCodec, @NonNull MediaFormat mediaFormat) {} } private class DecoderThread extends Thread { @@ -245,7 +249,7 @@ public class VideoManager { } ByteBuffer inBuffer = mMediaCodec.getInputBuffer(inputBuffer); byte[] data = event.getDisplayFrame().getFrameData().toByteArray(); - inBuffer.put(data); + Objects.requireNonNull(inBuffer).put(data); if (mRecordEncoderOutput) { mStorageFile.writeOutputFile(data); } diff --git a/samples/VirtualDeviceManager/demos/src/com/example/android/vdmdemo/demos/LatencyDemoActivity.java b/samples/VirtualDeviceManager/demos/src/com/example/android/vdmdemo/demos/LatencyDemoActivity.java index 49d3858f2..74e613744 100644 --- a/samples/VirtualDeviceManager/demos/src/com/example/android/vdmdemo/demos/LatencyDemoActivity.java +++ b/samples/VirtualDeviceManager/demos/src/com/example/android/vdmdemo/demos/LatencyDemoActivity.java @@ -42,7 +42,7 @@ public final class LatencyDemoActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.latency_demo_activity); - View counter = findViewById(R.id.counter); + View counter = requireViewById(R.id.counter); mScheduledFuture = mExecutor.scheduleAtFixedRate( diff --git a/samples/VirtualDeviceManager/demos/src/com/example/android/vdmdemo/demos/RotationDemoActivity.java b/samples/VirtualDeviceManager/demos/src/com/example/android/vdmdemo/demos/RotationDemoActivity.java index b489b8441..07158cba9 100644 --- a/samples/VirtualDeviceManager/demos/src/com/example/android/vdmdemo/demos/RotationDemoActivity.java +++ b/samples/VirtualDeviceManager/demos/src/com/example/android/vdmdemo/demos/RotationDemoActivity.java @@ -35,7 +35,7 @@ public final class RotationDemoActivity extends AppCompatActivity { @Override public void onResume() { super.onResume(); - ((TextView) findViewById(R.id.current_orientation)) + ((TextView) requireViewById(R.id.current_orientation)) .setText(getString(R.string.current_orientation, getOrientationString())); } diff --git a/samples/VirtualDeviceManager/demos/src/com/example/android/vdmdemo/demos/SensorDemoActivity.java b/samples/VirtualDeviceManager/demos/src/com/example/android/vdmdemo/demos/SensorDemoActivity.java index 80cde97aa..aada6ac5f 100644 --- a/samples/VirtualDeviceManager/demos/src/com/example/android/vdmdemo/demos/SensorDemoActivity.java +++ b/samples/VirtualDeviceManager/demos/src/com/example/android/vdmdemo/demos/SensorDemoActivity.java @@ -32,6 +32,7 @@ import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import java.util.List; +import java.util.Objects; /** * A minimal activity switching between sensor data from the default device and the virtual devices. @@ -52,7 +53,7 @@ public final class SensorDemoActivity extends AppCompatActivity implements Senso setContentView(R.layout.sensor_demo_activity); - mBeam = findViewById(R.id.beam); + mBeam = requireViewById(R.id.beam); mVirtualDeviceManager = getSystemService(VirtualDeviceManager.class); mSensorManager = getSystemService(SensorManager.class); @@ -75,7 +76,8 @@ public final class SensorDemoActivity extends AppCompatActivity implements Senso if (context.getDeviceId() == Context.DEVICE_ID_DEFAULT) { deviceName = DEVICE_NAME_DEFAULT; } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { - deviceName = mVirtualDeviceManager.getVirtualDevice(context.getDeviceId()).getName(); + VirtualDevice device = mVirtualDeviceManager.getVirtualDevice(context.getDeviceId()); + deviceName = Objects.requireNonNull(device).getName(); } else { for (VirtualDevice virtualDevice : mVirtualDeviceManager.getVirtualDevices()) { if (virtualDevice.getDeviceId() == context.getDeviceId()) { @@ -84,7 +86,7 @@ public final class SensorDemoActivity extends AppCompatActivity implements Senso } } } - TextView currentDevice = findViewById(R.id.current_device); + TextView currentDevice = requireViewById(R.id.current_device); currentDevice.setText(context.getString(R.string.current_device, deviceName)); } @@ -119,6 +121,7 @@ public final class SensorDemoActivity extends AppCompatActivity implements Senso updateCurrentDeviceTextView(mDeviceContext); mSensorManager = mDeviceContext.getSystemService(SensorManager.class); + Objects.requireNonNull(mSensorManager); Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); if (sensor != null) { mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI); diff --git a/samples/VirtualDeviceManager/demos/src/com/example/android/vdmdemo/demos/VibrationDemoActivity.java b/samples/VirtualDeviceManager/demos/src/com/example/android/vdmdemo/demos/VibrationDemoActivity.java index 4f7da0d18..49fe0d436 100644 --- a/samples/VirtualDeviceManager/demos/src/com/example/android/vdmdemo/demos/VibrationDemoActivity.java +++ b/samples/VirtualDeviceManager/demos/src/com/example/android/vdmdemo/demos/VibrationDemoActivity.java @@ -35,6 +35,7 @@ import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import java.util.List; +import java.util.Objects; /** A minimal activity switching between vibration on the default device and the virtual devices. */ public final class VibrationDemoActivity extends AppCompatActivity { @@ -74,7 +75,8 @@ public final class VibrationDemoActivity extends AppCompatActivity { if (context.getDeviceId() == Context.DEVICE_ID_DEFAULT) { deviceName = DEVICE_NAME_DEFAULT; } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { - deviceName = mVirtualDeviceManager.getVirtualDevice(context.getDeviceId()).getName(); + VirtualDevice device = mVirtualDeviceManager.getVirtualDevice(context.getDeviceId()); + deviceName = Objects.requireNonNull(device).getName(); } else { for (VirtualDevice virtualDevice : mVirtualDeviceManager.getVirtualDevices()) { if (virtualDevice.getDeviceId() == context.getDeviceId()) { @@ -83,7 +85,7 @@ public final class VibrationDemoActivity extends AppCompatActivity { } } } - TextView currentDevice = findViewById(R.id.current_device); + TextView currentDevice = requireViewById(R.id.current_device); currentDevice.setText(context.getString(R.string.current_device, deviceName)); } diff --git a/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/AudioStreamer.java b/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/AudioStreamer.java index a6aeb695d..a6fcb08ed 100644 --- a/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/AudioStreamer.java +++ b/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/AudioStreamer.java @@ -41,7 +41,6 @@ import com.example.android.vdmdemo.common.RemoteEventProto.RemoteEvent; import com.example.android.vdmdemo.common.RemoteEventProto.StartAudio; import com.example.android.vdmdemo.common.RemoteEventProto.StopAudio; import com.example.android.vdmdemo.common.RemoteIo; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.protobuf.ByteString; @@ -51,8 +50,8 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Objects; +import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.stream.Collector; import java.util.stream.Collectors; import javax.inject.Inject; @@ -201,8 +200,7 @@ final class AudioStreamer implements AutoCloseable { // The ghost audio record needs to be kept alive, releasing it would cause // destruction of remote submix instances and potential problems when updating the // UID-based render policy pointing to the remote submix device. - ImmutableList<AudioDeviceInfo> preexistingRemoteSubmixDevices = - getRemoteSubmixDevices(); + List<AudioDeviceInfo> preexistingRemoteSubmixDevices = getRemoteSubmixDevices(); mGhostRecord = mAudioPolicy.createAudioRecordSink(audioMix); mGhostRecord.startRecording(); mRemoteSubmixDevice = getNewRemoteSubmixAudioDevice(preexistingRemoteSubmixDevices); @@ -215,15 +213,15 @@ final class AudioStreamer implements AutoCloseable { private @Nullable AudioDeviceInfo getNewRemoteSubmixAudioDevice( Collection<AudioDeviceInfo> preexistingDevices) { - ImmutableSet<String> preexistingAddresses = + Set<String> preexistingAddresses = preexistingDevices.stream() .map(AudioDeviceInfo::getAddress) - .collect(toImmutableSet()); + .collect(Collectors.toSet()); - ImmutableList<AudioDeviceInfo> newDevices = + List<AudioDeviceInfo> newDevices = getRemoteSubmixDevices().stream() .filter(dev -> !preexistingAddresses.contains(dev.getAddress())) - .collect(toImmutableList()); + .collect(Collectors.toList()); if (newDevices.size() > 1) { Log.e(TAG, "There's more than 1 new remote submix device"); @@ -236,7 +234,7 @@ final class AudioStreamer implements AutoCloseable { return getOnlyElement(newDevices); } - public void updateVdmUids(ImmutableSet<Integer> uids) { + public void updateVdmUids(Set<Integer> uids) { Log.w(TAG, "Updating mixing rule to reroute uids " + uids); mHandler.post( () -> { @@ -263,7 +261,7 @@ final class AudioStreamer implements AutoCloseable { // That's inefficient and leads to audio leaks. But this is the most correct way to do this at // this time. @GuardedBy("mLock") - private void updateAudioPolicies(ImmutableSet<Integer> uids) { + private void updateAudioPolicies(Set<Integer> uids) { // TODO(b/293279299) Use Flagged API // if (com.android.media.audio.flags.Flags.FLAG_AUDIO_POLICY_UPDATE_MIXING_RULES_API && // uidAudioMix != null && (!reroutedUids.isEmpty() && !uids.isEmpty())) { @@ -341,10 +339,10 @@ final class AudioStreamer implements AutoCloseable { return builder.build(); } - private ImmutableList<AudioDeviceInfo> getRemoteSubmixDevices() { + private List<AudioDeviceInfo> getRemoteSubmixDevices() { return Arrays.stream(mAudioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS)) .filter(AudioStreamer::deviceIsRemoteSubmixOut) - .collect(toImmutableList()); + .collect(Collectors.toList()); } private static boolean deviceIsRemoteSubmixOut(AudioDeviceInfo info) { @@ -404,12 +402,4 @@ final class AudioStreamer implements AutoCloseable { mIsRunning.set(false); } } - - private static <E> Collector<E, ?, ImmutableList<E>> toImmutableList() { - return Collectors.collectingAndThen(Collectors.toList(), ImmutableList::copyOf); - } - - private static <E> Collector<E, ?, ImmutableSet<E>> toImmutableSet() { - return Collectors.collectingAndThen(Collectors.toList(), ImmutableSet::copyOf); - } } diff --git a/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/LauncherAdapter.java b/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/LauncherAdapter.java index 31f854ae9..3b159d991 100644 --- a/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/LauncherAdapter.java +++ b/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/LauncherAdapter.java @@ -88,12 +88,12 @@ final class LauncherAdapter extends BaseAdapter { LayoutInflater.from(parent.getContext()) .inflate(R.layout.launcher_grid_item, parent, false); } - ImageView imageView = convertView.findViewById(R.id.app_icon); + ImageView imageView = convertView.requireViewById(R.id.app_icon); final Drawable background = new ShapeDrawable(new OvalShape()); imageView.setBackground(background); imageView.setImageDrawable(img); - TextView textView = convertView.findViewById(R.id.app_title); + TextView textView = convertView.requireViewById(R.id.app_title); textView.setText(ri.loadLabel(mPackageManager)); textView.setTextColor(mTextColor); return convertView; diff --git a/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/MainActivity.java b/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/MainActivity.java index 00327519d..01247ce8d 100644 --- a/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/MainActivity.java +++ b/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/MainActivity.java @@ -91,7 +91,7 @@ public class MainActivity extends Hilt_MainActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - Toolbar toolbar = findViewById(R.id.main_tool_bar); + Toolbar toolbar = requireViewById(R.id.main_tool_bar); toolbar.setOverflowIcon(getDrawable(R.drawable.settings)); setSupportActionBar(toolbar); diff --git a/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/RunningVdmUidsTracker.java b/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/RunningVdmUidsTracker.java index 0dee5c1c4..20505893e 100644 --- a/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/RunningVdmUidsTracker.java +++ b/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/RunningVdmUidsTracker.java @@ -26,14 +26,13 @@ import android.util.Log; import androidx.annotation.GuardedBy; import androidx.annotation.NonNull; -import com.google.common.collect.ImmutableSet; - import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Objects; import java.util.Optional; -import java.util.stream.Collector; +import java.util.Set; import java.util.stream.Collectors; final class RunningVdmUidsTracker implements ActivityListener { @@ -48,7 +47,7 @@ final class RunningVdmUidsTracker implements ActivityListener { private final HashMap<Integer, HashSet<Integer>> mDisplayIdToRunningUids = new HashMap<>(); @GuardedBy("mLock") - private ImmutableSet<Integer> mRunningVdmUids = ImmutableSet.of(); + private Set<Integer> mRunningVdmUids = Collections.emptySet(); RunningVdmUidsTracker(@NonNull Context context, @NonNull AudioStreamer audioStreamer) { mPackageManager = Objects.requireNonNull(context).getPackageManager(); @@ -64,7 +63,7 @@ final class RunningVdmUidsTracker implements ActivityListener { return; } - ImmutableSet<Integer> updatedUids; + final Set<Integer> updatedUids; synchronized (mLock) { HashSet<Integer> displayUidSet = mDisplayIdToRunningUids.computeIfAbsent(displayId, k -> new HashSet<>()); @@ -72,7 +71,7 @@ final class RunningVdmUidsTracker implements ActivityListener { mRunningVdmUids = mDisplayIdToRunningUids.values().stream() .flatMap(Collection::stream) - .collect(toImmutableSet()); + .collect(Collectors.toSet()); updatedUids = mRunningVdmUids; } @@ -81,15 +80,15 @@ final class RunningVdmUidsTracker implements ActivityListener { @Override public void onDisplayEmpty(int displayId) { - ImmutableSet<Integer> uidsBefore; - ImmutableSet<Integer> uidsAfter; + Set<Integer> uidsBefore; + Set<Integer> uidsAfter; synchronized (mLock) { uidsBefore = mRunningVdmUids; mDisplayIdToRunningUids.remove(displayId); mRunningVdmUids = mDisplayIdToRunningUids.values().stream() .flatMap(Collection::stream) - .collect(toImmutableSet()); + .collect(Collectors.toSet()); uidsAfter = mRunningVdmUids; } @@ -106,8 +105,4 @@ final class RunningVdmUidsTracker implements ActivityListener { return Optional.empty(); } } - - private static <E> Collector<E, ?, ImmutableSet<E>> toImmutableSet() { - return Collectors.collectingAndThen(Collectors.toList(), ImmutableSet::copyOf); - } } diff --git a/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/VdmService.java b/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/VdmService.java index 24635b311..deea50957 100644 --- a/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/VdmService.java +++ b/samples/VirtualDeviceManager/host/src/com/example/android/vdmdemo/host/VdmService.java @@ -159,7 +159,7 @@ public final class VdmService extends Hilt_VdmService { CHANNEL_ID, "VDM Service Channel", NotificationManager.IMPORTANCE_LOW); notificationChannel.enableVibration(false); NotificationManager notificationManager = getSystemService(NotificationManager.class); - notificationManager.createNotificationChannel(notificationChannel); + Objects.requireNonNull(notificationManager).createNotificationChannel(notificationChannel); Intent openIntent = new Intent(this, MainActivity.class); PendingIntent pendingIntentOpen = @@ -193,7 +193,7 @@ public final class VdmService extends Hilt_VdmService { mConnectionManager.addConnectionCallback(mConnectionCallback); mDisplayManager = getSystemService(DisplayManager.class); - mDisplayManager.registerDisplayListener(mDisplayListener, null); + Objects.requireNonNull(mDisplayManager).registerDisplayListener(mDisplayListener, null); mRemoteIo.addMessageConsumer(mRemoteEventConsumer); @@ -241,8 +241,9 @@ public final class VdmService extends Hilt_VdmService { } private void associateAndCreateVirtualDevice() { - CompanionDeviceManager cdm = getSystemService(CompanionDeviceManager.class); - RoleManager rm = getSystemService(RoleManager.class); + CompanionDeviceManager cdm = + Objects.requireNonNull(getSystemService(CompanionDeviceManager.class)); + RoleManager rm = Objects.requireNonNull(getSystemService(RoleManager.class)); final String deviceProfile = mSettings.deviceStreaming ? AssociationRequest.DEVICE_PROFILE_NEARBY_DEVICE_STREAMING @@ -255,6 +256,7 @@ public final class VdmService extends Hilt_VdmService { if (!rm.isRoleHeld(deviceProfile)) { cdm.disassociate(associationInfo.getId()); } else if (Objects.equals(associationInfo.getPackageName(), getPackageName()) + && associationInfo.getDisplayName() != null && Objects.equals( associationInfo.getDisplayName().toString(), mDeviceCapabilities.getDeviceName())) { @@ -346,7 +348,8 @@ public final class VdmService extends Hilt_VdmService { } } - VirtualDeviceManager vdm = getSystemService(VirtualDeviceManager.class); + VirtualDeviceManager vdm = + Objects.requireNonNull(getSystemService(VirtualDeviceManager.class)); mVirtualDevice = vdm.createVirtualDevice(associationInfo.getId(), virtualDeviceBuilder.build()); if (mRemoteSensorManager != null) { |