yukuku / androidbible

Open Source Bible for Android
https://alkitab.app
Apache License 2.0
321 stars 177 forks source link

Feature Request: More than one internal versions #57

Open ghost opened 6 years ago

ghost commented 6 years ago

It will be better to have more than one .Yes files as internal (inbuilt). Can you add this feature please?

yukuku commented 4 years ago

@Gechamare Could you explain the use case or reasons why this could be useful?

joielechong commented 1 year ago

Instead of using internal file, you can try installing the .yes file when loading app. Something like this:

BibleUtil.installBible(context);

BibleUtil class:

package com.bible.util;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import com.afollestad.materialdialogs.MaterialDialog;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;
import yuku.alkitab.base.S;
import yuku.alkitab.base.model.MVersionDb;
import yuku.alkitab.base.storage.YesReaderFactory;
import yuku.alkitab.base.util.AddonManager;
import yuku.alkitab.io.BibleReader;

public class BibleUtil {
    public static final String Tag = "BibleUtil";
    public static final String yetOrignalFileName = "kjv.yes";

    boolean installBible(Context context) {
        // Check if .yet file is already exist.
        final File existingFile = AddonManager.getReadableVersionFile(yetOrignalFileName);
        if (existingFile != null) {
            return true;
        }

        try {
            Resources res = context.getResources();
            InputStream is = res.openRawResource(R.raw.kjv); // copy .yet from asset folder

            File outputDir = context.getCacheDir(); // context being the Activity pointer
            File outputFile = File.createTempFile("kjv", ".yes", outputDir);
            copyInputStreamToFile(is, outputFile);

            final File localFile = AddonManager.getWritableVersionFile(tbOrignalFileName);
//            copyStreamToFile(is, localFile);
            copyInputStreamToFile(is, localFile);
            is.close();

            handleFileOpenYes(context, outputFile);
            return true;
        } catch (Exception e) {
            Log.d(Tag, "Failed to install: " + e.getMessage());
            return false;
        }
    }

    // Copy an InputStream to a File.
    private void copyInputStreamToFile(InputStream in, File file) {
        OutputStream out = null;
        try {
            out = new FileOutputStream(file);
            byte[] buf = new byte[1024];
            int len;
            while((len=in.read(buf))>0){
                out.write(buf,0,len);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            // Ensure that the InputStreams are closed even if there's an exception.
            try {
                if ( out != null ) {
                    out.close();
                }

                // If you want to close the "in" InputStream yourself then remove this
                // from here but ensure that you close it yourself eventually.
                in.close();
            }
            catch ( IOException e ) {
                e.printStackTrace();
            }
        }
    }

    void handleFileOpenYes(Context context, File file) {
        try {
            final BibleReader reader = YesReaderFactory.createYesReader(file.getAbsolutePath());
            if (reader == null) {
                throw new Exception("Not a valid YES file.");
            }

            int maxOrdering = S.getDb().getVersionMaxOrdering();
            if (maxOrdering == 0) maxOrdering = MVersionDb.DEFAULT_ORDERING_START;

            final MVersionDb mvDb = new MVersionDb();
            mvDb.locale = reader.getLocale();
            mvDb.shortName = reader.getShortName();
            mvDb.longName = reader.getLongName();
            mvDb.description = reader.getDescription();
            mvDb.filename = file.getAbsolutePath();
            mvDb.ordering = maxOrdering + 1;
            mvDb.preset_name = null;

            S.getDb().insertOrUpdateVersionWithActive(mvDb, true);
            S.setActiveVersion(mvDb);
            MVersionDb.clearVersionImplCache();
        } catch (Exception e) {
            if( BuildConfig.DEBUG ) {
                new MaterialDialog.Builder(context)
                    .title(R.string.ed_error_encountered)
                    .content(e.getClass().getSimpleName() + ": " + e.getMessage())
                    .positiveText(R.string.ok)
                    .show();
            }
            Log.d(Tag, Objects.requireNonNull(e.getMessage()));
        }
    }
}