OSDN Git Service

撮影画像自動転送機能のいったん登録。
[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.graphics.Bitmap;
5 import android.os.Bundle;
6 import android.os.Vibrator;
7 import android.util.Log;
8 import android.view.LayoutInflater;
9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.Button;
12 import android.widget.CheckBox;
13 import android.widget.ImageButton;
14 import android.widget.ImageView;
15 import android.widget.ProgressBar;
16 import android.widget.TextView;
17
18 import net.osdn.gokigen.pkremote.R;
19 import net.osdn.gokigen.pkremote.camera.interfaces.IInterfaceProvider;
20
21 import androidx.annotation.NonNull;
22 import androidx.appcompat.app.AppCompatActivity;
23 import androidx.fragment.app.Fragment;
24
25 import static android.content.Context.VIBRATOR_SERVICE;
26
27 /**
28  *   自動転送クラス
29  *
30  */
31 public class AutoTransferFragment extends Fragment implements View.OnClickListener, ITransferMessage
32 {
33     private final String TAG = this.toString();
34
35     private static final int SLEEP_MS = 3000;   // 待機時間
36
37     private AppCompatActivity activity = null;
38     private FileAutoTransferMain transferMain = null;
39     private View myView = null;
40     private boolean transferThreadIsRunning = false;
41
42     public static AutoTransferFragment newInstance(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider provider)
43     {
44         AutoTransferFragment instance = new AutoTransferFragment();
45         instance.prepare(context, provider);
46
47         // パラメータはBundleにまとめておく
48         Bundle arguments = new Bundle();
49         //arguments.putString("title", title);
50         //arguments.putString("message", message);
51         instance.setArguments(arguments);
52
53         return (instance);
54     }
55
56     /**
57      *
58      */
59     private void prepare(@NonNull AppCompatActivity activity, @NonNull IInterfaceProvider interfaceProvider)
60     {
61         Log.v(TAG, "prepare()");
62         this.activity = activity;
63         transferMain = new FileAutoTransferMain(activity, interfaceProvider, this);
64     }
65
66     /**
67      *
68      *
69      */
70     @Override
71     public void onCreate(Bundle savedInstanceState)
72     {
73         super.onCreate(savedInstanceState);
74         Log.v(TAG, "onCreate()");
75     }
76
77     /**
78      *
79      *
80      */
81     @Override
82     public void onAttach(Context context)
83     {
84         super.onAttach(context);
85         Log.v(TAG, "onAttach()");
86     }
87
88     /**
89      *
90      *
91      */
92     @Override
93     public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
94     {
95         super.onCreateView(inflater, container, savedInstanceState);
96
97         Log.v(TAG, "onCreateView()");
98         if (myView != null)
99         {
100             // Viewを再利用。。。
101             Log.v(TAG, "onCreateView() : called again, so do nothing... : " + myView);
102             return (myView);
103         }
104
105         myView = inflater.inflate(R.layout.fragment_auto_transfer, container, false);
106         try
107         {
108             prepare(myView);
109         }
110         catch (Exception e)
111         {
112             e.printStackTrace();
113         }
114         return (myView);
115     }
116
117     @Override
118     public void onResume()
119     {
120         super.onResume();
121     }
122
123     @Override
124     public void onPause()
125     {
126         super.onPause();
127
128         // 画面を抜ける時に転送モードであった場合は、自動転送を停止させる
129         if (transferThreadIsRunning)
130         {
131             finishTransfer();
132         }
133     }
134
135     private void prepare(@NonNull View view)
136     {
137         try
138         {
139             Button start = view.findViewById(R.id.transfer_start_button);
140             if (start != null)
141             {
142                 start.setOnClickListener(this);
143             }
144
145             Button stop = view.findViewById(R.id.transfer_stop_button);
146             if (stop != null)
147             {
148                 stop.setOnClickListener(this);
149             }
150         }
151         catch (Exception e)
152         {
153             e.printStackTrace();
154         }
155     }
156
157     /**
158      *   転送開始
159      *
160      */
161     private void startTransfer()
162     {
163         if (activity == null)
164         {
165             //  activityがない場合は動かない。
166             Log.v(TAG, "ACTIVITY IS NULL...");
167             return;
168         }
169         try
170         {
171             // STARTボタンを無効化してぶるぶるする...
172             controlButton(false);
173             Vibrator vibrator = (activity != null) ? (Vibrator) activity.getSystemService(VIBRATOR_SERVICE) : null;
174             if (vibrator != null)
175             {
176                 vibrator.vibrate(50);
177             }
178
179             // 画面上にある自動転送の設定を取得
180             CheckBox raw = activity.findViewById(R.id.check_auto_download_raw);
181             CheckBox original = activity.findViewById(R.id.check_auto_download_original);
182             final boolean isRaw = raw.isChecked();
183             final boolean isSmallSize = !original.isChecked();  // 画面上のチェックとは逆にする...
184
185             Thread thread = new Thread(new Runnable()
186             {
187                 @Override
188                 public void run()
189                 {
190                     int count = 0;
191                     if (transferMain != null)
192                     {
193                         // 前処理...
194                         transferMain.start(isRaw, isSmallSize);
195                     }
196                     while (transferThreadIsRunning)
197                     {
198                         try
199                         {
200                             if (transferMain != null)
201                             {
202                                 // チェックして追加ファイルがあったらダウンロード
203                                 transferMain.downloadFiles();
204                             }
205                             count++;
206                             Log.v(TAG, "TRANSFER LOOP : " + count);
207
208                             // ちょっと待機...
209                             Thread.sleep(SLEEP_MS);
210                         }
211                         catch (Exception e)
212                         {
213                             e.printStackTrace();
214                         }
215                     }
216                     if (transferMain != null)
217                     {
218                         // 後処理...
219                         transferMain.finish();
220                     }
221                 }
222             });
223
224             // 転送の開始
225             transferThreadIsRunning = true;
226             thread.start();
227         }
228         catch (Exception e)
229         {
230             e.printStackTrace();
231         }
232     }
233
234
235     /**
236      *   転送終了
237      *
238      */
239     private void finishTransfer()
240     {
241         try
242         {
243             // 転送モードを止める
244             transferThreadIsRunning = false;
245
246             // STARTボタンを有効化
247             controlButton(true);
248
249             // ぶるぶるする
250             Vibrator vibrator = (activity != null) ? (Vibrator) activity.getSystemService(VIBRATOR_SERVICE) : null;
251             if (vibrator != null)
252             {
253                 vibrator.vibrate(150);
254             }
255         }
256         catch (Exception e)
257         {
258             e.printStackTrace();
259         }
260     }
261
262     /**
263      *   画面上のボタンを制御する
264      *
265      */
266     private void controlButton(boolean isStartButtonEnable)
267     {
268         try
269         {
270             Button start = activity.findViewById(R.id.transfer_start_button);
271             Button stop = activity.findViewById(R.id.transfer_stop_button);
272             if ((start != null)&&(stop != null))
273             {
274                 start.setEnabled(isStartButtonEnable);
275                 stop.setEnabled(!isStartButtonEnable);
276                 CheckBox check = activity.findViewById(R.id.check_auto_download_raw);
277                 CheckBox original = activity.findViewById(R.id.check_auto_download_original);
278                 ProgressBar bar = activity.findViewById(R.id.auto_transfer_progress_bar);
279                 ImageButton reload = activity.findViewById(R.id.button_reload);
280                 ImageButton connect = activity.findViewById(R.id.button_wifi_connect);
281                 if (isStartButtonEnable)
282                 {
283                     if (bar != null)
284                     {
285                         bar.setVisibility(View.GONE);
286                     }
287                     if (check != null)
288                     {
289                         check.setEnabled(true);
290                     }
291                     if (original != null)
292                     {
293                         original.setEnabled(true);
294                     }
295                     if (reload != null)
296                     {
297                         reload.setVisibility(View.VISIBLE);
298                         reload.setEnabled(true);
299                     }
300                     if (connect != null)
301                     {
302                         connect.setVisibility(View.VISIBLE);
303                         connect.setEnabled(true);
304                     }
305                 }
306                 else
307                 {
308                     if (bar != null)
309                     {
310                         bar.setVisibility(View.VISIBLE);
311                     }
312                     if (check != null)
313                     {
314                         check.setEnabled(false);
315                     }
316                     if (original != null)
317                     {
318                         original.setEnabled(false);
319                     }
320                     if (reload != null)
321                     {
322                         reload.setEnabled(false);
323                         reload.setVisibility(View.INVISIBLE);
324                     }
325                     if (connect != null)
326                     {
327                         connect.setEnabled(false);
328                         connect.setVisibility(View.INVISIBLE);
329                     }
330                 }
331             }
332         }
333         catch (Exception e)
334         {
335             e.printStackTrace();
336         }
337     }
338
339     /**
340      *
341      *
342      */
343     @Override
344     public void onClick(View v)
345     {
346         int id = v.getId();
347         if (id == R.id.transfer_start_button)
348         {
349             //
350             Log.v(TAG, "TRANSFER START");
351             startTransfer();
352         }
353         else if  (id == R.id.transfer_stop_button)
354         {
355             Log.v(TAG, "TRANSFER FINISH");
356             finishTransfer();
357         }
358     }
359
360     /**
361      *
362      *
363      */
364     @Override
365     public void storedImage(@NonNull final String filename, final Bitmap picture)
366     {
367         if (activity != null)
368         {
369             activity.runOnUiThread(new Runnable() {
370                 @Override
371                 public void run()
372                 {
373                     TextView textView = activity.findViewById(R.id.image_view_information);
374                     if (textView != null)
375                     {
376                         textView.setText(filename);
377                     }
378                     try
379                     {
380                         if (picture != null)
381                         {
382                             ImageView imageView = activity.findViewById(R.id.image_view_area);
383                             if (imageView != null)
384                             {
385                                 imageView.setImageBitmap(picture);
386                             }
387                         }
388                     }
389                     catch (Throwable t)
390                     {
391                         t.printStackTrace();
392                     }
393                 }
394             });
395         }
396     }
397
398     /**
399      *
400      *
401      */
402     @Override
403     public void showInformation(@NonNull final String message)
404     {
405         if (activity != null)
406         {
407             activity.runOnUiThread(new Runnable() {
408                 @Override
409                 public void run()
410                 {
411                     TextView textView = activity.findViewById(R.id.auto_download_information_text);
412                     if (textView != null)
413                     {
414                         textView.setText(message);
415                     }
416                 }
417             });
418         }
419     }
420 }