Skip to content

Commit

Permalink
Prevent host lookup from hanging
Browse files Browse the repository at this point in the history
  • Loading branch information
kescherCode committed Feb 17, 2022
1 parent 6fa1677 commit 7a5c843
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ android {

// version code: each number in the version string gets two digits (leading zero removed for first one). one additional digit for pre-release versions.
// So: 2.0.0 is: 02 + 00 + 00 + 0
versionCode 200020
versionName "2.0.2"
versionCode 200030
versionName "2.0.3"
}

buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import java.util.Arrays;
import java.util.List;

import at.kescher.pulsedroid.adapters.DurationAdapter;
import at.kescher.pulsedroid.adapters.ChannelAdapter;
import at.kescher.pulsedroid.adapters.DurationAdapter;
import at.kescher.pulsedroid.adapters.SimpleRowAdapter;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class PulsePlaybackService extends Service implements PulsePlaybackWorker
private final IBinder binder = new LocalBinder();
private final Handler handler = new Handler(Looper.getMainLooper());

private NotificationManager notifManager;
private NotificationManager notificationManager;
private PowerManager.WakeLock wakeLock;
private PendingIntent stopPendingIntent;

Expand All @@ -56,7 +56,7 @@ public IBinder onBind(Intent intent) {

@Override
public void onCreate() {
notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
playState.setValue(PlayState.STOPPED);

Intent intent = new Intent(this, PulsePlaybackService.class)
Expand All @@ -69,7 +69,7 @@ public void onCreate() {
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "pulsedroid:wakelock");

if (Build.VERSION.SDK_INT >= 26) {
notifManager.createNotificationChannel(new NotificationChannel(
notificationManager.createNotificationChannel(new NotificationChannel(
getString(R.string.service_notification_channel),
getString(R.string.playback_service_label),
NotificationManager.IMPORTANCE_LOW));
Expand All @@ -92,7 +92,7 @@ public void onDestroy() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
stopForeground(true);
} else {
notifManager.cancel(NOTIFICATION);
notificationManager.cancel(NOTIFICATION);
}

stop();
Expand Down Expand Up @@ -227,7 +227,7 @@ private void notifyState(@NonNull PlayState state) {
throw new IllegalArgumentException();
}
Notification notif = buildNotification(statusResId).build();
notifManager.notify(NOTIFICATION, notif);
notificationManager.notify(NOTIFICATION, notif);
}

public boolean isStartable() {
Expand Down
24 changes: 19 additions & 5 deletions app/src/main/java/at/kescher/pulsedroid/PulsePlaybackWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
import java.time.Clock;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import at.kescher.pulsedroid.exception.StoppedException;
Expand Down Expand Up @@ -146,7 +152,7 @@ public void run() {
}
}

private void setup() throws IOException {
private void setup() throws IOException, ExecutionException, InterruptedException, TimeoutException {
// bytes per second = sample rate * 2 bytes per sample * 2 channels
byteRate = sampleRate * 2 * 2;

Expand Down Expand Up @@ -302,10 +308,18 @@ private void cleanup() {
* @throws IOException If connection to the host fails.
* @throws StoppedException If {@link #stopped} was set.
*/
private void connect() throws IOException {

// We may hang here to resolve a host name. No way to interrupt this so far.
InetAddress[] addresses = InetAddress.getAllByName(host);
private void connect() throws IOException, ExecutionException, InterruptedException, TimeoutException {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<InetAddress[]> future = executor.submit(() -> InetAddress.getAllByName(host));
InetAddress[] addresses;
try {
addresses = future.get(5, TimeUnit.SECONDS);
} catch (TimeoutException | ExecutionException | InterruptedException e) {
future.cancel(true);
throw e;
} finally {
executor.shutdownNow();
}
if (addresses.length == 0) {
throw new UnknownHostException("No addresses returned by InetAddress.getAllByName()");
}
Expand Down

0 comments on commit 7a5c843

Please sign in to comment.