Search in sources :

Example 1 with EXTRA_ZIM_FILE

use of org.kiwix.kiwixmobile.utils.Constants.EXTRA_ZIM_FILE in project kiwix-android by kiwix.

the class DownloadService method downloadBook.

private void downloadBook(String url, int notificationID, LibraryNetworkEntity.Book book) {
    if (downloadFragment != null) {
        downloadFragment.addDownload(notificationID, book, KIWIX_ROOT + StorageUtils.getFileNameFromUrl(book.getUrl()));
    }
    TestingUtils.bindResource(DownloadService.class);
    if (book.file != null && (book.file.exists() || new File(book.file.getPath() + ".part").exists())) {
        // Calculate initial download progress
        int initial = (int) (getCurrentSize(book) / (Long.valueOf(book.getSize()) * BOOK_SIZE_OFFSET));
        notification.get(notificationID).setProgress(100, initial, false);
        updateDownloadFragmentProgress(initial, notificationID);
        notificationManager.notify(notificationID, notification.get(notificationID).build());
    }
    kiwixService.getMetaLinks(url).retryWhen(errors -> errors.flatMap(error -> Observable.timer(5, TimeUnit.SECONDS))).subscribeOn(AndroidSchedulers.mainThread()).flatMap(metaLink -> getMetaLinkContentLength(metaLink.getRelevantUrl().getValue())).flatMap(pair -> Observable.fromIterable(ChunkUtils.getChunks(pair.first, pair.second, notificationID))).concatMap(this::downloadChunk).distinctUntilChanged().doOnComplete(() -> updateDownloadFragmentComplete(notificationID)).subscribe(progress -> {
        if (progress == 100) {
            notification.get(notificationID).setOngoing(false);
            notification.get(notificationID).setContentTitle(notificationTitle + " " + getResources().getString(R.string.zim_file_downloaded));
            notification.get(notificationID).setContentText(getString(R.string.zim_file_downloaded));
            final Intent target = new Intent(this, KiwixMobileActivity.class);
            target.putExtra(EXTRA_ZIM_FILE, KIWIX_ROOT + StorageUtils.getFileNameFromUrl(book.getUrl()));
            target.putExtra(EXTRA_NOTIFICATION_ID, notificationID);
            PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, target, PendingIntent.FLAG_CANCEL_CURRENT);
            book.downloaded = true;
            bookDao.deleteBook(book.id);
            notification.get(notificationID).setContentIntent(pendingIntent);
            notification.get(notificationID).mActions.clear();
            TestingUtils.unbindResource(DownloadService.class);
        }
        notification.get(notificationID).setProgress(100, progress, false);
        if (progress != 100 && timeRemaining.get(notificationID) != -1)
            notification.get(notificationID).setContentText(DownloadFragment.toHumanReadableTime(timeRemaining.get(notificationID)));
        notificationManager.notify(notificationID, notification.get(notificationID).build());
        if (progress == 0 || progress == 100) {
            // Tells android to not kill the service
            updateForeground();
        }
        updateDownloadFragmentProgress(progress, notificationID);
        if (progress == 100) {
            stopSelf();
        }
    }, Throwable::printStackTrace);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) EXTRA_LIBRARY(org.kiwix.kiwixmobile.utils.Constants.EXTRA_LIBRARY) PendingIntent(android.app.PendingIntent) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) IBinder(android.os.IBinder) BookDao(org.kiwix.kiwixmobile.database.BookDao) EXTRA_ZIM_FILE(org.kiwix.kiwixmobile.utils.Constants.EXTRA_ZIM_FILE) Handler(android.os.Handler) Looper(android.os.Looper) FileUtils.getCurrentSize(org.kiwix.kiwixmobile.utils.files.FileUtils.getCurrentSize) EXTRA_NOTIFICATION_ID(org.kiwix.kiwixmobile.utils.Constants.EXTRA_NOTIFICATION_ID) PreferenceManager(android.preference.PreferenceManager) Log(android.util.Log) KiwixMobileActivity(org.kiwix.kiwixmobile.KiwixMobileActivity) EXTRA_BOOK(org.kiwix.kiwixmobile.utils.Constants.EXTRA_BOOK) Request(okhttp3.Request) NotificationManager(android.app.NotificationManager) Service(android.app.Service) SparseArray(android.util.SparseArray) BufferedSource(okio.BufferedSource) TestingUtils(org.kiwix.kiwixmobile.utils.TestingUtils) FileUtils(org.kiwix.kiwixmobile.utils.files.FileUtils) PREF_STORAGE(org.kiwix.kiwixmobile.utils.Constants.PREF_STORAGE) SparseIntArray(android.util.SparseIntArray) Environment(android.os.Environment) Pair(android.util.Pair) Intent(android.content.Intent) KiwixApplication(org.kiwix.kiwixmobile.KiwixApplication) ArrayList(java.util.ArrayList) Binder(android.os.Binder) Inject(javax.inject.Inject) Toast(android.widget.Toast) Response(okhttp3.Response) R(org.kiwix.kiwixmobile.R) Observable(io.reactivex.Observable) LibraryNetworkEntity(org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity) IOException(java.io.IOException) File(java.io.File) KiwixService(org.kiwix.kiwixmobile.network.KiwixService) Color(android.graphics.Color) TimeUnit(java.util.concurrent.TimeUnit) KiwixDatabase(org.kiwix.kiwixmobile.database.KiwixDatabase) OkHttpClient(okhttp3.OkHttpClient) StorageUtils(org.kiwix.kiwixmobile.utils.StorageUtils) NotificationCompat(android.support.v4.app.NotificationCompat) SharedPreferenceUtil(org.kiwix.kiwixmobile.utils.SharedPreferenceUtil) NetworkUtils(org.kiwix.kiwixmobile.utils.NetworkUtils) LibraryFragment(org.kiwix.kiwixmobile.zim_manager.library_view.LibraryFragment) ZimManageActivity(org.kiwix.kiwixmobile.zim_manager.ZimManageActivity) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 Service (android.app.Service)1 Intent (android.content.Intent)1 Color (android.graphics.Color)1 Binder (android.os.Binder)1 Environment (android.os.Environment)1 Handler (android.os.Handler)1 IBinder (android.os.IBinder)1 Looper (android.os.Looper)1 PreferenceManager (android.preference.PreferenceManager)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 Log (android.util.Log)1 Pair (android.util.Pair)1 SparseArray (android.util.SparseArray)1 SparseIntArray (android.util.SparseIntArray)1 Toast (android.widget.Toast)1 Observable (io.reactivex.Observable)1 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)1 File (java.io.File)1