OSDN Git Service

https://osdn.net/cvs/view/gokigen/MeMoMa/ から コピーしてくる。(+API27でビルドできるよう、少し調整。)
[gokigen/MeMoMa.git] / app / src / main / java / jp / sourceforge / gokigen / memoma / fileio / ViewCaptureExporter.java
1 package jp.sourceforge.gokigen.memoma.fileio;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.OutputStream;
6 import java.text.SimpleDateFormat;
7 import java.util.Calendar;
8 import android.app.ProgressDialog;
9 import android.content.Context;
10 import android.graphics.Bitmap;
11 import android.graphics.Bitmap.CompressFormat;
12 import android.os.AsyncTask;
13 import android.util.Log;
14 import android.view.View;
15
16 import jp.sourceforge.gokigen.memoma.Main;
17 import jp.sourceforge.gokigen.memoma.R;
18
19 /**
20  *  データをファイルに保存するとき用 アクセスラッパ (非同期処理を実行)
21  *  Viewの情報を画像形式(png形式)で保存する。
22  *  どのViewを保存するのかは、ICaptureExporter.getCaptureTargetView()クラスを使って教えてもらう。
23  *  
24  *  AsyncTask
25  *    String       : 実行時に渡すクラス(Param)           : ファイル名をもらう
26  *    Integer    : 途中経過を伝えるクラス(Progress)   : 今回は使っていない
27  *    String      : 処理結果を伝えるクラス(Result)      : 結果を応答する。
28  *    
29  * @author MRSa
30  *
31  */
32 public class ViewCaptureExporter extends AsyncTask<String, Integer, String>
33 {
34         private ICaptureExporter receiver = null;
35         private ExternalStorageFileUtility fileUtility = null;
36         private String exportedFileName = null;
37
38         private ProgressDialog savingDialog = null;
39         
40         private Bitmap targetBitmap = null;
41
42         /**
43          *   コンストラクタ
44          */
45     public ViewCaptureExporter(Context context, ExternalStorageFileUtility utility,  ICaptureExporter resultReceiver)
46     {
47         receiver = resultReceiver;
48         fileUtility = utility;
49
50         //  プログレスダイアログ(「保存中...」)を表示する。
51         savingDialog = new ProgressDialog(context);
52         savingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
53         savingDialog.setMessage(context.getString(R.string.dataSaving));
54         savingDialog.setIndeterminate(true);
55         savingDialog.setCancelable(false);
56         savingDialog.show();
57
58         /** ファイルをバックアップするディレクトリを作成する **/
59         File dir = new File(fileUtility.getGokigenDirectory() + "/exported");
60         dir.mkdir();
61     }
62         
63     /**
64      *  非同期処理実施前の前処理
65      * 
66      */
67     @Override
68     protected void onPreExecute()
69     {
70         try
71         {
72                 targetBitmap = null;
73             if (receiver != null)
74             {
75                 // 画面のキャプチャを実施する
76                 View targetView = receiver.getCaptureTargetView();
77                 targetView.setDrawingCacheEnabled(false);
78                 targetView.setDrawingCacheEnabled(true);
79                 targetBitmap = Bitmap.createBitmap(targetView.getDrawingCache());
80                 targetView.setDrawingCacheEnabled(false);
81             }
82         }
83         catch (Exception ex)
84         {
85                 Log.v(Main.APP_IDENTIFIER, "ViewCaptureExporter::onPreExecute() : " + ex.toString());
86         }
87     }
88     
89     /**
90      *    ビットマップデータを(PNG形式で)保管する。
91      *
92          *
93      */
94     private String exportToFile(String fileName)
95     {
96         String resultMessage = "";
97         try
98         {
99                 if (targetBitmap == null)
100                 {
101                         // ビットマップが取れないため、ここで折り返す。
102                         return ("SCREEN DATA GET FAILURE...");
103                 }
104                 
105                 // エクスポートするファイル名を決定する
106             Calendar calendar = Calendar.getInstance();
107             SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
108             exportedFileName = fileName + "_" + outFormat.format(calendar.getTime()) + ".png";
109
110             // PNG形式でファイル出力を行う。
111             OutputStream out = new FileOutputStream(exportedFileName);
112             targetBitmap.compress(CompressFormat.PNG, 100, out);
113             out.flush();
114             out.close();            
115         }
116         catch (Exception e)
117         {
118                 resultMessage = " ERR(png)>" + e.toString();
119             Log.v(Main.APP_IDENTIFIER, resultMessage);
120             e.printStackTrace();
121         } 
122         return (resultMessage);
123     }
124
125     /**
126      *  非同期処理
127      *  (バックグラウンドで実行する(このメソッドは、UIスレッドと別のところで実行する))
128      * 
129      */
130     @Override
131     protected String doInBackground(String... datas)
132     {
133         // ファイル名の設定 ... (拡張子なし)
134         String fileName = fileUtility.getGokigenDirectory() + "/exported/" + datas[0];
135
136         // データを保管する
137         String result = exportToFile(fileName);
138
139         System.gc();
140
141                 return (result);
142     }
143
144     /**
145      *  非同期処理の進捗状況の更新
146      * 
147      */
148         @Override
149         protected void onProgressUpdate(Integer... values)
150         {
151         // 今回は何もしない
152         }
153
154     /**
155      *  非同期処理の後処理
156      *  (結果を応答する)
157      */
158     @Override
159     protected void onPostExecute(String result)
160     {
161         try
162         {
163             if (receiver != null)
164             {
165                 receiver.onCaptureExportedResult(exportedFileName, result);
166             }
167         }
168         catch (Exception ex)
169         {
170                 Log.v(Main.APP_IDENTIFIER, "ViewCaptureExporter::onPostExecute() : " + ex.toString());
171         }
172         // プログレスダイアログを消す
173         if (savingDialog != null)
174         {
175             savingDialog.dismiss();
176         }
177         return;
178     }     
179  
180     /**
181      *    結果報告用のインタフェース
182      *    
183      * @author MRSa
184      *
185      */
186     public interface ICaptureExporter
187     {
188         /** データをキャプチャする Viewを取得する **/
189         public abstract View getCaptureTargetView();
190         
191         /**  保存結果の報告 **/
192         public abstract void onCaptureExportedResult(String exportedFileName, String detail);
193     }
194 }