package ca.ualberta.cs.volivare;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Base64;

public class ClaimListManager {
	Context context;
	static final String prefFile = "ClaimList";
	static final String clkey = "ClaimList";
	public ClaimListManager(Context context) {
		this.context = context;
	}
	
	public ClaimList loadClaimList() throws StreamCorruptedException, IOException, ClassNotFoundException {
		SharedPreferences settings = context.getSharedPreferences(prefFile, context.MODE_PRIVATE);
		String claimListData = settings.getString("ClaimList", "");
		if (claimListData.equals("")) {
			return new ClaimList();
		} else {
			return claimListFromString(claimListData);
		}
	}
	
	static public ClaimList claimListFromString(String claimListData) throws StreamCorruptedException, IOException, ClassNotFoundException {
		ByteArrayInputStream bo = new ByteArrayInputStream(Base64.decode(claimListData, Base64.DEFAULT));
		ObjectInputStream oi = new ObjectInputStream(bo);
		return (ClaimList)oi.readObject();
	}

	public void saveClaimList(ClaimList cl) throws IOException {
		SharedPreferences settings = context.getSharedPreferences(prefFile, context.MODE_PRIVATE);
		Editor editor = settings.edit();
		try {
			editor.putString(clkey, claimListToString(cl));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		editor.commit();
	}

	static public String claimListToString(ClaimList cl) throws IOException {
		ByteArrayOutputStream bo = new ByteArrayOutputStream();
		ObjectOutputStream oo = new ObjectOutputStream(bo);
		oo.writeObject(cl);
		oo.flush();
		oo.close();
		byte bytes[] = bo.toByteArray();
		return Base64.encodeToString(bytes, Base64.DEFAULT);
	}
}

@@ -1,8 +1,563 @@
+/* Copyright 2015 victor olivares%0ALicensed under the Apache License, Version 2.0 (the %22License%22);%0Ayou may not use this file except in compliance with the License.%0AYou may obtain a copy of the License at%0Ahttp://www.apache.org/licenses/LICENSE-2.0%0AUnless required by applicable law or agreed to in writing, software%0Adistributed under the License is distributed on an %22AS IS%22 BASIS,%0AWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0ASee the License for the specific language governing permissions and%0Alimitations under the License. */%0A
 package 

Update ClaimListManager.java