Android Open Source - opentraining Data Helper






From Project

Back to project page opentraining.

License

The source code is released under:

GNU General Public License

If you think the Android project opentraining 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 de.skubware.opentraining.db;
// w w w . ja  v a  2s . c  o  m
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.Log;

/**
 * A class to handle I/O stuff that is not related to database or parsing .xml
 * files.
 * 
 */
public class DataHelper {

  /** Tag for logging */
  private static final String TAG = "DataHelper";

  private Context mContext;

  /**
   * Constructor.
   * 
   * @param context
   *            The current context.
   */
  public DataHelper(Context context) {
    mContext = context;
  }

  /**
   * Returns a {@code Drawable} when an image with such a name is there (in
   * assets folder or the custom exercise folder).
   * 
   * @param name
   *            The name of the image
   * @return The generated Drawable or null if the image could not be found
   */
  public Drawable getDrawable(String name) {
    Log.v(TAG, "Trying to get drawable " + name);
    if(!drawableExist(name)){
      return null;
    }
    
    InputStream is = null;
    Drawable img = null;
    try {
      is = mContext.getAssets().open(DataProvider.EXERCISE_FOLDER + "/" + name);
      img = Drawable.createFromStream(is, "icon");
      is.close();
    } catch (FileNotFoundException e) {      
      if(img == null){
        // Could not find drawable in assets folder. Will try to find image in custom image folder
        img = Drawable.createFromPath(mContext.getFilesDir().toString() + "/" + IDataProvider.CUSTOM_IMAGES_FOLDER + "/" + name);
      }
      
      if(img == null){
        // Could not find drawable in assets or custom image folder. Will try to find image in synced image folder
        img = Drawable.createFromPath(mContext.getFilesDir().toString() + "/" + IDataProvider.SYNCED_IMAGES_FOLDER + "/" + name);
      }

      if(img == null){
        Log.e(TAG, "Could not find drawable in assets, custom image folder or in synced image folder: " + name + "\n", e);
      }
      
    } catch (IOException e) {
      Log.e(TAG, "Could not find drawable :" + name + "\n", e);
    }
  
    
    return img;
  }
  
  public boolean drawableExist(String name){
    // check default exercises
    boolean imageInDefaultFolderExists = false;
    try{
      // assets have to be treated special
      // e.g. there are only lower case filenames
      String[] defaultImages = mContext.getAssets().list(DataProvider.EXERCISE_FOLDER);
      if(java.util.Arrays.asList(defaultImages).contains(name)){
        imageInDefaultFolderExists = true;
      }  
    }catch (IOException e) {
      Log.e(TAG, "IOException during searching for drawable: " + name + "\n", e);
    }
    
    // check custom and synced images
    File imgInCustomFolder = new File(mContext.getFilesDir().toString() + "/" + DataProvider.CUSTOM_IMAGES_FOLDER + "/" + name);
    File imgInSyncedFolder = new File(mContext.getFilesDir().toString() + "/" + DataProvider.SYNCED_IMAGES_FOLDER + "/" + name);
    
    boolean exactlyOneExists = imageInDefaultFolderExists ^ imgInCustomFolder.exists() ^ imgInSyncedFolder.exists();
    boolean atLeastOneExists =  imageInDefaultFolderExists || imgInCustomFolder.exists() || imgInSyncedFolder.exists();
    if(atLeastOneExists && !exactlyOneExists){
      Log.wtf(TAG, "There seems to be more than one image with the name: " + name + ". This should not happen.");
    }
    if(!atLeastOneExists){
      Log.v(TAG, "Drawable " + name + " does not exist (yet).");
    }
    return atLeastOneExists;
  }
  
  
  /**
   * Copies the image with the Uri to the custom images folder.
   * 
   * @param source
   *            The Uri of the image to copy
   * 
   * @return The name of the created image
   */
  public String copyImageToCustomImageFolder(Uri source) {
    File destinationFolder = new File(mContext.getFilesDir().toString() + "/" + IDataProvider.CUSTOM_IMAGES_FOLDER);
    destinationFolder.mkdirs();
    
    String image_base_name = "img_";
    String image_name = image_base_name + "0.jpg";
    List<String> files = Arrays.asList(destinationFolder.list());
    for(Integer i = 0; files.contains(image_name); i++){
      image_name = image_base_name + i + ".jpg";
    }
    
    File destination  = new File(destinationFolder.toString() + "/"+ image_name);
    
    InputStream inputStream = null;
    OutputStream outputStream = null;

    try {
      ContentResolver content = mContext.getContentResolver();
      inputStream = content.openInputStream(source);

      outputStream = new FileOutputStream(destination);
      if (outputStream != null) {
        Log.e(TAG, "Output Stream Opened successfully");
      }

      byte[] buffer = new byte[1000];
      while ( inputStream.read(buffer, 0, buffer.length) >= 0) {
        outputStream.write(buffer, 0, buffer.length);
      }
      outputStream.close();
    } catch (Exception e) {
      Log.e(TAG, "Exception occurred during saving.", e);
      return null;
    }
    
    Log.d(TAG, "Copied image to " + destination.toString());
    
    return image_name;
  }
  

