Back to project page vocabulary-list.
The source code is released under:
Apache License
If you think the Android project vocabulary-list listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.rulych.vocabularylist.model.persistence.impl; //ww w . jav a2 s . com import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.drive.Contents; import com.google.android.gms.drive.DriveFile; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.rulych.vocabularylist.model.Card; import com.rulych.vocabularylist.model.persistence.impl.exception.CouldNotReadFileException; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; import rx.Observable; import rx.functions.Func0; import rx.schedulers.Schedulers; import rx.util.async.Async; import timber.log.Timber; import static com.google.android.gms.drive.DriveApi.ContentsResult; import static com.google.android.gms.drive.DriveFile.MODE_READ_ONLY; public class CardListReaderImpl implements CardListReader { private Gson mGson; private GoogleApiClient mApiClient; private DriveFile mDriveFile; public CardListReaderImpl(Gson gson, GoogleApiClient apiClient, DriveFile driveFile) { mGson = gson; mApiClient = apiClient; mDriveFile = driveFile; } public Observable<List<Card>> read() { return Async.start(new CardObservableOnSubscribe(mDriveFile), Schedulers.io()); } private class CardObservableOnSubscribe implements Func0<List<Card>> { private final DriveFile mDriveFile; private Contents mContents; public CardObservableOnSubscribe(DriveFile driveFile) { mDriveFile = driveFile; } @Override public List<Card> call() { getFileContents(); return readFile(); } private void getFileContents() { Timber.d("Got file with ID: %s", mDriveFile.getDriveId().encodeToString()); ContentsResult result = mDriveFile.openContents(mApiClient, MODE_READ_ONLY, null) .await(); if (!result.getStatus().isSuccess()) { throw new CouldNotReadFileException("Error while trying to open file: (%d) %s", result.getStatus().getStatusCode(), result.getStatus().getStatusMessage()); } mContents = result.getContents(); } private List<Card> readFile() { InputStreamReader inputStreamReader = null; try { InputStream inputStream = mContents.getInputStream(); inputStreamReader = new InputStreamReader(inputStream); Type type = new TypeToken<List<Card>>() {}.getType(); List<Card> cards = mGson.fromJson(inputStreamReader, type); if (cards == null) { cards = new ArrayList<Card>(); } return cards; } finally { try { inputStreamReader.close(); } catch (Exception e) { // Nothing! } } } } }