diff --git a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java index 6388aca..e82372c 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java @@ -67,6 +67,7 @@ import java.util.regex.Pattern; import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI; import app.fedilab.fedilabtube.client.data.AccountData.Account; import app.fedilab.fedilabtube.client.data.InstanceData; +import app.fedilab.fedilabtube.client.data.VideoData; import app.fedilab.fedilabtube.client.entities.Error; import app.fedilab.fedilabtube.client.entities.OauthParams; import app.fedilab.fedilabtube.client.entities.PeertubeInformation; @@ -90,10 +91,10 @@ 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 su.litvak.chromecast.api.v2.MediaStatus; 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; @@ -109,6 +110,9 @@ public class MainActivity extends AppCompatActivity implements ChromeCastsListen private DisplayOverviewFragment overviewFragment; private ActivityMainBinding binding; private BroadcastReceiver manage_chromecast; + public static List chromeCasts; + public static ChromeCast chromeCast; + private VideoData.Video castedTube; @SuppressLint("ApplySharedPref") public static void showRadioButtonDialogFullInstances(Activity activity, boolean storeInDb) { @@ -206,9 +210,17 @@ public class MainActivity extends AppCompatActivity implements ChromeCastsListen return true; }; - @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); @@ -224,6 +236,7 @@ public class MainActivity extends AppCompatActivity implements ChromeCastsListen chromeCasts.add(chromeCast); } } + } @Override @@ -238,17 +251,25 @@ public class MainActivity extends AppCompatActivity implements ChromeCastsListen ChromeCasts.unregisterListener(this); if (manage_chromecast != null) { LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(manage_chromecast); - if (chromeCasts != null && chromeCasts.size() > 0) { - for (ChromeCast cast : chromeCasts) { - try { - cast.disconnect(); - } catch (IOException e) { - e.printStackTrace(); + + new Thread(() -> { + if (chromeCasts != null && chromeCasts.size() > 0) { + for (ChromeCast cast : chromeCasts) { + try { + cast.disconnect(); + } catch (IOException e) { + e.printStackTrace(); + } } } - } + }).start(); + } + if (chromeCasts != null) { chromeCasts = null; } + if (chromeCast != null) { + chromeCast = null; + } } //Method for discovering cast devices @@ -409,35 +430,93 @@ 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.castTogglePlay.setOnClickListener(v -> { + if (chromeCast != null) { + new Thread(() -> { + try { + Handler mainHandler = new Handler(Looper.getMainLooper()); + Runnable myRunnable = () -> binding.castTogglePlay.setVisibility(View.GONE); + mainHandler.post(myRunnable); + int icon = -1; + if (chromeCast.getMediaStatus().playerState == MediaStatus.PlayerState.PLAYING) { + chromeCast.pause(); + icon = R.drawable.ic_baseline_play_arrow_32; + } else if (chromeCast.getMediaStatus().playerState == MediaStatus.PlayerState.PAUSED) { + chromeCast.play(); + icon = R.drawable.ic_baseline_pause_32; + } + if (icon != -1) { + int finalIcon = icon; + myRunnable = () -> binding.castTogglePlay.setImageResource(finalIcon); + mainHandler.post(myRunnable); + } + myRunnable = () -> binding.castTogglePlay.setVisibility(View.VISIBLE); + mainHandler.post(myRunnable); + } catch (IOException e) { + e.printStackTrace(); + } + }).start(); + } + }); manage_chromecast = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Bundle b = intent.getExtras(); assert b != null; - boolean state = b.getBoolean("state_asked"); - if (state) { + int state = b.getInt("state_asked", -1); + int displayed = b.getInt("displayed", -1); + castedTube = b.getParcelable("castedTube"); + + if (state == 1) { discoverCast(); - } else { + } else if (state == 0) { new Thread(() -> { - ChromeCasts.get(); - if (ChromeCasts.get().size() > 0) { - for (ChromeCast cast : ChromeCasts.get()) { - try { - cast.disconnect(); - } catch (IOException e) { - e.printStackTrace(); - } + try { + if (chromeCast != null) { + chromeCast.disconnect(); } + } catch (IOException e) { + e.printStackTrace(); } }).start(); } + if (displayed == 1) { + if (castedTube != null) { + binding.castInfo.setVisibility(View.VISIBLE); + Helper.loadGiF(MainActivity.this, castedTube.getThumbnailPath(), binding.castView); + binding.castTitle.setText(castedTube.getTitle()); + binding.castDescription.setText(castedTube.getDescription()); + } + } else if (displayed == 0) { + new Thread(() -> { + try { + if (chromeCast != null) { + chromeCast.disconnect(); + } + } catch (IOException e) { + e.printStackTrace(); + } + }).start(); + binding.castInfo.setVisibility(View.GONE); + } } }; final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); LocalBroadcastManager.getInstance(MainActivity.this).registerReceiver(manage_chromecast, new IntentFilter(Helper.RECEIVE_CAST_SETTINGS)); - boolean search_cast = sharedpreferences.getBoolean(getString(R.string.set_cast_choice), false); - if (search_cast) { + int search_cast = sharedpreferences.getInt(getString(R.string.set_cast_choice), 0); + if (search_cast == 1) { discoverCast(); } } @@ -657,6 +736,7 @@ public class MainActivity extends AppCompatActivity implements ChromeCastsListen return true; } + private void checkIfConnectedUsers() { new Thread(() -> { try { diff --git a/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java b/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java index 722efa5..84f7bb5 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java @@ -70,6 +70,7 @@ import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.core.graphics.drawable.DrawableCompat; import androidx.lifecycle.ViewModelProvider; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -155,6 +156,8 @@ import su.litvak.chromecast.api.v2.ChromeCast; import su.litvak.chromecast.api.v2.MediaStatus; 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.client.RetrofitPeertubeAPI.ActionType.ADD_COMMENT; import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.RATEVIDEO; import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.REPLY; @@ -202,8 +205,6 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd private String currentCaption; private boolean isRemote; private boolean willPlayFromIntent; - public static List chromeCasts; - private ChromeCast chromeCast; private String chromeCastVideoURL; public static void hideKeyboard(Activity activity) { @@ -735,11 +736,24 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd chromecast_choice[i] = cc.getTitle(); i++; } + i = 0; + for (ChromeCast cc : chromeCasts) { + if (cc.isConnected()) { + break; + } + i++; + } + alt_bld.setSingleChoiceItems(chromecast_choice, i, (dialog, position) -> { chromeCast = chromeCasts.get(position); new Thread(() -> { if (chromeCast != null) { if (chromeCast.isConnected()) { + 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; @@ -754,6 +768,12 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd e.printStackTrace(); } } else { + Intent intentBC = new Intent(Helper.RECEIVE_CAST_SETTINGS); + Bundle b = new Bundle(); + b.putInt("displayed", 1); + b.putParcelable("castedTube", peertube); + intentBC.putExtras(b); + LocalBroadcastManager.getInstance(PeertubeActivity.this).sendBroadcast(intentBC); try { Handler mainHandler = new Handler(Looper.getMainLooper()); Runnable myRunnable = () -> { @@ -934,6 +954,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd } return; } + if (apiResponse != null && apiResponse.getPeertubes() != null && apiResponse.getPeertubes().size() > 0 && apiResponse.getPeertubes().get(0).getErrorMessage() != null) { Toasty.error(PeertubeActivity.this, apiResponse.getPeertubes().get(0).getErrorMessage(), Toast.LENGTH_LONG).show(); binding.loader.setVisibility(View.GONE); @@ -1611,7 +1632,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd int height = displayMetrics.heightPixels; binding.doubleTapPlayerView.hideController(); List menuItemVideos = new ArrayList<>(); - if (peertube.getFiles() != null && peertube.getFiles().size() > 0) { + if (peertube.getAllFile(PeertubeActivity.this) != null && peertube.getAllFile(PeertubeActivity.this).size() > 0) { MenuItemVideo resolutionItem = new MenuItemVideo(); resolutionItem.setIcon(R.drawable.ic_baseline_high_quality_24); resolutionItem.setTitle(getString(R.string.pickup_resolution)); diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/data/VideoData.java b/app/src/main/java/app/fedilab/fedilabtube/client/data/VideoData.java index 559300a..2779088 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/data/VideoData.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/data/VideoData.java @@ -195,35 +195,63 @@ public class VideoData { SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); int mode = sharedpreferences.getInt(Helper.SET_VIDEO_MODE, Helper.VIDEO_MODE_NORMAL); if (files != null && files.size() > 0) { - if (resolution != null) { - for (File file : files) { - if (file.getResolutions().getLabel().compareTo(resolution) == 0) { - if (mode == Helper.VIDEO_MODE_MAGNET) { - return file.getMagnetUri(); - } else if (mode == Helper.VIDEO_MODE_TORRENT) { - return file.getTorrentUrl(); - } else { - return file.getFileUrl(); - } + return getFile(context, files, resolution, mode); + } else if (streamingPlaylists != null && streamingPlaylists.size() > 0) { + List files = new ArrayList<>(); + for (StreamingPlaylists streamingPlaylists : streamingPlaylists) { + if (streamingPlaylists.getFiles().size() > 0) { + files.addAll(streamingPlaylists.getFiles()); + } else { + return streamingPlaylists.getPlaylistUrl(); + } + } + return getFile(context, files, resolution, mode); + } + return null; + } + + public List getAllFile(Context context) { + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + int mode = sharedpreferences.getInt(Helper.SET_VIDEO_MODE, Helper.VIDEO_MODE_NORMAL); + if (files != null) { + return files; + } else if (streamingPlaylists != null) { + List files = new ArrayList<>(); + for (StreamingPlaylists streamingPlaylists : streamingPlaylists) { + files.addAll(streamingPlaylists.getFiles()); + } + return files; + } + return null; + } + + + private String getFile(Context context, List files, String resolution, int mode) { + if (resolution != null) { + for (File file : files) { + if (file.getResolutions().getLabel().compareTo(resolution) == 0) { + if (mode == Helper.VIDEO_MODE_MAGNET) { + return file.getMagnetUri(); + } else if (mode == Helper.VIDEO_MODE_TORRENT) { + return file.getTorrentUrl(); + } else { + return file.getFileUrl(); } } } - File file = Helper.defaultFile(context, files); - if (file != null) { - if (mode == Helper.VIDEO_MODE_MAGNET) { - return file.getMagnetUri(); - } else if (mode == Helper.VIDEO_MODE_TORRENT) { - return file.getTorrentUrl(); - } else { - return file.getFileUrl(); - } - } else { - return null; - } - } else if (streamingPlaylists != null && streamingPlaylists.size() > 0) { - return streamingPlaylists.get(0).getPlaylistUrl(); } - return null; + File file = Helper.defaultFile(context, files); + if (file != null) { + if (mode == Helper.VIDEO_MODE_MAGNET) { + return file.getMagnetUri(); + } else if (mode == Helper.VIDEO_MODE_TORRENT) { + return file.getTorrentUrl(); + } else { + return file.getFileUrl(); + } + } else { + return null; + } } public String getTorrentUrl(String resolution, Context context) { diff --git a/app/src/main/java/app/fedilab/fedilabtube/fragment/SettingsFragment.java b/app/src/main/java/app/fedilab/fedilabtube/fragment/SettingsFragment.java index 85e6162..b61a599 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/fragment/SettingsFragment.java +++ b/app/src/main/java/app/fedilab/fedilabtube/fragment/SettingsFragment.java @@ -236,10 +236,10 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Shared if (key.compareTo(getString(R.string.set_cast_choice)) == 0) { SwitchPreference set_cast_choice = findPreference(getString(R.string.set_cast_choice)); assert set_cast_choice != null; - editor.putBoolean(getString(R.string.set_cast_choice), set_cast_choice.isChecked()); + editor.putInt(getString(R.string.set_cast_choice), set_cast_choice.isChecked() ? 1 : 0); Intent intentBC = new Intent(Helper.RECEIVE_CAST_SETTINGS); Bundle b = new Bundle(); - b.putBoolean("state_asked", set_cast_choice.isChecked()); + b.putInt("state_asked", set_cast_choice.isChecked() ? 1 : 0); intentBC.putExtras(b); LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intentBC); } @@ -407,10 +407,10 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Shared set_video_in_list_choice.setChecked(videosInList); //****** Allow Chromecast ******* - boolean cast = sharedpref.getBoolean(getString(R.string.set_cast), false); + int cast = sharedpref.getInt(getString(R.string.set_cast_choice), 0); SwitchPreference set_cast_choice = findPreference(getString(R.string.set_cast_choice)); assert set_cast_choice != null; - set_cast_choice.setChecked(cast); + set_cast_choice.setChecked(cast == 1); //****** Language filter ********* LinkedHashMap languages = new LinkedHashMap<>(Helper.peertubeInformation.getLanguages()); diff --git a/app/src/main/res/drawable/ic_baseline_close_white_24.xml b/app/src/main/res/drawable/ic_baseline_close_white_24.xml new file mode 100644 index 0000000..49ac6f1 --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_close_white_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 5f457bd..5ef3945 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -62,24 +62,102 @@ - - + + + android:layout_gravity="bottom"> + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file