Merge pull request #23 from lacerta-doc/source/add_files

画像ファイルの追加(保存), メタデータの管理
This commit is contained in:
ろむねこ 2024-01-08 16:18:36 +09:00 committed by GitHub
commit 717b8e9637
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 1348 additions and 3 deletions

View File

@ -1,5 +1,6 @@
plugins {
alias(libs.plugins.com.android.library)
id 'com.google.dagger.hilt.android'
}
android {
@ -33,8 +34,16 @@ dependencies {
androidTestImplementation libs.androidx.test.ext.junit
androidTestImplementation libs.androidx.test.espresso.core
// Hilt (DI)
implementation libs.com.google.dagger.hilt.android
annotationProcessor libs.com.google.dagger.hilt.compiler
implementation 'com.websitebeaver:documentscanner:1.0.0'
implementation project(':shared:ui')
implementation project(':model')
implementation project(':processor')
implementation project(':utils')
}

View File

@ -25,15 +25,32 @@ import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;
import javax.inject.Inject;
import dagger.hilt.android.AndroidEntryPoint;
import one.nem.lacerta.model.document.DocumentDetail;
import one.nem.lacerta.model.document.DocumentMeta;
import one.nem.lacerta.model.document.path.DocumentPath;
import one.nem.lacerta.processor.DocumentProcessor;
import one.nem.lacerta.processor.factory.DocumentProcessorFactory;
import one.nem.lacerta.utils.LacertaLogger;
import one.nem.lacerta.utils.repository.DeviceInfoUtils;
/**
* A simple {@link Fragment} subclass.
* Use the {@link ScannerDataManagerStubFragment#newInstance} factory method to
* create an instance of this fragment.
*/
@AndroidEntryPoint
public class ScannerDataManagerStubFragment extends Fragment {
// TODO-rca: 時間があったらcacheを使うようにする
@ -43,6 +60,19 @@ public class ScannerDataManagerStubFragment extends Fragment {
private Uri photoURI;
private DocumentDetail documentDetail;
private DocumentProcessor documentProcessor;
@Inject
DocumentProcessorFactory documentProcessorFactory;
@Inject
LacertaLogger logger;
@Inject
DeviceInfoUtils deviceInfoUtils;
private final ActivityResultLauncher<Intent> cameraLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
@ -120,6 +150,65 @@ public class ScannerDataManagerStubFragment extends Fragment {
}
updateResults();
});
view.findViewById(R.id.button_create_documnent).setOnClickListener(v -> {
Log.d("ScannerDataManagerStubFragment", "button_create_documnent clicked");
Toast.makeText(getActivity(), "button_create_documnent clicked", Toast.LENGTH_LONG).show();
this.documentDetail = createSampleDocumentDetail();
});
view.findViewById(R.id.button_init_document_processor).setOnClickListener(v -> {
Log.d("ScannerDataManagerStubFragment", "button_init_document_processor clicked");
Toast.makeText(getActivity(), "button_init_document_processor clicked", Toast.LENGTH_LONG).show();
// TODO-rca: ここでDocumentProcessorを初期化する
if (this.documentDetail == null) {
Toast.makeText(getActivity(), "documentDetail is null", Toast.LENGTH_LONG).show();
return;
}
this.documentProcessor = documentProcessorFactory.create(this.documentDetail);
Toast.makeText(getActivity(), "documentProcessor created", Toast.LENGTH_LONG).show();
this.documentProcessor.init();
Toast.makeText(getActivity(), "documentProcessor initialized", Toast.LENGTH_LONG).show();
});
view.findViewById(R.id.button_add_page).setOnClickListener(v -> {
Log.d("ScannerDataManagerStubFragment", "button_add_page clicked");
Toast.makeText(getActivity(), "button_add_page clicked", Toast.LENGTH_LONG).show();
if (this.documentProcessor == null) {
Toast.makeText(getActivity(), "documentProcessor is null", Toast.LENGTH_LONG).show();
return;
}
Bitmap[] bitmaps = new Bitmap[results.size()];
for (int i = 0; i < results.size(); i++) {
bitmaps[i] = results.get(i).getBitmap();
}
this.documentProcessor.addNewPagesToLast(bitmaps);
this.documentProcessor.close();
});
}
public DocumentDetail createSampleDocumentDetail() {
String id = UUID.randomUUID().toString();
Toast.makeText(getActivity(), "Generated id: " + id, Toast.LENGTH_LONG).show();
//logger.debug("CreateSample", "Generated id: " + id);
DocumentMeta meta = new DocumentMeta(
id,
"Sample" + DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now()),
new Date(),
new Date());
DocumentPath path = new DocumentPath(
deviceInfoUtils.getExternalStorageDirectoryString(),
"Sample" + DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now()));
return new DocumentDetail(meta, path, "SampleAuthor", "SampleDefaultBranch");
}
@Override