  /**
   * Loads a file from the raw folder.
   * 
   * @param fileName
   *            The name/path of the file
   * 
   * @return String with the content of the file
   * 
   * @throws IOException
   *             if loading file fails
   */
  public String loadFileFromRaw(String fileName) throws IOException {
    Resources resources = mContext.getResources();

    // Create a InputStream to read the file into
    int rID = resources.getIdentifier("de.skubware.opentraining:raw/" + fileName, null, null);

    // get the file as a stream
    InputStream is = resources.openRawResource(rID);

    return loadFile(is);
  }

  /**
   * Loads a file from the assets folder.
   * 
   * @param fileName
   *            The name/path of the file
   * 
   * @return String with the content of the file
   * 
   * @throws IOException
   *             if loading file fails
   */
  public String loadFileFromAssets(String fileName) throws IOException {
    Resources resources = mContext.getResources();
    InputStream is = resources.getAssets().open(fileName);
    return loadFile(is);
  }

  /**
   * Loads a file from the assets folder.
   * 
   * @param fileName
   *            The name/path of the file
   * 
   * @return String with the content of the file
   * 
   * @throws IOException
   *             if loading file fails
   */
  public String loadFileFromFileSystem(String fileName) throws IOException {
    InputStream is = new FileInputStream(new File(fileName));
    return loadFile(is);
  }

  /**
   * Loads the InputStream.
   * 
   * @param is
   *            The InputStream to read
   * 
   * @return String with the content of the file
   * 
   * @throws IOException
   *             if loading file fails
   */
  public String loadFile(InputStream is) throws IOException {
    // create a buffer that has the same size as the InputStream
    byte[] buffer = new byte[is.available()];
    is.read(buffer);
    ByteArrayOutputStream oS = new ByteArrayOutputStream();
    oS.write(buffer);
    oS.close();
    is.close();

    // return the output stream as a String
    return oS.toString();
  }

}




Java Source Code List

