Search in sources :

Example 1 with LocalTranslation

use of com.quran.labs.androidquran.common.LocalTranslation in project quran_android by quran.

the class QuranDataProvider method getSuggestions.

private Cursor getSuggestions(String query) {
    if (query.length() < 3) {
        return null;
    }
    final boolean queryIsArabic = QuranUtils.doesStringContainArabic(query);
    final boolean haveArabic = queryIsArabic && quranFileUtils.hasTranslation(getContext(), QURAN_ARABIC_DATABASE);
    List<LocalTranslation> translations = getAvailableTranslations();
    if (translations.size() == 0 && (queryIsArabic && !haveArabic)) {
        return null;
    }
    int total = translations.size();
    int start = haveArabic ? -1 : 0;
    String[] cols = new String[] { BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1, SearchManager.SUGGEST_COLUMN_TEXT_2, SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID };
    MatrixCursor mc = new MatrixCursor(cols);
    Context context = getContext();
    boolean gotResults = false;
    for (int i = start; i < total; i++) {
        if (gotResults) {
            continue;
        }
        String database;
        if (i < 0) {
            database = QURAN_ARABIC_DATABASE;
        } else {
            LocalTranslation translation = translations.get(i);
            // skip non-arabic databases if the query is in arabic
            if (queryIsArabic && translation.languageCode != null && !"ar".equals(translation.languageCode)) {
                continue;
            } else if (!queryIsArabic && "ar".equals(translation.languageCode)) {
                // skip arabic databases when the query isn't arabic
                continue;
            }
            database = translation.filename;
        }
        Cursor suggestions = null;
        try {
            suggestions = search(query, database, false);
            if (suggestions != null && suggestions.moveToFirst()) {
                do {
                    int sura = suggestions.getInt(1);
                    int ayah = suggestions.getInt(2);
                    String text = suggestions.getString(3);
                    String foundText = context.getString(R.string.found_in_sura, quranInfo.getSuraName(context, sura, false), ayah);
                    gotResults = true;
                    MatrixCursor.RowBuilder row = mc.newRow();
                    int id = suggestions.getInt(0);
                    row.add(id);
                    row.add(text);
                    row.add(foundText);
                    row.add(id);
                } while (suggestions.moveToNext());
            }
        } finally {
            DatabaseUtils.closeCursor(suggestions);
        }
    }
    return mc;
}
Also used : Context(android.content.Context) LocalTranslation(com.quran.labs.androidquran.common.LocalTranslation) MatrixCursor(android.database.MatrixCursor) Cursor(android.database.Cursor) MatrixCursor(android.database.MatrixCursor)

Example 2 with LocalTranslation

use of com.quran.labs.androidquran.common.LocalTranslation in project quran_android by quran.

the class TranslationsDBAdapter method getTranslationsHash.

public SparseArray<LocalTranslation> getTranslationsHash() {
    List<LocalTranslation> items = getTranslations();
    SparseArray<LocalTranslation> result = new SparseArray<>();
    for (int i = 0, itemsSize = items.size(); i < itemsSize; i++) {
        LocalTranslation item = items.get(i);
        result.put(item.id, item);
    }
    return result;
}
Also used : SparseArray(android.util.SparseArray) LocalTranslation(com.quran.labs.androidquran.common.LocalTranslation)

Example 3 with LocalTranslation

use of com.quran.labs.androidquran.common.LocalTranslation in project quran_android by quran.

the class TranslationManagerPresenter method mergeWithServerTranslations.

private List<TranslationItem> mergeWithServerTranslations(List<Translation> serverTranslations) {
    List<TranslationItem> results = new ArrayList<>(serverTranslations.size());
    SparseArray<LocalTranslation> localTranslations = translationsDBAdapter.getTranslationsHash();
    String databaseDir = quranFileUtils.getQuranDatabaseDirectory(appContext);
    List<TranslationItem> updates = new ArrayList<>();
    for (int i = 0, count = serverTranslations.size(); i < count; i++) {
        Translation translation = serverTranslations.get(i);
        LocalTranslation local = localTranslations.get(translation.getId());
        File dbFile = new File(databaseDir, translation.getFileName());
        boolean exists = dbFile.exists();
        TranslationItem item;
        if (exists) {
            int version = local == null ? getVersionFromDatabase(translation.getFileName()) : local.version;
            item = new TranslationItem(translation, version);
        } else {
            item = new TranslationItem(translation);
        }
        if (exists && !item.exists()) {
            // delete the file, it has been corrupted
            if (dbFile.delete()) {
                exists = false;
            }
        }
        if ((local == null && exists) || (local != null && !exists)) {
            updates.add(item);
        } else if (local != null && local.languageCode == null) {
            // older items don't have a language code
            updates.add(item);
        }
        results.add(item);
    }
    if (!updates.isEmpty()) {
        translationsDBAdapter.writeTranslationUpdates(updates);
    }
    return results;
}
Also used : TranslationItem(com.quran.labs.androidquran.dao.translation.TranslationItem) Translation(com.quran.labs.androidquran.dao.translation.Translation) LocalTranslation(com.quran.labs.androidquran.common.LocalTranslation) LocalTranslation(com.quran.labs.androidquran.common.LocalTranslation) ArrayList(java.util.ArrayList) File(java.io.File)

