OSDN Git Service

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