some fixes

This commit is contained in:
Thomas 2020-12-09 15:45:54 +01:00
parent fcc10f97b5
commit 5172695784
6 changed files with 283 additions and 66 deletions

View File

@ -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<ChromeCast> 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 {

View File

@ -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<ChromeCast> 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<MenuItemVideo> 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));

View File

@ -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<File> 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<File> 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<File> files = new ArrayList<>();
for (StreamingPlaylists streamingPlaylists : streamingPlaylists) {
files.addAll(streamingPlaylists.getFiles());
}
return files;
}
return null;
}
private String getFile(Context context, List<File> 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) {

View File

@ -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<String, String> languages = new LinkedHashMap<>(Helper.peertubeInformation.getLanguages());

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#FFFFFF"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z" />
</vector>

View File

@ -62,24 +62,102 @@
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_marginBottom="?attr/actionBarSize"
android:id="@+id/viewpager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="?attr/actionBarSize" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_gravity="bottom"
android:layout_height="match_parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:itemIconTint="@color/bottom_nav_color"
app:itemTextColor="@color/bottom_nav_color"
/>
android:layout_gravity="bottom">
<LinearLayout
android:id="@+id/cast_info"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@android:color/black"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/castView"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:contentDescription="@string/preview"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/cast_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textColor="@color/white" />
<TextView
android:id="@+id/cast_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:textColor="@color/white"
android:textSize="12sp" />
</LinearLayout>
<ImageView
android:id="@+id/cast_toggle_play"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:contentDescription="@string/play"
android:src="@drawable/ic_baseline_pause_32" />
<ImageView
android:id="@+id/cast_close"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:contentDescription="@string/close"
android:src="@drawable/ic_baseline_close_white_24" />
</LinearLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:itemIconTint="@color/bottom_nav_color"
app:itemTextColor="@color/bottom_nav_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.drawerlayout.widget.DrawerLayout>