From de4e6eee25fc2a03b79296ee6b66be2bf9b30ea4 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Sat, 23 May 2020 09:01:30 +0100 Subject: [PATCH] switch to fullscreen mode on rotate when viewing media --- app/build.gradle | 2 +- .../java/io/lbry/browser/MainActivity.java | 65 ++++++--- .../listener/ScreenOrientationListener.java | 6 + .../ui/findcontent/FileViewFragment.java | 132 ++++++++++++------ 4 files changed, 137 insertions(+), 68 deletions(-) create mode 100644 app/src/main/java/io/lbry/browser/listener/ScreenOrientationListener.java diff --git a/app/build.gradle b/app/build.gradle index d66e7038..9464b20e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -52,7 +52,7 @@ task printVersionName { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'androidx.appcompat:appcompat:1.2.0-rc01' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'com.google.android.material:material:1.2.0-alpha06' implementation "androidx.cardview:cardview:1.0.0" diff --git a/app/src/main/java/io/lbry/browser/MainActivity.java b/app/src/main/java/io/lbry/browser/MainActivity.java index ed711318..9e2e4281 100644 --- a/app/src/main/java/io/lbry/browser/MainActivity.java +++ b/app/src/main/java/io/lbry/browser/MainActivity.java @@ -111,6 +111,7 @@ import io.lbry.browser.listener.DownloadActionListener; import io.lbry.browser.listener.FetchChannelsListener; import io.lbry.browser.listener.FetchClaimsListener; import io.lbry.browser.listener.FilePickerListener; +import io.lbry.browser.listener.ScreenOrientationListener; import io.lbry.browser.listener.SdkStatusListener; import io.lbry.browser.listener.StoragePermissionListener; import io.lbry.browser.listener.WalletBalanceListener; @@ -173,6 +174,8 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener private Map specialRouteFragmentClassMap; private boolean inPictureInPictureMode; + @Getter + private boolean inFullscreenMode; public static SimpleExoPlayer appPlayer; public static Cache playerCache; public static boolean playerReassigned; @@ -300,6 +303,7 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener private List cameraPermissionListeners; private List downloadActionListeners; private List filePickerListeners; + private List screenOrientationListeners; private List sdkStatusListeners; private List storagePermissionListeners; private List walletBalanceListeners; @@ -394,31 +398,19 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener // setup uri bar setupUriBar(); - /*View decorView = getWindow().getDecorView(); - decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() { - @Override - public void onSystemUiVisibilityChange(int visibility) { - if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) { - findViewById(R.id.app_bar_main_container).setFitsSystemWindows(false); - findViewById(R.id.drawer_layout).setFitsSystemWindows(false); - } else { - findViewById(R.id.app_bar_main_container).setFitsSystemWindows(true); - findViewById(R.id.drawer_layout).setFitsSystemWindows(true); - } - } - });*/ - // other pendingSyncSetQueue = new ArrayList<>(); openNavFragments = new HashMap<>(); + cameraPermissionListeners = new ArrayList<>(); downloadActionListeners = new ArrayList<>(); + fetchChannelsListeners = new ArrayList<>(); + fetchClaimsListeners = new ArrayList<>(); filePickerListeners = new ArrayList<>(); + screenOrientationListeners = new ArrayList<>(); sdkStatusListeners = new ArrayList<>(); storagePermissionListeners = new ArrayList<>(); walletBalanceListeners = new ArrayList<>(); - fetchClaimsListeners = new ArrayList<>(); - fetchChannelsListeners = new ArrayList<>(); sdkStatusListeners.add(this); @@ -531,14 +523,30 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener checkNotificationOpenIntent(intent); } - public void addCameraPermissionListener(CameraPermissionListener listener) { - if (!cameraPermissionListeners.contains(listener)) { - cameraPermissionListeners.add(listener); + public void onConfigurationChanged(Configuration newConfig) { + super.onConfigurationChanged(newConfig); + switch (newConfig.orientation) { + case Configuration.ORIENTATION_PORTRAIT: + for (ScreenOrientationListener listener : screenOrientationListeners) { + listener.onPortraitOrientationEntered(); + } + break; + case Configuration.ORIENTATION_LANDSCAPE: + for (ScreenOrientationListener listener : screenOrientationListeners) { + listener.onLandscapeOrientationEntered(); + } + break; } } - public void removeCameraPermissionListener(CameraPermissionListener listener) { - cameraPermissionListeners.remove(listener); + public void addScreenOrientationListener(ScreenOrientationListener listener) { + if (!screenOrientationListeners.contains(listener)) { + screenOrientationListeners.add(listener); + } + } + + public void removeScreenOrientationListener(ScreenOrientationListener listener) { + screenOrientationListeners.remove(listener); } public void addDownloadActionListener(DownloadActionListener listener) { @@ -561,6 +569,17 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener filePickerListeners.remove(listener); } + public void addCameraPermissionListener(CameraPermissionListener listener) { + if (!cameraPermissionListeners.contains(listener)) { + cameraPermissionListeners.add(listener); + } + } + + public void removeCameraPermissionListener(CameraPermissionListener listener) { + cameraPermissionListeners.remove(listener); + } + + public void addSdkStatusListener(SdkStatusListener listener) { if (!sdkStatusListeners.contains(listener)) { sdkStatusListeners.add(listener); @@ -764,7 +783,7 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener private void renderFullMode() { getSupportActionBar().show(); findViewById(R.id.content_main).setVisibility(View.VISIBLE); - findViewById(R.id.floating_balance_main_container).setVisibility(View.VISIBLE); + findViewById(R.id.floating_balance_main_container).setVisibility(inFullscreenMode ? View.INVISIBLE : View.VISIBLE); Fragment fragment = getCurrentFragment(); if (!(fragment instanceof FileViewFragment)) { findViewById(R.id.global_now_playing_card).setVisibility(View.VISIBLE); @@ -1294,6 +1313,7 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener } public void enterFullScreenMode() { + inFullscreenMode = true; hideFloatingWalletBalance(); ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { @@ -1337,6 +1357,7 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener if (actionBar != null) { actionBar.show(); } + inFullscreenMode = false; } private void initKeyStore() { diff --git a/app/src/main/java/io/lbry/browser/listener/ScreenOrientationListener.java b/app/src/main/java/io/lbry/browser/listener/ScreenOrientationListener.java new file mode 100644 index 00000000..773d2340 --- /dev/null +++ b/app/src/main/java/io/lbry/browser/listener/ScreenOrientationListener.java @@ -0,0 +1,6 @@ +package io.lbry.browser.listener; + +public interface ScreenOrientationListener { + void onPortraitOrientationEntered(); + void onLandscapeOrientationEntered(); +} diff --git a/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java b/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java index 3b1a7b82..f464af14 100644 --- a/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java +++ b/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java @@ -94,6 +94,7 @@ import io.lbry.browser.dialog.SendTipDialogFragment; import io.lbry.browser.exceptions.LbryUriException; import io.lbry.browser.listener.DownloadActionListener; import io.lbry.browser.listener.FetchClaimsListener; +import io.lbry.browser.listener.ScreenOrientationListener; import io.lbry.browser.listener.SdkStatusListener; import io.lbry.browser.listener.StoragePermissionListener; import io.lbry.browser.model.Claim; @@ -135,7 +136,11 @@ import io.lbry.lbrysdk.LbrynetService; import io.lbry.lbrysdk.Utils; public class FileViewFragment extends BaseFragment implements - MainActivity.BackPressInterceptor, DownloadActionListener, FetchClaimsListener, SdkStatusListener, StoragePermissionListener { + MainActivity.BackPressInterceptor, DownloadActionListener, + FetchClaimsListener, + ScreenOrientationListener, + SdkStatusListener, + StoragePermissionListener { private static final int RELATED_CONTENT_SIZE = 16; private static final String DEFAULT_PLAYBACK_SPEED = "1x"; @@ -218,6 +223,7 @@ public class FileViewFragment extends BaseFragment implements MainActivity activity = (MainActivity) context; activity.setBackPressInterceptor(this); activity.addDownloadActionListener(this); + activity.addScreenOrientationListener(this); activity.addFetchClaimsListener(this); if (!MainActivity.hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, context)) { activity.addStoragePermissionListener(this); @@ -290,6 +296,7 @@ public class FileViewFragment extends BaseFragment implements if (claim != null) { Helper.saveViewHistory(currentUrl, claim); + checkAndLoadRelatedContent(); renderClaim(); if (claim.getFile() == null) { loadFile(); @@ -352,8 +359,8 @@ public class FileViewFragment extends BaseFragment implements } } - private void initWebView(View view) { - WebView webView = view.findViewById(R.id.file_view_webview); + private void initWebView(View root) { + WebView webView = root.findViewById(R.id.file_view_webview); webView.setWebViewClient(new LbryWebViewClient(getContext())); WebSettings webSettings = webView.getSettings(); webSettings.setAllowFileAccess(true); @@ -389,6 +396,7 @@ public class FileViewFragment extends BaseFragment implements logUrlEvent(url); resetViewCount(); resetFee(); + View root = getView(); if (root != null) { ((RecyclerView) root.findViewById(R.id.file_view_related_content_list)).setAdapter(null); @@ -480,6 +488,7 @@ public class FileViewFragment extends BaseFragment implements if (claim != null) { Helper.saveViewHistory(url, claim); + checkAndLoadRelatedContent(); renderClaim(); } } @@ -531,8 +540,10 @@ public class FileViewFragment extends BaseFragment implements MainActivity activity = (MainActivity) context; activity.removeDownloadActionListener(this); activity.removeFetchClaimsListener(this); + activity.removeScreenOrientationListener(this); activity.removeSdkStatusListener(this); activity.removeStoragePermissionListener(this); + //activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); } } @@ -578,6 +589,8 @@ public class FileViewFragment extends BaseFragment implements checkAndResetNowPlayingClaim(); loadFile(); + + checkAndLoadRelatedContent(); renderClaim(); } else { // render nothing at location @@ -594,8 +607,6 @@ public class FileViewFragment extends BaseFragment implements } private void initUi(View root) { - initWebView(root); - buttonPublishSomething.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -1189,13 +1200,18 @@ public class FileViewFragment extends BaseFragment implements } else { restoreMainActionButton(); } + } + checkOwnClaim(); + } + private void checkAndLoadRelatedContent() { + View root = getView(); + if (root != null) { RecyclerView relatedContentList = root.findViewById(R.id.file_view_related_content_list); if (relatedContentList == null || relatedContentList.getAdapter() == null || relatedContentList.getAdapter().getItemCount() == 0) { loadRelatedContent(); } } - checkOwnClaim(); } private void showUnsupportedView() { @@ -1553,25 +1569,31 @@ public class FileViewFragment extends BaseFragment implements if (!fileExists) { showError(getString(R.string.claim_file_not_found, claimFile != null ? claimFile.getDownloadPath() : "")); } else if (fileUri != null) { - if (mediaType.startsWith("image")) { - // display the image - View container = getView().findViewById(R.id.file_view_imageviewer_container); - PhotoView photoView = getView().findViewById(R.id.file_view_imageviewer); + View root = getView(); + Context context = getContext(); + if (root != null) { + if (mediaType.startsWith("image")) { + // display the image + View container = root.findViewById(R.id.file_view_imageviewer_container); + PhotoView photoView = root.findViewById(R.id.file_view_imageviewer); - Glide.with(getContext().getApplicationContext()).load(fileUri).centerInside().into(photoView); - hideFloatingWalletBalance(); - container.setVisibility(View.VISIBLE); - } else if (mediaType.startsWith("text")) { - // show web view (and parse markdown too) - View container = getView().findViewById(R.id.file_view_webview_container); - WebView webView = getView().findViewById(R.id.file_view_webview); - if (Arrays.asList("text/markdown", "text/md").contains(mediaType.toLowerCase())) { - loadMarkdownFromFile(claimFile.getDownloadPath()); - } else { - webView.loadUrl(fileUri.toString()); + if (context != null) { + Glide.with(context.getApplicationContext()).load(fileUri).centerInside().into(photoView); + } + hideFloatingWalletBalance(); + container.setVisibility(View.VISIBLE); + } else if (mediaType.startsWith("text")) { + // show web view (and parse markdown too) + View container = root.findViewById(R.id.file_view_webview_container); + WebView webView = root.findViewById(R.id.file_view_webview); + if (Arrays.asList("text/markdown", "text/md").contains(mediaType.toLowerCase())) { + loadMarkdownFromFile(claimFile.getDownloadPath()); + } else { + webView.loadUrl(fileUri.toString()); + } + hideFloatingWalletBalance(); + container.setVisibility(View.VISIBLE); } - hideFloatingWalletBalance(); - container.setVisibility(View.VISIBLE); } handled = true; } @@ -1644,6 +1666,7 @@ public class FileViewFragment extends BaseFragment implements SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); canShowMatureContent = sp.getBoolean(MainActivity.PREFERENCE_KEY_SHOW_MATURE_CONTENT, false); } + LighthouseSearchTask relatedTask = new LighthouseSearchTask( title, RELATED_CONTENT_SIZE, 0, canShowMatureContent, claimId, relatedLoading, new ClaimSearchResultHandler() { @Override @@ -1655,30 +1678,33 @@ public class FileViewFragment extends BaseFragment implements } } - relatedContentAdapter = new ClaimListAdapter(filteredClaims, context); - relatedContentAdapter.setListener(new ClaimListAdapter.ClaimListItemListener() { - @Override - public void onClaimClicked(Claim claim) { - if (context instanceof MainActivity) { - MainActivity activity = (MainActivity) context; - if (claim.getName().startsWith("@")) { - activity.openChannelUrl(claim.getPermanentUrl()); - } else { - activity.openFileUrl(claim.getPermanentUrl()); //openClaimUrl(claim.getPermanentUrl()); + Context ctx = getContext(); + if (ctx != null) { + relatedContentAdapter = new ClaimListAdapter(filteredClaims, ctx); + relatedContentAdapter.setListener(new ClaimListAdapter.ClaimListItemListener() { + @Override + public void onClaimClicked(Claim claim) { + if (context instanceof MainActivity) { + MainActivity activity = (MainActivity) context; + if (claim.getName().startsWith("@")) { + activity.openChannelUrl(claim.getPermanentUrl()); + } else { + activity.openFileUrl(claim.getPermanentUrl()); //openClaimUrl(claim.getPermanentUrl()); + } } } + }); + + View v = getView(); + if (v != null) { + RecyclerView relatedContentList = root.findViewById(R.id.file_view_related_content_list); + relatedContentList.setAdapter(relatedContentAdapter); + relatedContentAdapter.notifyDataSetChanged(); + + Helper.setViewVisibility( + v.findViewById(R.id.file_view_no_related_content), + relatedContentAdapter == null || relatedContentAdapter.getItemCount() == 0 ? View.VISIBLE : View.GONE); } - }); - - View v = getView(); - if (v != null) { - RecyclerView relatedContentList = root.findViewById(R.id.file_view_related_content_list); - relatedContentList.setAdapter(relatedContentAdapter); - relatedContentAdapter.notifyDataSetChanged(); - - Helper.setViewVisibility( - v.findViewById(R.id.file_view_no_related_content), - relatedContentAdapter == null || relatedContentAdapter.getItemCount() == 0 ? View.VISIBLE : View.GONE); } } @@ -1748,12 +1774,12 @@ public class FileViewFragment extends BaseFragment implements } } + @SuppressLint("SourceLockedOrientationActivity") private void disableFullScreenMode() { Context context = getContext(); if (context instanceof MainActivity) { MainActivity activity = (MainActivity) context; View root = getView(); - RelativeLayout mediaContainer = root.findViewById(R.id.file_view_media_container); View exoplayerContainer = root.findViewById(R.id.file_view_exoplayer_container); ((ViewGroup) exoplayerContainer.getParent()).removeView(exoplayerContainer); @@ -2055,6 +2081,22 @@ public class FileViewFragment extends BaseFragment implements checkOwnClaim(); } + @Override + public void onPortraitOrientationEntered() { + Context context = getContext(); + if (context instanceof MainActivity && ((MainActivity) context).isInFullscreenMode()) { + disableFullScreenMode(); + } + } + + @Override + public void onLandscapeOrientationEntered() { + Context context = getContext(); + if (claim != null && claim.isPlayable() && context instanceof MainActivity && !((MainActivity) context).isInFullscreenMode()) { + enableFullScreenMode(); + } + } + private static class LbryWebViewClient extends WebViewClient { private Context context; public LbryWebViewClient(Context context) {