package com.loopbook.cuhk_loopbook;

import java.util.*;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import android.content.Context;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Tag;
import org.jsoup.Jsoup; 

import com.loopbook.cuhk_loopbook.LibConn;

public class DataIO {
    static String filename = "books.html";

    public static Element refreshStoredData(Context c) {
        LibConn myLib = new LibConn("1155032703", "19940122");
        myLib.login();
        Element elm = myLib.getBooksElement();

        saveData(c, elm);
        return elm;
    }

    public static void saveData(Context c, Element elm) {
        FileOutputStream out;

        try {
            out = c.openFileOutput(filename, Context.MODE_PRIVATE);
            out.write(elm.outerHtml().getBytes());
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Element getStoredData(Context c) {
        InputStream in;

        try {
            in = new BufferedInputStream(c.openFileInput(filename));
            Element elm = Jsoup.parse(in, "UTF-8", "")
                               .select("table.patFunc").first();
            in.close();
            return elm;
        } catch (Exception e) {
            return new Element(Tag.valueOf("table"), "").classNames(
                           new HashSet(Arrays.asList("patFunc"))
                       );
        }
    }
}

@@ -179,16 +179,103 @@
 ontext;%0A
+import android.content.SharedPreferences;%0Aimport android.preference.PreferenceManager;%0A
 import o
@@ -298,16 +298,16 @@
 lement;%0A
-
 import o
@@ -512,32 +512,238 @@
 ta(Context c) %7B%0A
+        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);%0A        String user_id = prefs.getString(%22user_id%22, %22%22);%0A        String user_passwd = prefs.getString(%22user_passwd%22, %22%22);%0A
         LibConn 
@@ -766,32 +766,28 @@
 onn(
-%221155032703%22, %2219940122%22
+user_id, user_passwd
 );%0A 

use data from setting to login: