Skip to content

Commit

Permalink
メインスレッドに振り替える処理をライブラリに追加
Browse files Browse the repository at this point in the history
  • Loading branch information
tnoho committed Feb 18, 2024
1 parent 9135f43 commit 435c19e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package jp.shiguredo.sora.audiomanager;

import android.content.Context;

class MainThreadWrapper extends SoraAudioManager {
private SoraAudioManager soraAudioManager;

private static class OnChangeRouteObserver implements SoraAudioManager.OnChangeRouteObserver {
SoraAudioManager.OnChangeRouteObserver observer;

public OnChangeRouteObserver(SoraAudioManager.OnChangeRouteObserver observer) {
this.observer = observer;
}

public void OnChangeRoute() {
//メインスレッドでない場合はメインスレッドで再実行
if (!SoraThreadUtils.runOnMainThread(this::OnChangeRoute)) {
return;
}

if (this.observer == null) {
return;
}
this.observer.OnChangeRoute();
}
}

MainThreadWrapper(Context context) {
//メインスレッドでない場合はメインスレッドで再実行
if (!SoraThreadUtils.runOnMainThread(() -> {
this.soraAudioManager = SoraAudioManagerFactory.create(context);
})) {
return;
}
this.soraAudioManager = SoraAudioManagerFactory.create(context);
}

@Override
public void start(SoraAudioManager.OnChangeRouteObserver observer) {
//メインスレッドでない場合はメインスレッドで再実行
if (!SoraThreadUtils.runOnMainThread(() -> start(observer))) {
return;
}
soraAudioManager.start(new OnChangeRouteObserver(observer));
}

@Override
public void stop() {
//メインスレッドでない場合はメインスレッドで再実行
if (!SoraThreadUtils.runOnMainThread(this::stop)) {
return;
}
soraAudioManager.stop();
}

@Override
public void setHandsfree(boolean on) {
//メインスレッドでない場合はメインスレッドで再実行
if (!SoraThreadUtils.runOnMainThread(() -> setHandsfree(on))) {
return;
}
soraAudioManager.setHandsfree(on);
}

@Override
public boolean isHandsfree() {
return soraAudioManager.isHandsfree();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@

public class SoraAudioManager {
private static final String TAG = "SoraAudioManager";
protected final Context context;
protected final AudioManager audioManager;
protected final BroadcastReceiver wiredHeadsetReceiver;
protected Context context;
protected AudioManager audioManager;
protected BroadcastReceiver wiredHeadsetReceiver;
protected boolean running;
private int savedAudioMode = AudioManager.MODE_INVALID;
private boolean savedIsMicrophoneMute;
Expand All @@ -54,6 +54,9 @@ public void onReceive(Context context, Intent intent) {
}
}

protected SoraAudioManager() {
}

protected SoraAudioManager(Context context) {
SoraThreadUtils.checkIsOnMainThread();
this.context = context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ public static SoraAudioManager create(Context context) {
return SoraAudioManagerLegacy.create(context);
}
}

public static SoraAudioManager createWithMainThreadWrapper(Context context) {
return new MainThreadWrapper(context);
}
}

0 comments on commit 435c19e

Please sign in to comment.