|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 Tobias Schoene www.yaacc.de |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU General Public License |
| 6 | + * as published by the Free Software Foundation; either version 3 |
| 7 | + * of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | + */ |
| 18 | +package de.yaacc.imageviewer; |
| 19 | + |
| 20 | +import android.graphics.Bitmap; |
| 21 | +import android.net.Uri; |
| 22 | +import android.os.Bundle; |
| 23 | +import android.view.LayoutInflater; |
| 24 | +import android.view.View; |
| 25 | +import android.view.ViewGroup; |
| 26 | +import android.widget.ImageView; |
| 27 | +import android.widget.ProgressBar; |
| 28 | + |
| 29 | +import androidx.annotation.NonNull; |
| 30 | +import androidx.annotation.Nullable; |
| 31 | +import androidx.fragment.app.Fragment; |
| 32 | +import androidx.lifecycle.Lifecycle; |
| 33 | +import androidx.lifecycle.LifecycleEventObserver; |
| 34 | + |
| 35 | +import de.yaacc.R; |
| 36 | +import de.yaacc.Yaacc; |
| 37 | +import de.yaacc.util.YaaccLogger; |
| 38 | + |
| 39 | +/** |
| 40 | + * Fragment for displaying a single image in ViewPager2. |
| 41 | + */ |
| 42 | +public class ImageFragment extends Fragment { |
| 43 | + private static final String ARG_URI = "uri"; |
| 44 | + private Uri uri; |
| 45 | + private ImageView imageView; |
| 46 | + private ProgressBar progressBar; |
| 47 | + |
| 48 | + public static ImageFragment newInstance(Uri uri) { |
| 49 | + ImageFragment fragment = new ImageFragment(); |
| 50 | + Bundle args = new Bundle(); |
| 51 | + args.putParcelable(ARG_URI, uri); |
| 52 | + fragment.setArguments(args); |
| 53 | + return fragment; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void onCreate(@Nullable Bundle savedInstanceState) { |
| 58 | + super.onCreate(savedInstanceState); |
| 59 | + if (getArguments() != null) { |
| 60 | + uri = getArguments().getParcelable(ARG_URI); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Nullable |
| 65 | + @Override |
| 66 | + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, |
| 67 | + @Nullable Bundle savedInstanceState) { |
| 68 | + View view = inflater.inflate(R.layout.fragment_image, container, false); |
| 69 | + imageView = view.findViewById(R.id.imageView); |
| 70 | + progressBar = view.findViewById(R.id.progressBar); |
| 71 | + |
| 72 | + // Add click listener to toggle controls |
| 73 | + imageView.setOnClickListener(v -> { |
| 74 | + if (getActivity() instanceof ImageViewerActivity) { |
| 75 | + ((ImageViewerActivity) getActivity()).toggleControlsFromFragment(); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + return view; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { |
| 84 | + super.onViewCreated(view, savedInstanceState); |
| 85 | + if (uri != null) { |
| 86 | + loadImage(uri); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public void loadImage(Uri imageUri) { |
| 91 | + if (imageView == null || imageUri == null) return; |
| 92 | + |
| 93 | + YaaccLogger.d(getClass().getName(), "Loading image: " + imageUri); |
| 94 | + |
| 95 | + // Show loading indicator |
| 96 | + if (progressBar != null) { |
| 97 | + progressBar.setVisibility(View.VISIBLE); |
| 98 | + } |
| 99 | + imageView.setImageBitmap(null); |
| 100 | + |
| 101 | + // Use existing ContentLoadExecutor |
| 102 | + Yaacc app = (Yaacc) requireActivity().getApplication(); |
| 103 | + app.getContentLoadExecutor().execute(() -> { |
| 104 | + Bitmap bitmap = loadBitmap(imageUri); |
| 105 | + if (getActivity() != null) { |
| 106 | + getActivity().runOnUiThread(() -> { |
| 107 | + // Hide loading indicator |
| 108 | + if (progressBar != null) { |
| 109 | + progressBar.setVisibility(View.GONE); |
| 110 | + } |
| 111 | + if (imageView != null && bitmap != null) { |
| 112 | + imageView.setImageBitmap(bitmap); |
| 113 | + } else if (imageView != null) { |
| 114 | + imageView.setImageResource(R.drawable.yaacc192_32); |
| 115 | + } |
| 116 | + }); |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + private Bitmap loadBitmap(Uri imageUri) { |
| 122 | + try { |
| 123 | + if (getActivity() != null) { |
| 124 | + String scheme = imageUri.getScheme(); |
| 125 | + if ("http".equals(scheme) || "https".equals(scheme)) { |
| 126 | + // Download from HTTP |
| 127 | + java.net.URL url = new java.net.URL(imageUri.toString()); |
| 128 | + return android.graphics.BitmapFactory.decodeStream(url.openStream()); |
| 129 | + } else { |
| 130 | + // Local file via ContentResolver |
| 131 | + return android.graphics.BitmapFactory.decodeStream( |
| 132 | + getActivity().getContentResolver().openInputStream(imageUri)); |
| 133 | + } |
| 134 | + } |
| 135 | + } catch (Exception e) { |
| 136 | + YaaccLogger.e(getClass().getName(), "Failed to load image: " + imageUri, e); |
| 137 | + } |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + public void setImageUri(Uri uri) { |
| 142 | + this.uri = uri; |
| 143 | + if (imageView != null) { |
| 144 | + loadImage(uri); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments