Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: More than one internal versions #57

Open
ghost opened this issue Dec 18, 2017 · 2 comments
Open

Feature Request: More than one internal versions #57

ghost opened this issue Dec 18, 2017 · 2 comments

Comments

@ghost
Copy link

ghost commented Dec 18, 2017

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

@yukuku yukuku changed the title Feature Request Feature Request: More than one internal versions Feb 13, 2020
@yukuku
Copy link
Owner

yukuku commented Feb 13, 2020

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

@joielechong
Copy link

joielechong commented May 6, 2023

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()));
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants