Search in sources :

Example 6 with SingleOnSubscribe

use of io.reactivex.SingleOnSubscribe in project AntennaPod by AntennaPod.

the class NetworkUtils method getFeedMediaSizeObservable.

public static Single<Long> getFeedMediaSizeObservable(FeedMedia media) {
    return Single.create((SingleOnSubscribe<Long>) emitter -> {
        if (!NetworkUtils.isEpisodeHeadDownloadAllowed()) {
            emitter.onSuccess(0L);
            return;
        }
        long size = Integer.MIN_VALUE;
        if (media.isDownloaded()) {
            File mediaFile = new File(media.getLocalMediaUrl());
            if (mediaFile.exists()) {
                size = mediaFile.length();
            }
        } else if (!media.checkedOnSizeButUnknown()) {
            String url = media.getDownload_url();
            if (TextUtils.isEmpty(url)) {
                emitter.onSuccess(0L);
                return;
            }
            OkHttpClient client = AntennapodHttpClient.getHttpClient();
            Request.Builder httpReq = new Request.Builder().url(url).header("Accept-Encoding", "identity").head();
            try {
                Response response = client.newCall(httpReq.build()).execute();
                if (response.isSuccessful()) {
                    String contentLength = response.header("Content-Length");
                    try {
                        size = Integer.parseInt(contentLength);
                    } catch (NumberFormatException e) {
                        Log.e(TAG, Log.getStackTraceString(e));
                    }
                }
            } catch (IOException e) {
                emitter.onSuccess(0L);
                Log.e(TAG, Log.getStackTraceString(e));
                return;
            }
        }
        Log.d(TAG, "new size: " + size);
        if (size <= 0) {
            media.setCheckedOnSizeButUnknown();
        } else {
            media.setSize(size);
        }
        emitter.onSuccess(size);
        DBWriter.setFeedMedia(media);
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
}
Also used : Context(android.content.Context) Arrays(java.util.Arrays) WifiInfo(android.net.wifi.WifiInfo) NetworkCapabilities(android.net.NetworkCapabilities) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) UserPreferences(de.danoeh.antennapod.core.preferences.UserPreferences) Matcher(java.util.regex.Matcher) Response(okhttp3.Response) Schedulers(io.reactivex.schedulers.Schedulers) Build(android.os.Build) AntennapodHttpClient(de.danoeh.antennapod.core.service.download.AntennapodHttpClient) Log(android.util.Log) SingleOnSubscribe(io.reactivex.SingleOnSubscribe) ConnectivityManager(android.net.ConnectivityManager) FeedMedia(de.danoeh.antennapod.model.feed.FeedMedia) Request(okhttp3.Request) NetworkInfo(android.net.NetworkInfo) TextUtils(android.text.TextUtils) IOException(java.io.IOException) Network(android.net.Network) File(java.io.File) WifiManager(android.net.wifi.WifiManager) List(java.util.List) OkHttpClient(okhttp3.OkHttpClient) DBWriter(de.danoeh.antennapod.core.storage.DBWriter) Pattern(java.util.regex.Pattern) DownloadService(de.danoeh.antennapod.core.service.download.DownloadService) DBTasks(de.danoeh.antennapod.core.storage.DBTasks) Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) File(java.io.File)

Example 7 with SingleOnSubscribe

use of io.reactivex.SingleOnSubscribe in project kripton by xcesco.

the class BindKripton180BeanInsertSelectDataSource method execute.