View File

@ -11,6 +11,7 @@
android:id="@+id/action_button_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:weightSum="2"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
@ -19,7 +20,7 @@
<com.google.android.material.button.MaterialButton
android:id="@+id/button_call_camera"
style="@style/Widget.Material3.Button.IconButton.Filled"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16px"
android:layout_weight="1"
@ -27,6 +28,58 @@
</LinearLayout>
<LinearLayout
android:id="@+id/init_button_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/action_button_container">
<com.google.android.material.button.MaterialButton
android:id="@+id/button_create_documnent"
style="@style/Widget.Material3.Button.IconButton.Filled"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16px"
android:layout_weight="1"
android:text="Create Doc Obj" />
<com.google.android.material.button.MaterialButton
android:id="@+id/button_init_document_processor"
style="@style/Widget.Material3.Button.IconButton.Filled"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16px"
android:layout_weight="1"
android:text="Init Processor" />
</LinearLayout>
<LinearLayout
android:id="@+id/doc_button_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:weightSum="2"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/init_button_container">
<com.google.android.material.button.MaterialButton
android:id="@+id/button_add_page"
style="@style/Widget.Material3.Button.IconButton.Filled"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16px"
android:layout_weight="1"
android:text="Add page to last" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/result_recycler_view"
android:layout_width="match_parent"
@ -35,6 +88,6 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/action_button_container" />
app:layout_constraintTop_toBottomOf="@id/doc_button_container" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -65,4 +65,7 @@ dependencies {
// Scanner
implementation project(':component:scanner')
// Processor
implementation project(':processor')
}

View File

@ -0,0 +1,44 @@
package one.nem.lacerta.feature.debug;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
* Use the {@link DebugMenuDocProcessorTesterFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class DebugMenuDocProcessorTesterFragment extends Fragment {
public DebugMenuDocProcessorTesterFragment() {
// Required empty public constructor
}
public static DebugMenuDocProcessorTesterFragment newInstance() {
DebugMenuDocProcessorTesterFragment fragment = new DebugMenuDocProcessorTesterFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_debug_menu_doc_processor_tester, container, false);
return view;
}
}

View File

@ -0,0 +1,110 @@
package one.nem.lacerta.feature.debug;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import dagger.hilt.android.AndroidEntryPoint;
import one.nem.lacerta.source.file.FileManager;
import one.nem.lacerta.source.file.factory.FileManagerFactory;
import one.nem.lacerta.utils.repository.DeviceInfoUtils;
import javax.inject.Inject;
/**
* A simple {@link Fragment} subclass.
* Use the {@link DebugMenuFileManagerTesterFragment#newInstance} factory method to
* create an instance of this fragment.
*/
@AndroidEntryPoint
public class DebugMenuFileManagerTesterFragment extends Fragment {
public DebugMenuFileManagerTesterFragment() {
// Required empty public constructor
}
@Inject
FileManagerFactory fileManagerFactory;
@Inject
DeviceInfoUtils deviceInfoUtils;
// TODO: Rename and change types and number of parameters
public static DebugMenuFileManagerTesterFragment newInstance() {
DebugMenuFileManagerTesterFragment fragment = new DebugMenuFileManagerTesterFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_debug_menu_file_manager_tester, container, false);
view.findViewById(R.id.button_create_directory).setOnClickListener(v -> {
EditText editText = view.findViewById(R.id.edit_text_dir_name);
String dirName = editText.getText().toString();
FileManager fileManager = fileManagerFactory.create(deviceInfoUtils.getExternalStorageDirectory());
fileManager.createDir(dirName);
});
view.findViewById(R.id.button_save_item).setOnClickListener(v -> {
FileManager fileManager = fileManagerFactory.create(deviceInfoUtils.getExternalStorageDirectory());
fileManager.createDir("test");
fileManager.changeDir("test");
Bitmap bitmap = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888);
// Bitmapに描画処理を行う
Canvas canvas = new Canvas(bitmap);
// 大きな山の形状を作成
android.graphics.Path bigMountainPath = new android.graphics.Path();
bigMountainPath.moveTo(100, 800); // 左下の開始点
bigMountainPath.lineTo(500, 300); // 頂点
bigMountainPath.lineTo(900, 800); // 右下
bigMountainPath.close(); // パスを閉じる
// 山の描画設定
Paint mountainPaint = new Paint();
mountainPaint.setColor(Color.GREEN);
mountainPaint.setStyle(Paint.Style.FILL);
// 大きな山を描画
canvas.drawPath(bigMountainPath, mountainPaint);
// 小さな山の形状を作成
android.graphics.Path smallMountainPath = new android.graphics.Path();
smallMountainPath.moveTo(400, 800); // 左下の開始点
smallMountainPath.lineTo(650, 400); // 頂点
smallMountainPath.lineTo(900, 800); // 右下
smallMountainPath.close(); // パスを閉じる
Paint smallMountainPaint = new Paint();
smallMountainPaint.setColor(Color.parseColor("#006e54"));
smallMountainPaint.setStyle(Paint.Style.FILL);
// 小さな山を描画
canvas.drawPath(smallMountainPath, smallMountainPaint);
fileManager.saveBitmapAtCurrent(bitmap, "test.png");
});
return view;
}
}

