OSDN Git Service

44f66dbd0c348b325ae6903778d88212e8df6c6e
[gokigen/MeMoMa.git] / app / src / main / java / jp / sourceforge / gokigen / memoma / MeMoMaDataInOutManager.java
1 package jp.sourceforge.gokigen.memoma;
2
3 import java.text.SimpleDateFormat;
4 import java.util.Calendar;
5
6 import android.support.v7.app.ActionBar;
7 import android.app.Activity;
8 import android.content.SharedPreferences;
9 import android.preference.PreferenceManager;
10 import android.util.Log;
11 import android.widget.Toast;
12
13 public class MeMoMaDataInOutManager implements MeMoMaFileSavingProcess.ISavingStatusHolder, MeMoMaFileSavingProcess.IResultReceiver, MeMoMaFileLoadingProcess.IResultReceiver,  ActionBar.OnNavigationListener, ObjectLayoutCaptureExporter.ICaptureLayoutExporter
14 {
15         private Activity parent = null;
16         private MeMoMaObjectHolder objectHolder = null;
17         private ExternalStorageFileUtility fileUtility = null;
18     private MeMoMaDataFileHolder dataFileHolder = null;
19         
20         private boolean isSaving = false;       
21         private boolean isShareExportedData = false;
22         
23         /**
24          *    コンストラクタ
25          * 
26          */
27         public MeMoMaDataInOutManager(Activity activity)
28         {
29             parent = activity;
30         fileUtility = new ExternalStorageFileUtility(Main.APP_BASEDIR);
31         }
32
33         /**
34          *
35          *
36          */
37         public void prepare(MeMoMaObjectHolder objectHolder, ActionBar bar, String fileName)
38         {
39         this.objectHolder = objectHolder;
40         //this.lineHolder = lineHolder;
41         
42         // データファイルフォルダを更新する
43         dataFileHolder = new MeMoMaDataFileHolder(parent, android.R.layout.simple_spinner_dropdown_item, fileUtility, ".xml");
44         int index = dataFileHolder.updateFileList(fileName, null);
45
46         try
47                 {
48                         // アクションバーを設定する
49                         prepareActionBar(bar);
50                 }
51                 catch (Exception e)
52                 {
53                         e.printStackTrace();
54                 }
55
56         // タイトルの設定を変更する
57         if ((bar != null)&&(index >= 0))
58         {
59             bar.setSelectedNavigationItem(index);  // 実験...
60         }
61         }
62
63         /**
64          *   データファイル一覧を更新し、アクションバーに反映させる
65          *
66          */
67         public void updateFileList(String titleName, ActionBar bar)
68         {
69                 if (dataFileHolder != null)
70                 {
71                         // データファイル一覧を更新する
72             int index = dataFileHolder.updateFileList(titleName, null);
73
74             // タイトルをオブジェクトフォルダに記憶させる
75                 objectHolder.setDataTitle(titleName);
76
77                 // タイトルの設定を変更する
78             if ((bar != null)&&(index >= 0))
79             {
80                 bar.setSelectedNavigationItem(index);  // 実験...
81             }
82                 }
83         }
84
85     /**
86      *   データの保存を行う (同名のファイルが存在していた場合、 *.BAKにリネーム(上書き)してから保存する)
87      *   
88      *   
89      *   @param forceOverwrite  trueの時は、ファイル名が確定していたときは(確認せずに)上書き保存を自動で行う。
90      *   
91      */
92         public void saveFile(String dataTitle, boolean forceOverwrite)
93         {
94                 if (objectHolder == null)
95                 {
96                         Log.e(Main.APP_IDENTIFIER, "ERR>MeMoMaDataInOutManager::saveFile() : "  + dataTitle);
97                         return;
98                 }
99
100                 // タイトルをオブジェクトフォルダに記憶させる
101                 objectHolder.setDataTitle(dataTitle);
102                 Log.v(Main.APP_IDENTIFIER, "MeMoMaDataInOutManager::saveFile() : "  + dataTitle);
103
104                 // 同期型でファイルを保存する。。。
105                 String message = saveFileSynchronous();
106                 onSavedResult(message);
107         }
108
109         /**
110          *    データファイルのフルパスを応答する
111          * 
112          * @param dataTitle
113          * @return
114          */
115         public String getDataFileFullPath(String dataTitle, String extension)
116         {
117                 return (fileUtility.getGokigenDirectory() + "/" + dataTitle + extension);
118         }
119         
120         /**  保存中状態を設定する **/
121     public void setSavingStatus(boolean isSaving)
122     {
123         this.isSaving = isSaving;
124     }
125     
126     /** 保存中状態を取得する **/
127     public boolean getSavingStatus()
128     {
129         return (isSaving);
130     }
131
132         /**
133          *    保存終了時の処理
134          */
135     public  void onSavedResult(String detail)
136     {
137         // 保存したことを伝達する
138                 String outputMessage = parent.getString(R.string.save_data) + " " + objectHolder.getDataTitle() + " " + detail;
139         Toast.makeText(parent, outputMessage, Toast.LENGTH_SHORT).show();       
140
141                 // ファイルリスト更新 ... (ここでやっちゃあ、AsyncTaskにしている意味ないなあ...)
142         dataFileHolder.updateFileList(objectHolder.getDataTitle(), null);
143     }
144
145     /**
146          *    読み込み終了時の処理
147          */
148     public  void onLoadedResult(String detail)
149     {
150         // 読み込みしたことを伝達する
151                 String outputMessage = parent.getString(R.string.load_data) + " " + objectHolder.getDataTitle() + " " + detail;
152         Toast.makeText(parent, outputMessage, Toast.LENGTH_SHORT).show();
153
154         // 画面を再描画する
155         final GokigenSurfaceView surfaceview = (GokigenSurfaceView) parent.findViewById(R.id.GraphicView);
156         surfaceview.doDraw();
157     }
158
159     /**
160      *    ファイルをロードする途中のバックグラウンド処理...
161      * 
162      */
163         public void onLoadingProcess()
164         {
165         // 何もしない...
166         }
167
168     /**
169      *    ファイルからデータを読み込む。
170      * 
171      * @param dataTitle
172      */
173     public void loadFile(String dataTitle)
174     {
175         loadFileWithName(dataTitle);
176     }
177     
178     
179     /**
180      *   ファイルからのデータ読み込み処理
181      * 
182      * @param dataTitle
183      */
184         private void loadFileWithName(String dataTitle)
185         {
186         if (objectHolder == null)
187                 {
188                         Log.e(Main.APP_IDENTIFIER, "ERR>MeMoMaDataInOutManager::loadFile() : "  + dataTitle);
189                         return;
190                 }
191
192                 // タイトルをオブジェクトフォルダに記憶させる
193                 objectHolder.setDataTitle(dataTitle);
194                 Log.v(Main.APP_IDENTIFIER, "MeMoMaDataInOutManager::loadFile() : "  + dataTitle);
195
196                 // AsyncTaskを使ってデータを読み込む
197                 MeMoMaFileLoadingProcess asyncTask = new MeMoMaFileLoadingProcess(parent, fileUtility, this);
198         asyncTask.execute(objectHolder);
199         }
200
201         /**
202          *    アクションバーを更新する...
203          * 
204          * @param bar
205          */
206         private void prepareActionBar(ActionBar bar)
207         {
208                 try {
209                         bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);  // リストを入れる
210                         bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);   // タイトルの表示をマスクする
211                         bar.setListNavigationCallbacks(dataFileHolder, this);
212                 }
213                 catch (Exception e)
214                 {
215                         e.printStackTrace();
216                 }
217         }
218
219         /**
220          *    ファイルを保存する...同期型で。
221          * 
222          * @return
223          */
224         private String saveFileSynchronous()
225         {
226                 // 同期型でファイルを保存する。。。
227         SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(parent);
228         String backgroundUri = preferences.getString("backgroundUri","");
229         String userCheckboxString = preferences.getString("userCheckboxString","");
230         MeMoMaFileSavingEngine saveEngine = new MeMoMaFileSavingEngine(fileUtility, backgroundUri, userCheckboxString);
231         String message = saveEngine.saveObjects(objectHolder);
232         return (message);               
233         }
234         
235         
236         /**
237          * 
238          * 
239          */
240         public boolean onNavigationItemSelected(int itemPosition, long itemId)
241         {
242                 String data = dataFileHolder.getItem(itemPosition);
243                 Log.v(Main.APP_IDENTIFIER, "onNavigationItemSelected(" + itemPosition + "," + itemId + ") : " + data);
244
245                 // 同期型で現在のファイルを保存する。。。
246                 String message = saveFileSynchronous();
247                 if (message.length() != 0)
248                 {
249             onSavedResult(message);
250                 }
251                 
252         // 選択したファイル名をタイトルに反映し、またPreferenceにも記憶する
253         parent.setTitle(data);
254         SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(parent);
255         SharedPreferences.Editor editor = preferences.edit();
256         editor.putString("MeMoMaInfo", data);
257         editor.commit();
258
259                 // 選択したアイテムをロードする!
260         loadFileWithName(data);
261
262                  return (true);
263         }
264
265         /**
266          *    スクリーンキャプチャを実施する
267          * 
268          */
269         public void doScreenCapture(String title, MeMoMaObjectHolder holder, MeMoMaCanvasDrawer drawer, boolean isShare)
270         {
271                 isShareExportedData = isShare;
272                 
273         // AsyncTaskを使ってデータをエクスポートする
274                 ObjectLayoutCaptureExporter asyncTask = new ObjectLayoutCaptureExporter(parent, fileUtility, holder, drawer, this);
275         asyncTask.execute(title);
276         }
277         
278     /**
279      *    ファイルのエクスポート結果を受け取る
280      * 
281      */
282         public void onCaptureLayoutExportedResult(String exportedFileName, String detail)
283     {
284                 Log.v(Main.APP_IDENTIFIER, "MeMoMaDataInOutManager::onCaptureExportedResult() '"  + objectHolder.getDataTitle() +"' : " + detail);
285
286                 // エクスポートしたことを伝達する
287                 String outputMessage = parent.getString(R.string.capture_data) + " " + objectHolder.getDataTitle() + " " + detail;
288         Toast.makeText(parent, outputMessage, Toast.LENGTH_SHORT).show();
289         
290         if (isShareExportedData == true)
291         {
292                 // エクスポートしたファイルを共有する
293                 shareContent(exportedFileName);
294         }
295         isShareExportedData = false;
296     }
297
298     /**
299      *    エクスポートしたファイルを共有する
300      * 
301      * @param fileName
302      */
303     private void shareContent(String fileName)
304     {
305         String message = "";
306         try
307         {
308                 // 現在の時刻を取得する
309             Calendar calendar = Calendar.getInstance();
310                 SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
311             String date =  outFormat.format(calendar.getTime());
312
313             // メールタイトル
314             String title = parent.getString(R.string.app_name) + " | "+ objectHolder.getDataTitle() + " | " + date;
315
316             // メールの本文を構築する
317             message = message + "Name : " + objectHolder.getDataTitle() + "\n";
318             message = message + "exported : " + date + "\n";
319             message = message + "number of objects : " + objectHolder.getCount() + "\n";
320
321             // Share Intentを発行する。
322             SharedIntentInvoker.shareContent(parent, MeMoMaListener.MENU_ID_SHARE, title, message,  fileName, "image/png");
323         }
324         catch (Exception ex)
325         {
326                 ex.printStackTrace();
327         }
328     }
329
330 }