public <T> Single<T> execute(final SingleTransaction<T> transaction) {
    SingleOnSubscribe<T> emitter = new SingleOnSubscribe<T>() {

        @Override
        public void subscribe(SingleEmitter<T> emitter) {
            boolean needToOpened = !BindKripton180BeanInsertSelectDataSource.this.isOpenInWriteMode();
            boolean success = false;
            @SuppressWarnings("resource") SQLiteDatabase connection = needToOpened ? openWritableDatabase() : database();
            DataSourceSingleThread currentDaoFactory = _daoFactorySingleThread.bindToThread();
            currentDaoFactory.onSessionOpened();
            try {
                connection.beginTransaction();
                if (transaction != null && TransactionResult.COMMIT == transaction.onExecute(currentDaoFactory, emitter)) {
                    connection.setTransactionSuccessful();
                    success = true;
                }
            // no onComplete;
            } catch (Throwable e) {
                Logger.error(e.getMessage());
                e.printStackTrace();
                emitter.onError(e);
                currentDaoFactory.onSessionClear();
            } finally {
                try {
                    connection.endTransaction();
                } catch (Throwable e) {
                }
                if (needToOpened) {
                    close();
                }
                if (success) {
                    currentDaoFactory.onSessionClosed();
                } else {
                    currentDaoFactory.onSessionClear();
                }
            }
            return;
        }
    };
    Single<T> result = Single.create(emitter);
    if (globalSubscribeOn != null)
        result.subscribeOn(globalSubscribeOn);
    if (globalObserveOn != null)
        result.observeOn(globalObserveOn);
    return result;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SingleOnSubscribe(io.reactivex.SingleOnSubscribe) SingleEmitter(io.reactivex.SingleEmitter)

Example 8 with SingleOnSubscribe

use of io.reactivex.SingleOnSubscribe in project kripton by xcesco.

the class BindXenoDataSource method execute.

public <T> Single<T> execute(final SingleTransaction<T> transaction) {
    SingleOnSubscribe<T> emitter = new SingleOnSubscribe<T>() {

        @Override
        public void subscribe(SingleEmitter<T> emitter) {
            boolean needToOpened = !BindXenoDataSource.this.isOpenInWriteMode();
            boolean success = false;
            @SuppressWarnings("resource") SQLiteDatabase connection = needToOpened ? openWritableDatabase() : database();
            DataSourceSingleThread currentDaoFactory = _daoFactorySingleThread.bindToThread();
            currentDaoFactory.onSessionOpened();
            try {
                connection.beginTransaction();
                if (transaction != null && TransactionResult.COMMIT == transaction.onExecute(currentDaoFactory, emitter)) {
                    connection.setTransactionSuccessful();
                    success = true;
                }
            // no onComplete;
            } catch (Throwable e) {
                Logger.error(e.getMessage());
                e.printStackTrace();
                emitter.onError(e);
                currentDaoFactory.onSessionClear();
            } finally {
                try {
                    connection.endTransaction();
                } catch (Throwable e) {
                }
                if (needToOpened) {
                    close();
                }
                if (success) {
                    currentDaoFactory.onSessionClosed();
                } else {
                    currentDaoFactory.onSessionClear();
                }
            }
            return;
        }
    };
    Single<T> result = Single.create(emitter);
    if (globalSubscribeOn != null)
        result.subscribeOn(globalSubscribeOn);
    if (globalObserveOn != null)
        result.observeOn(globalObserveOn);
    return result;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SingleOnSubscribe(io.reactivex.SingleOnSubscribe) SingleEmitter(io.reactivex.SingleEmitter)

Example 9 with SingleOnSubscribe

use of io.reactivex.SingleOnSubscribe in project AntennaPod by AntennaPod.

the class TranslatorsFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getListView().setDivider(null);
    getListView().setSelector(android.R.color.transparent);
    translatorsLoader = Single.create((SingleOnSubscribe<ArrayList<SimpleIconListAdapter.ListItem>>) emitter -> {
        ArrayList<SimpleIconListAdapter.ListItem> translators = new ArrayList<>();
        BufferedReader reader = new BufferedReader(new InputStreamReader(getContext().getAssets().open("translators.csv"), "UTF-8"));
        String line;
        while ((line = reader.readLine()) != null) {
            String[] info = line.split(";");
            translators.add(new SimpleIconListAdapter.ListItem(info[0], info[1], null));
        }
        emitter.onSuccess(translators);
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(translators -> setListAdapter(new SimpleIconListAdapter<>(getContext(), translators)), error -> Toast.makeText(getContext(), error.getMessage(), Toast.LENGTH_LONG).show());
}
Also used : Bundle(android.os.Bundle) NonNull(androidx.annotation.NonNull) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) InputStreamReader(java.io.InputStreamReader) SimpleIconListAdapter(de.danoeh.antennapod.adapter.SimpleIconListAdapter) ArrayList(java.util.ArrayList) Disposable(io.reactivex.disposables.Disposable) Nullable(androidx.annotation.Nullable) Toast(android.widget.Toast) View(android.view.View) ListFragment(androidx.fragment.app.ListFragment) Schedulers(io.reactivex.schedulers.Schedulers) BufferedReader(java.io.BufferedReader) SingleOnSubscribe(io.reactivex.SingleOnSubscribe) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) SimpleIconListAdapter(de.danoeh.antennapod.adapter.SimpleIconListAdapter)

Example 10 with SingleOnSubscribe

use of io.reactivex.SingleOnSubscribe in project AntennaPod by AntennaPod.

the class DevelopersFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getListView().setDivider(null);
    getListView().setSelector(android.R.color.transparent);
    developersLoader = Single.create((SingleOnSubscribe<ArrayList<SimpleIconListAdapter.ListItem>>) emitter -> {
        ArrayList<SimpleIconListAdapter.ListItem> developers = new ArrayList<>();
        BufferedReader reader = new BufferedReader(new InputStreamReader(getContext().getAssets().open("developers.csv"), "UTF-8"));
        String line;
        while ((line = reader.readLine()) != null) {
            String[] info = line.split(";");
            developers.add(new SimpleIconListAdapter.ListItem(info[0], info[2], "https://avatars2.githubusercontent.com/u/" + info[1] + "?s=60&v=4"));
        }
        emitter.onSuccess(developers);
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(developers -> setListAdapter(new SimpleIconListAdapter<>(getContext(), developers)), error -> Toast.makeText(getContext(), error.getMessage(), Toast.LENGTH_LONG).show());
}
Also used : Bundle(android.os.Bundle) NonNull(androidx.annotation.NonNull) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) InputStreamReader(java.io.InputStreamReader) SimpleIconListAdapter(de.danoeh.antennapod.adapter.SimpleIconListAdapter) ArrayList(java.util.ArrayList) Disposable(io.reactivex.disposables.Disposable) Nullable(androidx.annotation.Nullable) Toast(android.widget.Toast) View(android.view.View) ListFragment(androidx.fragment.app.ListFragment) Schedulers(io.reactivex.schedulers.Schedulers) BufferedReader(java.io.BufferedReader) SingleOnSubscribe(io.reactivex.SingleOnSubscribe) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) SimpleIconListAdapter(de.danoeh.antennapod.adapter.SimpleIconListAdapter)

Aggregations

SingleOnSubscribe (io.reactivex.SingleOnSubscribe)10 Single (io.reactivex.Single)6 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)6 Schedulers (io.reactivex.schedulers.Schedulers)6 ArrayList (java.util.ArrayList)5 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)4 SingleEmitter (io.reactivex.SingleEmitter)4 Disposable (io.reactivex.disposables.Disposable)4 Bundle (android.os.Bundle)3 View (android.view.View)3 Toast (android.widget.Toast)3 NonNull (androidx.annotation.NonNull)3 Nullable (androidx.annotation.Nullable)3 ListFragment (androidx.fragment.app.ListFragment)3 SimpleIconListAdapter (de.danoeh.antennapod.adapter.SimpleIconListAdapter)3 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 List (java.util.List)3 TextUtils (android.text.TextUtils)2 Log (android.util.Log)2