Search in sources :

Example 1 with FileData

use of com.evernote.client.conn.mobile.FileData 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 2 with FileData

use of com.evernote.client.conn.mobile.FileData 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)

Aggregations

EvernoteNoteStoreClient (com.evernote.client.android.asyncclient.EvernoteNoteStoreClient)2 FileData (com.evernote.client.conn.mobile.FileData)2 EDAMNotFoundException (com.evernote.edam.error.EDAMNotFoundException)2 EDAMSystemException (com.evernote.edam.error.EDAMSystemException)2 EDAMUserException (com.evernote.edam.error.EDAMUserException)2 Note (com.evernote.edam.type.Note)2 Notebook (com.evernote.edam.type.Notebook)2 Resource (com.evernote.edam.type.Resource)2 ResourceAttributes (com.evernote.edam.type.ResourceAttributes)2 TException (com.evernote.thrift.TException)2 BufferedInputStream (java.io.BufferedInputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Activity (android.app.Activity)1 Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 TextUtils (android.text.TextUtils)1 EvernoteSession (com.evernote.client.android.EvernoteSession)1