Search in sources :

Example 1 with TranslationItem

use of com.quran.labs.androidquran.dao.translation.TranslationItem in project quran_android by quran.

the class TranslationsDBAdapter method writeTranslationUpdates.

public boolean writeTranslationUpdates(List<TranslationItem> updates) {
    boolean result = true;
    db.beginTransaction();
    try {
        for (int i = 0, updatesSize = updates.size(); i < updatesSize; i++) {
            TranslationItem item = updates.get(i);
            if (item.exists()) {
                ContentValues values = new ContentValues();
                values.put(TranslationsTable.ID, item.translation.getId());
                values.put(TranslationsTable.NAME, item.translation.getDisplayName());
                values.put(TranslationsTable.TRANSLATOR, item.translation.getTranslator());
                values.put(TranslationsTable.TRANSLATOR_FOREIGN, item.translation.getTranslatorNameLocalized());
                values.put(TranslationsTable.FILENAME, item.translation.getFileName());
                values.put(TranslationsTable.URL, item.translation.getFileUrl());
                values.put(TranslationsTable.LANGUAGE_CODE, item.translation.getLanguageCode());
                values.put(TranslationsTable.VERSION, item.localVersion);
                db.replace(TranslationsTable.TABLE_NAME, null, values);
            } else {
                db.delete(TranslationsTable.TABLE_NAME, TranslationsTable.ID + " = " + item.translation.getId(), null);
            }
        }
        db.setTransactionSuccessful();
        // clear the cached translations
        this.cachedTranslations = null;
    } catch (Exception e) {
        result = false;
        Timber.d(e, "error writing translation updates");
    } finally {
        db.endTransaction();
    }
    return result;
}
Also used : TranslationItem(com.quran.labs.androidquran.dao.translation.TranslationItem) ContentValues(android.content.ContentValues)

Example 2 with TranslationItem

use of com.quran.labs.androidquran.dao.translation.TranslationItem in project quran_android by quran.

the class TranslationManagerActivity method removeItem.

private void removeItem(final TranslationRowData translationRowData) {
    if (adapter == null) {
        return;
    }
    final TranslationItem selectedItem = (TranslationItem) translationRowData;
    String msg = String.format(getString(R.string.remove_dlg_msg), selectedItem.name());
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.remove_dlg_title).setMessage(msg).setPositiveButton(R.string.remove_button, (dialog, id) -> {
        quranFileUtils.removeTranslation(TranslationManagerActivity.this, selectedItem.translation.getFileName());
        TranslationItem updatedItem = selectedItem.withTranslationRemoved();
        updateTranslationItem(updatedItem);
        // remove from active translations
        QuranSettings settings = QuranSettings.getInstance(this);
        Set<String> activeTranslations = settings.getActiveTranslations();
        activeTranslations.remove(selectedItem.translation.getFileName());
        settings.setActiveTranslations(activeTranslations);
        generateListItems();
    }).setNegativeButton(R.string.cancel, (dialog, i) -> dialog.dismiss());
    builder.show();
}
Also used : TranslationItem(com.quran.labs.androidquran.dao.translation.TranslationItem) AlertDialog(android.support.v7.app.AlertDialog) Bundle(android.os.Bundle) TranslationRowData(com.quran.labs.androidquran.dao.translation.TranslationRowData) SparseIntArray(android.util.SparseIntArray) QuranDownloadService(com.quran.labs.androidquran.service.QuranDownloadService) ServiceIntentHelper(com.quran.labs.androidquran.service.util.ServiceIntentHelper) Intent(android.content.Intent) LocalBroadcastManager(android.support.v4.content.LocalBroadcastManager) TranslationsAdapter(com.quran.labs.androidquran.ui.adapter.TranslationsAdapter) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) TranslationHeader(com.quran.labs.androidquran.dao.translation.TranslationHeader) R(com.quran.labs.androidquran.R) QuranFileUtils(com.quran.labs.androidquran.util.QuranFileUtils) QuranSettings(com.quran.labs.androidquran.util.QuranSettings) ActionBar(android.support.v7.app.ActionBar) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) IntentFilter(android.content.IntentFilter) QuranDownloadNotifier(com.quran.labs.androidquran.service.util.QuranDownloadNotifier) Set(java.util.Set) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) TranslationManagerPresenter(com.quran.labs.androidquran.presenter.translation.TranslationManagerPresenter) QuranApplication(com.quran.labs.androidquran.QuranApplication) File(java.io.File) Timber(timber.log.Timber) RecyclerView(android.support.v7.widget.RecyclerView) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) AlertDialog(android.support.v7.app.AlertDialog) TranslationItem(com.quran.labs.androidquran.dao.translation.TranslationItem) DefaultDownloadReceiver(com.quran.labs.androidquran.service.util.DefaultDownloadReceiver) Snackbar(android.support.design.widget.Snackbar) Set(java.util.Set) QuranSettings(com.quran.labs.androidquran.util.QuranSettings)

Example 3 with TranslationItem

use of com.quran.labs.androidquran.dao.translation.TranslationItem in project quran_android by quran.

the class TranslationManagerActivity method downloadItem.