at.technikum.mti.fancycoverflow.FancyCoverFlowAdapter.java
at.technikum.mti.fancycoverflow.FancyCoverFlowItemWrapper.java
at.technikum.mti.fancycoverflow.FancyCoverFlow.java
de.skubware.opentraining.activity.ChangeLogDialog.java
de.skubware.opentraining.activity.DisclaimerDialog.java
de.skubware.opentraining.activity.MainActivity.java
de.skubware.opentraining.activity.NavigationGalleryAdapter.java
de.skubware.opentraining.activity.SelectWorkoutDialogFragment.java
de.skubware.opentraining.activity.WhatsNewDialog.java
de.skubware.opentraining.activity.acra.ACRACrashReportMailer.java
de.skubware.opentraining.activity.acra.ACRAFeedbackMailer.java
de.skubware.opentraining.activity.acra.OpenTrainingApplication.java
de.skubware.opentraining.activity.acra.RequestExerciseUpdate.java
de.skubware.opentraining.activity.create_exercise.CreateExerciseActivity.java
de.skubware.opentraining.activity.create_exercise.CustomSpinner.java
de.skubware.opentraining.activity.create_exercise.DescriptionFragment.java
de.skubware.opentraining.activity.create_exercise.EditImageMetadataDialog.java
de.skubware.opentraining.activity.create_exercise.EquipmentDataFragment.java
de.skubware.opentraining.activity.create_exercise.ExerciseImageListAdapter.java
de.skubware.opentraining.activity.create_exercise.ImageFragment.java
de.skubware.opentraining.activity.create_exercise.MuscleDataFragment.java
de.skubware.opentraining.activity.create_exercise.NameFragment.java
de.skubware.opentraining.activity.create_exercise.SimpleDataFragment.java
de.skubware.opentraining.activity.create_exercise.SpinnerDataFragment.java
de.skubware.opentraining.activity.create_workout.DialogFilterMusclesAndEquipment.java
de.skubware.opentraining.activity.create_workout.DialogWorkoutOverviewFragment.java
de.skubware.opentraining.activity.create_workout.ExerciseDetailOnGestureListener.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeDetailActivity.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeDetailFragment.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeListActivity.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeListAdapter.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeListFragment.java
de.skubware.opentraining.activity.create_workout.SelectMuscleDialog.java
de.skubware.opentraining.activity.create_workout.SendExerciseFeedbackDialogFragment.java
de.skubware.opentraining.activity.create_workout.upload_exercise.ExerciseImage.java
de.skubware.opentraining.activity.create_workout.upload_exercise.UploadExerciseAsyncTask.java
de.skubware.opentraining.activity.create_workout.upload_exercise.UploadExerciseImagesAsyncTask.java
de.skubware.opentraining.activity.create_workout.upload_exercise.WgerRestService.java
de.skubware.opentraining.activity.manage_workouts.RenameWorkoutDialogFragment.java
de.skubware.opentraining.activity.manage_workouts.WorkoutDetailActivity.java
de.skubware.opentraining.activity.manage_workouts.WorkoutDetailFragment.java
de.skubware.opentraining.activity.manage_workouts.WorkoutListActivity.java
de.skubware.opentraining.activity.manage_workouts.WorkoutListFragment.java
de.skubware.opentraining.activity.settings.LicenseDialog.java
de.skubware.opentraining.activity.settings.SettingsActivity.java
de.skubware.opentraining.activity.settings.sync.OpenTrainingSyncResultReceiver.java
de.skubware.opentraining.activity.settings.sync.OpenTrainingSyncService.java
de.skubware.opentraining.activity.settings.sync.RestClient.java
de.skubware.opentraining.activity.settings.sync.SyncFinishedDialog.java
de.skubware.opentraining.activity.settings.sync.WgerImageDownloader.java
de.skubware.opentraining.activity.settings.sync.WgerJSONParser.java
de.skubware.opentraining.activity.start_training.DialogFragmentAddEntry.java
de.skubware.opentraining.activity.start_training.DialogFragmentTrainingEntryTable.java
de.skubware.opentraining.activity.start_training.FExDetailActivity.java
de.skubware.opentraining.activity.start_training.FExDetailFragment.java
de.skubware.opentraining.activity.start_training.FExListActivity.java
de.skubware.opentraining.activity.start_training.FExListAdapter.java
de.skubware.opentraining.activity.start_training.FExListFragment.java
de.skubware.opentraining.activity.start_training.RecoveryTimerManager.java
de.skubware.opentraining.activity.start_training.SwipeDismissListViewTouchListener.java
de.skubware.opentraining.activity.start_training.SwipeDismissTouchListener.java
de.skubware.opentraining.activity.start_training.TrainingEntryListAdapter.java
de.skubware.opentraining.basic.ActivationLevel.java
de.skubware.opentraining.basic.ExerciseTag.java
de.skubware.opentraining.basic.ExerciseType.java
de.skubware.opentraining.basic.FSet.java
de.skubware.opentraining.basic.FitnessExercise.java
de.skubware.opentraining.basic.IExercise.java
de.skubware.opentraining.basic.License.java
de.skubware.opentraining.basic.Muscle.java
de.skubware.opentraining.basic.SportsEquipment.java
de.skubware.opentraining.basic.TrainingEntry.java
de.skubware.opentraining.basic.Translatable.java
de.skubware.opentraining.basic.Workout.java
de.skubware.opentraining.db.Cache.java
de.skubware.opentraining.db.DataHelper.java
de.skubware.opentraining.db.DataProvider.java
de.skubware.opentraining.db.IDataProvider.java
de.skubware.opentraining.db.parser.AbstractJSONParser.java
de.skubware.opentraining.db.parser.ExerciseTagJSONParser.java
de.skubware.opentraining.db.parser.ExerciseTypeXMLParser.java
de.skubware.opentraining.db.parser.IParser.java
de.skubware.opentraining.db.parser.MuscleJSONParser.java
de.skubware.opentraining.db.parser.SportsEquipmentJSONParser.java
de.skubware.opentraining.db.parser.WorkoutXMLParser.java
de.skubware.opentraining.db.parser.XMLSaver.java
de.skubware.opentraining.db.rest.ExerciseImageGSONSerializer.java
de.skubware.opentraining.db.rest.ExerciseTypeGSONSerializer.java
de.skubware.opentraining.db.rest.LanguageGSONDeserializer.java
de.skubware.opentraining.db.rest.MuscleGSONDeserializer.java
de.skubware.opentraining.db.rest.ServerModel.java
de.skubware.opentraining.db.rest.SportsEquipmentGSONDeserializer.java
de.skubware.opentraining.exporter.HTMLExporter.java
de.skubware.opentraining.exporter.LaTeXExporter.java
de.skubware.opentraining.exporter.WorkoutExporter.java