From 0c048877e065c4d8221042514562dab1d2e98e3d Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 5 Nov 2020 17:38:57 +0100 Subject: [PATCH] Some new features --- app/src/acad/res/values/strings.xml | 6 ++ .../play/release-notes/en-US/default.txt | 8 ++ app/src/full/res/values/strings.xml | 4 + .../app/fedilab/fedilabtube/MainActivity.java | 45 ++++---- .../fedilab/fedilabtube/PeertubeActivity.java | 102 +++++++++++++----- .../fragment/SettingsFragment.java | 12 +++ .../fedilab/fedilabtube/helper/Helper.java | 12 +++ .../fedilabtube/sqlite/AccountDAO.java | 16 +++ .../worker/NotificationsWorker.java | 1 + .../ic_baseline_screen_lock_portrait_24.xml | 10 ++ app/src/main/res/xml/main_preferences.xml | 6 ++ 11 files changed, 175 insertions(+), 47 deletions(-) create mode 100644 app/src/main/res/drawable/ic_baseline_screen_lock_portrait_24.xml diff --git a/app/src/acad/res/values/strings.xml b/app/src/acad/res/values/strings.xml index 70d536d..f708c7a 100644 --- a/app/src/acad/res/values/strings.xml +++ b/app/src/acad/res/values/strings.xml @@ -12,6 +12,12 @@ set_fullscreen_choice set_autoplay_next_video_choice set_store_in_history + set_play_screen_lock_choice + + + Verrouillage d\'écran + Continuer à lire des vidéos lorsque l\'écran est verrouillé + Modifier la photo de profil Le compte a été mis à jour ! diff --git a/app/src/fdroid_full/play/release-notes/en-US/default.txt b/app/src/fdroid_full/play/release-notes/en-US/default.txt index 2c606f4..b5a6a55 100644 --- a/app/src/fdroid_full/play/release-notes/en-US/default.txt +++ b/app/src/fdroid_full/play/release-notes/en-US/default.txt @@ -1,2 +1,10 @@ Added: - Double tap video left/right to seek - or + 10 seconds +- Automatically adapt full-screen in portrait or landscape +- Incognito: Enable/disable reccords in history +- Video suggestions +- Enable/Disable playback when screen is off (default: disabled) + +Fix: +- Enable to end playing with top back button +- Fix FP message with token \ No newline at end of file diff --git a/app/src/full/res/values/strings.xml b/app/src/full/res/values/strings.xml index aca83d5..3d18211 100644 --- a/app/src/full/res/values/strings.xml +++ b/app/src/full/res/values/strings.xml @@ -11,6 +11,10 @@ set_autoplay_next_video_choice set_theme_choice set_fullscreen_choice + set_play_screen_lock_choice + + Screen lock + Keep playing videos when the screen is locked Save Enable history diff --git a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java index a5a18f8..43fabe0 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java @@ -169,9 +169,10 @@ public class MainActivity extends AppCompatActivity { if (active == null) { active = overviewFragment; } - fm.beginTransaction().add(R.id.nav_host_fragment, locaFragment, "5").hide(locaFragment).commit(); + if (!Helper.isLoggedIn(MainActivity.this)) { + fm.beginTransaction().add(R.id.nav_host_fragment, locaFragment, "5").hide(locaFragment).commit(); fm.beginTransaction().add(R.id.nav_host_fragment, recentFragment, "4").hide(recentFragment).commit(); fm.beginTransaction().add(R.id.nav_host_fragment, mostLikedFragment, "3").hide(mostLikedFragment).commit(); fm.beginTransaction().add(R.id.nav_host_fragment, trendingFragment, "2").hide(trendingFragment).commit(); @@ -189,30 +190,35 @@ public class MainActivity extends AppCompatActivity { if (Helper.isLoggedIn(MainActivity.this)) { navView.inflateMenu(R.menu.bottom_nav_menu_connected); - final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); - String tokenStr = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); - String instance = Helper.getLiveInstance(MainActivity.this); - SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - - Account account = new AccountDAO(MainActivity.this, db).getAccountByToken(tokenStr); - if (account != null) { - OauthParams oauthParams = new OauthParams(); - oauthParams.setGrant_type("refresh_token"); - oauthParams.setClient_id(account.getClient_id()); - oauthParams.setClient_secret(account.getClient_secret()); - oauthParams.setRefresh_token(account.getRefresh_token()); - oauthParams.setAccess_token(account.getToken()); new Thread(() -> { + final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + String tokenStr = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); + String instance = Helper.getLiveInstance(MainActivity.this); + SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + String instanceShar = sharedpreferences.getString(Helper.PREF_INSTANCE, null); + String userIdShar = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + Account account = new AccountDAO(MainActivity.this, db).getAccountByToken(tokenStr); + if( account == null) { + account = new AccountDAO(MainActivity.this, db).getAccountByIdInstance(userIdShar, instanceShar); + } + if (account != null) { + Account finalAccount = account; + OauthParams oauthParams = new OauthParams(); + oauthParams.setGrant_type("refresh_token"); + oauthParams.setClient_id(account.getClient_id()); + oauthParams.setClient_secret(account.getClient_secret()); + oauthParams.setRefresh_token(account.getRefresh_token()); + oauthParams.setAccess_token(account.getToken()); try { Token token = new RetrofitPeertubeAPI(MainActivity.this).manageToken(oauthParams); - if (token == null) { - runOnUiThread(() -> Helper.logoutCurrentUser(MainActivity.this, account)); + if (token == null && Helper.instanceOnline(instance)) { + runOnUiThread(() -> Helper.logoutCurrentUser(MainActivity.this, finalAccount)); return; } - runOnUiThread(() -> { //To avoid a token issue with subscriptions, adding fragment is done when the token is refreshed. new Handler().post(() -> { + fm.beginTransaction().add(R.id.nav_host_fragment, locaFragment, "5").hide(locaFragment).commit(); fm.beginTransaction().add(R.id.nav_host_fragment, recentFragment, "4").hide(recentFragment).commitAllowingStateLoss(); fm.beginTransaction().add(R.id.nav_host_fragment, trendingFragment, "3").hide(trendingFragment).commitAllowingStateLoss(); fm.beginTransaction().add(R.id.nav_host_fragment, subscriptionFragment, "2").hide(subscriptionFragment).commitAllowingStateLoss(); @@ -243,11 +249,10 @@ public class MainActivity extends AppCompatActivity { } } } catch (Error error) { - runOnUiThread(() -> Helper.logoutCurrentUser(MainActivity.this, account)); + runOnUiThread(() -> Helper.logoutCurrentUser(MainActivity.this, finalAccount)); error.printStackTrace(); } - }).start(); - } + }}).start(); } else { navView.inflateMenu(R.menu.bottom_nav_menu); diff --git a/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java b/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java index 386c0cb..8b4f77e 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java @@ -89,6 +89,7 @@ import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.Util; +import com.google.android.exoplayer2.video.VideoListener; import org.jetbrains.annotations.NotNull; @@ -141,7 +142,7 @@ import static app.fedilab.fedilabtube.helper.Helper.isLoggedIn; import static com.google.android.exoplayer2.Player.MEDIA_ITEM_TRANSITION_REASON_AUTO; -public class PeertubeActivity extends AppCompatActivity implements CommentListAdapter.AllCommentRemoved, Player.EventListener { +public class PeertubeActivity extends AppCompatActivity implements CommentListAdapter.AllCommentRemoved, Player.EventListener, VideoListener { public static String video_id; private String peertubeInstance, videoUuid; @@ -171,6 +172,22 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd public static List playedVideos = new ArrayList<>(); private VideoData.Video nextVideo; + @Override + public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) { + if(width < height){ + videoOrientationType = videoOrientation.PORTRAIT; + }else{ + videoOrientationType = videoOrientation.LANDSCAPE; + } + } + + enum videoOrientation { + LANDSCAPE, + PORTRAIT + } + + private videoOrientation videoOrientationType; + public static void hideKeyboard(Activity activity) { if (activity != null && activity.getWindow() != null) { activity.getWindow().getDecorView(); @@ -188,7 +205,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd binding = ActivityPeertubeBinding.inflate(getLayoutInflater()); View view = binding.getRoot(); setContentView(view); - + videoOrientationType = videoOrientation.LANDSCAPE; max_id = "0"; urlIntent = null; SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); @@ -258,7 +275,11 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd getWindow().setAttributes(attrs); getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE); binding.peertubeInformationContainer.setVisibility(View.GONE); - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + if (videoOrientationType == videoOrientation.LANDSCAPE) { + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + }else { + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + } } else { WindowManager.LayoutParams attrs = getWindow().getAttributes(); attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN; @@ -341,7 +362,11 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd registBroadcastReceiver(); if( autoFullscreen && autoPlay) { openFullscreenDialog(); - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + if (videoOrientationType == videoOrientation.LANDSCAPE) { + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + }else { + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + } } } @@ -480,7 +505,11 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd setFullscreen(FullScreenMediaController.fullscreen.ON); fullScreenMode = true; openFullscreenDialog(); - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + if (videoOrientationType == videoOrientation.LANDSCAPE) { + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + }else { + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + } } else { fullscreen = FullScreenMediaController.fullscreen.OFF; setFullscreen(FullScreenMediaController.fullscreen.OFF); @@ -514,10 +543,12 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { if (playInMinimized && player != null) { - enterVideoMode(); - } else { - finish(); + finishAndRemoveTask(); + Intent intent = new Intent(PeertubeActivity.this, MainActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP); + startActivity(intent); } + finish(); return true; } return super.onOptionsItemSelected(item); @@ -749,6 +780,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd ProgressiveMediaSource videoSource; player = new SimpleExoPlayer.Builder(PeertubeActivity.this).build(); + player.addVideoListener(PeertubeActivity.this); player.addListener(this); binding.mediaVideo.player(player); binding.doubleTapPlayerView.setPlayer(player); @@ -980,6 +1012,21 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd } } + @Override + public void onStop() { + super.onStop(); + onStopCalled = true; + } + + @Override + public void onResume() { + super.onResume(); + onStopCalled = false; + if (player != null && !player.isPlaying()) { + player.setPlayWhenReady(autoPlay); + } + } + @Override protected void onPause() { super.onPause(); @@ -987,7 +1034,10 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd updateHistory(player.getCurrentPosition()/1000); } if (player != null && !isPlayInMinimized) { - player.setPlayWhenReady(false); + final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); + if( !sharedpreferences.getBoolean(getString(R.string.set_play_screen_lock_choice), false)) { + player.setPlayWhenReady(false); + } } } @@ -995,14 +1045,16 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd final IntentFilter theFilter = new IntentFilter(); theFilter.addAction(Intent.ACTION_SCREEN_ON); theFilter.addAction(Intent.ACTION_SCREEN_OFF); - + final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); mPowerKeyReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String strAction = intent.getAction(); if (strAction.equals(Intent.ACTION_SCREEN_OFF)) { if (player != null && isPlayInMinimized) { - player.setPlayWhenReady(false); + if( !sharedpreferences.getBoolean(getString(R.string.set_play_screen_lock_choice), false)) { + player.setPlayWhenReady(false); + } } } } @@ -1075,20 +1127,6 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd } } - @Override - public void onStop() { - super.onStop(); - onStopCalled = true; - } - - @Override - public void onResume() { - super.onResume(); - onStopCalled = false; - if (player != null && !player.isPlaying()) { - player.setPlayWhenReady(autoPlay); - } - } public void displayResolution() { SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); @@ -1295,7 +1333,11 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd fullScreenButton.setOnClickListener(v -> { if (!fullScreenMode) { openFullscreenDialog(); - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + if (videoOrientationType == videoOrientation.LANDSCAPE) { + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + }else { + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + } } else { closeFullscreenDialog(); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); @@ -1308,7 +1350,11 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd playButton.setOnClickListener(v->{ if(autoFullscreen && !fullScreenMode) { openFullscreenDialog(); - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + if (videoOrientationType == videoOrientation.LANDSCAPE) { + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + }else { + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + } } player.setPlayWhenReady(true); }); @@ -1485,6 +1531,8 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd } } + + @Override public void onPlayerError(ExoPlaybackException error) { 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 7b35aa0..1c12d72 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/fragment/SettingsFragment.java +++ b/app/src/main/java/app/fedilab/fedilabtube/fragment/SettingsFragment.java @@ -191,6 +191,11 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Shared }).start(); } } + if (key.compareTo(getString(R.string.set_play_screen_lock_choice)) == 0) { + SwitchPreference set_play_screen_lock_choice = findPreference(getString(R.string.set_play_screen_lock_choice)); + assert set_play_screen_lock_choice != null; + editor.putBoolean(getString(R.string.set_play_screen_lock_choice), set_play_screen_lock_choice.isChecked()); + } if (key.compareTo(getString(R.string.set_video_language_choice)) == 0) { MultiSelectListPreference set_video_language_choice = findPreference(getString(R.string.set_video_language_choice)); assert set_video_language_choice != null; @@ -332,6 +337,13 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Shared assert set_autoplay_next_video_choice != null; set_autoplay_next_video_choice.setChecked(autoplayNextVideo); + + //****** Screen lock ******* + boolean playScreenLock = sharedpref.getBoolean(getString(R.string.set_play_screen_lock_choice), false); + SwitchPreference set_play_screen_lock_choice = findPreference(getString(R.string.set_play_screen_lock_choice)); + assert set_play_screen_lock_choice != null; + set_play_screen_lock_choice.setChecked(playScreenLock); + //****** Language filter ********* LinkedHashMap languages = new LinkedHashMap<>(peertubeInformation.getLanguages()); List entriesLanguages = new ArrayList<>(); 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 7881838..bf03a63 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java +++ b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java @@ -49,6 +49,7 @@ import com.bumptech.glide.load.resource.bitmap.CenterCrop; import com.bumptech.glide.load.resource.bitmap.RoundedCorners; import com.bumptech.glide.request.RequestOptions; +import java.net.InetAddress; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -735,4 +736,15 @@ public class Helper { activity.startActivity(chooserIntent); } } + + + public static boolean instanceOnline(String host) { + try { + InetAddress ipAddr = InetAddress.getByName(host); + return ipAddr.toString().trim().compareTo("") != 0; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/sqlite/AccountDAO.java b/app/src/main/java/app/fedilab/fedilabtube/sqlite/AccountDAO.java index a75a8d5..5ed353f 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/sqlite/AccountDAO.java +++ b/app/src/main/java/app/fedilab/fedilabtube/sqlite/AccountDAO.java @@ -249,7 +249,23 @@ public class AccountDAO { } } + /** + * Returns an Account by id and instance + * + * @param id String + * @param instance String + * @return Account + */ + public Account getAccountByIdInstance(String id, String instance) { + try { + Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_USER_ID + " = \"" + id + "\" AND " + Sqlite.COL_INSTANCE + " = \"" + instance + "\"", null, null, null, null, "1"); + return cursorToUser(c); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } /** * Test if the current user is already stored in data base * diff --git a/app/src/main/java/app/fedilab/fedilabtube/worker/NotificationsWorker.java b/app/src/main/java/app/fedilab/fedilabtube/worker/NotificationsWorker.java index cc6b711..17e1dd6 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/worker/NotificationsWorker.java +++ b/app/src/main/java/app/fedilab/fedilabtube/worker/NotificationsWorker.java @@ -301,6 +301,7 @@ public class NotificationsWorker extends Worker { NotificationChannel channel = new NotificationChannel(FETCH_NOTIFICATION_CHANNEL_ID, getApplicationContext().getString(R.string.fetch_notification_channel_name), NotificationManager.IMPORTANCE_DEFAULT); + channel.setSound(null, null); notificationManager.createNotificationChannel(channel); } Intent myIntent = new Intent(getApplicationContext(), MainActivity.class); diff --git a/app/src/main/res/drawable/ic_baseline_screen_lock_portrait_24.xml b/app/src/main/res/drawable/ic_baseline_screen_lock_portrait_24.xml new file mode 100644 index 0000000..c2f370f --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_screen_lock_portrait_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/xml/main_preferences.xml b/app/src/main/res/xml/main_preferences.xml index 53569b0..af04cd6 100644 --- a/app/src/main/res/xml/main_preferences.xml +++ b/app/src/main/res/xml/main_preferences.xml @@ -47,6 +47,12 @@ android:summary="@string/set_autoplay_next_video_description" android:title="@string/set_autoplay_next_video" /> + +