private void downloadItem(TranslationRowData translationRowData) {
    TranslationItem selectedItem = (TranslationItem) translationRowData;
    if (selectedItem.exists() && !selectedItem.needsUpgrade()) {
        return;
    }
    downloadingItem = selectedItem;
    if (mDownloadReceiver == null) {
        mDownloadReceiver = new DefaultDownloadReceiver(this, QuranDownloadService.DOWNLOAD_TYPE_TRANSLATION);
        LocalBroadcastManager.getInstance(this).registerReceiver(mDownloadReceiver, new IntentFilter(QuranDownloadNotifier.ProgressIntent.INTENT_NAME));
    }
    mDownloadReceiver.setListener(this);
    // actually start the download
    String url = selectedItem.translation.getFileUrl();
    if (selectedItem.translation.getFileUrl() == null) {
        return;
    }
    String destination = databaseDirectory;
    Timber.d("downloading %s to %s", url, destination);
    if (selectedItem.exists()) {
        try {
            File f = new File(destination, selectedItem.translation.getFileName());
            if (f.exists()) {
                File newPath = new File(destination, selectedItem.translation.getFileName() + UPGRADING_EXTENSION);
                if (newPath.exists()) {
                    newPath.delete();
                }
                f.renameTo(newPath);
            }
        } catch (Exception e) {
            Timber.d(e, "error backing database file up");
        }
    }
    // start the download
    String notificationTitle = selectedItem.name();
    Intent intent = ServiceIntentHelper.getDownloadIntent(this, url, destination, notificationTitle, TRANSLATION_DOWNLOAD_KEY, QuranDownloadService.DOWNLOAD_TYPE_TRANSLATION);
    String filename = selectedItem.translation.getFileName();
    if (url.endsWith("zip")) {
        filename += ".zip";
    }
    intent.putExtra(QuranDownloadService.EXTRA_OUTPUT_FILE_NAME, filename);
    startService(intent);
}
Also used : TranslationItem(com.quran.labs.androidquran.dao.translation.TranslationItem) IntentFilter(android.content.IntentFilter) DefaultDownloadReceiver(com.quran.labs.androidquran.service.util.DefaultDownloadReceiver) Intent(android.content.Intent) File(java.io.File)

Example 4 with TranslationItem

use of com.quran.labs.androidquran.dao.translation.TranslationItem in project quran_android by quran.

the class TranslationManagerActivity method handleDownloadSuccess.

@Override
public void handleDownloadSuccess() {
    if (downloadingItem != null) {
        if (downloadingItem.exists()) {
            try {
                File f = new File(databaseDirectory, downloadingItem.translation.getFileName() + UPGRADING_EXTENSION);
                if (f.exists()) {
                    f.delete();
                }
            } catch (Exception e) {
                Timber.d(e, "error removing old database file");
            }
        }
        TranslationItem updated = downloadingItem.withTranslationVersion(downloadingItem.translation.getCurrentVersion());
        updateTranslationItem(updated);
        // update active translations and add this item to it
        QuranSettings settings = QuranSettings.getInstance(this);
        Set<String> activeTranslations = settings.getActiveTranslations();
        activeTranslations.add(downloadingItem.translation.getFileName());
        settings.setActiveTranslations(activeTranslations);
    }
    downloadingItem = null;
    generateListItems();
}
Also used : TranslationItem(com.quran.labs.androidquran.dao.translation.TranslationItem) QuranSettings(com.quran.labs.androidquran.util.QuranSettings) File(java.io.File)

Example 5 with TranslationItem

use of com.quran.labs.androidquran.dao.translation.TranslationItem in project quran_android by quran.

the class TranslationManagerActivity method onTranslationsUpdated.

public void onTranslationsUpdated(List<TranslationItem> items) {
    translationSwipeRefresh.setRefreshing(false);
    SparseIntArray itemsSparseArray = new SparseIntArray(items.size());
    for (int i = 0, itemsSize = items.size(); i < itemsSize; i++) {
        TranslationItem item = items.get(i);
        itemsSparseArray.put(item.translation.getId(), i);
    }
    allItems = items;
    translationPositions = itemsSparseArray;
    generateListItems();
}
Also used : TranslationItem(com.quran.labs.androidquran.dao.translation.TranslationItem) SparseIntArray(android.util.SparseIntArray)

Aggregations

TranslationItem (com.quran.labs.androidquran.dao.translation.TranslationItem)8 File (java.io.File)4 TranslationRowData (com.quran.labs.androidquran.dao.translation.TranslationRowData)3 ArrayList (java.util.ArrayList)3 Intent (android.content.Intent)2 IntentFilter (android.content.IntentFilter)2 SparseIntArray (android.util.SparseIntArray)2 TranslationHeader (com.quran.labs.androidquran.dao.translation.TranslationHeader)2 DefaultDownloadReceiver (com.quran.labs.androidquran.service.util.DefaultDownloadReceiver)2 QuranSettings (com.quran.labs.androidquran.util.QuranSettings)2 ContentValues (android.content.ContentValues)1 Bundle (android.os.Bundle)1 Snackbar (android.support.design.widget.Snackbar)1 LocalBroadcastManager (android.support.v4.content.LocalBroadcastManager)1 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)1 ActionBar (android.support.v7.app.ActionBar)1 AlertDialog (android.support.v7.app.AlertDialog)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 MenuItem (android.view.MenuItem)1