From 413792087ef37976a0a2eca1862e9afd32565b4d Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 5 Dec 2020 11:27:27 +0100 Subject: [PATCH] #123 - some tests --- .../app/fedilab/fedilabtube/MainActivity.java | 70 ++++++++++++++++--- .../fedilab/fedilabtube/PeertubeActivity.java | 13 +--- .../fedilab/fedilabtube/helper/Helper.java | 2 +- 3 files changed, 62 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java index 8eccdfb..3836897 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java @@ -51,6 +51,7 @@ import org.jetbrains.annotations.NotNull; import java.io.IOException; import java.net.InetAddress; import java.net.NetworkInterface; +import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; import java.util.LinkedHashMap; @@ -83,14 +84,17 @@ import app.fedilab.fedilabtube.sqlite.Sqlite; import app.fedilab.fedilabtube.sqlite.StoredInstanceDAO; import app.fedilab.fedilabtube.viewmodel.TimelineVM; 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.ChromeCastsListener; import static app.fedilab.fedilabtube.MainActivity.TypeOfConnection.NORMAL; 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.HelperAcadInstance.academies; -public class MainActivity extends AppCompatActivity { +public class MainActivity extends AppCompatActivity implements ChromeCastsListener { public static int PICK_INSTANCE = 5641; @@ -198,14 +202,51 @@ public class MainActivity extends AppCompatActivity { 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(() -> { + if (chromeCasts != null) { + for (ChromeCast cast : chromeCasts) { + try { + cast.disconnect(); + } catch (IOException e) { + e.printStackTrace(); + } + } + chromeCasts = null; + } + chromeCasts = new ArrayList<>(); try { List interfaces; interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); @@ -215,12 +256,10 @@ public class MainActivity extends AppCompatActivity { while (inetAddressEnumeration.hasMoreElements()) { InetAddress inetAddress = inetAddressEnumeration.nextElement(); ChromeCasts.restartDiscovery(inetAddress); - PeertubeActivity.chromeCasts = ChromeCasts.get(); int tryFind = 0; - while (PeertubeActivity.chromeCasts.isEmpty() && tryFind < 5) { + while (ChromeCasts.get().isEmpty() && tryFind < 5) { try { Thread.sleep(1000); - PeertubeActivity.chromeCasts = ChromeCasts.get(); tryFind++; } catch (InterruptedException ignored) { } @@ -235,6 +274,17 @@ public class MainActivity extends AppCompatActivity { e.printStackTrace(); } }).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); setSupportActionBar(toolbar); diff --git a/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java b/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java index 6a4802a..524777e 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java @@ -168,7 +168,7 @@ import static app.fedilab.fedilabtube.helper.Helper.peertubeInformation; 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 List playedVideos = new ArrayList<>(); @@ -251,14 +251,6 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd public void onStreamStopped() { } - @Override - public void newChromeCastDiscovered(ChromeCast chromeCast) { - } - - @Override - public void chromeCastRemoved(ChromeCast chromeCast) { - } - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -283,8 +275,6 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd .build(); - ChromeCastsListener chromeCastsListener = this; - ChromeCasts.registerListener(chromeCastsListener); fullScreenMode = false; torrentStream = TorrentStream.init(torrentOptions); torrentStream.addListener(PeertubeActivity.this); @@ -1351,7 +1341,6 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd if (torrentStream != null && torrentStream.isStreaming()) { torrentStream.stopStream(); } - ChromeCasts.unregisterListener(this); if (chromeCast != null) { try { chromeCast.disconnect(); diff --git a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java index 2f4d372..3ea35be 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java +++ b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java @@ -127,7 +127,7 @@ public class Helper { 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 APP_PREFS = "app_prefs"; - + public static final String CAST_ID = "D402501A"; public static final int VIDEOS_PER_PAGE = 10; public static final String RECEIVE_ACTION = "receive_action"; public static final String SET_UNFOLLOW_VALIDATION = "set_unfollow_validation";