Android Open Source - Android-Counter-App Counter Save Model






From Project

Back to project page Android-Counter-App.

License

The source code is released under:

Apache License

If you think the Android project Android-Counter-App listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
  Counter Save Model//from   w  w w .  j a v  a  2s  . com
  
  This class defines the saving and loading methods for Google Gson.
  This class is important for saving the list of Counter Models.
  
  Copyright 2014 David Yee

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */

package ca.ualberta.cs.asn1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/*
 * The CounterSaveModel is a generic class that allows a list of 
 * CounterModels to be saved to a JSON and to be read from JSON.
 * Since persistent data is critical in this app, the saveData and 
 * loadData methods in this class are called frequently. For this 
 * reason, they have been abstracted into this class and accept 
 * any given list of CounterModels with any given name.
 */
public class CounterSaveModel
{
  private Context fileContext;
        private String FILE_NAME;
    
  public CounterSaveModel(Context fileContext, String fileName) {
    super();
    this.fileContext = fileContext;
    this.FILE_NAME = fileName;
  }

  public void saveData(List<CounterModel> list){
        try {
            FileOutputStream fos = fileContext.openFileOutput(FILE_NAME,
                        Context.MODE_PRIVATE);
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            Gson gson = new Gson();
            Type fooType = new TypeToken<ArrayList<CounterModel>>() {}.getType();
            gson.toJson(list, fooType, osw);
            osw.close();
            fos.close();
        } catch (FileNotFoundException e)
            {
                e.printStackTrace();
        } catch (IOException e)
            {
                e.printStackTrace();
            }
    }
  
  public List<CounterModel> loadData(){
      FileInputStream fis;
      List<CounterModel> list = new ArrayList<CounterModel>();
            try
            {
                fis = fileContext.openFileInput(FILE_NAME);
                InputStreamReader isr = new InputStreamReader(fis);
                Gson gson = new Gson();
                
                Type fooType = new TypeToken<ArrayList<CounterModel>>() {}.getType();
                List<CounterModel> list_temp = gson.fromJson(isr, fooType);
                if(list_temp != null)
                  list = list_temp;
                isr.close();
                fis.close();
            } catch (FileNotFoundException e)
                {
                    e.printStackTrace();
            } catch (IOException e)
                {
                    e.printStackTrace();
                }
      
      return list;
  }
}




Java Source Code List

ca.ualberta.cs.asn1.CounterActivity.java
ca.ualberta.cs.asn1.CounterListAdapter.java
ca.ualberta.cs.asn1.CounterModel.java
ca.ualberta.cs.asn1.CounterSaveModel.java
ca.ualberta.cs.asn1.StatisticActivity.java
ca.ualberta.cs.asn1.StatisticModel.java