Search in sources :

Example 1 with EDAMUserException

use of com.evernote.edam.error.EDAMUserException in project Notes by lguipeng.

the class EvernoteSearchHelper method findAllNotes.

protected List<NotesMetadataList> findAllNotes(Search search, EvernoteNoteStoreClient client, NoteFilter filter) throws Exception {
    List<NotesMetadataList> result = new ArrayList<>();
    final int maxNotes = search.getMaxNotes();
    int offset = search.getOffset();
    int remaining = maxNotes - offset;
    while (remaining > 0) {
        try {
            NotesMetadataList notesMetadata = client.findNotesMetadata(filter, offset, maxNotes, search.getResultSpec());
            remaining = notesMetadata.getTotalNotes() - (notesMetadata.getStartIndex() + notesMetadata.getNotesSize());
            result.add(notesMetadata);
        } catch (EDAMUserException | EDAMSystemException | TException | EDAMNotFoundException e) {
            maybeRethrow(search, e);
            remaining -= search.getPageSize();
        }
        offset += search.getPageSize();
    }
    return result;
}
Also used : TException(com.evernote.thrift.TException) EDAMNotFoundException(com.evernote.edam.error.EDAMNotFoundException) EDAMSystemException(com.evernote.edam.error.EDAMSystemException) ArrayList(java.util.ArrayList) NotesMetadataList(com.evernote.edam.notestore.NotesMetadataList) EDAMUserException(com.evernote.edam.error.EDAMUserException)

Example 2 with EDAMUserException

use of com.evernote.edam.error.EDAMUserException in project Notes by lguipeng.

the class EverNoteUtils method tryCreateNoteBook.

private Notebook tryCreateNoteBook(String bookName) throws Exception {
    Notebook notebook = new Notebook();
    notebook.setName(bookName);
    try {
        Notebook result = mEvernoteSession.getEvernoteClientFactory().getNoteStoreClient().createNotebook(notebook);
        mPreferenceUtils.saveParam(PreferenceUtils.EVERNOTE_NOTEBOOK_GUID_KEY, result.getGuid());
        return result;
    } catch (EDAMUserException e) {
        if (e.getErrorCode() == EDAMErrorCode.DATA_CONFLICT) {
            List<Notebook> books = listNotebooks();
            for (Notebook book : books) {
                if (TextUtils.equals(book.getName(), bookName)) {
                    mPreferenceUtils.saveParam(PreferenceUtils.EVERNOTE_NOTEBOOK_GUID_KEY, book.getGuid());
                    return book;
                }
            }
        }
        handleException(e);
        return null;
    }
}
Also used : Notebook(com.evernote.edam.type.Notebook) ArrayList(java.util.ArrayList) NotesMetadataList(com.evernote.edam.notestore.NotesMetadataList) List(java.util.List) EDAMUserException(com.evernote.edam.error.EDAMUserException)

Example 3 with EDAMUserException

use of com.evernote.edam.error.EDAMUserException 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);
                }
            };
        }
    });
}
Also used : TException(com.evernote.thrift.TException) EDAMSystemException(com.evernote.edam.error.EDAMSystemException) Context(android.content.Context) BufferedInputStream(java.io.BufferedInputStream) Singleton(javax.inject.Singleton) Observable(rx.Observable) Inject(javax.inject.Inject) EvernoteUtil(com.evernote.client.android.EvernoteUtil) ResourceAttributes(com.evernote.edam.type.ResourceAttributes) JSONException(org.json.JSONException) Notebook(com.evernote.edam.type.Notebook) JSONObject(org.json.JSONObject) TException(com.evernote.thrift.TException) User(com.evernote.edam.type.User) Locale(java.util.Locale) ContextLife(com.yydcdut.note.injector.ContextLife) FilePathUtils(com.yydcdut.note.utils.FilePathUtils) Schedulers(rx.schedulers.Schedulers) QQToken(com.tencent.connect.auth.QQToken) R(com.yydcdut.note.R) EvernoteUser(com.yydcdut.note.entity.user.EvernoteUser) WeakReference(java.lang.ref.WeakReference) Tencent(com.tencent.tauth.Tencent) Subscriber(rx.Subscriber) Resource(com.evernote.edam.type.Resource) IUser(com.yydcdut.note.entity.user.IUser) EDAMUserException(com.evernote.edam.error.EDAMUserException) TextUtils(android.text.TextUtils) EDAMNotFoundException(com.evernote.edam.error.EDAMNotFoundException) QQUser(com.yydcdut.note.entity.user.QQUser) IOException(java.io.IOException) UserInfo(com.tencent.connect.UserInfo) FileInputStream(java.io.FileInputStream) Note(com.evernote.edam.type.Note) FileData(com.evernote.client.conn.mobile.FileData) UiError(com.tencent.tauth.UiError) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) EvernoteNoteStoreClient(com.evernote.client.android.asyncclient.EvernoteNoteStoreClient) RxException(com.yydcdut.note.model.rx.exception.RxException) List(java.util.List) IUiListener(com.tencent.tauth.IUiListener) SharedPreferences(android.content.SharedPreferences) EvernoteSession(com.evernote.client.android.EvernoteSession) BuildConfig(com.yydcdut.note.BuildConfig) Activity(android.app.Activity) YLog(com.yydcdut.note.utils.YLog) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) EDAMUserException(com.evernote.edam.error.EDAMUserException) EDAMSystemException(com.evernote.edam.error.EDAMSystemException) Subscriber(rx.Subscriber) BufferedInputStream(java.io.BufferedInputStream) List(java.util.List) FileData(com.evernote.client.conn.mobile.FileData) Notebook(com.evernote.edam.type.Notebook) EvernoteNoteStoreClient(com.evernote.client.android.asyncclient.EvernoteNoteStoreClient) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Resource(com.evernote.edam.type.Resource) IOException(java.io.IOException) Observable(rx.Observable) FileInputStream(java.io.FileInputStream) ResourceAttributes(com.evernote.edam.type.ResourceAttributes) EDAMNotFoundException(com.evernote.edam.error.EDAMNotFoundException) Note(com.evernote.edam.type.Note) File(java.io.File)

