1 package net.osdn.gokigen.pkremote.transfer;
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;
18 import net.osdn.gokigen.pkremote.R;
19 import net.osdn.gokigen.pkremote.camera.interfaces.IInterfaceProvider;
21 import androidx.annotation.NonNull;
22 import androidx.appcompat.app.AppCompatActivity;
23 import androidx.fragment.app.Fragment;
25 import static android.content.Context.VIBRATOR_SERVICE;
31 public class AutoTransferFragment extends Fragment implements View.OnClickListener, ITransferMessage
33 private final String TAG = this.toString();
35 private static final int SLEEP_MS = 3000; // 待機時間
37 private AppCompatActivity activity = null;
38 private FileAutoTransferMain transferMain = null;
39 private View myView = null;
40 private boolean transferThreadIsRunning = false;
42 public static AutoTransferFragment newInstance(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider provider)
44 AutoTransferFragment instance = new AutoTransferFragment();
45 instance.prepare(context, provider);
47 // パラメータはBundleにまとめておく
48 Bundle arguments = new Bundle();
49 //arguments.putString("title", title);
50 //arguments.putString("message", message);
51 instance.setArguments(arguments);
59 private void prepare(@NonNull AppCompatActivity activity, @NonNull IInterfaceProvider interfaceProvider)
61 Log.v(TAG, "prepare()");
62 this.activity = activity;
63 transferMain = new FileAutoTransferMain(activity, interfaceProvider, this);
71 public void onCreate(Bundle savedInstanceState)
73 super.onCreate(savedInstanceState);
74 Log.v(TAG, "onCreate()");
82 public void onAttach(Context context)
84 super.onAttach(context);
85 Log.v(TAG, "onAttach()");
93 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
95 super.onCreateView(inflater, container, savedInstanceState);
97 Log.v(TAG, "onCreateView()");
101 Log.v(TAG, "onCreateView() : called again, so do nothing... : " + myView);
105 myView = inflater.inflate(R.layout.fragment_auto_transfer, container, false);
118 public void onResume()
124 public void onPause()
128 // 画面を抜ける時に転送モードであった場合は、自動転送を停止させる
129 if (transferThreadIsRunning)
135 private void prepare(@NonNull View view)
139 Button start = view.findViewById(R.id.transfer_start_button);
142 start.setOnClickListener(this);
145 Button stop = view.findViewById(R.id.transfer_stop_button);
148 stop.setOnClickListener(this);
161 private void startTransfer()
163 if (activity == null)
165 // activityがない場合は動かない。
166 Log.v(TAG, "ACTIVITY IS NULL...");
171 // STARTボタンを無効化してぶるぶるする...
172 controlButton(false);
173 Vibrator vibrator = (activity != null) ? (Vibrator) activity.getSystemService(VIBRATOR_SERVICE) : null;
174 if (vibrator != null)
176 vibrator.vibrate(50);
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(); // 画面上のチェックとは逆にする...
185 Thread thread = new Thread(new Runnable()
191 if (transferMain != null)
194 transferMain.start(isRaw, isSmallSize);
196 while (transferThreadIsRunning)
200 if (transferMain != null)
202 // チェックして追加ファイルがあったらダウンロード
203 transferMain.downloadFiles();
206 Log.v(TAG, "TRANSFER LOOP : " + count);
209 Thread.sleep(SLEEP_MS);
216 if (transferMain != null)
219 transferMain.finish();
225 transferThreadIsRunning = true;
239 private void finishTransfer()
244 transferThreadIsRunning = false;
250 Vibrator vibrator = (activity != null) ? (Vibrator) activity.getSystemService(VIBRATOR_SERVICE) : null;
251 if (vibrator != null)
253 vibrator.vibrate(150);
266 private void controlButton(boolean isStartButtonEnable)
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))
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)
285 bar.setVisibility(View.GONE);
289 check.setEnabled(true);
291 if (original != null)
293 original.setEnabled(true);
297 reload.setVisibility(View.VISIBLE);
298 reload.setEnabled(true);
302 connect.setVisibility(View.VISIBLE);
303 connect.setEnabled(true);
310 bar.setVisibility(View.VISIBLE);
314 check.setEnabled(false);
316 if (original != null)
318 original.setEnabled(false);
322 reload.setEnabled(false);
323 reload.setVisibility(View.INVISIBLE);
327 connect.setEnabled(false);
328 connect.setVisibility(View.INVISIBLE);
344 public void onClick(View v)
347 if (id == R.id.transfer_start_button)
350 Log.v(TAG, "TRANSFER START");
353 else if (id == R.id.transfer_stop_button)
355 Log.v(TAG, "TRANSFER FINISH");
365 public void storedImage(@NonNull final String filename, final Bitmap picture)
367 if (activity != null)
369 activity.runOnUiThread(new Runnable() {
373 TextView textView = activity.findViewById(R.id.image_view_information);
374 if (textView != null)
376 textView.setText(filename);
382 ImageView imageView = activity.findViewById(R.id.image_view_area);
383 if (imageView != null)
385 imageView.setImageBitmap(picture);
403 public void showInformation(@NonNull final String message)
405 if (activity != null)
407 activity.runOnUiThread(new Runnable() {
411 TextView textView = activity.findViewById(R.id.auto_download_information_text);
412 if (textView != null)
414 textView.setText(message);