OSDN Git Service

da0409966f88a81ae8bbf92b8f18ba960804f8aa
[gokigen/PKRemote.git] / app / src / main / java / net / osdn / gokigen / pkremote / transfer / FileAutoTransferMain.java
1 package net.osdn.gokigen.pkremote.transfer;
2
3 import android.util.Log;
4
5 import net.osdn.gokigen.pkremote.R;
6 import net.osdn.gokigen.pkremote.camera.interfaces.IInterfaceProvider;
7 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraContent;
8 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraContentListCallback;
9 import net.osdn.gokigen.pkremote.playback.detail.MyContentDownloader;
10
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14
15 import androidx.annotation.NonNull;
16 import androidx.appcompat.app.AppCompatActivity;
17
18 /**
19  *   画像の自動転送を実現するクラス
20  *   (UIスレッドでは動いていないので注意)
21  */
22 class FileAutoTransferMain implements ICameraContentListCallback
23 {
24     private final String TAG = this.toString();
25
26     private final IInterfaceProvider interfaceProvider;
27     private final AppCompatActivity activity;
28     private final ITransferMessage messageInterface;
29     private final MyContentDownloader downloader;
30     private boolean firstContent = false;
31     private List<ICameraContent> baseContentList = null;
32     private List<ICameraContent> currentContentList = null;
33     private HashMap<String, ICameraContent> contentHashMap;
34     private boolean getRaw = false;
35     private boolean smallSize = false;
36     private boolean isChecking = false;
37
38
39     FileAutoTransferMain(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider provider, @NonNull ITransferMessage messageInterface)
40     {
41         this.activity = context;
42         this.interfaceProvider = provider;
43         this.messageInterface = messageInterface;
44         this.downloader = new MyContentDownloader(context, provider.getPlaybackControl());
45         this.contentHashMap = new HashMap<>();
46     }
47
48     /**
49      *   画像の自動転送 前処理
50      *
51      */
52     void start(boolean getRaw, boolean smallSize)
53     {
54         String message = "TRANSFER START [raw:" + getRaw + "] [small:" + smallSize + "]";
55         Log.v(TAG, message);
56
57         try
58         {
59             // 内部データの初期化
60             baseContentList = null;
61             currentContentList = null;
62             firstContent = true;
63             contentHashMap.clear();
64
65             this.getRaw = getRaw;
66             this.smallSize = smallSize;
67
68             // RunモードをRecordingに変更する。
69             //  (Olympus Air向けだったのだが ... でも liveview が開始されていないと撮影できなさそう...)
70             interfaceProvider.getCameraRunMode().changeRunMode(true);
71
72             // 現在のカメラ画像一覧をとってくる
73             interfaceProvider.getPlaybackControl().getCameraContentList(this);
74         }
75         catch (Exception e)
76         {
77             e.printStackTrace();
78         }
79     }
80
81     boolean isChecking()
82     {
83         return (isChecking);
84     }
85
86     /**
87      *   画像の自動転送  本処理
88      *
89      */
90     void checkFiles()
91     {
92         try
93         {
94             Log.v(TAG, "CHECK FILE");
95             isChecking = true;
96             interfaceProvider.getPlaybackControl().getCameraContentList(this);
97         }
98         catch (Exception e)
99         {
100             e.printStackTrace();
101         }
102     }
103
104     /**
105      *   画像の自動転送 後処理
106      *
107      */
108     void finish()
109     {
110         try
111         {
112             Log.v(TAG, "FINISH");
113             messageInterface.showInformation("");
114             baseContentList = null;
115             currentContentList = null;
116
117             // RunモードをPlaybackモードに戻す。
118             interfaceProvider.getCameraRunMode().changeRunMode(false);
119         }
120         catch (Exception e)
121         {
122             e.printStackTrace();
123         }
124         System.gc();
125     }
126
127     private boolean downloadImages()
128     {
129         Log.v(TAG, "downloadImages()");
130         boolean isDownload = false;
131         try
132         {
133             ArrayList<ICameraContent> addContent = new ArrayList<>();
134             for (ICameraContent content : currentContentList)
135             {
136                 String key = (content.getContentPath() + "/" + content.getContentName()).toLowerCase();
137                 //Log.v(TAG, "KEY : " + key);
138
139                 // 追加ファイル発見!
140                 if (!contentHashMap.containsKey(key))
141                 {
142                     Log.v(TAG, "FILE(add) : " + key);
143                     contentHashMap.put(key, content);
144                     if ((key.endsWith(".jpg"))||(getRaw))
145                     {
146                         addContent.add(content);
147                     }
148                 }
149             }
150
151             // 見つけた画像を(連続して)ダウンロードする (ここから)
152             messageInterface.showInformation(activity.getString(R.string.add_image_pics) + " " + addContent.size());
153             if (addContent.size() > 0)
154             {
155                 //  一括ダウンロードする
156                 startDownloadBatch(addContent, smallSize);
157                 isDownload = true;
158             }
159         }
160         catch (Exception e)
161         {
162             e.printStackTrace();
163         }
164         return (isDownload);
165     }
166
167     /**
168      *    一括ダウンロードの開始
169      *
170      * @param isSmall  小さいサイズ(JPEG)
171      */
172     private void startDownloadBatch(final ArrayList<ICameraContent> imageContentList, final boolean isSmall)
173     {
174         try
175         {
176             int count = 1;
177             int totalSize = imageContentList.size();
178             for (ICameraContent content : imageContentList)
179             {
180                 downloader.startDownload(content, " (" + count + "/" + totalSize + ") ", null, isSmall);
181                 do
182                 {
183                     try
184                     {
185                         // ここでダウンロードが終わるまで、すこし待つ
186                         Thread.sleep(300);
187                     }
188                     catch (Exception e)
189                     {
190                         e.printStackTrace();
191                     }
192                 } while (downloader.isDownloading());
193                 count++;
194             }
195         }
196         catch (Exception e)
197         {
198             e.printStackTrace();
199         }
200     }
201
202     // ICameraContentListCallback
203     @Override
204     public void onCompleted(List<ICameraContent> contentList)
205     {
206         Log.v(TAG, "RECEIVE CONTENT LIST");
207         try
208         {
209             if (firstContent)
210             {
211                 baseContentList = contentList;
212                 if ((baseContentList != null)&&(baseContentList.size() > 0))
213                 {
214                     firstContent = false;
215
216                     //  初期データをHashMapに突っ込んでおく...
217                     for (ICameraContent content : baseContentList)
218                     {
219                         String key = (content.getContentPath() + "/" + content.getContentName()).toLowerCase();
220                         //Log.v(TAG, "FILE : " + key);
221                         contentHashMap.put(key, content);
222                     }
223                 }
224             }
225             else
226             {
227                 currentContentList = contentList;
228             }
229             if ((baseContentList != null)&&(currentContentList != null)&&(currentContentList.size() > 0))
230             {
231                 // コンテンツ数の差異を確認する。
232                 int baseSize = baseContentList.size();
233                 int currentSize = currentContentList.size();
234                 if (baseSize != currentSize)
235                 {
236                     // 画像ファイル数が変わった!
237                     messageInterface.showInformation(activity.getString(R.string.image_checking) + " " + currentSize);
238
239                     // 画像のダウンロードを実行する
240                     if (downloadImages())
241                     {
242                         // 実行がうまくいった場合は表示を更新する
243                         messageInterface.showInformation(activity.getString(R.string.image_download_done));
244                     }
245
246                     ////////////////////////////////////////  現在のカメラ内画像情報を差し替えて、次の増加分にそなえる
247                     baseContentList = currentContentList;
248                     currentContentList = null;
249                 }
250                 else
251                 {
252                     // 画像ファイル数が変わっていない場合は表示を消す
253                     messageInterface.showInformation(" ");
254                 }
255             }
256         }
257         catch (Exception e)
258         {
259             e.printStackTrace();
260         }
261         isChecking = false;
262     }
263
264     // ICameraContentListCallback
265     @Override
266     public void onErrorOccurred(Exception e)
267     {
268         Log.v(TAG, "RECEIVE FAILURE...");
269         e.printStackTrace();
270         isChecking = false;
271     }
272 }