From 6feccf3973d1189781dd1826d2fea1fb0b11996b Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 23 Jun 2019 22:36:49 +0200 Subject: [PATCH] fix #72, add Print functionality at fullscreen images --- .../pro/activities/ViewPagerActivity.kt | 64 +++++++++++++++++++ .../gallery/pro/helpers/Constants.kt | 1 + app/src/main/res/menu/menu_viewpager.xml | 4 ++ 3 files changed, 69 insertions(+) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt index 285277d44..eedd54868 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt @@ -10,6 +10,7 @@ import android.content.pm.ShortcutInfo import android.content.pm.ShortcutManager import android.content.res.Configuration import android.database.Cursor +import android.graphics.Bitmap import android.graphics.Color import android.graphics.drawable.ColorDrawable import android.graphics.drawable.Icon @@ -24,7 +25,15 @@ import android.view.View import android.view.WindowManager import android.view.animation.DecelerateInterpolator import android.widget.Toast +import androidx.print.PrintHelper import androidx.viewpager.widget.ViewPager +import com.bumptech.glide.Glide +import com.bumptech.glide.load.DataSource +import com.bumptech.glide.load.engine.DiskCacheStrategy +import com.bumptech.glide.load.engine.GlideException +import com.bumptech.glide.request.RequestListener +import com.bumptech.glide.request.RequestOptions +import com.bumptech.glide.request.target.Target import com.simplemobiletools.commons.dialogs.PropertiesDialog import com.simplemobiletools.commons.dialogs.RenameItemDialog import com.simplemobiletools.commons.extensions.* @@ -160,6 +169,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View findItem(R.id.menu_copy_to).isVisible = visibleBottomActions and BOTTOM_ACTION_COPY == 0 findItem(R.id.menu_move_to).isVisible = visibleBottomActions and BOTTOM_ACTION_MOVE == 0 findItem(R.id.menu_save_as).isVisible = rotationDegrees != 0 + findItem(R.id.menu_print).isVisible = currentMedium.isImage() || currentMedium.isRaw() findItem(R.id.menu_hide).isVisible = !currentMedium.isHidden() && visibleBottomActions and BOTTOM_ACTION_TOGGLE_VISIBILITY == 0 && !currentMedium.getIsInRecycleBin() findItem(R.id.menu_unhide).isVisible = currentMedium.isHidden() && visibleBottomActions and BOTTOM_ACTION_TOGGLE_VISIBILITY == 0 && !currentMedium.getIsInRecycleBin() findItem(R.id.menu_add_to_favorites).isVisible = !currentMedium.isFavorite && visibleBottomActions and BOTTOM_ACTION_TOGGLE_FAVORITE == 0 && !currentMedium.getIsInRecycleBin() @@ -197,6 +207,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View R.id.menu_share -> shareMediumPath(getCurrentPath()) R.id.menu_delete -> checkDeleteConfirmation() R.id.menu_rename -> renameFile() + R.id.menu_print -> printFile() R.id.menu_edit -> openEditor(getCurrentPath()) R.id.menu_properties -> showProperties() R.id.menu_show_on_map -> showOnMap() @@ -874,6 +885,59 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View }.start() } + private fun printFile() { + sendPrintIntent(getCurrentPath()) + } + + private fun sendPrintIntent(path: String) { + val printHelper = PrintHelper(this) + printHelper.scaleMode = PrintHelper.SCALE_MODE_FIT + printHelper.orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT + + try { + val resolution = path.getImageResolution() + if (resolution == null) { + toast(R.string.unknown_error_occurred) + return + } + + var requestedWidth = resolution.x + var requestedHeight = resolution.y + + if (requestedWidth >= MAX_PRINT_SIDE_SIZE) { + requestedHeight = (requestedHeight / (requestedWidth / MAX_PRINT_SIDE_SIZE.toFloat())).toInt() + requestedWidth = MAX_PRINT_SIDE_SIZE + } else if (requestedHeight >= MAX_PRINT_SIDE_SIZE) { + requestedWidth = (requestedWidth / (requestedHeight / MAX_PRINT_SIDE_SIZE.toFloat())).toInt() + requestedHeight = MAX_PRINT_SIDE_SIZE + } + + val options = RequestOptions() + .skipMemoryCache(true) + .diskCacheStrategy(DiskCacheStrategy.NONE) + + Glide.with(this) + .asBitmap() + .load(path) + .apply(options) + .listener(object : RequestListener { + override fun onLoadFailed(e: GlideException?, model: Any?, target: Target?, isFirstResource: Boolean): Boolean { + showErrorToast(e?.localizedMessage ?: "") + return false + } + + override fun onResourceReady(bitmap: Bitmap?, model: Any?, target: Target?, dataSource: DataSource?, isFirstResource: Boolean): Boolean { + if (bitmap != null) { + printHelper.printBitmap(path.getFilenameFromPath(), bitmap) + } + + return false + } + }).submit(requestedWidth, requestedHeight) + } catch (e: Exception) { + } + } + private fun restoreFile() { restoreRecycleBinPath(getCurrentPath()) { refreshViewPager() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Constants.kt index 8193e9de8..015c12beb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Constants.kt @@ -115,6 +115,7 @@ const val DRAG_THRESHOLD = 8 const val MONTH_MILLISECONDS = MONTH_SECONDS * 1000L const val MIN_SKIP_LENGTH = 2000 const val HIDE_SYSTEM_UI_DELAY = 500L +const val MAX_PRINT_SIDE_SIZE = 4096 const val DIRECTORY = "directory" const val MEDIUM = "medium" diff --git a/app/src/main/res/menu/menu_viewpager.xml b/app/src/main/res/menu/menu_viewpager.xml index f995fe0f1..ed3dcc8ac 100644 --- a/app/src/main/res/menu/menu_viewpager.xml +++ b/app/src/main/res/menu/menu_viewpager.xml @@ -110,6 +110,10 @@ android:icon="@drawable/ic_rename_new" android:title="@string/rename" app:showAsAction="ifRoom"/> +