Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package nextrek.minstrek.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import nextrek.minstrek.core.ConditionList;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class BookUtils {
    private static final String TAG = "Utils";

    public static Bitmap getBitmapFromAsset(Context context, String strName) {

        InputStream fis = null;
        try {
            fis = context.getAssets().open(strName, AssetManager.ACCESS_STREAMING); // context.openFileInput(strName);
            // Log.d(TAG, strName + " loaded");
            return BitmapFactory.decodeStream(fis);
        } catch (IOException e) {
            //e.printStackTrace();
            // ignore it
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {

            }
        }

        Log.e(TAG, strName + " *FAILED*");
        return null;
    }

    public static String readStringFile(Context context, String filename) {

        InputStream fis = null;
        try {
            fis = context.getAssets().open(filename, AssetManager.ACCESS_STREAMING); // context.openFileInput(strName);

            return readAsString(fis);
        } catch (IOException e) {
            // ignore it, we will return null
            //Log.e(TAG, "readStringFile: ", e);
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                // ignore it
            }
        }

        return null;
    }

    public static String readAsString(InputStream in) throws IOException {
        StringBuilder sb = new StringBuilder();

        InputStreamReader isr = new InputStreamReader(in);
        try {
            BufferedReader r = new BufferedReader(isr, 8192);
            try {
                for (String line = r.readLine(); line != null; line = r.readLine()) {
                    sb.append(line);
                }
                return sb.toString();
            } finally {
                if (r != null) {
                    r.close();
                    r = null;
                }
            }
        } finally {
            if (isr != null) {
                isr.close();
                isr = null;
            }
        }
    }

    public static ConditionList loadConditionList(Context context, SharedPreferences prefs, String sharedPrefsKey, String javascriptFilename, String javascriptKey) {
        ConditionList eventsList = null;
        String eventsData = prefs.getString(sharedPrefsKey, null);
        if (eventsData != null) {
            eventsList = ConditionList.CreateFromSharedPrefs(eventsData);
            Log.d(TAG, sharedPrefsKey + "(SP): " + eventsData);
        } else {
            Log.d(TAG, sharedPrefsKey + "(SP): *no savegame*");
            eventsData = BookUtils.readStringFile(context, javascriptFilename);
            if (eventsData != null) {
                eventsList = ConditionList.CreateFromJavascript(javascriptKey, eventsData);
                Log.d(TAG, sharedPrefsKey + "(JS): " + eventsData);
            } else {
                Log.w(TAG, sharedPrefsKey + "(JS): *missing data file*");
            }
        }
        return eventsList;
    }
}

Commits for Nextrek/Android/Minstrek/MinstrekLib/src/nextrek/minstrek/utils/BookUtils.java

Diff revisions: vs.
Revision Author Commited Message
4 FMMortaroli picture FMMortaroli Fri 19 Apr, 2013 16:54:38 +0000