OSDN Git Service

画像の自動転送機能はOPCも使えないようにする。
[gokigen/PKRemote.git] / app / src / main / java / net / osdn / gokigen / pkremote / transfer / AutoTransferFragment.java
1 package net.osdn.gokigen.pkremote.transfer;
2
3 import android.content.Context;
4 import android.content.SharedPreferences;
5 import android.graphics.Bitmap;
6 import android.net.Uri;
7 import android.os.Bundle;
8 import android.os.Vibrator;
9 import android.util.Log;
10 import android.view.LayoutInflater;
11 import android.view.View;
12 import android.view.ViewGroup;
13 import android.widget.Button;
14 import android.widget.CheckBox;
15 import android.widget.ImageButton;
16 import android.widget.ImageView;
17 import android.widget.ProgressBar;
18 import android.widget.TextView;
19
20 import net.osdn.gokigen.pkremote.R;
21 import net.osdn.gokigen.pkremote.camera.interfaces.IInterfaceProvider;
22 import net.osdn.gokigen.pkremote.playback.IContentDownloadNotify;
23 import net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor;
24
25 import androidx.annotation.NonNull;
26 import androidx.appcompat.app.AppCompatActivity;
27 import androidx.fragment.app.Fragment;
28 import androidx.preference.PreferenceManager;
29
30 import static android.content.Context.VIBRATOR_SERVICE;
31
32 /**
33  *   自動転送クラス
34  *
35  */
36 public class AutoTransferFragment extends Fragment implements View.OnClickListener, ITransferMessage, IContentDownloadNotify
37 {
38     private final String TAG = this.toString();
39
40     private static final int SLEEP_MS = 300;         // 動作チェック待ち時間
41     private static final int SLEEP_WAIT_MS = 4500;  // 一覧確認の待機時間 (4.5秒おき)
42
43     private AppCompatActivity activity = null;
44     private FileAutoTransferMain transferMain = null;
45     private View myView = null;
46     private boolean transferThreadIsRunning = false;
47     private boolean startTransferReceived = false;
48
49     public static AutoTransferFragment newInstance(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider provider)
50     {
51         AutoTransferFragment instance = new AutoTransferFragment();
52         instance.prepare(context, provider);
53
54         // パラメータはBundleにまとめておく
55         Bundle arguments = new Bundle();
56         //arguments.putString("title", title);
57         //arguments.putString("message", message);
58         instance.setArguments(arguments);
59
60         return (instance);
61     }
62
63     /**
64      *
65      */
66     private void prepare(@NonNull AppCompatActivity activity, @NonNull IInterfaceProvider interfaceProvider)
67     {
68         Log.v(TAG, "prepare()");
69         this.activity = activity;
70         transferMain = new FileAutoTransferMain(activity, interfaceProvider, this, this);
71     }
72
73     /**
74      *
75      *
76      */
77     @Override
78     public void onCreate(Bundle savedInstanceState)
79     {
80         super.onCreate(savedInstanceState);
81         Log.v(TAG, "onCreate()");
82     }
83
84     /**
85      *
86      *
87      */
88     @Override
89     public void onAttach(Context context)
90     {
91         super.onAttach(context);
92         Log.v(TAG, "onAttach()");
93     }
94
95     /**
96      *
97      *
98      */
99     @Override
100     public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
101     {
102         super.onCreateView(inflater, container, savedInstanceState);
103
104         Log.v(TAG, "onCreateView()");
105         if (myView != null)
106         {
107             // Viewを再利用。。。
108             Log.v(TAG, "onCreateView() : called again, so do nothing... : " + myView);
109             return (myView);
110         }
111
112         myView = inflater.inflate(R.layout.fragment_auto_transfer, container, false);
113         try
114         {
115             prepare(myView);
116         }
117         catch (Exception e)
118         {
119             e.printStackTrace();
120         }
121         return (myView);
122     }
123
124     @Override
125     public void onResume()
126     {
127         super.onResume();
128         controlButton(true);
129     }
130
131     @Override
132     public void onPause()
133     {
134         super.onPause();
135
136         // 画面を抜ける時に転送モードであった場合は、自動転送を停止させる
137         if (transferThreadIsRunning)
138         {
139             finishTransfer();
140         }
141     }
142
143     private void prepare(@NonNull View view)
144     {
145         try
146         {
147             Button start = view.findViewById(R.id.transfer_start_button);
148             if (start != null)
149             {
150                 start.setOnClickListener(this);
151             }
152
153             Button stop = view.findViewById(R.id.transfer_stop_button);
154             if (stop != null)
155             {
156                 stop.setOnClickListener(this);
157             }
158         }
159         catch (Exception e)
160         {
161             e.printStackTrace();
162         }
163     }
164
165     /**
166      *   転送開始
167      *
168      */
169     private void startTransfer()
170     {
171         if (activity == null)
172         {
173             //  activityがない場合は動かない。
174             Log.v(TAG, "ACTIVITY IS NULL...");
175             return;
176         }
177         try
178         {
179             // とにかく転送スレッドを止める指示を出す
180             transferThreadIsRunning = false;
181             while (startTransferReceived)
182             {
183                 // すでにコマンドは発行状態...終わるまで待つ
184                 Thread.sleep(SLEEP_MS);
185             }
186
187             // STARTボタンを無効化してぶるぶるする...
188             controlButton(false);
189             Vibrator vibrator = (activity != null) ? (Vibrator) activity.getSystemService(VIBRATOR_SERVICE) : null;
190             if (vibrator != null)
191             {
192                 vibrator.vibrate(50);
193             }
194             startTransferReceived = true;
195
196             // 画像を初期データにする
197             ImageView imageView = activity.findViewById(R.id.image_view_area);
198             imageView.setImageResource(R.drawable.ic_satellite_grey_24dp);
199
200             // 画面上にある自動転送の設定を取得
201             CheckBox raw = activity.findViewById(R.id.check_auto_download_raw);
202             CheckBox original = activity.findViewById(R.id.check_auto_download_original);
203             final boolean isRaw = raw.isChecked();
204             final boolean isSmallSize = !original.isChecked();  // 画面上のチェックとは逆にする...
205
206             Thread thread = new Thread(new Runnable()
207             {
208                 @Override
209                 public void run()
210                 {
211                     //int count = 0;
212                     if (transferMain != null)
213                     {
214                         // 前処理...
215                         transferMain.start(isRaw, isSmallSize);
216                     }
217                     while (transferThreadIsRunning)
218                     {
219                         try
220                         {
221                             if (transferMain != null)
222                             {
223                                 //// 現在時刻を取得する
224                                 //long checkStartTime = System.currentTimeMillis();
225
226                                 // チェックして追加ファイルがあったらダウンロード
227                                 transferMain.checkFiles();
228
229                                 // 画像数確認と画像取得が終わるまで、ちょっと待機...
230                                 while (transferMain.isChecking())
231                                 {
232                                     Thread.sleep(SLEEP_MS);
233                                 }
234                                 //long checkTime = Math.abs(System.currentTimeMillis() - checkStartTime);
235                                 //if (checkTime < SLEEP_WAIT_MS)
236                                 //{
237                                 //    // 画像数確認の時間が規定時間よりも短い場合は、しばらく待つ
238                                 //    Thread.sleep(SLEEP_WAIT_MS - checkTime);
239                                 //}
240
241                                 // 一定時間しばらく待つ (急ぎすぎると、GR2の電源が落ちる...
242                                 Thread.sleep(SLEEP_WAIT_MS);
243                             }
244                             //count++;
245                             //Log.v(TAG, "TRANSFER LOOP : " + count);
246                         }
247                         catch (Exception e)
248                         {
249                             e.printStackTrace();
250                         }
251                     }
252                     if (transferMain != null)
253                     {
254                         // 後処理...
255                         transferMain.finish();
256                     }
257                     startTransferReceived = false;
258                 }
259             });
260
261             // 転送の開始
262             transferThreadIsRunning = true;
263             thread.start();
264         }
265         catch (Exception e)
266         {
267             e.printStackTrace();
268         }
269     }
270
271
272     /**
273      *   転送終了
274      *
275      */
276     private void finishTransfer()
277     {
278         try
279         {
280             // 転送モードを止める
281             transferThreadIsRunning = false;
282
283             // STARTボタンを有効化
284             controlButton(true);
285
286             // ぶるぶるする
287             Vibrator vibrator = (activity != null) ? (Vibrator) activity.getSystemService(VIBRATOR_SERVICE) : null;
288             if (vibrator != null)
289             {
290                 vibrator.vibrate(150);
291             }
292         }
293         catch (Exception e)
294         {
295             e.printStackTrace();
296         }
297     }
298
299     /**
300      *   画面上のボタンを制御する
301      *
302      */
303     private void controlButton(boolean isStartButtonEnable)
304     {
305         try
306         {
307             Button start = activity.findViewById(R.id.transfer_start_button);
308             Button stop = activity.findViewById(R.id.transfer_stop_button);
309             if ((start != null)&&(stop != null))
310             {
311                 start.setEnabled(isStartButtonEnable);
312                 stop.setEnabled(!isStartButtonEnable);
313                 CheckBox check = activity.findViewById(R.id.check_auto_download_raw);
314                 CheckBox original = activity.findViewById(R.id.check_auto_download_original);
315                 ProgressBar bar = activity.findViewById(R.id.auto_transfer_progress_bar);
316                 ImageButton reload = activity.findViewById(R.id.button_reload);
317                 ImageButton connect = activity.findViewById(R.id.button_wifi_connect);
318                 if (isStartButtonEnable)
319                 {
320                     if (bar != null)
321                     {
322                         bar.setVisibility(View.GONE);
323                     }
324                     if (check != null)
325                     {
326                         check.setEnabled(true);
327                     }
328                     if (original != null)
329                     {
330                         original.setEnabled(true);
331                     }
332                     if (reload != null)
333                     {
334                         reload.setVisibility(View.VISIBLE);
335                         reload.setEnabled(true);
336                     }
337                     if (connect != null)
338                     {
339                         connect.setVisibility(View.VISIBLE);
340                         connect.setEnabled(true);
341                     }
342                 }
343                 else
344                 {
345                     if (bar != null)
346                     {
347                         bar.setVisibility(View.VISIBLE);
348                     }
349                     if (check != null)
350                     {
351                         check.setEnabled(false);
352                     }
353                     if (original != null)
354                     {
355                         original.setEnabled(false);
356                     }
357                     if (reload != null)
358                     {
359                         reload.setEnabled(false);
360                         reload.setVisibility(View.INVISIBLE);
361                     }
362                     if (connect != null)
363                     {
364                         connect.setEnabled(false);
365                         connect.setVisibility(View.INVISIBLE);
366                     }
367                 }
368                 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
369                 if (preferences != null)
370                 {
371                     TextView textView = activity.findViewById(R.id.auto_download_information_text);
372                     String connectionMethod = preferences.getString(IPreferencePropertyAccessor.CONNECTION_METHOD, "RICOH");
373                     if (!connectionMethod.contains("RICOH"))
374                     {
375                         // FUJI Xシリーズ/OPCの場合は、この画面の操作系統は、すべて無効化する
376                         start.setEnabled(false);
377                         stop.setEnabled(false);
378                         if (bar != null)
379                         {
380                             bar.setVisibility(View.GONE);
381                         }
382                         if (check != null)
383                         {
384                             check.setEnabled(false);
385                         }
386                         if (original != null)
387                         {
388                             original.setEnabled(false);
389                         }
390                         if (reload != null)
391                         {
392                             reload.setVisibility(View.VISIBLE);
393                             reload.setEnabled(true);
394                         }
395                         if (connect != null)
396                         {
397                             connect.setVisibility(View.VISIBLE);
398                             connect.setEnabled(true);
399                         }
400                         if (textView != null)
401                         {
402                             textView.setText(R.string.does_not_support_this_feature);
403                         }
404                     }
405                     else
406                     {
407                         if (textView != null)
408                         {
409                             textView.setText("");
410                         }
411                     }
412                 }
413             }
414         }
415         catch (Exception e)
416         {
417             e.printStackTrace();
418         }
419     }
420
421     /**
422      *
423      *
424      */
425     @Override
426     public void onClick(View v)
427     {
428         int id = v.getId();
429         if (id == R.id.transfer_start_button)
430         {
431             //
432             Log.v(TAG, "TRANSFER START");
433             startTransfer();
434         }
435         else if  (id == R.id.transfer_stop_button)
436         {
437             Log.v(TAG, "TRANSFER FINISH");
438             finishTransfer();
439         }
440     }
441
442     /**
443      *
444      *
445      */
446     @Override
447     public void storedImage(@NonNull final String filename, final Bitmap picture)
448     {
449         if (activity != null)
450         {
451             activity.runOnUiThread(new Runnable() {
452                 @Override
453                 public void run()
454                 {
455                     TextView textView = activity.findViewById(R.id.image_view_information);
456                     if (textView != null)
457                     {
458                         textView.setText(filename);
459                     }
460                     try
461                     {
462                         if (picture != null)
463                         {
464                             ImageView imageView = activity.findViewById(R.id.image_view_area);
465                             if (imageView != null)
466                             {
467                                 imageView.setImageBitmap(picture);
468                             }
469                         }
470                     }
471                     catch (Throwable t)
472                     {
473                         t.printStackTrace();
474                     }
475                 }
476             });
477         }
478     }
479
480     /**
481      *
482      *
483      */
484     @Override
485     public void showInformation(@NonNull final String message)
486     {
487         if (activity != null)
488         {
489             activity.runOnUiThread(new Runnable() {
490                 @Override
491                 public void run()
492                 {
493                     TextView textView = activity.findViewById(R.id.auto_download_information_text);
494                     if (textView != null)
495                     {
496                         textView.setText(message);
497                     }
498                 }
499             });
500         }
501     }
502
503     // IContentDownloadNotify
504     @Override
505     public void downloadedImage(final String contentInfo, final Uri content)
506     {
507         if (activity != null)
508         {
509             activity.runOnUiThread(new Runnable() {
510                 @Override
511                 public void run() {
512                     ImageView imageView = activity.findViewById(R.id.image_view_area);
513                     TextView textView = activity.findViewById(R.id.image_view_information);
514                     try
515                     {
516                         if ((imageView != null)&&(content != null))
517                         {
518                             imageView.setImageURI(content);
519                         }
520                         if ((textView != null)&&(contentInfo != null))
521                         {
522                             textView.setText(contentInfo);
523                         }
524                     }
525                     catch (Throwable t)
526                     {
527                         t.printStackTrace();
528                     }
529                 }
530             });
531         }
532     }
533 }