mirror of
				https://framagit.org/tom79/fedilab-tube
				synced 2025-06-05 21:09:11 +02:00 
			
		
		
		
	Some improvements with cast
This commit is contained in:
		@@ -114,6 +114,8 @@ public class MainActivity extends AppCompatActivity implements ChromeCastsListen
 | 
			
		||||
    public static ChromeCast chromeCast;
 | 
			
		||||
    private VideoData.Video castedTube;
 | 
			
		||||
 | 
			
		||||
    public static boolean chromecastActivated = false;
 | 
			
		||||
 | 
			
		||||
    @SuppressLint("ApplySharedPref")
 | 
			
		||||
    public static void showRadioButtonDialogFullInstances(Activity activity, boolean storeInDb) {
 | 
			
		||||
        final SharedPreferences sharedpreferences = activity.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
 | 
			
		||||
@@ -212,15 +214,6 @@ public class MainActivity extends AppCompatActivity implements ChromeCastsListen
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void newChromeCastDiscovered(ChromeCast chromeCast) {
 | 
			
		||||
        try {
 | 
			
		||||
            if (chromeCast.isAppRunning(Helper.CAST_ID) && chromeCast.getMediaStatus() != null && chromeCast.getMediaStatus().playerState != null) {
 | 
			
		||||
                if (binding.castInfo.getVisibility() == View.GONE) {
 | 
			
		||||
                    binding.castInfo.setVisibility(View.VISIBLE);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
        if (chromeCasts == null) {
 | 
			
		||||
            chromeCasts = new ArrayList<>();
 | 
			
		||||
            chromeCasts.add(chromeCast);
 | 
			
		||||
@@ -236,6 +229,15 @@ public class MainActivity extends AppCompatActivity implements ChromeCastsListen
 | 
			
		||||
                chromeCasts.add(chromeCast);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        try {
 | 
			
		||||
            if (chromeCast.isAppRunning(Helper.CAST_ID) && chromeCast.getMediaStatus() != null && chromeCast.getMediaStatus().playerState != null) {
 | 
			
		||||
                if (binding.castInfo.getVisibility() == View.GONE) {
 | 
			
		||||
                    binding.castInfo.setVisibility(View.VISIBLE);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -256,6 +258,7 @@ public class MainActivity extends AppCompatActivity implements ChromeCastsListen
 | 
			
		||||
                if (chromeCasts != null && chromeCasts.size() > 0) {
 | 
			
		||||
                    for (ChromeCast cast : chromeCasts) {
 | 
			
		||||
                        try {
 | 
			
		||||
                            cast.stopApp();
 | 
			
		||||
                            cast.disconnect();
 | 
			
		||||
                        } catch (IOException e) {
 | 
			
		||||
                            e.printStackTrace();
 | 
			
		||||
@@ -430,16 +433,13 @@ public class MainActivity extends AppCompatActivity implements ChromeCastsListen
 | 
			
		||||
            PlaylistExportHelper.manageIntentUrl(MainActivity.this, getIntent());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        binding.castClose.setOnClickListener(v -> new Thread(() -> {
 | 
			
		||||
            try {
 | 
			
		||||
                Handler mainHandler = new Handler(Looper.getMainLooper());
 | 
			
		||||
                chromeCast.disconnect();
 | 
			
		||||
                Runnable myRunnable = () -> binding.castTogglePlay.setVisibility(View.GONE);
 | 
			
		||||
                mainHandler.post(myRunnable);
 | 
			
		||||
            } catch (IOException e) {
 | 
			
		||||
                e.printStackTrace();
 | 
			
		||||
            }
 | 
			
		||||
        }));
 | 
			
		||||
        binding.castClose.setOnClickListener(v -> {
 | 
			
		||||
            Intent intentBC = new Intent(Helper.RECEIVE_CAST_SETTINGS);
 | 
			
		||||
            Bundle b = new Bundle();
 | 
			
		||||
            b.putInt("displayed", 0);
 | 
			
		||||
            intentBC.putExtras(b);
 | 
			
		||||
            LocalBroadcastManager.getInstance(MainActivity.this).sendBroadcast(intentBC);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        binding.castTogglePlay.setOnClickListener(v -> {
 | 
			
		||||
            if (chromeCast != null) {
 | 
			
		||||
@@ -484,6 +484,7 @@ public class MainActivity extends AppCompatActivity implements ChromeCastsListen
 | 
			
		||||
                    new Thread(() -> {
 | 
			
		||||
                        try {
 | 
			
		||||
                            if (chromeCast != null) {
 | 
			
		||||
                                chromeCast.stopApp();
 | 
			
		||||
                                chromeCast.disconnect();
 | 
			
		||||
                            }
 | 
			
		||||
                        } catch (IOException e) {
 | 
			
		||||
@@ -492,6 +493,7 @@ public class MainActivity extends AppCompatActivity implements ChromeCastsListen
 | 
			
		||||
                    }).start();
 | 
			
		||||
                }
 | 
			
		||||
                if (displayed == 1) {
 | 
			
		||||
                    chromecastActivated = true;
 | 
			
		||||
                    if (castedTube != null) {
 | 
			
		||||
                        binding.castInfo.setVisibility(View.VISIBLE);
 | 
			
		||||
                        Helper.loadGiF(MainActivity.this, castedTube.getThumbnailPath(), binding.castView);
 | 
			
		||||
@@ -499,16 +501,17 @@ public class MainActivity extends AppCompatActivity implements ChromeCastsListen
 | 
			
		||||
                        binding.castDescription.setText(castedTube.getDescription());
 | 
			
		||||
                    }
 | 
			
		||||
                } else if (displayed == 0) {
 | 
			
		||||
                    chromecastActivated = false;
 | 
			
		||||
                    binding.castInfo.setVisibility(View.GONE);
 | 
			
		||||
                    new Thread(() -> {
 | 
			
		||||
                        try {
 | 
			
		||||
                            if (chromeCast != null) {
 | 
			
		||||
                                chromeCast.disconnect();
 | 
			
		||||
                                chromeCast.stopApp();
 | 
			
		||||
                            }
 | 
			
		||||
                        } catch (IOException e) {
 | 
			
		||||
                            e.printStackTrace();
 | 
			
		||||
                        }
 | 
			
		||||
                    }).start();
 | 
			
		||||
                    binding.castInfo.setVisibility(View.GONE);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
 
 | 
			
		||||
@@ -158,6 +158,7 @@ import su.litvak.chromecast.api.v2.Status;
 | 
			
		||||
 | 
			
		||||
import static app.fedilab.fedilabtube.MainActivity.chromeCast;
 | 
			
		||||
import static app.fedilab.fedilabtube.MainActivity.chromeCasts;
 | 
			
		||||
import static app.fedilab.fedilabtube.MainActivity.chromecastActivated;
 | 
			
		||||
import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.ADD_COMMENT;
 | 
			
		||||
import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.RATEVIDEO;
 | 
			
		||||
import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.REPLY;
 | 
			
		||||
@@ -738,7 +739,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd
 | 
			
		||||
                }
 | 
			
		||||
                i = 0;
 | 
			
		||||
                for (ChromeCast cc : chromeCasts) {
 | 
			
		||||
                    if (cc.isConnected()) {
 | 
			
		||||
                    if (chromecastActivated && cc.isConnected()) {
 | 
			
		||||
                        break;
 | 
			
		||||
                    }
 | 
			
		||||
                    i++;
 | 
			
		||||
@@ -748,25 +749,19 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd
 | 
			
		||||
                    chromeCast = chromeCasts.get(position);
 | 
			
		||||
                    new Thread(() -> {
 | 
			
		||||
                        if (chromeCast != null) {
 | 
			
		||||
                            if (chromeCast.isConnected()) {
 | 
			
		||||
                            if (chromecastActivated) {
 | 
			
		||||
                                Intent intentBC = new Intent(Helper.RECEIVE_CAST_SETTINGS);
 | 
			
		||||
                                Bundle b = new Bundle();
 | 
			
		||||
                                b.putInt("displayed", 0);
 | 
			
		||||
                                intentBC.putExtras(b);
 | 
			
		||||
                                LocalBroadcastManager.getInstance(PeertubeActivity.this).sendBroadcast(intentBC);
 | 
			
		||||
                                try {
 | 
			
		||||
                                    chromeCast.disconnect();
 | 
			
		||||
                                    chromeCast = null;
 | 
			
		||||
                                    Handler mainHandler = new Handler(Looper.getMainLooper());
 | 
			
		||||
                                    Runnable myRunnable = () -> {
 | 
			
		||||
                                        binding.doubleTapPlayerView.setVisibility(View.VISIBLE);
 | 
			
		||||
                                        binding.castController.setVisibility(View.GONE);
 | 
			
		||||
                                    };
 | 
			
		||||
                                    mainHandler.post(myRunnable);
 | 
			
		||||
                                Handler mainHandler = new Handler(Looper.getMainLooper());
 | 
			
		||||
                                Runnable myRunnable = () -> {
 | 
			
		||||
                                    binding.doubleTapPlayerView.setVisibility(View.VISIBLE);
 | 
			
		||||
                                    binding.castController.setVisibility(View.GONE);
 | 
			
		||||
                                };
 | 
			
		||||
                                mainHandler.post(myRunnable);
 | 
			
		||||
 | 
			
		||||
                                } catch (IOException e) {
 | 
			
		||||
                                    e.printStackTrace();
 | 
			
		||||
                                }
 | 
			
		||||
                            } else {
 | 
			
		||||
                                Intent intentBC = new Intent(Helper.RECEIVE_CAST_SETTINGS);
 | 
			
		||||
                                Bundle b = new Bundle();
 | 
			
		||||
@@ -802,7 +797,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd
 | 
			
		||||
 | 
			
		||||
                                    if (chromeCastVideoURL != null) {
 | 
			
		||||
                                        String mime = MimeTypeMap.getFileExtensionFromUrl(chromeCastVideoURL);
 | 
			
		||||
                                        chromeCast.setRequestTimeout(120000);
 | 
			
		||||
                                        chromeCast.setRequestTimeout(60000);
 | 
			
		||||
                                        chromeCast.load(peertube.getTitle(), null, chromeCastVideoURL, mime);
 | 
			
		||||
                                        chromeCast.play();
 | 
			
		||||
                                        binding.castPlay.setImageResource(R.drawable.ic_baseline_pause_32);
 | 
			
		||||
@@ -1419,15 +1414,6 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd
 | 
			
		||||
        if (torrentStream != null && torrentStream.isStreaming()) {
 | 
			
		||||
            torrentStream.stopStream();
 | 
			
		||||
        }
 | 
			
		||||
        if (chromeCast != null && chromeCast.isConnected()) {
 | 
			
		||||
            new Thread(() -> {
 | 
			
		||||
                try {
 | 
			
		||||
                    chromeCast.disconnect();
 | 
			
		||||
                } catch (IOException e) {
 | 
			
		||||
                    e.printStackTrace();
 | 
			
		||||
                }
 | 
			
		||||
            }).start();
 | 
			
		||||
        }
 | 
			
		||||
        unregisterReceiver();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -131,6 +131,8 @@
 | 
			
		||||
                    android:layout_gravity="center"
 | 
			
		||||
                    android:layout_margin="5dp"
 | 
			
		||||
                    android:contentDescription="@string/play"
 | 
			
		||||
                    android:clickable="true"
 | 
			
		||||
                    android:focusable="true"
 | 
			
		||||
                    android:src="@drawable/ic_baseline_pause_32" />
 | 
			
		||||
 | 
			
		||||
                <ImageView
 | 
			
		||||
@@ -140,6 +142,8 @@
 | 
			
		||||
                    android:layout_gravity="center"
 | 
			
		||||
                    android:layout_margin="5dp"
 | 
			
		||||
                    android:contentDescription="@string/close"
 | 
			
		||||
                    android:clickable="true"
 | 
			
		||||
                    android:focusable="true"
 | 
			
		||||
                    android:src="@drawable/ic_baseline_close_white_24" />
 | 
			
		||||
            </LinearLayout>
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user