Example 4 with EDAMUserException

use of com.evernote.edam.error.EDAMUserException 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;
}
Also used : TException(com.evernote.thrift.TException) Notebook(com.evernote.edam.type.Notebook) EvernoteNoteStoreClient(com.evernote.client.android.asyncclient.EvernoteNoteStoreClient) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Resource(com.evernote.edam.type.Resource) IOException(java.io.IOException) EDAMUserException(com.evernote.edam.error.EDAMUserException) FileInputStream(java.io.FileInputStream) ResourceAttributes(com.evernote.edam.type.ResourceAttributes) EDAMNotFoundException(com.evernote.edam.error.EDAMNotFoundException) EDAMSystemException(com.evernote.edam.error.EDAMSystemException) BufferedInputStream(java.io.BufferedInputStream) RxPhotoNote(com.yydcdut.note.model.rx.RxPhotoNote) Note(com.evernote.edam.type.Note) FileData(com.evernote.client.conn.mobile.FileData) File(java.io.File)

Example 5 with EDAMUserException

use of com.evernote.edam.error.EDAMUserException in project PhotoNoter by yydcdut.

the class RxUser method saveEvernote.

public Observable<IUser> saveEvernote() {
    return Observable.create(new Observable.OnSubscribe<IUser>() {

        @Override
        public void call(Subscriber<? super IUser> subscriber) {
            if (mEvernoteSession.isLoggedIn()) {
                try {
                    User user = mEvernoteSession.getEvernoteClientFactory().getUserStoreClient().getUser();
                    mEvernoteUser = new EvernoteUser(user.getUsername());
                    SharedPreferences.Editor editor = mSharedPreferences.edit();
                    editor.putString(EVERNOTE_NAME, mEvernoteUser.getName());
                    editor.commit();
                    subscriber.onNext(mEvernoteUser);
                } 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);
                }
            } else {
                subscriber.onError(new RxException("没有登录"));
            }
            subscriber.onCompleted();
        }
    }).subscribeOn(Schedulers.io());
}
Also used : TException(com.evernote.thrift.TException) User(com.evernote.edam.type.User) EvernoteUser(com.yydcdut.note.entity.user.EvernoteUser) IUser(com.yydcdut.note.entity.user.IUser) QQUser(com.yydcdut.note.entity.user.QQUser) EDAMSystemException(com.evernote.edam.error.EDAMSystemException) Subscriber(rx.Subscriber) SharedPreferences(android.content.SharedPreferences) EvernoteUser(com.yydcdut.note.entity.user.EvernoteUser) IUser(com.yydcdut.note.entity.user.IUser) RxException(com.yydcdut.note.model.rx.exception.RxException) EDAMUserException(com.evernote.edam.error.EDAMUserException)

Aggregations

EDAMUserException (com.evernote.edam.error.EDAMUserException)5 EDAMSystemException (com.evernote.edam.error.EDAMSystemException)4 TException (com.evernote.thrift.TException)4 EDAMNotFoundException (com.evernote.edam.error.EDAMNotFoundException)3 Notebook (com.evernote.edam.type.Notebook)3 SharedPreferences (android.content.SharedPreferences)2 EvernoteNoteStoreClient (com.evernote.client.android.asyncclient.EvernoteNoteStoreClient)2 FileData (com.evernote.client.conn.mobile.FileData)2 NotesMetadataList (com.evernote.edam.notestore.NotesMetadataList)2 Note (com.evernote.edam.type.Note)2 Resource (com.evernote.edam.type.Resource)2 ResourceAttributes (com.evernote.edam.type.ResourceAttributes)2 User (com.evernote.edam.type.User)2 EvernoteUser (com.yydcdut.note.entity.user.EvernoteUser)2 IUser (com.yydcdut.note.entity.user.IUser)2 QQUser (com.yydcdut.note.entity.user.QQUser)2 RxException (com.yydcdut.note.model.rx.exception.RxException)2 BufferedInputStream (java.io.BufferedInputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2