Android Open Source - WindNow Load Save Ops






From Project

Back to project page WindNow.

License

The source code is released under:

GNU General Public License

If you think the Android project WindNow 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.windnow;
//from  w w w . ja  v a  2s . c  o  m
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.res.AssetManager;
import android.media.MediaScannerConnection;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.util.Log;

/**
 * 
 * This Class is part of WindNow.
 * 
 * It is responsible for writing and loading the stations file. Also for writing
 * the error log.
 * 
 * @author Florian Hauser Copyright (C) 2014
 * 
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version.
 * 
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY; without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details.
 * 
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

@SuppressLint("SimpleDateFormat")
public class LoadSaveOps {
  public static String userDir = PreferenceManager.getDefaultSharedPreferences(
      OnlyContext.getContext()).getString("user_dir", "WindNow");;
  private static File localDir = new File(Environment
      .getExternalStorageDirectory().getAbsolutePath() + "/" + userDir);
  private static File stationsFile = new File(Environment
      .getExternalStorageDirectory().getAbsolutePath()
      + "/"
      + userDir
      + "/" + "stations.txt");
  private static File errorLogFile = new File(Environment
      .getExternalStorageDirectory().getAbsolutePath()
      + "/"
      + userDir
      + "/" + "errorlog.txt");

  private static String sep = ";";

  public static ArrayList<Station> loadStations() {
    ArrayList<Station> stations = new ArrayList<Station>();
    try {
      mkLocalDir();
      BufferedReader br = null;
      boolean saveFile = !stationsFile.exists();
      if (saveFile) {
        AssetManager assetManager = OnlyContext.getContext()
            .getAssets();
        br = new BufferedReader(new InputStreamReader(
            assetManager.open("stations.txt")));
      } else {
        br = new BufferedReader(new FileReader(stationsFile));
      }

      String line;
      while ((line = br.readLine()) != null) {
        String[] arr = line.split(sep);
        if (arr.length > 1)
          stations.add(new Station(arr[0], arr[1]));
      }
      br.close();
      if (saveFile) {
        saveStations(stations);
      }
    } catch (IOException e) {
      printErrorToLog(e);
    }
    return stations;
  }

  public static void saveStations(ArrayList<Station> stations) {
    try {
      mkLocalDir();
      FileOutputStream fos = new FileOutputStream(stationsFile);
      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
      for (Station station : stations) {
        bw.write(station.getName() + sep + station.getUrl());
        bw.newLine();
      }
      bw.close();
      MediaScannerConnection.scanFile(OnlyContext.getContext(), new String[] { stationsFile.getAbsolutePath() }, null, null);
    } catch (IOException e) {
      printErrorToLog(e);
    }
  }

  static void printErrorToLog(Exception e) {
    try {
      mkLocalDir();
      boolean append = false;
      SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
      if (fmt.format(new Date(errorLogFile.lastModified())).equals(fmt.format(new Date()))) {
        append = true;
      }
      String line = Arrays.toString(e.getStackTrace());
      FileOutputStream fos = new FileOutputStream(errorLogFile, append);
      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
      bw.write(sdf.format(new Date()) + "-------------");
      bw.newLine();
      bw.write(e.toString());
      bw.newLine();
      bw.write(line);
      bw.newLine();
      bw.close();
      MediaScannerConnection.scanFile(OnlyContext.getContext(), new String[] { errorLogFile.getAbsolutePath() }, null, null);
    } catch (IOException ex) {
      Log.e("Error writing errorLog", ex.toString());
    }
    
  }
  
  private static void mkLocalDir() {
    if (!localDir.exists()) {
      localDir.mkdirs();
    }
    MediaScannerConnection.scanFile(OnlyContext.getContext(), new String[] { localDir.getAbsolutePath() }, null, null);
  }

}




Java Source Code List

com.windnow.AboutDialog.java
com.windnow.HelpActivity.java
com.windnow.LoadSaveOps.java
com.windnow.MainActivity.java
com.windnow.OnlyContext.java
com.windnow.SettingsActivity.java
com.windnow.StationListAdapter.java
com.windnow.StationPicActivity.java
com.windnow.StationTextActivity.java
com.windnow.Station.java