OSDN Git Service

日本語リソースを準備。
[gokigen/PKRemote.git] / app / src / main / java / net / osdn / gokigen / pkremote / playback / detail / ImagePagerViewFragment.java
1 package net.osdn.gokigen.pkremote.playback.detail;
2
3
4 import android.app.Activity;
5 import android.graphics.Bitmap;
6 import android.os.Bundle;
7 import android.util.Log;
8 import android.view.LayoutInflater;
9 import android.view.Menu;
10 import android.view.MenuInflater;
11 import android.view.MenuItem;
12 import android.view.View;
13 import android.view.ViewGroup;
14 import android.widget.ImageView;
15
16 import net.osdn.gokigen.pkremote.R;
17 import net.osdn.gokigen.pkremote.camera.interfaces.control.ICameraRunMode;
18 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraContent;
19 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraFileInfo;
20 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IContentInfoCallback;
21 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IDownloadThumbnailImageCallback;
22 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IPlaybackControl;
23
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26 import java.util.List;
27 import java.util.Locale;
28 import java.util.Map;
29
30 import androidx.annotation.NonNull;
31 import androidx.appcompat.app.ActionBar;
32 import androidx.appcompat.app.AlertDialog;
33 import androidx.appcompat.app.AppCompatActivity;
34 import androidx.collection.LruCache;
35 import androidx.fragment.app.Fragment;
36 import androidx.fragment.app.FragmentActivity;
37 import androidx.viewpager.widget.PagerAdapter;
38 import androidx.viewpager.widget.ViewPager;
39
40 public class ImagePagerViewFragment extends Fragment
41 {
42     private final String TAG = this.toString();
43     private static final String JPEG_SUFFIX = ".JPG";
44
45     private IPlaybackControl playbackControl;
46     private ICameraRunMode runMode;
47
48         private List<CameraContentEx> contentList = null;
49         private int contentIndex = 0;
50
51         private LayoutInflater layoutInflater = null;
52         private ViewPager viewPager = null;
53         private LruCache<String, Bitmap> imageCache =null;
54
55
56     public static ImagePagerViewFragment newInstance(@NonNull IPlaybackControl playbackControl, @NonNull ICameraRunMode runMode, @NonNull List<CameraContentEx> contentList, int contentIndex)
57         {
58                 ImagePagerViewFragment fragment = new ImagePagerViewFragment();
59                 fragment.setInterface(playbackControl, runMode);
60                 fragment.setContentList(contentList, contentIndex);
61                 return (fragment);
62         }
63
64
65         private void setInterface(@NonNull IPlaybackControl playbackControl, @NonNull ICameraRunMode runMode)
66     {
67         this.playbackControl = playbackControl;
68         this.runMode = runMode;
69     }
70
71
72         private void setContentList(@NonNull List<CameraContentEx> contentList, int contentIndex)
73         {
74                 this.contentList = contentList;
75                 this.contentIndex = contentIndex;
76         }
77
78         @Override
79         public void onCreate(Bundle savedInstanceState)
80         {
81                 super.onCreate(savedInstanceState);
82                 imageCache = new LruCache<>(7);
83                 setHasOptionsMenu(true);
84         }
85
86         @Override
87         public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
88         {
89                 layoutInflater = inflater;
90                 View view = layoutInflater.inflate(R.layout.fragment_image_pager_view, container, false);
91                 viewPager = view.findViewById(R.id.viewPager1);
92                 viewPager.setAdapter(new ImagePagerAdapter());
93                 viewPager.addOnPageChangeListener(new ImagePageChangeListener());
94                 
95                 return (view);
96         }
97
98         @Override
99         public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
100         {
101                 try
102                 {
103             CameraContentEx info  = contentList.get(contentIndex);
104             ICameraContent file = info.getFileInfo();
105                         String path = file.getContentPath() + "/" + file.getContentName();
106
107                         AppCompatActivity activity = (AppCompatActivity) getActivity();
108                         if (activity != null)
109                         {
110                 ActionBar bar = activity.getSupportActionBar();
111                 if (bar != null)
112                 {
113                     bar.show();
114                     bar.setTitle(path);
115                 }
116             }
117                         String lowerCasePath = path.toUpperCase();
118                         if (lowerCasePath.endsWith(JPEG_SUFFIX))
119             {
120                 if (info.hasRaw())
121                 {
122                     inflater.inflate(R.menu.image_view_with_raw, menu);
123                     MenuItem downloadMenuItem = menu.findItem(R.id.action_download_with_raw);
124                     downloadMenuItem.setEnabled(true);
125                 }
126                 else
127                 {
128                     inflater.inflate(R.menu.image_view, menu);
129                     MenuItem downloadMenuItem = menu.findItem(R.id.action_download);
130                     downloadMenuItem.setEnabled(true);
131                 }
132                         }
133             else
134             {
135                                 inflater.inflate(R.menu.movie_view, menu);
136                                 MenuItem downloadMenuItem = menu.findItem(R.id.action_download_movie);
137                                 downloadMenuItem.setEnabled(true);
138                         }
139                 }
140                 catch (Exception e)
141                 {
142                         e.printStackTrace();
143                 }
144         }
145
146         @Override
147         public boolean onOptionsItemSelected(MenuItem item)
148         {
149                 boolean doDownload = false;
150         boolean getInformation = false;
151                 boolean isSmallSize = false;
152                 boolean isRaw = false;
153         if ((item.getItemId() == R.id.action_get_information)||(item.getItemId() == R.id.action_get_information_raw))
154         {
155             getInformation = true;
156         }
157         else if ((item.getItemId() == R.id.action_download_original_size)||(item.getItemId() == R.id.action_download_original_size_raw))
158         {
159                         doDownload = true;
160                 }
161         else if ((item.getItemId() == R.id.action_download_640x480)||(item.getItemId() == R.id.action_download_640x480_raw))
162         {
163             isSmallSize = true;
164             doDownload = true;
165         }
166         else if (item.getItemId() == R.id.action_download_original_movie)
167         {
168             doDownload = true;
169         }
170         else if (item.getItemId() == R.id.action_download_raw)
171         {
172             doDownload = true;
173             isRaw = true;
174                 }
175
176                 if (getInformation)
177         {
178                 Thread thread = new Thread(new Runnable() {
179                                 @Override
180                                 public void run()
181                 {
182                     showFileInformation((contentList.get(contentIndex)).getFileInfo());
183                 }
184                 });
185                 try
186             {
187                 thread.start();
188             }
189             catch (Exception e)
190             {
191                 e.printStackTrace();
192             }
193         }
194
195                 if (doDownload)
196                 {
197                         try
198                         {
199                                 //  ダイアログを表示して保存する
200                                 saveImageWithDialog(isRaw, isSmallSize);
201                         }
202                         catch (Exception e)
203                         {
204                                 e.printStackTrace();
205                         }
206                 }
207                 return (super.onOptionsItemSelected(item));
208         }
209
210     /**
211      *
212      *
213      */
214     private void showFileInformation(final ICameraContent contentInfo)
215     {
216         if (playbackControl != null)
217         {
218             playbackControl.getContentInfo(contentInfo.getContentPath(), contentInfo.getContentName(), new IContentInfoCallback() {
219                 /**
220                  *
221                  *
222                  */
223                 @Override
224                 public void onCompleted(final ICameraFileInfo fileInfo)
225                 {
226                     runOnUiThread(new Runnable() {
227                         @Override
228                         public void run() {
229                             String message = "";
230                             try {
231                                 String model = fileInfo.getModel();
232                                 String dateTime = "";
233                                 Date date = fileInfo.getDatetime();
234                                 if (date != null) {
235                                     dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US).format(date);
236                                 }
237                                 String path = fileInfo.getDirectoryPath() + "/" + fileInfo.getFilename();
238                                 String shutter = fileInfo.getShutterSpeed();
239                                 shutter = shutter.replace(".", "/");
240                                 String aperture = fileInfo.getAperature();
241                                 String iso = fileInfo.getIsoSensitivity();
242                                 String expRev = fileInfo.getExpRev();
243
244                                 message = path + "\r\n" + dateTime + "\r\n" + "- - - - - - - - - -\r\n  " + shutter + "  F" + aperture + " (" + expRev + ")" + " ISO" + iso + "\r\n" + "- - - - - - - - - -\r\n" + "  model : " + model + "\r\n";
245                             }
246                             catch (Exception e)
247                             {
248                                 e.printStackTrace();
249                             }
250                             presentMessage(getString(R.string.download_control_get_information_title), message);
251                             System.gc();
252                         }
253                     });
254                 }
255
256                 /**
257                  *
258                  *
259                  */
260                 @Override
261                 public void onErrorOccurred(Exception e)
262                 {
263                     e.printStackTrace();
264                 }
265             });
266         }
267
268     }
269
270     @Override
271         public void onResume()
272     {
273                 super.onResume();
274                 AppCompatActivity activity = (AppCompatActivity)getActivity();
275                 if (activity != null)
276                 {
277             ActionBar bar = activity.getSupportActionBar();
278             if (bar != null)
279             {
280                 bar.setDisplayShowHomeEnabled(true);
281                 bar.show();
282             }
283         }
284
285         if (runMode.isRecordingMode())
286         {
287             // Threadで呼んではダメみたいだ...
288             runMode.changeRunMode(false);
289         }
290
291         viewPager.setCurrentItem(contentIndex);
292         }
293
294         @Override
295         public void onPause()
296         {
297                 super.onPause();
298                 AppCompatActivity activity = (AppCompatActivity)getActivity();
299                 if (activity != null)
300                 {
301             ActionBar bar = activity.getSupportActionBar();
302             if (bar != null)
303             {
304                 bar.hide();
305             }
306         }
307
308         if (!runMode.isRecordingMode())
309         {
310             // Threadで呼んではダメみたいだ...
311             runMode.changeRunMode(true);
312         }
313     }
314
315         private class ImagePagerAdapter extends PagerAdapter
316     {
317
318                 @Override
319                 public int getCount()
320         {
321             int count = 0;
322                     try
323             {
324                 count = contentList.size();
325             }
326             catch (Exception e)
327             {
328                 e.printStackTrace();
329             }
330                         return (count);
331                 }
332
333                 @Override
334                 public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
335         {
336                         return (view.equals(object));
337                 }
338                 
339                 @Override
340                 public @NonNull Object instantiateItem(@NonNull ViewGroup container, int position)
341         {
342                         ImageView view = (ImageView)layoutInflater.inflate(R.layout.view_image_page, container, false);
343                         container.addView(view);
344                         downloadImage(position, view);
345                         return (view);
346                 }
347                 
348                 @Override
349                 public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object)
350                 {
351                         container.removeView((ImageView)object);
352                 }
353                 
354         }
355
356         private class ImagePageChangeListener implements ViewPager.OnPageChangeListener
357         {
358
359                 @Override
360                 public void onPageScrollStateChanged(int state)
361                 {
362
363                 }
364
365                 @Override
366                 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
367                 {
368
369                 }
370
371                 @Override
372                 public void onPageSelected(int position)
373                 {
374                         contentIndex = position;
375                         try
376                         {
377                 ICameraContent file = (contentList.get(contentIndex)).getFileInfo();
378                 String path = file.getContentPath() + "/" + file.getContentName();
379
380                 AppCompatActivity activity = (AppCompatActivity) getActivity();
381                 if (activity != null)
382                 {
383                     ActionBar bar = activity.getSupportActionBar();
384                     if (bar != null)
385                     {
386                         bar.setTitle(path);
387                     }
388                     //activity.getSupportActionBar().setTitle(path);
389                     activity.getFragmentManager().invalidateOptionsMenu();
390                 }
391                         }
392                         catch (Exception e)
393             {
394                 e.printStackTrace();
395             }
396                 }
397         }
398
399     /**
400      *
401      *
402      */
403         private void downloadImage(final int position, final ImageView view)
404         {
405                 Thread thread = new Thread(new Runnable() {
406                         @Override
407                         public void run() {
408                                 downloadImageImpl(position, view);
409                         }
410                 });
411                 try
412                 {
413                         thread.start();
414                 }
415                 catch (Exception e)
416                 {
417                         e.printStackTrace();
418                 }
419         }
420
421     /**
422      *
423      *
424      */
425         private void downloadImageImpl(int position, final ImageView view)
426     {
427         try
428         {
429             ICameraContent file = (contentList.get(position)).getFileInfo();
430             final String path = file.getContentPath() + "/" + file.getContentName();
431
432             // Get the cached image.
433             final Bitmap bitmap = imageCache.get(path);
434             if (bitmap != null)
435             {
436                 if (view != null && viewPager.indexOfChild(view) > -1)
437                 {
438                     runOnUiThread(new Runnable()
439                     {
440                         @Override
441                         public void run()
442                         {
443                             view.setImageBitmap(bitmap);
444                         }
445                     });
446                 }
447                 return;
448             }
449
450             // Download the image.
451             playbackControl.downloadContentScreennail(path, new IDownloadThumbnailImageCallback() {
452                 @Override
453                                 //public void onCompleted(final byte[] data, final Map<String, Object> metadata) {
454                 public void onCompleted(final Bitmap bitmap, final Map<String, Object> metadata) {
455                     // Cache the downloaded image.
456
457                     //final Bitmap bitmap = createRotatedBitmap(data, metadata);
458                     try {
459                         if (bitmap == null)
460                         {
461                             System.gc();
462                             return;
463                         }
464                         if (imageCache != null)
465                                                 {
466                             imageCache.put(path, bitmap);
467                         }
468                     }
469                     catch (Exception e)
470                     {
471                         e.printStackTrace();
472                     }
473                     runOnUiThread(new Runnable() {
474                         @Override
475                         public void run() {
476                             if ((bitmap != null) && (view != null) && (viewPager.indexOfChild(view) > -1))
477                             {
478                                 view.setImageBitmap(bitmap);
479                             }
480                         }
481                     });
482                 }
483
484                 @Override
485                 public void onErrorOccurred(Exception e) {
486                     final String message = e.getMessage();
487                     runOnUiThread(new Runnable() {
488                         @Override
489                         public void run() {
490                             presentMessage("Load failed", message);
491                         }
492                     });
493                 }
494             });
495         }
496         catch (Exception e)
497         {
498             e.printStackTrace();
499         }
500         }
501
502         /**
503          *   デバイスに画像ファイルをダウンロード(保存)する
504          *
505          * @param isRaw           RAWファイルをダウンロードするか
506          * @param isSmallSize    小さいサイズの量にするか
507      */
508         private void saveImageWithDialog(final boolean isRaw, final boolean isSmallSize)
509         {
510         Log.v(TAG, "saveImageWithDialog() : raw : " + isRaw + " (small : " + isSmallSize + ")");
511         try
512         {
513             final Activity activity = getActivity();
514             if (activity != null)
515             {
516                 Thread thread = new Thread(new Runnable() {
517                     @Override
518                     public void run() {
519                         MyContentDownloader contentDownloader = new MyContentDownloader(activity, playbackControl);
520                         CameraContentEx infoEx = contentList.get(contentIndex);
521                         if (infoEx != null)
522                         {
523                             ICameraContent fileInfo = infoEx.getFileInfo();
524                             contentDownloader.startDownload(fileInfo, "", (isRaw) ? infoEx.getRawSuffix() : null, isSmallSize);
525                         }
526                     }
527                 });
528                 thread.start();
529             }
530         }
531         catch (Exception e)
532         {
533             e.printStackTrace();
534         }
535         }
536
537         // -------------------------------------------------------------------------
538         // Helpers
539         // -------------------------------------------------------------------------
540         
541         private void presentMessage(final String title, final String message)
542     {
543         runOnUiThread(new Runnable() {
544                 @Override
545                 public void run() {
546                     FragmentActivity activity = getActivity();
547                     if (activity != null) {
548                         AlertDialog.Builder builder = new AlertDialog.Builder(activity);
549                         builder.setTitle(title).setMessage(message);
550                         builder.show();
551                     }
552                 }
553         });
554         }
555         
556         private void runOnUiThread(Runnable action)
557     {
558                 if (getActivity() == null)
559         {
560                         return;
561                 }
562                 getActivity().runOnUiThread(action);
563         }
564
565 /*
566         private Bitmap createRotatedBitmap(byte[] data, Map<String, Object> metadata)
567         {
568                 Bitmap bitmap;
569                 try
570                 {
571                         bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
572                 }
573                 catch (OutOfMemoryError e)
574                 {
575                         e.printStackTrace();
576                         bitmap = null;
577                         System.gc();
578                 }
579                 if (bitmap == null)
580                 {
581                         return (null);
582                 }
583
584                 // ビットマップの回転を行う
585                 int degrees = getRotationDegrees(data, metadata);
586                 if (degrees != 0)
587                 {
588                         Matrix m = new Matrix();
589                         m.postRotate(degrees);
590                         try
591                         {
592                                 bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
593                         }
594                         catch (OutOfMemoryError e)
595                         {
596                                 e.printStackTrace();
597                                 bitmap = null;
598                                 System.gc();
599                         }
600                 }
601                 return (bitmap);
602         }
603         
604         private int getRotationDegrees(byte[] data, Map<String, Object> metadata)
605         {
606                 int degrees = 0;
607                 int orientation = ExifInterface.ORIENTATION_UNDEFINED;
608                 
609                 if (metadata != null && metadata.containsKey("Orientation")) {
610                         orientation = Integer.parseInt((String)metadata.get("Orientation"));
611                 }
612                 else
613                 {
614                         // Gets image orientation to display a picture.
615                         try
616             {
617                                 File tempFile = File.createTempFile("temp", null);
618                                 {
619                                         FileOutputStream outStream = new FileOutputStream(tempFile.getAbsolutePath());
620                                         outStream.write(data);
621                                         outStream.close();
622                                 }
623                                 ExifInterface exifInterface = new ExifInterface(tempFile.getAbsolutePath());
624                                 orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
625                                 if (!tempFile.delete())
626                 {
627                     Log.v(TAG, "temp file delete failure.");
628                 }
629                         }
630             catch (IOException e)
631             {
632                 e.printStackTrace();
633                         }
634                 }
635
636                 switch (orientation)
637                 {
638             case ExifInterface.ORIENTATION_NORMAL:
639                 degrees = 0;
640                 break;
641             case ExifInterface.ORIENTATION_ROTATE_90:
642                 degrees = 90;
643                 break;
644             case ExifInterface.ORIENTATION_ROTATE_180:
645                 degrees = 180;
646                 break;
647             case ExifInterface.ORIENTATION_ROTATE_270:
648                 degrees = 270;
649                 break;
650             default:
651                 break;
652                 }
653                 return degrees;
654         }
655 */
656 }