Android Open Source - generalsbuzz Store Data Task






From Project

Back to project page generalsbuzz.

License

The source code is released under:

MIT License

If you think the Android project generalsbuzz 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 me.kworden.wlcalendar2.async;
//from  w w  w. j a  v  a  2s  . c om
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import me.kworden.wlcalendar2.util.MD5;

import android.content.Context;
import android.os.AsyncTask;

public class StoreDataTask extends AsyncTask<String, Void, Boolean>
{
  private Context m_context;
  
  public StoreDataTask(Context p_context)
  {
    m_context = p_context;
  }
  
  /**
   * p_args[0] = filename
   * p_args[1] = data
   * @return 
   */
  @Override
  protected Boolean doInBackground(String ... p_args)
  {
    try
    {
      boolean t_update = false;
      
      if(localFileExists(p_args[0]))
      {
        // Is the current local data equal to the data we're passing in? //
        if(!MD5.areSame(loadFile(p_args[0]), p_args[1]))
          t_update = true;
      }
      else
        t_update = true;
      
      if(t_update)
      {
        FileOutputStream t_fos = m_context.openFileOutput(p_args[0], Context.MODE_PRIVATE);
        t_fos.write(p_args[1].getBytes());
        t_fos.flush();
        t_fos.close();
      }
      
      return t_update;
    }
    catch(Exception err)
    {
      err.printStackTrace();
    }
    
    return false;
  }
  
  @Override
  protected void onPostExecute(Boolean p_update)
  {
    super.onPostExecute(p_update);
    
    if(p_update)
      System.out.println("Successfully wrote to file.");
    else
      System.out.println("Did not write out to file. Hashes match.");
  }
  
  private String loadFile(String p_file_name) throws IOException
  {
    String i_read;
      StringBuffer t_buffer = new StringBuffer();
    FileInputStream t_fis = m_context.openFileInput(p_file_name);
    BufferedReader t_reader = new BufferedReader(new InputStreamReader(t_fis));
    
      if(t_fis != null)
      {                            
          while((i_read = t_reader.readLine()) != null)
              t_buffer.append(i_read + "\n" );
      }       
    t_fis.close();
    
    return t_buffer.toString();
  }
  
  private boolean localFileExists(String p_file_name)
  {
    return m_context.getFileStreamPath(p_file_name).exists();
  }
}




Java Source Code List

me.kworden.wlcalendar2.BuildConfig.java
me.kworden.wlcalendar2.BuildConfig.java
me.kworden.wlcalendar2.MainActivity.java
me.kworden.wlcalendar2.SettingsActivity.java
me.kworden.wlcalendar2.adapter.EventAdapter.java
me.kworden.wlcalendar2.async.CheckLocalDataTask.java
me.kworden.wlcalendar2.async.ParseDataTask.java
me.kworden.wlcalendar2.async.ReadLocalDataTask.java
me.kworden.wlcalendar2.async.RequestRemoteDataTask.java
me.kworden.wlcalendar2.async.StoreDataTask.java
me.kworden.wlcalendar2.fragment.EventDisplayFragment.java
me.kworden.wlcalendar2.fragment.EventInfoFragment.java
me.kworden.wlcalendar2.fragment.SettingsFragment.java
me.kworden.wlcalendar2.receiver.CheckLocalDataReceiver.java
me.kworden.wlcalendar2.receiver.RequestRemoteDataReceiver.java
me.kworden.wlcalendar2.receiver.UpdateRowsReceiver.java
me.kworden.wlcalendar2.struct.MonthYearParcel.java
me.kworden.wlcalendar2.struct.WLEventClassifier.java
me.kworden.wlcalendar2.struct.WLEventLinebreak.java
me.kworden.wlcalendar2.struct.WLEvent.java
me.kworden.wlcalendar2.struct.WLHeader.java
me.kworden.wlcalendar2.util.APP.java
me.kworden.wlcalendar2.util.BROADCAST.java
me.kworden.wlcalendar2.util.MD5.java
me.kworden.wlcalendar2.util.PATTERNS.java
me.kworden.wlcalendar2.util.STATUS.java