1 package net.osdn.gokigen.pkremote.camera.playback;
3 import android.content.DialogInterface;
4 import android.graphics.Color;
5 import android.util.Log;
7 import net.osdn.gokigen.pkremote.IInformationReceiver;
8 import net.osdn.gokigen.pkremote.R;
9 import net.osdn.gokigen.pkremote.camera.interfaces.IInterfaceProvider;
10 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraContent;
11 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraContentListCallback;
12 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraContentsRecognizer;
13 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IPlaybackControl;
15 import java.text.SimpleDateFormat;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.Comparator;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Locale;
23 import androidx.annotation.NonNull;
24 import androidx.appcompat.app.AlertDialog;
25 import androidx.appcompat.app.AppCompatActivity;
28 * 遠隔カメラのコンテンツを解析・保持するクラス
32 public class CameraContentsRecognizer implements ICameraContentsRecognizer, ICameraContentListCallback
34 private final String TAG = toString();
35 private final AppCompatActivity activity;
36 private final IInformationReceiver informationReceiver;
37 private final IInterfaceProvider interfaceProvider;
38 private ICameraContentsListCallback contentsListCallback = null;
39 private List<ICameraContent> cameraContentsList = null;
40 private boolean isLoadedContents = false;
46 public CameraContentsRecognizer(@NonNull AppCompatActivity activity, @NonNull IInterfaceProvider interfaceProvider)
48 this.activity = activity;
49 this.interfaceProvider = interfaceProvider;
50 this.informationReceiver = interfaceProvider.getInformationReceiver();
58 public void getRemoteCameraContentsList(boolean isReload, ICameraContentsListCallback callback)
60 contentsListCallback = callback;
61 if ((isLoadedContents)&&(cameraContentsList != null)&&(!isReload))
63 Log.v(TAG, "getRemoteCameraContentsList() : cached data.");
66 callback.contentsListCreated(cameraContentsList.size());
70 getRemoteCameraContentsListImpl(this);
77 private void getRemoteCameraContentsListImpl(final ICameraContentListCallback callback)
79 Thread thread = new Thread(new Runnable() {
85 IPlaybackControl playbackControl = interfaceProvider.getPlaybackControl();
86 isLoadedContents = false;
87 playbackControl.getCameraContentList(callback);
97 if (informationReceiver != null)
99 informationReceiver.updateMessage(activity.getString(R.string.get_camera_contents_wait), false, false, 0);
110 public void onCompleted(List<ICameraContent> contentList)
112 cameraContentsList = contentList;
113 isLoadedContents = true;
114 if (contentsListCallback != null)
118 // 遠隔のカメラ内のコンテンツ一覧を引っ張ることができたよ、の通知
119 contentsListCallback.contentsListCreated(contentList.size());
120 if (informationReceiver != null)
122 String message = activity.getString(R.string.get_camera_contents_finished) + " : " + contentList.size();
123 informationReceiver.updateMessage(message, false, false, 0);
127 Collections.sort(contentList, new Comparator<ICameraContent>() {
129 public int compare(ICameraContent lhs, ICameraContent rhs)
131 long diff = rhs.getCapturedDate().getTime() - lhs.getCapturedDate().getTime();
134 diff = rhs.getContentName().compareTo(lhs.getContentName());
136 return (int)Math.min(Math.max(-1, diff), 1);
140 //// とりあえず、できたコンテンツ一覧をログにダンプしてみる。
141 //dumpCameraContentList();
151 public void onErrorOccurred(Exception e)
153 isLoadedContents = false;
154 cameraContentsList = null;
155 if (informationReceiver != null)
157 informationReceiver.updateMessage(activity.getString(R.string.get_camera_contents_error), false, true, Color.RED);
162 final AlertDialog.Builder builder = new AlertDialog.Builder(activity)
164 .setTitle(activity.getString(R.string.get_camera_contents_error))
165 .setMessage(activity.getString(R.string.get_camera_contents_error_retry))
166 .setPositiveButton(activity.getString(R.string.dialog_title_button_retry), new DialogInterface.OnClickListener() {
168 public void onClick(DialogInterface dialog, int which)
170 getRemoteCameraContentsList(true, contentsListCallback);
173 activity.runOnUiThread(new Runnable()
184 private void dumpCameraContentList()
186 Log.v(TAG, "dumpCameraContentList()");
187 if (cameraContentsList == null)
193 for (ICameraContent content : cameraContentsList)
195 String cameraId = content.getCameraId();
196 String cardId = content.getCardId();
197 String path = content.getContentPath();
198 String name = content.getContentName();
199 Date date = content.getCapturedDate();
200 Log.v(TAG, index + " [" + cameraId + "] " + cardId + " : " + path + " " + name + " " + date);
217 public List<ICameraContent> getContentsList()
219 if (cameraContentsList == null)
221 getRemoteCameraContentsListImpl(this);
222 return (new ArrayList<>());
224 return (cameraContentsList);
228 * 指定された年月日(yyyy/MM/DD)に含まれている一覧を応答する
233 public List<ICameraContent> getContentsListAtDate(String date)
235 Log.v(TAG, "getContentsListAtDate() : " + date);
236 if (date.equals("ALL"))
238 return (getContentsList());
240 if (cameraContentsList == null)
242 getRemoteCameraContentsListImpl(this);
243 return (new ArrayList<>());
246 ArrayList<ICameraContent> targetList = new ArrayList<>();
247 SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd", Locale.ENGLISH);
248 for (ICameraContent content : cameraContentsList)
250 String capturedDate = format.format(content.getCapturedDate());
251 if (date.equals(capturedDate))
253 targetList.add(content);
256 Collections.sort(targetList, new Comparator<ICameraContent>() {
258 public int compare(ICameraContent lhs, ICameraContent rhs)
260 long diff = rhs.getCapturedDate().getTime() - lhs.getCapturedDate().getTime();
263 diff = rhs.getContentName().compareTo(lhs.getContentName());
265 return (int)Math.min(Math.max(-1, diff), 1);
268 //Collections.sort(targetList);
273 * 指定されたパスに入っている一覧を応答する
278 public List<ICameraContent> getContentsListAtPath(String path)
280 Log.v(TAG, "getContentsListAtPath() : " + path);
281 if (path.equals("ALL"))
284 return (getContentsList());
286 if (cameraContentsList == null)
288 getRemoteCameraContentsListImpl(this);
289 return (new ArrayList<>());
292 ArrayList<ICameraContent> targetList = new ArrayList<>();
293 for (ICameraContent content : cameraContentsList)
295 if (path.equals(content.getContentPath()))
297 targetList.add(content);
300 Collections.sort(targetList, new Comparator<ICameraContent>() {
302 public int compare(ICameraContent lhs, ICameraContent rhs)
304 long diff = rhs.getCapturedDate().getTime() - lhs.getCapturedDate().getTime();
307 diff = rhs.getContentName().compareTo(lhs.getContentName());
309 return (int)Math.min(Math.max(-1, diff), 1);
312 //Collections.sort(targetList);
313 //Log.v(TAG, "getContentsListAtPath() " + targetList.size());
323 public List<String> getDateList()
325 if (cameraContentsList == null)
327 return (new ArrayList<>());
329 SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd", Locale.ENGLISH);
330 HashMap<String, String> map = new HashMap<>();
331 for (ICameraContent content : cameraContentsList)
333 map.put(format.format(content.getCapturedDate()), content.getContentName());
335 ArrayList<String> dateList = new ArrayList<>(map.keySet());
336 Collections.sort(dateList, Collections.reverseOrder());
341 * ファイルパス(ディレクトリ)の一覧を取得する
342 * たぶん、新しい画像が入ったディレクトリからのリスト
346 public List<String> getPathList()
348 if (cameraContentsList == null)
350 return (new ArrayList<>());
352 HashMap<String, String> map = new HashMap<>();
353 for (ICameraContent content : cameraContentsList)
355 map.put(content.getContentPath(), content.getContentName());
357 ArrayList<String> pathList = new ArrayList<>(map.keySet());
358 Collections.sort(pathList, Collections.reverseOrder());