package com.twolf.stressball11; import android.content.Context; import android.content.SharedPreferences; import android.graphics.Color; import android.graphics.PorterDuff; import android.graphics.drawable.LayerDrawable; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.EditText; import android.widget.RatingBar; import android.widget.Toast; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Calendar; public class Log extends AppCompatActivity { EditText journalData; RatingBar userRating; /****************************************************************************************** * This class allows the user to write a little about their day or week. Their is no alarm * to remind them to do this. This is completely up to the user if she/he wants to log something * that may be helpful to remember or reflect on what changed their mood. This way the user * can keep track of and maybe start finding patterns in their stress levels based on daily * activity. * * This actually uses 2 file locations in saved preferences one to save all the names of the * files for the spinner. The other uses the file name to store the text data from the journal * itself. * * Also the spinner needs to know how many files it is looking to fill the spinner with so * their is a shared pref to keep track of the number of times the save button was hit. * -Thomas Wolf * * Help From: * https://www.youtube.com/watch?v=D8jSGCkIltM&feature=youtu.be saving to internal storage * ****saving files to internal storage * http://stackoverflow.com/questions/32810341/android-change-color-of-ratingbar-to-golden ****Used to change color of rating bar. * */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_log); RatingBar ratingBar=(RatingBar) findViewById(R.id.ratingBar); LayerDrawable stars= (LayerDrawable) ratingBar.getProgressDrawable(); stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP); journalData= (EditText)findViewById(R.id.journalText); userRating=(RatingBar) findViewById(R.id.ratingBar); } /*********************************************************************************** * Function Name: saveJournal(View v) * Purpose: Saves the text and rating from the user input into a file marked by a timestamp * to avoid multiple saved files with same name. This information is private and can * only be accesed by this application. * Usage: When the user wishes to save how their day was into the apps internal storage */ public void saveJournal(View v) { String message= "Mood Rating: "+userRating.getRating()+" Star/s"+'\n'+ journalData.getText().toString(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar c= Calendar.getInstance(); String fName= sdf.format(c.getTime()); String fData = fName+'\n'; try { // save journal entry to file //if the file name does not exist it will create the file FileOutputStream fileOutputStream = openFileOutput(fName,MODE_PRIVATE); fileOutputStream.write(message.getBytes()); fileOutputStream.close(); //Save the list of file names fileOutputStream = openFileOutput("savedDates",MODE_APPEND | MODE_PRIVATE); fileOutputStream.write(fData.getBytes()); fileOutputStream.close(); //update the number of saved files that are in the savedDates file SharedPreferences preferences = getSharedPreferences("numberSaves", Context.MODE_PRIVATE);//ONLY THIS APP CAN USE SharedPreferences.Editor editor = preferences.edit(); int i = preferences.getInt("saves",1);//if file was not found will return i++; editor.putInt("saves", i );// record the number of saved files to fill the spinner editor.apply(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } journalData.setText(""); Toast.makeText(this,"Information saved",Toast.LENGTH_LONG).show(); } }