#123 - some tests

This commit is contained in:
Thomas 2020-12-05 11:27:27 +01:00
parent 21c53d2b4d
commit 413792087e
3 changed files with 62 additions and 23 deletions

View File

@ -51,6 +51,7 @@ import org.jetbrains.annotations.NotNull;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.NetworkInterface; import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -83,14 +84,17 @@ import app.fedilab.fedilabtube.sqlite.Sqlite;
import app.fedilab.fedilabtube.sqlite.StoredInstanceDAO; import app.fedilab.fedilabtube.sqlite.StoredInstanceDAO;
import app.fedilab.fedilabtube.viewmodel.TimelineVM; import app.fedilab.fedilabtube.viewmodel.TimelineVM;
import es.dmoral.toasty.Toasty; import es.dmoral.toasty.Toasty;
import su.litvak.chromecast.api.v2.ChromeCast;
import su.litvak.chromecast.api.v2.ChromeCasts; import su.litvak.chromecast.api.v2.ChromeCasts;
import su.litvak.chromecast.api.v2.ChromeCastsListener;
import static app.fedilab.fedilabtube.MainActivity.TypeOfConnection.NORMAL; import static app.fedilab.fedilabtube.MainActivity.TypeOfConnection.NORMAL;
import static app.fedilab.fedilabtube.MainActivity.TypeOfConnection.SURFING; import static app.fedilab.fedilabtube.MainActivity.TypeOfConnection.SURFING;
import static app.fedilab.fedilabtube.PeertubeActivity.chromeCasts;
import static app.fedilab.fedilabtube.helper.Helper.peertubeInformation; import static app.fedilab.fedilabtube.helper.Helper.peertubeInformation;
import static app.fedilab.fedilabtube.helper.HelperAcadInstance.academies; import static app.fedilab.fedilabtube.helper.HelperAcadInstance.academies;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity implements ChromeCastsListener {
public static int PICK_INSTANCE = 5641; public static int PICK_INSTANCE = 5641;
@ -198,14 +202,51 @@ public class MainActivity extends AppCompatActivity {
return true; return true;
}; };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
@Override
public void newChromeCastDiscovered(ChromeCast chromeCast) {
if (chromeCasts == null) {
chromeCasts = new ArrayList<>();
chromeCasts.add(chromeCast);
} else {
boolean canBeAdded = true;
for (ChromeCast cast : chromeCasts) {
if (cast.getName().compareTo(chromeCast.getName()) == 0) {
canBeAdded = false;
break;
}
}
if (canBeAdded) {
chromeCasts.add(chromeCast);
}
}
}
@Override
public void chromeCastRemoved(ChromeCast chromeCast) {
}
@Override
public void onDestroy() {
super.onDestroy();
binding = null;
ChromeCasts.unregisterListener(this);
}
public void discoverCast() {
new Thread(() -> { new Thread(() -> {
if (chromeCasts != null) {
for (ChromeCast cast : chromeCasts) {
try {
cast.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
chromeCasts = null;
}
chromeCasts = new ArrayList<>();
try { try {
List<NetworkInterface> interfaces; List<NetworkInterface> interfaces;
interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
@ -215,12 +256,10 @@ public class MainActivity extends AppCompatActivity {
while (inetAddressEnumeration.hasMoreElements()) { while (inetAddressEnumeration.hasMoreElements()) {
InetAddress inetAddress = inetAddressEnumeration.nextElement(); InetAddress inetAddress = inetAddressEnumeration.nextElement();
ChromeCasts.restartDiscovery(inetAddress); ChromeCasts.restartDiscovery(inetAddress);
PeertubeActivity.chromeCasts = ChromeCasts.get();
int tryFind = 0; int tryFind = 0;
while (PeertubeActivity.chromeCasts.isEmpty() && tryFind < 5) { while (ChromeCasts.get().isEmpty() && tryFind < 5) {
try { try {
Thread.sleep(1000); Thread.sleep(1000);
PeertubeActivity.chromeCasts = ChromeCasts.get();
tryFind++; tryFind++;
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
} }
@ -235,6 +274,17 @@ public class MainActivity extends AppCompatActivity {
e.printStackTrace(); e.printStackTrace();
} }
}).start(); }).start();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
ChromeCastsListener chromeCastsListener = this;
ChromeCasts.registerListener(chromeCastsListener);
discoverCast();
Toolbar toolbar = findViewById(R.id.toolbar); Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);

View File

@ -168,7 +168,7 @@ import static app.fedilab.fedilabtube.helper.Helper.peertubeInformation;
import static com.google.android.exoplayer2.Player.MEDIA_ITEM_TRANSITION_REASON_AUTO; import static com.google.android.exoplayer2.Player.MEDIA_ITEM_TRANSITION_REASON_AUTO;
public class PeertubeActivity extends AppCompatActivity implements CommentListAdapter.AllCommentRemoved, Player.EventListener, VideoListener, TorrentListener, MenuAdapter.ItemClicked, MenuItemAdapter.ItemAction, ChromeCastsListener { public class PeertubeActivity extends AppCompatActivity implements CommentListAdapter.AllCommentRemoved, Player.EventListener, VideoListener, TorrentListener, MenuAdapter.ItemClicked, MenuItemAdapter.ItemAction {
public static String video_id; public static String video_id;
public static List<String> playedVideos = new ArrayList<>(); public static List<String> playedVideos = new ArrayList<>();
@ -251,14 +251,6 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd
public void onStreamStopped() { public void onStreamStopped() {
} }
@Override
public void newChromeCastDiscovered(ChromeCast chromeCast) {
}
@Override
public void chromeCastRemoved(ChromeCast chromeCast) {
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -283,8 +275,6 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd
.build(); .build();
ChromeCastsListener chromeCastsListener = this;
ChromeCasts.registerListener(chromeCastsListener);
fullScreenMode = false; fullScreenMode = false;
torrentStream = TorrentStream.init(torrentOptions); torrentStream = TorrentStream.init(torrentOptions);
torrentStream.addListener(PeertubeActivity.this); torrentStream.addListener(PeertubeActivity.this);
@ -1351,7 +1341,6 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd
if (torrentStream != null && torrentStream.isStreaming()) { if (torrentStream != null && torrentStream.isStreaming()) {
torrentStream.stopStream(); torrentStream.stopStream();
} }
ChromeCasts.unregisterListener(this);
if (chromeCast != null) { if (chromeCast != null) {
try { try {
chromeCast.disconnect(); chromeCast.disconnect();

View File

@ -127,7 +127,7 @@ public class Helper {
public static final String SET_VIDEOS_PER_PAGE = "set_videos_per_page"; public static final String SET_VIDEOS_PER_PAGE = "set_videos_per_page";
public static final String VIDEO_ID = "video_id_update"; public static final String VIDEO_ID = "video_id_update";
public static final String APP_PREFS = "app_prefs"; public static final String APP_PREFS = "app_prefs";
public static final String CAST_ID = "D402501A";
public static final int VIDEOS_PER_PAGE = 10; public static final int VIDEOS_PER_PAGE = 10;
public static final String RECEIVE_ACTION = "receive_action"; public static final String RECEIVE_ACTION = "receive_action";
public static final String SET_UNFOLLOW_VALIDATION = "set_unfollow_validation"; public static final String SET_UNFOLLOW_VALIDATION = "set_unfollow_validation";