Example 4 with LocalTranslation

use of com.quran.labs.androidquran.common.LocalTranslation in project quran_android by quran.

the class PagerActivity method requestTranslationsList.

private void requestTranslationsList() {
    compositeDisposable.add(Single.fromCallable(() -> translationsDBAdapter.getTranslations()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableSingleObserver<List<LocalTranslation>>() {

        @Override
        public void onSuccess(List<LocalTranslation> translationList) {
            int items = translationList.size();
            String[] titles = new String[items];
            for (int i = 0; i < items; i++) {
                LocalTranslation item = translationList.get(i);
                if (!TextUtils.isEmpty(item.translatorForeign)) {
                    titles[i] = item.translatorForeign;
                } else if (!TextUtils.isEmpty(item.translator)) {
                    titles[i] = item.translator;
                } else {
                    titles[i] = item.name;
                }
            }
            Set<String> currentActiveTranslations = quranSettings.getActiveTranslations();
            if (currentActiveTranslations.isEmpty() && items > 0) {
                currentActiveTranslations = new HashSet<>();
                for (int i = 0; i < items; i++) {
                    currentActiveTranslations.add(translationList.get(i).filename);
                }
            }
            activeTranslations = currentActiveTranslations;
            if (translationsSpinnerAdapter != null) {
                translationsSpinnerAdapter.updateItems(titles, translationList, activeTranslations);
            }
            translationItems = titles;
            translations = translationList;
            if (showingTranslation) {
                // Since translation items have changed, need to
                updateActionBarSpinner();
            }
            refreshPages();
        }

        @Override
        public void onError(Throwable e) {
        }
    }));
}
Also used : DisposableSingleObserver(io.reactivex.observers.DisposableSingleObserver) LocalTranslation(com.quran.labs.androidquran.common.LocalTranslation) List(java.util.List)

Example 5 with LocalTranslation

use of com.quran.labs.androidquran.common.LocalTranslation in project quran_android by quran.

the class BaseTranslationPresenter method getTranslationNames.

String[] getTranslationNames(@NonNull List<String> translations, @NonNull Map<String, LocalTranslation> translationMap) {
    int translationCount = translations.size();
    String[] result = new String[translationCount];
    for (int i = 0; i < translationCount; i++) {
        String translation = translations.get(i);
        LocalTranslation localTranslation = translationMap.get(translation);
        result[i] = localTranslation == null ? translation : localTranslation.getTranslatorName();
    }
    // fallback to all translations when the map is empty
    if (translationCount == 0) {
        Set<String> keys = translationMap.keySet();
        int mapSize = keys.size();
        result = new String[mapSize];
        int i = 0;
        for (String key : keys) {
            result[i++] = translationMap.get(key).filename;
        }
    }
    return result;
}
Also used : LocalTranslation(com.quran.labs.androidquran.common.LocalTranslation)

Aggregations

LocalTranslation (com.quran.labs.androidquran.common.LocalTranslation)8 Cursor (android.database.Cursor)3 Context (android.content.Context)2 MatrixCursor (android.database.MatrixCursor)2 ArrayList (java.util.ArrayList)2 NonNull (android.support.annotation.NonNull)1 WorkerThread (android.support.annotation.WorkerThread)1 SparseArray (android.util.SparseArray)1 Translation (com.quran.labs.androidquran.dao.translation.Translation)1 TranslationItem (com.quran.labs.androidquran.dao.translation.TranslationItem)1 DisposableSingleObserver (io.reactivex.observers.DisposableSingleObserver)1 File (java.io.File)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Test (org.junit.Test)1