View File

@ -12,6 +12,7 @@ import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import dagger.hilt.android.AndroidEntryPoint;
import one.nem.lacerta.feature.debug.common.adapter.DebugMenuListItemAdapter;
import one.nem.lacerta.feature.debug.common.model.DebugMenuListItem;
@ -20,6 +21,7 @@ import one.nem.lacerta.feature.debug.common.model.DebugMenuListItem;
* Use the {@link DebugMenuTopFragment#newInstance} factory method to
* create an instance of this fragment.
*/
@AndroidEntryPoint
public class DebugMenuTopFragment extends Fragment {
public DebugMenuTopFragment() {
// Required empty public constructor
@ -49,6 +51,7 @@ public class DebugMenuTopFragment extends Fragment {
debugMenuListItems.add(new DebugMenuListItem("Meta Data", "View meta data", R.id.action_debugMenuTopFragment_to_debugMenuMetaDataFragment, true));
debugMenuListItems.add(new DebugMenuListItem("Document Tester", "placeholder", R.id.action_debugMenuTopFragment_to_debugMenuDocumentTesterTopFragment, true));
debugMenuListItems.add(new DebugMenuListItem("Scanner", "placeholder", R.id.action_debugMenuTopFragment_to_scannerDataManagerStubFragment, true));
debugMenuListItems.add(new DebugMenuListItem("File Manager", "placeholder", R.id.action_debugMenuTopFragment_to_debugMenuFileManagerTesterFragment, true));
DebugMenuListItemAdapter adapter = new DebugMenuListItemAdapter(debugMenuListItems);
recyclerView.setAdapter(adapter);

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/Theme.Lacerta"
tools:context=".DebugMenuDocProcessorTesterFragment" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/button_gen_random_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Generate random image" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DebugMenuFileManagerTesterFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/edit_text_dir_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/button_create_directory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create dir" />
<Button
android:id="@+id/button_dir_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="List item in current" />
<Button
android:id="@+id/button_save_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save Test Image" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -19,6 +19,9 @@
<action
android:id="@+id/action_debugMenuTopFragment_to_scannerDataManagerStubFragment"
app:destination="@id/scannerDataManagerStubFragment" />
<action
android:id="@+id/action_debugMenuTopFragment_to_debugMenuFileManagerTesterFragment"
app:destination="@id/debugMenuFileManagerTesterFragment" />
</fragment>
<fragment
android:id="@+id/debugMenuMetaDataFragment"
@ -51,4 +54,9 @@
android:id="@+id/scannerDataManagerStubFragment"
android:name="one.nem.lacerta.component.scanner.ScannerDataManagerStubFragment"
android:label="ScannerDataManagerStubFragment" />
<fragment
android:id="@+id/debugMenuFileManagerTesterFragment"
android:name="one.nem.lacerta.feature.debug.DebugMenuFileManagerTesterFragment"
android:label="fragment_debug_menu_file_manager_tester"
tools:layout="@layout/fragment_debug_menu_file_manager_tester" />
</navigation>

View File

@ -0,0 +1,104 @@
package one.nem.lacerta.model.document.internal;
import java.util.ArrayList;
public class XmlMetaModel {
String title;
String author;
String description;
// Date created;
// Date updated;
String defaultBranch;
ArrayList<XmlMetaPageModel> pages;
// Constructor
public XmlMetaModel() {
}
public XmlMetaModel(String title, String author, String description, String defaultBranch, ArrayList<XmlMetaPageModel> pages) {
this.title = title;
this.author = author;
this.description = description;
this.defaultBranch = defaultBranch;
this.pages = pages;
}
// Getter
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getDescription() {
return description;
}
public String getDefaultBranch() {
return defaultBranch;
}
public ArrayList<XmlMetaPageModel> getPages() {
return pages;
}
// Setter
public void setTitle(String title) {
this.title = title;
}
public void setAuthor(String author) {
this.author = author;
}
public void setDescription(String description) {
this.description = description;
}
public void setDefaultBranch(String defaultBranch) {
this.defaultBranch = defaultBranch;
}
public void setPages(ArrayList<XmlMetaPageModel> pages) {
this.pages = pages;
}
// Public Methods
public void addPage(XmlMetaPageModel page) {
this.pages.add(page);
}
public void addPageAfterIndex(int index, XmlMetaPageModel page) {
this.pages.add(index, page);
//Update index
this.updateIndex();
}
public void removePage(XmlMetaPageModel page) {
this.pages.remove(page);
}
public void removePageAtIndex(int index) {
this.pages.remove(index);
//Update index
this.updateIndex();
}
// Internal Methods
private void updateIndex() { // TODO-rca: 効率悪そう
for (int i = 0; i < this.pages.size(); i++) {
this.pages.get(i).setIndex(i);
}
}
}

View File

@ -0,0 +1,37 @@
package one.nem.lacerta.model.document.internal;
public class XmlMetaPageModel {
int index;
String filename;
// Constructor
public XmlMetaPageModel() {
}
public XmlMetaPageModel(int index, String filename) {
this.index = index;
this.filename = filename;
}
// Getter
public int getIndex() {
return index;
}
public String getFilename() {
return filename;
}
// Setter
public void setIndex(int index) {
this.index = index;
}
public void setFilename(String filename) {
this.filename = filename;
}
}

View File

@ -32,4 +32,16 @@ dependencies {
testImplementation libs.junit
androidTestImplementation libs.androidx.test.ext.junit
androidTestImplementation libs.androidx.test.espresso.core
// DI
implementation libs.com.google.dagger.hilt.android
annotationProcessor libs.com.google.dagger.hilt.compiler
// JGit
implementation 'org.eclipse.jgit:org.eclipse.jgit:6.8.0.202311291450-r'
implementation project(':model')
implementation project(':source')
implementation project(':utils')
implementation project(':data')
}

View File

@ -0,0 +1,24 @@
package one.nem.lacerta.processor;
import android.graphics.Bitmap;
public interface DocumentProcessor {
// ページ操作
void addNewPageToLast(Bitmap bitmap);
void addNewPagesToLast(Bitmap[] bitmaps);
void addNewPageAfterIndex(Bitmap bitmap, int index);
void addNewPageBeforeIndex(Bitmap bitmap, int index);
void removePageAtIndex(int index);
// 更新
void updatePageAtIndex(Bitmap bitmap, int index);
// ページ取得
Bitmap getPageAtIndex(int index);
int getPageCount();
void close();
void init();
}

View File

@ -0,0 +1,10 @@
package one.nem.lacerta.processor.factory;
import dagger.assisted.AssistedFactory;
import one.nem.lacerta.model.document.DocumentDetail;
import one.nem.lacerta.processor.impl.DocumentProcessorImpl;
@AssistedFactory
public interface DocumentProcessorFactory {
DocumentProcessorImpl create(DocumentDetail documentDetail);
}

View File

@ -0,0 +1,177 @@
package one.nem.lacerta.processor.impl;
import android.graphics.Bitmap;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.UUID;
import dagger.assisted.Assisted;
import dagger.assisted.AssistedInject;
import one.nem.lacerta.model.document.internal.XmlMetaPageModel;
import one.nem.lacerta.processor.DocumentProcessor;
import one.nem.lacerta.model.document.DocumentDetail;
import one.nem.lacerta.model.document.internal.XmlMetaModel;
import one.nem.lacerta.source.file.FileManager;
import one.nem.lacerta.source.file.factory.FileManagerFactory;
import one.nem.lacerta.utils.LacertaLogger;
import one.nem.lacerta.utils.XmlMetaParser;
public class DocumentProcessorImpl implements DocumentProcessor{
// Magic Numbers
private static final String DEFAULT_SAVE_DIR = "raw";
// Variables
private final DocumentDetail documentDetail;
private XmlMetaModel xmlMetaModel;
private Path documentRootPath;
private FileManager fileManager;
// Injection
private final FileManagerFactory fileManagerFactory;
private final LacertaLogger logger;
private final XmlMetaParser xmlMetaParser;
@AssistedInject
public DocumentProcessorImpl(FileManagerFactory fileManagerFactory, LacertaLogger logger, XmlMetaParser xmlMetaParser, @Assisted DocumentDetail documentDetail) {
this.fileManagerFactory = fileManagerFactory;
this.logger = logger;
this.xmlMetaParser = xmlMetaParser;
if (documentDetail == null) {
throw new IllegalArgumentException("documentDetail must not be null");
}
this.documentDetail = documentDetail;
}
@Override
public void init() {
logger.debug("init", "called");
// Init Variables
this.documentRootPath = this.documentDetail.getPath().getFullPath();
logger.debug("init", "documentRootPath: " + this.documentRootPath);
this.fileManager = fileManagerFactory.create(this.documentRootPath); //Initialize FileManager
logger.debug("init", "fileManager created");
this.fileManager.autoCreateDir(this.documentRootPath);
// rawディレクトリInit
this.fileManager.autoCreateDir(DEFAULT_SAVE_DIR);
// xmlファイルの読み込み
if (fileManager.isExist("meta.xml")) {
logger.debug("init", "meta.xml found");
try {
xmlMetaModel = xmlMetaParser.deserialize(this.fileManager.loadDocument("meta.xml"));
} catch (Exception e) {
logger.debug("init", "meta.xml parse failed");
logger.trace("init", e.getMessage());
}
} else {
logger.debug("init", "meta.xml not found");
xmlMetaModel = new XmlMetaModel();
xmlMetaModel.setTitle(this.documentDetail.getMeta().getTitle());
xmlMetaModel.setAuthor(this.documentDetail.getAuthor());
xmlMetaModel.setDescription(""); // FIXME-rca:
xmlMetaModel.setDefaultBranch(this.documentDetail.getDefaultBranch());
xmlMetaModel.setPages(new ArrayList<>());
try {
this.fileManager.saveDocument(xmlMetaParser.serialize(xmlMetaModel), "meta.xml");
logger.debug("init", "meta.xml saved");
} catch (Exception e) {
logger.error("init", "meta.xml save failed");
logger.trace("init", e.getMessage());
}
}
logger.info("init", "finished");
}
@Override
public void addNewPageToLast(Bitmap bitmap) {
logger.debug("addNewPageToLast", "called");
String filename = UUID.randomUUID().toString() + ".png"; // TODO-rca: 拡張子を動的にする
// FileManager
if (this.fileManager.getCurrentDir().equals(this.documentRootPath.resolve(DEFAULT_SAVE_DIR))) { // TODO-rca: 効率化
logger.debug("addNewPageToLast", "currentDir is documentRootPath");
} else {
logger.debug("addNewPageToLast", "currentDir is not documentRootPath");
this.fileManager.backRootDir();
this.fileManager.autoCreateDir(DEFAULT_SAVE_DIR);
this.fileManager.changeDir(DEFAULT_SAVE_DIR);
}
logger.debug("addNewPageToLast", "DirInit finished");
// Save file
this.fileManager.saveBitmapAtCurrent(bitmap, filename);
// Update meta
XmlMetaPageModel page = new XmlMetaPageModel();
page.setIndex(xmlMetaModel.getPages().size() + 1);
page.setFilename(filename);
xmlMetaModel.addPage(page);
}
@Override
public void addNewPagesToLast(Bitmap[] bitmaps) {
logger.debug("addNewPagesToLast", "called");
for (Bitmap bitmap : bitmaps) {
addNewPageToLast(bitmap);
} // TODO-rca: 効率悪いので改善する
}
@Override
public void addNewPageAfterIndex(Bitmap bitmap, int index) {
}
@Override
public void addNewPageBeforeIndex(Bitmap bitmap, int index) {
}
@Override
public void removePageAtIndex(int index) {
}
@Override
public void updatePageAtIndex(Bitmap bitmap, int index) {
}
@Override
public Bitmap getPageAtIndex(int index) {
return null;
}
@Override
public int getPageCount() {
return 0;
}
@Override
public void close() {
logger.debug("close", "called");
// TODO-rca: ここでxmlファイルを保存する
this.fileManager.backRootDir();
try {
this.fileManager.saveDocument(xmlMetaParser.serialize(xmlMetaModel), "meta.xml");
logger.debug("close", "meta.xml saved");
} catch (Exception e) {
logger.error("close", "meta.xml save failed");
logger.trace("close", e.getMessage());
}
logger.info("close", "finished");
}
}

View File

@ -0,0 +1,16 @@
package one.nem.lacerta.processor.module;
import dagger.assisted.Assisted;
import dagger.assisted.AssistedInject;
import one.nem.lacerta.model.document.DocumentDetail;
public class DocumentProcessorModule {
private final DocumentDetail documentDetail;
@AssistedInject
public DocumentProcessorModule(@Assisted DocumentDetail documentDetail) {
this.documentDetail = documentDetail;
}
}

View File

@ -0,0 +1,4 @@
<resources>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>

View File

@ -0,0 +1,59 @@
package one.nem.lacerta.source.file;
import android.graphics.Bitmap;
import org.w3c.dom.Document;
import java.io.File;
import java.nio.file.Path;
import java.util.List;
/** @noinspection unused*/
public interface FileManager {
Path getRootDir();
Path getCurrentDir();
void changeDir(String dirName); //cd
void changeDir(Path path); //cd
void backDir(); //cd ..
void backRootDir(); //cd /
List<Path> getList();
void createDir(String dirName);
void createDir(Path path);
void removeDir(String dirName);
void removeDir(Path path);
File createFile(String fileName);
void removeFile(String fileName);
File getFile(String fileName);
File getFile(Path path);
String loadText(String fileName);
String loadText(Path path);
void saveText(String text, String fileName);
void saveText(String text, Path path);
void saveDocument(Document document, String fileName);
void saveDocument(Document document, Path path);
Document loadDocument(String fileName);
Document loadDocument(Path path);
boolean isExist(Path path);
boolean isExist(String fileName);
void autoCreateDir(Path path);
void autoCreateDir(String dirName);
void autoCreateToCurrentDir();
void saveBitmapAtCurrent(Bitmap bitmap, String fileName);
Bitmap loadBitmap(Path path);
void removeBitmap(Path path);
}

View File

@ -0,0 +1,11 @@
package one.nem.lacerta.source.file.factory;
import java.nio.file.Path;
import dagger.assisted.AssistedFactory;
import one.nem.lacerta.source.file.impl.FileManagerImpl;
@AssistedFactory
public interface FileManagerFactory {
FileManagerImpl create(Path rootDir);
}

View File

@ -0,0 +1,347 @@
package one.nem.lacerta.source.file.impl;
import android.graphics.Bitmap;
import org.w3c.dom.Document;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import dagger.assisted.Assisted;
import dagger.assisted.AssistedInject;
import one.nem.lacerta.source.file.FileManager;
import one.nem.lacerta.utils.LacertaLogger;
public class FileManagerImpl implements FileManager {
// RootDir
private Path rootDir;
// CurrentDir
private Path currentDir;
// Internal Methods
private Path convertPath(String path) {
Path convertedPath = currentDir.resolve(path);
if (convertedPath.startsWith(rootDir)) { // 異常なパスの場合はnullを返す // TODO-rca: エラーハンドリング
return convertedPath;
} else {
return null;
}
}
// Injection
private LacertaLogger logger;
@AssistedInject
public FileManagerImpl(LacertaLogger logger, @Assisted Path rootDir) {
this.logger = logger;
this.rootDir = rootDir;
this.currentDir = rootDir;
}
@Override
public Path getRootDir() {
return rootDir;
}
@Override
public Path getCurrentDir() {
return currentDir;
}
@Override
public void changeDir(String dirName) {
this.currentDir = rootDir.resolve(dirName);
}
@Override
public void changeDir(Path path) {
if (path.startsWith(rootDir)) {
this.currentDir = path;
}
else {
logger.debug("changeDir", "invalid path: " + path);
// TODO-rca: 例外を投げる
}
}
@Override
public void backDir() {
this.currentDir = currentDir.getParent();
}
@Override
public void backRootDir() {
this.currentDir = rootDir;
}
@Override
public List<Path> getList() {
List<Path> list = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(currentDir)) {
for (Path entry : stream) { // TODO-rca: エラーハンドリング, 効率化
list.add(entry);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
@Override
public void createDir(String dirName) {
//ディレクトリ作成
logger.debug("createDir", "called");
Path path = currentDir.resolve(dirName);
logger.debug("createDir", "path: " + path);
try {
Files.createDirectory(path);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void createDir(Path path) {
logger.debug("createDir", "called");
try {
Files.createDirectory(path);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void removeDir(String dirName) {
logger.debug("removeDir", "called");
currentDir.resolve(dirName).toFile().delete(); // TODO-rca: エラーハンドリング
}
@Override
public void removeDir(Path path) {
logger.debug("removeDir", "called");
path.toFile().delete(); // TODO-rca: エラーハンドリング
}
@Override
public File createFile(String fileName) {
logger.debug("createFile", "called");
return currentDir.resolve(fileName).toFile();
}
@Override
public void removeFile(String fileName) {
logger.debug("removeFile", "called");
currentDir.resolve(fileName).toFile().delete(); // TODO-rca: エラーハンドリング
}
@Override
public File getFile(String fileName) {
logger.debug("getFile", "called");
return currentDir.resolve(fileName).toFile();
}
@Override
public File getFile(Path path) {
logger.debug("getFile", "called");
return path.toFile();
}
@Override
public String loadText(String fileName) { // TODO-rca: 統合
try(FileInputStream fileInputStream = new FileInputStream(currentDir.resolve(fileName).toFile())) {
byte[] bytes = new byte[fileInputStream.available()];
fileInputStream.read(bytes); // TODO-rca: エラーハンドリング
return new String(bytes);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
public String loadText(Path path) {
try(FileInputStream fileInputStream = new FileInputStream(path.toFile())) {
byte[] bytes = new byte[fileInputStream.available()];
fileInputStream.read(bytes); // TODO-rca: エラーハンドリング
return new String(bytes);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
public void saveText(String text, String fileName) { // TODO-rca: リファクタリング // TODO-rca: 統合
if (isExist(fileName)) {
logger.debug("saveText", "file already exists");
// Overwrite
try {
Files.write(currentDir.resolve(fileName), text.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
else {
try {
Files.createFile(currentDir.resolve(fileName));
Files.write(currentDir.resolve(fileName), text.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void saveText(String text, Path path) {
if (isExist(path)) {
logger.debug("saveText", "file already exists");
// Overwrite
try {
Files.write(path, text.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
else {
try {
Files.createFile(path);
Files.write(path, text.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void saveDocument(Document document, String fileName) {
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
File file = createFile(fileName);
StreamResult result = new StreamResult(file);
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void saveDocument(Document document, Path path) {
// TODO-rca 実装する
}
@Override
public Document loadDocument(String fileName) {
try {
File file = getFile(fileName);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(file);
return document;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public Document loadDocument(Path path) {
// TODO-rca 実装する
return null;
}
@Override
public boolean isExist(Path path) {
logger.debug("isExist", "called");
return Files.exists(path);
}
@Override
public boolean isExist(String fileName) {
logger.debug("isExist", "called");
return Files.exists(currentDir.resolve(fileName));
}
@Override
public void autoCreateDir(Path path) {
logger.debug("autoCreateDir", "called");
if (!Files.exists(path)) {
try {
Files.createDirectories(path);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void autoCreateDir(String dirName) {
logger.debug("autoCreateDir", "called");
if (!Files.exists(currentDir.resolve(dirName))) {
try {
Files.createDirectories(currentDir.resolve(dirName));
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void autoCreateToCurrentDir() {
logger.debug("autoGenerateToCurrentDir", "called");
if (isExist(currentDir)) {
logger.debug("autoGenerateToCurrentDir", "currentDir already exists");
return;
}
else {
try {
Files.createDirectories(currentDir);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void saveBitmapAtCurrent(Bitmap bitmap, String fileName) { // TODO-rca: ファイル形式を変更できるようにする
logger.debug("saveBitmapAtCurrent", "called");
try {
File file = currentDir.resolve(fileName).toFile();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, Files.newOutputStream(file.toPath()));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public Bitmap loadBitmap(Path path) {
return null;
}
@Override
public void removeBitmap(Path path) {
}
}

View File

@ -0,0 +1,20 @@
package one.nem.lacerta.source.file.module;
import java.nio.file.Path;
import dagger.assisted.Assisted;
import dagger.assisted.AssistedInject;
public class FileManagerModule {
private final Path rootDir;
@AssistedInject
public FileManagerModule(@Assisted Path rootDir) {
this.rootDir = rootDir;
}
public Path getRootDir() {
return rootDir;
}
}

View File

@ -37,5 +37,6 @@ dependencies {
implementation libs.com.google.dagger.hilt.android
annotationProcessor libs.com.google.dagger.hilt.compiler
//
// model
implementation project(':model')
}

View File

@ -0,0 +1,13 @@
package one.nem.lacerta.utils;
import org.w3c.dom.Document;
import one.nem.lacerta.model.document.internal.XmlMetaModel;
public interface XmlMetaParser {
XmlMetaModel deserialize(Document document);
Document serialize(XmlMetaModel meta);
}

View File

@ -0,0 +1,98 @@
package one.nem.lacerta.utils.impl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.util.ArrayList;
import javax.inject.Inject;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import one.nem.lacerta.model.document.internal.XmlMetaModel;
import one.nem.lacerta.model.document.internal.XmlMetaPageModel;
import one.nem.lacerta.utils.XmlMetaParser;
import one.nem.lacerta.utils.LacertaLogger;
public class XmlMetaParserImpl implements XmlMetaParser{
@Inject
LacertaLogger logger;
@Inject
public XmlMetaParserImpl() {
}
@Override
public XmlMetaModel deserialize(Document document) {
logger.debug("deserialize", "called");
try {
Element rootElement = document.getDocumentElement();
XmlMetaModel meta = new XmlMetaModel();
meta.setTitle(rootElement.getElementsByTagName("title").item(0).getTextContent());
meta.setAuthor(rootElement.getElementsByTagName("author").item(0).getTextContent());
meta.setDescription(rootElement.getElementsByTagName("description").item(0).getTextContent());
meta.setDefaultBranch(rootElement.getElementsByTagName("defaultBranch").item(0).getTextContent());
ArrayList<XmlMetaPageModel> pages = new ArrayList<>();
for(int i = 0; i < rootElement.getElementsByTagName("pages").getLength(); i++) {
Element pageElement = (Element) rootElement.getElementsByTagName("page").item(i);
XmlMetaPageModel page = new XmlMetaPageModel();
page.setIndex(Integer.parseInt(pageElement.getElementsByTagName("index").item(0).getTextContent()));
page.setFilename(pageElement.getElementsByTagName("filename").item(0).getTextContent());
pages.add(page);
}
meta.setPages(pages);
return meta;
} catch (Exception e) {
logger.error("deserialize", "something wrong");
logger.trace("deserialize", e.getMessage());
}
return null;
}
@Override
public Document serialize(XmlMetaModel meta) {
logger.debug("serialize", "called");
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element rootElement = document.createElement("meta");
appendElement(document, rootElement, "title", meta.getTitle());
appendElement(document, rootElement, "author", meta.getAuthor());
appendElement(document, rootElement, "description", meta.getDescription());
appendElement(document, rootElement, "defaultBranch", meta.getDefaultBranch());
Element pagesElement = document.createElement("pages");
for(XmlMetaPageModel page : meta.getPages()) {
Element pageElement = document.createElement("page");
appendElement(document, pageElement, "index", String.valueOf(page.getIndex()));
appendElement(document, pageElement, "filename", page.getFilename());
pagesElement.appendChild(pageElement);
}
rootElement.appendChild(pagesElement);
document.appendChild(rootElement);
return document;
} catch (Exception e) {
logger.error("serialize", "something wrong");
logger.trace("serialize", e.getMessage());
}
return null;
}
// Internal Methods
private void appendElement(Document document, Element rootElement, String name, String textContent) {
Element element = document.createElement(name);
element.setTextContent(textContent);
rootElement.appendChild(element);
}
}

View File

@ -0,0 +1,17 @@
package one.nem.lacerta.utils.module;
import dagger.Binds;
import dagger.Module;
import dagger.hilt.InstallIn;
import dagger.hilt.components.SingletonComponent;
import one.nem.lacerta.utils.XmlMetaParser;
import one.nem.lacerta.utils.impl.XmlMetaParserImpl;
@Module
@InstallIn(SingletonComponent.class)
abstract public class XmlMetaParserModule {
@Binds
public abstract XmlMetaParser bindXmlMetaParser(XmlMetaParserImpl impl);
}