1 package net.osdn.gokigen.pkremote.transfer;
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;
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;
25 import androidx.annotation.NonNull;
26 import androidx.appcompat.app.AppCompatActivity;
27 import androidx.fragment.app.Fragment;
28 import androidx.preference.PreferenceManager;
30 import static android.content.Context.VIBRATOR_SERVICE;
36 public class AutoTransferFragment extends Fragment implements View.OnClickListener, ITransferMessage, IContentDownloadNotify
38 private final String TAG = this.toString();
40 private static final int SLEEP_MS = 300; // 動作チェック待ち時間
41 private static final int SLEEP_WAIT_MS = 4500; // 一覧確認の待機時間 (4.5秒おき)
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;
49 public static AutoTransferFragment newInstance(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider provider)
51 AutoTransferFragment instance = new AutoTransferFragment();
52 instance.prepare(context, provider);
54 // パラメータはBundleにまとめておく
55 Bundle arguments = new Bundle();
56 //arguments.putString("title", title);
57 //arguments.putString("message", message);
58 instance.setArguments(arguments);
66 private void prepare(@NonNull AppCompatActivity activity, @NonNull IInterfaceProvider interfaceProvider)
68 Log.v(TAG, "prepare()");
69 this.activity = activity;
70 transferMain = new FileAutoTransferMain(activity, interfaceProvider, this, this);
78 public void onCreate(Bundle savedInstanceState)
80 super.onCreate(savedInstanceState);
81 Log.v(TAG, "onCreate()");
89 public void onAttach(Context context)
91 super.onAttach(context);
92 Log.v(TAG, "onAttach()");
100 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
102 super.onCreateView(inflater, container, savedInstanceState);
104 Log.v(TAG, "onCreateView()");
108 Log.v(TAG, "onCreateView() : called again, so do nothing... : " + myView);
112 myView = inflater.inflate(R.layout.fragment_auto_transfer, container, false);
125 public void onResume()
132 public void onPause()
136 // 画面を抜ける時に転送モードであった場合は、自動転送を停止させる
137 if (transferThreadIsRunning)
143 private void prepare(@NonNull View view)
147 Button start = view.findViewById(R.id.transfer_start_button);
150 start.setOnClickListener(this);
153 Button stop = view.findViewById(R.id.transfer_stop_button);
156 stop.setOnClickListener(this);
169 private void startTransfer()
171 if (activity == null)
173 // activityがない場合は動かない。
174 Log.v(TAG, "ACTIVITY IS NULL...");
179 // とにかく転送スレッドを止める指示を出す
180 transferThreadIsRunning = false;
181 while (startTransferReceived)
183 // すでにコマンドは発行状態...終わるまで待つ
184 Thread.sleep(SLEEP_MS);
187 // STARTボタンを無効化してぶるぶるする...
188 controlButton(false);
189 Vibrator vibrator = (activity != null) ? (Vibrator) activity.getSystemService(VIBRATOR_SERVICE) : null;
190 if (vibrator != null)
192 vibrator.vibrate(50);
194 startTransferReceived = true;
197 ImageView imageView = activity.findViewById(R.id.image_view_area);
198 imageView.setImageResource(R.drawable.ic_satellite_grey_24dp);
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(); // 画面上のチェックとは逆にする...
206 Thread thread = new Thread(new Runnable()
212 if (transferMain != null)
215 transferMain.start(isRaw, isSmallSize);
217 while (transferThreadIsRunning)
221 if (transferMain != null)
224 //long checkStartTime = System.currentTimeMillis();
226 // チェックして追加ファイルがあったらダウンロード
227 transferMain.checkFiles();
229 // 画像数確認と画像取得が終わるまで、ちょっと待機...
230 while (transferMain.isChecking())
232 Thread.sleep(SLEEP_MS);
234 //long checkTime = Math.abs(System.currentTimeMillis() - checkStartTime);
235 //if (checkTime < SLEEP_WAIT_MS)
237 // // 画像数確認の時間が規定時間よりも短い場合は、しばらく待つ
238 // Thread.sleep(SLEEP_WAIT_MS - checkTime);
241 // 一定時間しばらく待つ (急ぎすぎると、GR2の電源が落ちる...
242 Thread.sleep(SLEEP_WAIT_MS);
245 //Log.v(TAG, "TRANSFER LOOP : " + count);
252 if (transferMain != null)
255 transferMain.finish();
257 startTransferReceived = false;
262 transferThreadIsRunning = true;
276 private void finishTransfer()
281 transferThreadIsRunning = false;
287 Vibrator vibrator = (activity != null) ? (Vibrator) activity.getSystemService(VIBRATOR_SERVICE) : null;
288 if (vibrator != null)
290 vibrator.vibrate(150);
303 private void controlButton(boolean isStartButtonEnable)
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))
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)
322 bar.setVisibility(View.GONE);
326 check.setEnabled(true);
328 if (original != null)
330 original.setEnabled(true);
334 reload.setVisibility(View.VISIBLE);
335 reload.setEnabled(true);
339 connect.setVisibility(View.VISIBLE);
340 connect.setEnabled(true);
347 bar.setVisibility(View.VISIBLE);
351 check.setEnabled(false);
353 if (original != null)
355 original.setEnabled(false);
359 reload.setEnabled(false);
360 reload.setVisibility(View.INVISIBLE);
364 connect.setEnabled(false);
365 connect.setVisibility(View.INVISIBLE);
368 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
369 if (preferences != null)
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"))
375 // FUJI Xシリーズ/OPCの場合は、この画面の操作系統は、すべて無効化する
376 start.setEnabled(false);
377 stop.setEnabled(false);
380 bar.setVisibility(View.GONE);
384 check.setEnabled(false);
386 if (original != null)
388 original.setEnabled(false);
392 reload.setVisibility(View.VISIBLE);
393 reload.setEnabled(true);
397 connect.setVisibility(View.VISIBLE);
398 connect.setEnabled(true);
400 if (textView != null)
402 textView.setText(R.string.does_not_support_this_feature);
407 if (textView != null)
409 textView.setText("");
426 public void onClick(View v)
429 if (id == R.id.transfer_start_button)
432 Log.v(TAG, "TRANSFER START");
435 else if (id == R.id.transfer_stop_button)
437 Log.v(TAG, "TRANSFER FINISH");
447 public void storedImage(@NonNull final String filename, final Bitmap picture)
449 if (activity != null)
451 activity.runOnUiThread(new Runnable() {
455 TextView textView = activity.findViewById(R.id.image_view_information);
456 if (textView != null)
458 textView.setText(filename);
464 ImageView imageView = activity.findViewById(R.id.image_view_area);
465 if (imageView != null)
467 imageView.setImageBitmap(picture);
485 public void showInformation(@NonNull final String message)
487 if (activity != null)
489 activity.runOnUiThread(new Runnable() {
493 TextView textView = activity.findViewById(R.id.auto_download_information_text);
494 if (textView != null)
496 textView.setText(message);
503 // IContentDownloadNotify
505 public void downloadedImage(final String contentInfo, final Uri content)
507 if (activity != null)
509 activity.runOnUiThread(new Runnable() {
512 ImageView imageView = activity.findViewById(R.id.image_view_area);
513 TextView textView = activity.findViewById(R.id.image_view_information);
516 if ((imageView != null)&&(content != null))
518 imageView.setImageURI(content);
520 if ((textView != null)&&(contentInfo != null))
522 textView.setText(contentInfo);