OSDN Git Service

e2912af1066230fe706290e791d21734189c39c9
[gokigen/PKRemote.git] / app / src / main / java / net / osdn / gokigen / pkremote / camera / playback / CameraContentsRecognizer.java
1 package net.osdn.gokigen.pkremote.camera.playback;
2
3 import android.content.DialogInterface;
4 import android.graphics.Color;
5 import android.util.Log;
6
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;
14
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;
22
23 import androidx.annotation.NonNull;
24 import androidx.appcompat.app.AlertDialog;
25 import androidx.appcompat.app.AppCompatActivity;
26
27 /**
28  *   遠隔カメラのコンテンツを解析・保持するクラス
29  *
30  *
31  */
32 public class CameraContentsRecognizer implements ICameraContentsRecognizer, ICameraContentListCallback
33 {
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;
41
42     /**
43      *
44      *
45      */
46     public CameraContentsRecognizer(@NonNull AppCompatActivity activity, @NonNull IInterfaceProvider interfaceProvider)
47     {
48         this.activity = activity;
49         this.interfaceProvider = interfaceProvider;
50         this.informationReceiver = interfaceProvider.getInformationReceiver();
51     }
52
53     /**
54      *
55      *
56      */
57     @Override
58     public void getRemoteCameraContentsList(boolean isReload, ICameraContentsListCallback callback)
59     {
60         contentsListCallback = callback;
61         if ((isLoadedContents)&&(cameraContentsList != null)&&(!isReload))
62         {
63             Log.v(TAG, "getRemoteCameraContentsList() : cached data.");
64             if (callback != null)
65             {
66                 callback.contentsListCreated(cameraContentsList.size());
67             }
68             return;
69         }
70         getRemoteCameraContentsListImpl(this);
71     }
72
73     /**
74      *
75      *
76      */
77     private void getRemoteCameraContentsListImpl(final ICameraContentListCallback callback)
78     {
79         Thread thread = new Thread(new Runnable() {
80             @Override
81             public void run()
82             {
83                 try
84                 {
85                     IPlaybackControl playbackControl = interfaceProvider.getPlaybackControl();
86                     isLoadedContents = false;
87                     playbackControl.getCameraContentList(callback);
88                 }
89                 catch (Exception e)
90                 {
91                     e.printStackTrace();
92                 }
93             }
94         });
95         try
96         {
97             if (informationReceiver != null)
98             {
99                 informationReceiver.updateMessage(activity.getString(R.string.get_camera_contents_wait), false, false, 0);
100             }
101             thread.start();
102         }
103         catch (Exception e)
104         {
105             e.printStackTrace();
106         }
107     }
108
109     @Override
110     public void onCompleted(List<ICameraContent> contentList)
111     {
112         cameraContentsList = contentList;
113         isLoadedContents = true;
114         if (contentsListCallback != null)
115         {
116             try
117             {
118                 // 遠隔のカメラ内のコンテンツ一覧を引っ張ることができたよ、の通知
119                 contentsListCallback.contentsListCreated(contentList.size());
120                 if (informationReceiver != null)
121                 {
122                     String message = activity.getString(R.string.get_camera_contents_finished) + " : " + contentList.size();
123                     informationReceiver.updateMessage(message, false, false, 0);
124                 }
125
126                 // 最新の撮影データから並べる
127                 Collections.sort(contentList, new Comparator<ICameraContent>() {
128                     @Override
129                     public int compare(ICameraContent lhs, ICameraContent rhs)
130                     {
131                         long diff = rhs.getCapturedDate().getTime() - lhs.getCapturedDate().getTime();
132                         if (diff == 0)
133                         {
134                             diff = rhs.getContentName().compareTo(lhs.getContentName());
135                         }
136                         return (int)Math.min(Math.max(-1, diff), 1);
137                     }
138                 });
139
140                 //// とりあえず、できたコンテンツ一覧をログにダンプしてみる。
141                 //dumpCameraContentList();
142             }
143             catch (Exception e)
144             {
145                 e.printStackTrace();
146             }
147         }
148     }
149
150     @Override
151     public void onErrorOccurred(Exception e)
152     {
153         isLoadedContents = false;
154         cameraContentsList = null;
155         if (informationReceiver != null)
156         {
157             informationReceiver.updateMessage(activity.getString(R.string.get_camera_contents_error), false, true, Color.RED);
158         }
159
160         //  再試行する? の確認を出す
161         e.printStackTrace();
162         final AlertDialog.Builder builder = new AlertDialog.Builder(activity)
163                 .setCancelable(true)
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() {
167                     @Override
168                     public void onClick(DialogInterface dialog, int which)
169                     {
170                         getRemoteCameraContentsList(true, contentsListCallback);
171                     }
172                 });
173         activity.runOnUiThread(new Runnable()
174         {
175             @Override
176             public void run()
177             {
178                 builder.show();
179             }
180         });
181     }
182
183 /*
184     private void dumpCameraContentList()
185     {
186         Log.v(TAG, "dumpCameraContentList()");
187         if (cameraContentsList == null)
188         {
189             return;
190         }
191         try {
192             int index = 1;
193             for (ICameraContent content : cameraContentsList)
194             {
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);
201                 index++;
202             }
203         }
204         catch (Exception e)
205         {
206             e.printStackTrace();
207         }
208     }
209 */
210
211     /**
212      *    ファイル全件のリストを返す
213      *
214      *
215      */
216     @Override
217     public List<ICameraContent> getContentsList()
218     {
219         if (cameraContentsList == null)
220         {
221             getRemoteCameraContentsListImpl(this);
222             return (new ArrayList<>());
223         }
224         return (cameraContentsList);
225     }
226
227     /**
228      *  指定された年月日(yyyy/MM/DD)に含まれている一覧を応答する
229      *
230      *
231      */
232     @Override
233     public List<ICameraContent> getContentsListAtDate(String date)
234     {
235         Log.v(TAG, "getContentsListAtDate() : " + date);
236         if (date.equals("ALL"))
237         {
238             return (getContentsList());
239         }
240         if (cameraContentsList == null)
241         {
242             getRemoteCameraContentsListImpl(this);
243             return (new ArrayList<>());
244         }
245
246         ArrayList<ICameraContent> targetList = new ArrayList<>();
247         SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd", Locale.ENGLISH);
248         for (ICameraContent content : cameraContentsList)
249         {
250             String capturedDate = format.format(content.getCapturedDate());
251             if (date.equals(capturedDate))
252             {
253                 targetList.add(content);
254             }
255         }
256         Collections.sort(targetList, new Comparator<ICameraContent>() {
257             @Override
258             public int compare(ICameraContent lhs, ICameraContent rhs)
259             {
260                 long diff = rhs.getCapturedDate().getTime() - lhs.getCapturedDate().getTime();
261                 if (diff == 0)
262                 {
263                     diff = rhs.getContentName().compareTo(lhs.getContentName());
264                 }
265                 return (int)Math.min(Math.max(-1, diff), 1);
266             }
267         });
268         //Collections.sort(targetList);
269         return (targetList);
270     }
271
272     /**
273      *   指定されたパスに入っている一覧を応答する
274      *
275      *
276      */
277     @Override
278     public List<ICameraContent> getContentsListAtPath(String path)
279     {
280         Log.v(TAG, "getContentsListAtPath() : " + path);
281         if (path.equals("ALL"))
282         {
283             // 全件の場合...
284             return (getContentsList());
285         }
286         if (cameraContentsList == null)
287         {
288             getRemoteCameraContentsListImpl(this);
289             return (new ArrayList<>());
290         }
291
292         ArrayList<ICameraContent> targetList = new ArrayList<>();
293         for (ICameraContent content : cameraContentsList)
294         {
295             if (path.equals(content.getContentPath()))
296             {
297                 targetList.add(content);
298             }
299         }
300         Collections.sort(targetList, new Comparator<ICameraContent>() {
301             @Override
302             public int compare(ICameraContent lhs, ICameraContent rhs)
303             {
304                 long diff = rhs.getCapturedDate().getTime() - lhs.getCapturedDate().getTime();
305                 if (diff == 0)
306                 {
307                     diff = rhs.getContentName().compareTo(lhs.getContentName());
308                 }
309                 return (int)Math.min(Math.max(-1, diff), 1);
310             }
311         });
312         //Collections.sort(targetList);
313         //Log.v(TAG, "getContentsListAtPath() " + targetList.size());
314         return (targetList);
315     }
316
317     /**
318      *   撮影年月日の一覧を取得する
319      *   (最新の撮影日からの並びに整列する)
320      *
321      */
322     @Override
323     public List<String> getDateList()
324     {
325         if (cameraContentsList == null)
326         {
327             return (new ArrayList<>());
328         }
329         SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd", Locale.ENGLISH);
330         HashMap<String, String> map = new HashMap<>();
331         for (ICameraContent content : cameraContentsList)
332         {
333             map.put(format.format(content.getCapturedDate()), content.getContentName());
334         }
335         ArrayList<String> dateList = new ArrayList<>(map.keySet());
336         Collections.sort(dateList, Collections.reverseOrder());
337         return (dateList);
338     }
339
340     /**
341      *   ファイルパス(ディレクトリ)の一覧を取得する
342      *   たぶん、新しい画像が入ったディレクトリからのリスト
343      *
344      */
345     @Override
346     public List<String> getPathList()
347     {
348         if (cameraContentsList == null)
349         {
350             return (new ArrayList<>());
351         }
352         HashMap<String, String> map = new HashMap<>();
353         for (ICameraContent content : cameraContentsList)
354         {
355             map.put(content.getContentPath(), content.getContentName());
356         }
357         ArrayList<String> pathList = new ArrayList<>(map.keySet());
358         Collections.sort(pathList, Collections.reverseOrder());
359         return (pathList);
360     }
361 }