Android Open Source - TravelReminder Reminders Helper






From Project

Back to project page TravelReminder.

License

The source code is released under:

GNU General Public License

If you think the Android project TravelReminder 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

package com.ganterd.travelreminder;
//from   ww w.ja va2s .  c o m
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

import android.content.Context;
import android.util.Log;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;


public class RemindersHelper{
  private static Context context;
  private static final String fileExtension = ".reminder"; 
  
  public static void init(Context base) {
    context = base;
  }
  
  public static Reminder[] getAllReminders(){
    File[] files = getAllReminderFiles();
    
    Reminder[] allReminders = new Reminder[files.length];
    Log.d("RemindersHelper", "There are " + files.length + " files in internal storage");
    ObjectMapper mapper = new ObjectMapper();
    for(int i = 0; i < files.length; i++){
      Log.d("RemindersHelper", files[i].getName());
      try {
        allReminders[i] = mapper.readValue(files[i], Reminder.class);
      } catch (JsonParseException e) {
        e.printStackTrace();
      } catch (JsonMappingException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    
    return allReminders;
  }
  
  public static void saveReminder(Reminder reminder){
    File existingFile = new File(context.getFilesDir(), reminder.getReminderID() + fileExtension);
    
    if(existingFile.isFile() && existingFile.canRead()){
      Log.i("RemindersHelper", "Deleting existing reminder file " + existingFile.getAbsolutePath());
      existingFile.delete();
    }
    
    ObjectMapper mapper = new ObjectMapper();
    try {
      File f = new File(context.getFilesDir(), reminder.getReminderID() + fileExtension);
      
      if(f.isFile()){
        f.delete();
      }
      
      Log.i("RemindersHelper", "Saving reminder to " + f.getAbsolutePath());
      mapper.writeValue(f, reminder);
    } catch (JsonGenerationException e) {
      e.printStackTrace();
    } catch (JsonMappingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  
  public static Reminder getReminder(String reminderID){
    File f = new File(context.getFilesDir(), reminderID + fileExtension);
    
    /* Return null if the file doesn't exist */
    if(!f.isFile() || !f.canRead())
      return null;
    
    ObjectMapper mapper = new ObjectMapper();
    try {
      Reminder r = mapper.readValue(f, Reminder.class);
      return r;
    } catch (JsonParseException e) {
      e.printStackTrace();
    } catch (JsonMappingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    
    return null;
  }

  public static void deleteAllReminders() {
    File[] files = getAllReminderFiles();
    
    for(File f : files){
      f.delete();
    }
  }
  
  private static File[] getAllReminderFiles(){
    File[] files = context.getFilesDir().listFiles(new FileFilter() {
          @Override
          public boolean accept(File pathname) {
             return pathname.getName().endsWith(fileExtension);
          }
       });
    return files;
  }
}




Java Source Code List

android.UnusedStub.java
com.ganterd.travelreminder.CreateTravelReminderActivity.java
com.ganterd.travelreminder.Directions.java
com.ganterd.travelreminder.MainActivity.java
com.ganterd.travelreminder.Reminder.java
com.ganterd.travelreminder.RemindersHelper.java
com.ganterd.travelreminder.adapters.MainActivityReminderListAdapter.java
com.ganterd.travelreminder.fragments.ReminderEditArrivalTimePickerFragment.java
com.ganterd.travelreminder.fragments.ReminderEditFragment.java
com.ganterd.travelreminder.fragments.ReminderEditLocationInfo.java
com.ganterd.travelreminder.fragments.ReminderEditTravelInfo.java
com.ganterd.travelreminder.interfaces.IReminder.java