use of com.evernote.edam.type.Resource in project PhotoNoter by yydcdut.
the class RxUser method updateNote2Evernote.
public Observable<Boolean> updateNote2Evernote(String bigPhotoPathWithoutFile, String photoName, String noteTitle, String noteContent) {
return Observable.create(new Observable.OnSubscribe<List<Notebook>>() {
@Override
public void call(Subscriber<? super List<Notebook>> subscriber) {
if (mEvernoteSession == null) {
mEvernoteSession = new EvernoteSession.Builder(mContext).setLocale(Locale.SIMPLIFIED_CHINESE).setEvernoteService(EVERNOTE_SERVICE).setSupportAppLinkedNotebooks(SUPPORT_APP_LINKED_NOTEBOOKS).setForceAuthenticationInThirdPartyApp(true).build(BuildConfig.EVERNOTE_CONSUMER_KEY, BuildConfig.EVERNOTE_CONSUMER_SECRET).asSingleton();
}
EvernoteNoteStoreClient noteStoreClient = mEvernoteSession.getEvernoteClientFactory().getNoteStoreClient();
try {
List<Notebook> notebookList = noteStoreClient.listNotebooks();
subscriber.onNext(notebookList);
} catch (EDAMUserException e) {
YLog.e(e);
subscriber.onError(e);
} catch (EDAMSystemException e) {
YLog.e(e);
subscriber.onError(e);
} catch (TException e) {
YLog.e(e);
subscriber.onError(e);
}
subscriber.onCompleted();
}
}).subscribeOn(Schedulers.io()).flatMap(notebooks -> Observable.from(notebooks)).filter(notebook -> notebook.getName().equals(mContext.getResources().getString(R.string.app_name))).lift(new Observable.Operator<String, Notebook>() {
@Override
public Subscriber<? super Notebook> call(Subscriber<? super String> subscriber) {
return new Subscriber<Notebook>() {
private int mInTimes = 0;
@Override
public void onCompleted() {
if (mInTimes == 0) {
Notebook notebook = new Notebook();
notebook.setName(mContext.getResources().getString(R.string.app_name));
EvernoteNoteStoreClient noteStoreClient = mEvernoteSession.getEvernoteClientFactory().getNoteStoreClient();
try {
Notebook appNoteBook = noteStoreClient.createNotebook(notebook);
subscriber.onNext(appNoteBook.getGuid());
} catch (EDAMUserException e) {
YLog.e(e);
subscriber.onError(e);
} catch (EDAMSystemException e) {
YLog.e(e);
subscriber.onError(e);
} catch (TException e) {
YLog.e(e);
subscriber.onError(e);
}
}
subscriber.onCompleted();
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Notebook notebook) {
mInTimes++;
subscriber.onNext(notebook.getGuid());
}
};
}
}).map(s -> {
Note note = new Note();
note.setNotebookGuid(s);
return note;
}).lift(new Observable.Operator<Boolean, Note>() {
@Override
public Subscriber<? super Note> call(Subscriber<? super Boolean> subscriber) {
return new Subscriber<Note>() {
@Override
public void onCompleted() {
subscriber.onCompleted();
}
@Override
public void onError(Throwable e) {
subscriber.onError(e);
}
@Override
public void onNext(Note note) {
note.setTitle(noteTitle);
InputStream in = null;
// Hash the data in the image file. The hash is used to reference the file in the ENML note content.
try {
in = new BufferedInputStream(new FileInputStream(bigPhotoPathWithoutFile));
FileData data = new FileData(EvernoteUtil.hash(in), new File(bigPhotoPathWithoutFile));
ResourceAttributes attributes = new ResourceAttributes();
attributes.setFileName(photoName);
// Create a new Resource
Resource resource = new Resource();
resource.setData(data);
resource.setMime("image/jpeg");
resource.setAttributes(attributes);
note.addToResources(resource);
// Set the note's ENML content
String content = EvernoteUtil.NOTE_PREFIX + noteContent + EvernoteUtil.createEnMediaTag(resource) + EvernoteUtil.NOTE_SUFFIX;
note.setContent(content);
EvernoteNoteStoreClient noteStoreClient = mEvernoteSession.getEvernoteClientFactory().getNoteStoreClient();
noteStoreClient.createNote(note);
} catch (FileNotFoundException e) {
YLog.e(e);
subscriber.onError(e);
} catch (IOException e) {
YLog.e(e);
subscriber.onError(e);
} catch (EDAMNotFoundException e) {
YLog.e(e);
subscriber.onError(e);
} catch (TException e) {
YLog.e(e);
subscriber.onError(e);
} catch (EDAMUserException e) {
YLog.e(e);
subscriber.onError(e);
} catch (EDAMSystemException e) {
YLog.e(e);
subscriber.onError(e);
} finally {
FilePathUtils.closeStream(in);
}
subscriber.onNext(true);
}
};
}
});
}
use of com.evernote.edam.type.Resource in project PhotoNoter by yydcdut.
the class EditTextPresenterImpl method doUpdate2Evernote.
private boolean doUpdate2Evernote(String bigPhotoPathWithoutFile, String photoName) {
mRxUser.initEvernoteSession();
boolean isSuccess = true;
try {
EvernoteNoteStoreClient noteStoreClient = EvernoteSession.getInstance().getEvernoteClientFactory().getNoteStoreClient();
List<Notebook> notebookList = noteStoreClient.listNotebooks();
boolean hasNoteBook = false;
Notebook appNoteBook = null;
for (Notebook notebook : notebookList) {
if (notebook.getName().equals(mContext.getResources().getString(R.string.app_name))) {
hasNoteBook = true;
appNoteBook = notebook;
}
}
if (!hasNoteBook) {
Notebook notebook = new Notebook();
notebook.setName(mContext.getResources().getString(R.string.app_name));
appNoteBook = noteStoreClient.createNotebook(notebook);
}
Note note = new Note();
note.setTitle(mEditTextView.getNoteTitle());
if (appNoteBook != null) {
note.setNotebookGuid(appNoteBook.getGuid());
}
InputStream in = null;
try {
// Hash the data in the image file. The hash is used to reference the file in the ENML note content.
in = new BufferedInputStream(new FileInputStream(bigPhotoPathWithoutFile));
FileData data = null;
data = new FileData(EvernoteUtil.hash(in), new File(bigPhotoPathWithoutFile));
ResourceAttributes attributes = new ResourceAttributes();
attributes.setFileName(photoName);
// Create a new Resource
Resource resource = new Resource();
resource.setData(data);
resource.setMime("image/jpeg");
resource.setAttributes(attributes);
note.addToResources(resource);
// Set the note's ENML content
String content = EvernoteUtil.NOTE_PREFIX + mEditTextView.getNoteContent() + EvernoteUtil.createEnMediaTag(resource) + EvernoteUtil.NOTE_SUFFIX;
note.setContent(content);
try {
noteStoreClient.createNote(note);
} catch (EDAMNotFoundException e) {
YLog.e(e);
}
} catch (IOException e) {
YLog.e(e);
isSuccess = false;
} finally {
FilePathUtils.closeStream(in);
}
} catch (EDAMUserException e) {
YLog.e(e);
isSuccess = false;
} catch (EDAMSystemException e) {
YLog.e(e);
isSuccess = false;
} catch (TException e) {
YLog.e(e);
isSuccess = false;
}
return isSuccess;
}
Aggregations