From cbbd3b2aaf466613f3cc203c502af7ef2cfd44e7 Mon Sep 17 00:00:00 2001 From: wolpi Date: Sun, 5 May 2024 15:46:59 +0200 Subject: [PATCH] Avoiding to declare orientation in manifest as that kills review for TV. Instead using setRequestedOrientation() programmatically to avoid orientation change while copy dialog is shown, see GH isse #257 --- primitiveFTPd/AndroidManifest.xml | 4 +-- .../share/AbstractReceiveShareActivity.java | 30 ++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/primitiveFTPd/AndroidManifest.xml b/primitiveFTPd/AndroidManifest.xml index 0b6be96f..312f00e5 100644 --- a/primitiveFTPd/AndroidManifest.xml +++ b/primitiveFTPd/AndroidManifest.xml @@ -162,7 +162,7 @@ android:theme="@style/PrimFtpdDeviceTheme" android:name="org.primftpd.share.ReceiveSaveAsActivity" android:label="@string/saveAs" - android:screenOrientation="portrait" + android:configChanges="orientation" android:exported="true"> @@ -220,7 +220,7 @@ android:theme="@style/PrimFtpdDeviceTheme" android:name="org.primftpd.share.ReceiveQuickShareActivity" android:label="@string/quickShare" - android:screenOrientation="portrait" + android:configChanges="orientation" android:exported="true"> diff --git a/primitiveFTPd/src/org/primftpd/share/AbstractReceiveShareActivity.java b/primitiveFTPd/src/org/primftpd/share/AbstractReceiveShareActivity.java index 0030b8b8..1216c140 100644 --- a/primitiveFTPd/src/org/primftpd/share/AbstractReceiveShareActivity.java +++ b/primitiveFTPd/src/org/primftpd/share/AbstractReceiveShareActivity.java @@ -1,10 +1,15 @@ package org.primftpd.share; import android.app.ProgressDialog; +import android.content.pm.ActivityInfo; import android.net.Uri; import android.os.AsyncTask; +import android.os.Bundle; import android.os.Handler; import android.os.PowerManager; +import android.util.DisplayMetrics; +import android.view.Display; +import android.view.WindowManager; import org.primftpd.services.AbstractServerService; import org.primftpd.util.FilenameUnique; @@ -17,12 +22,32 @@ import java.io.OutputStream; import java.util.List; +import androidx.annotation.Nullable; import androidx.fragment.app.FragmentActivity; public abstract class AbstractReceiveShareActivity extends FragmentActivity { protected Logger logger = LoggerFactory.getLogger(getClass()); + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // if screen orientation changes and copy-dialog is currently shown + // the dialog will be destroyed + // to avoid that we request a specific orientation to prevent it being changed + // check first if we have a landscape or portrait like screen + // re-set after copy is finished + Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); + DisplayMetrics metrics = new DisplayMetrics(); + display.getMetrics(metrics); + boolean isWideScreen = metrics.heightPixels < metrics.widthPixels; + int requestedOrientation = isWideScreen + ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE + : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; + setRequestedOrientation(requestedOrientation); + } + protected void saveUris( ProgressDialog progressDialog, final TargetDir targetDir, @@ -145,7 +170,10 @@ protected void onPostExecute(Void result) { logger.warn("error while releasing wake lock", e); } - mainThreadHandler.post(() -> activity.onCopyFinished(targetDir)); + mainThreadHandler.post(() -> { + activity.onCopyFinished(targetDir); + activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); + }); } }