Skip to content
Snippets Groups Projects
Commit 4dd6d620 authored by ect1u17's avatar ect1u17
Browse files

Daily Commit

Changes:
- Implemented a button to reset the boxJSONData
- Implemented the addition to the boxJSON
parent 2d786d26
No related branches found
No related tags found
No related merge requests found
......@@ -21,9 +21,7 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import static android.util.Log.d;
public class BoxesActivity extends AppCompatActivity {
......@@ -53,27 +51,29 @@ public class BoxesActivity extends AppCompatActivity {
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
LinearLayout inFiveLayout = findViewById(R.id.BoxesInFiveLayout);
// JSON loading from local directory, could be implemented to stream from online
InputStream inputStream = getResources().openRawResource(R.raw.box);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
JSONArray boxes = new JSONArray();
loadAndDisplayJSON();
backHomeButton = findViewById(R.id.BoxesBackHomeButton);
backHomeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
protected void loadAndDisplayJSON(){
LinearLayout inFiveLayout = findViewById(R.id.BoxesInFiveLayout);
JSONArray boxes;
final String[] inFive = {"Move", "Eat", "Engage", "Cogitate", "Sleep"};
int ctr;
// Try, in order to avoid errors in compilation and catastrophic errors
try {
ctr = inputStream.read();
while (ctr != -1) {
byteArrayOutputStream.write(ctr);
ctr = inputStream.read();
}
inputStream.close();
d("boxesParse", FileStoreReader.readFile(this, "boxData.json"));
boxes = new JSONArray(byteArrayOutputStream.toString());
boxes = new JSONArray(FileStoreReader.readFile(this, "boxData.json"));
LinearLayout[] inFiveLayoutArray = new LinearLayout[5];
......@@ -154,17 +154,6 @@ public class BoxesActivity extends AppCompatActivity {
}
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
backHomeButton = findViewById(R.id.BoxesBackHomeButton);
backHomeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
......@@ -75,12 +75,7 @@ public class ExperimentSurveyCreatorActivity extends AppCompatActivity {
openPage.putExtra("ExperimentIndex", 0);
if(getIntent().getIntExtra("GroupIndex", 0)+1 >= boxInfo.getJSONArray("Experiments").length()){
d("sub", "end of json check 1");
// do the thing with saving into the current JSON, then return to the box page and show the new file (close every previous page if possible and launch new activity)
Intent intent = new Intent(getApplicationContext(), BoxesActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
allSurveyCompleted(boxInfo);
} else{
d("sub", "end of group");
openPage.putExtra("GroupIndex", getIntent().getIntExtra("GroupIndex", 0)+1);
......@@ -89,9 +84,7 @@ public class ExperimentSurveyCreatorActivity extends AppCompatActivity {
} else if (getIntent().getIntExtra("GroupIndex", 0)+1 >= boxInfo.getJSONArray("Experiments").length()) {
// Should never enter this check, but edge case just incase of bug, so run function just the same
d("sub", "end of json check 2");
openPage.putExtra("ExperimentIndex", getIntent().getIntExtra("ExperimentIndex", 0));
openPage.putExtra("GroupIndex", getIntent().getIntExtra("GroupIndex", 0));
startActivity(openPage);
allSurveyCompleted(boxInfo);
} else {
d("sub", "remain in the same group");
openPage.putExtra("ExperimentIndex", getIntent().getIntExtra("ExperimentIndex", 0) + 1);
......@@ -104,7 +97,7 @@ public class ExperimentSurveyCreatorActivity extends AppCompatActivity {
}
});
if(!boxInfo.getString("Image").isEmpty()){
if(!boxInfo.getString("Image").matches("null")){
Drawable yourDrawable;
InputStream inputStream = getContentResolver().openInputStream(Uri.parse(boxInfo.getString("Image")));
yourDrawable = Drawable.createFromStream(inputStream, boxInfo.getString("Image"));
......@@ -192,4 +185,27 @@ public class ExperimentSurveyCreatorActivity extends AppCompatActivity {
});
}
private void allSurveyCompleted(JSONObject boxInfo) {
// do the thing with saving into the current JSON, then return to the box page and show the new file (close every previous page if possible and launch new activity)
// Try, in order to avoid errors in compilation and catastrophic errors
JSONArray boxes;
try {
boxes = new JSONArray(FileStoreReader.readFile(ExperimentSurveyCreatorActivity.this, "boxData.json"));
boxes.put(boxInfo);
d("UpdatedJSON", boxes.toString());
FileStoreReader.saveFile(ExperimentSurveyCreatorActivity.this, "boxData.json", boxes.toString());
} catch (Exception e){
e.printStackTrace();
}
Intent intent = new Intent(getApplicationContext(), BoxesActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
......@@ -4,11 +4,12 @@ import android.content.Context;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class FileStoreReader {
......@@ -36,31 +37,24 @@ public class FileStoreReader {
}
public static String readFile(Context intentContext, String title){
String ret = "";
public static String readFile(Context context, String filename) {
try {
InputStream inputStream = intentContext.openFileInput(title);
if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
FileInputStream fis = context.openFileInput(filename);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line).append("\n");
}
return sb.toString();
} catch (FileNotFoundException e) {
return "";
} catch (UnsupportedEncodingException e) {
return "";
} catch (IOException e) {
e.printStackTrace();
return "";
}
return ret;
}
......
......@@ -6,10 +6,13 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.icu.text.SimpleDateFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
......@@ -22,6 +25,7 @@ import androidx.core.content.ContextCompat;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Random;
import static android.util.Log.d;
......@@ -31,6 +35,19 @@ public class MainActivity extends AppCompatActivity {
private Button homeButton, loginButton, shopButton, experimentsButton, progressButton, aboutButton;
private Toolbar toolbar;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem createBox = menu.add("Reset JSON!");
createBox.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
resetBoxJSON();
return false;
}
});
return true;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -43,33 +60,11 @@ public class MainActivity extends AppCompatActivity {
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
SharedPreferences savedSettings = this.getSharedPreferences("com.yearthreeproject.xbframework.PREFERENCE_FILE_KEY", Context.MODE_PRIVATE);
SharedPreferences.Editor savedSettingsEditor = savedSettings.edit();
askForWriteAccess();
// Load Box Json and initialise to internal storage
InputStream inputStream = getResources().openRawResource(R.raw.box);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
String boxesData = "";
int ctr;
try {
ctr = inputStream.read();
while (ctr != -1) {
byteArrayOutputStream.write(ctr);
ctr = inputStream.read();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
testingDateAndTime();
}
inputStream.close();
boxesData = byteArrayOutputStream.toString();
} catch (Exception e){
e.printStackTrace();
}
FileStoreReader.saveFile(this, "boxData.json", boxesData);
d("file", FileStoreReader.readFile(this, "boxData.json"));
createSalt();
homeButton = findViewById(R.id.HomeHomeButton);
loginButton = findViewById(R.id.HomeLoginButton);
......@@ -78,23 +73,6 @@ public class MainActivity extends AppCompatActivity {
progressButton = findViewById(R.id.HomeProgressButton);
aboutButton = findViewById(R.id.HomeAboutButton);
if (savedSettings.getString("salt", "").equals("")) {
Random saltGenerator = new Random();
StringBuilder saltString = new StringBuilder();
Character iterativeCharacter;
for (int i = 0; i < 10; i++) {
iterativeCharacter = Integer.toString(saltGenerator.nextInt(96) + 32).charAt(0);
saltString.append(iterativeCharacter);
}
savedSettingsEditor.putString("Salt", saltString.toString());
savedSettingsEditor.apply();
} else {
d("Salt", savedSettings.getString("Salt", ""));
}
homeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
d("testing", "homeButton clicked");
......@@ -138,6 +116,117 @@ public class MainActivity extends AppCompatActivity {
}
private void createSalt(){
SharedPreferences savedSettings = this.getSharedPreferences("com.yearthreeproject.xbframework.PREFERENCE_FILE_KEY", Context.MODE_PRIVATE);
SharedPreferences.Editor savedSettingsEditor = savedSettings.edit();
if (savedSettings.getString("salt", "").equals("")) {
Random saltGenerator = new Random();
StringBuilder saltString = new StringBuilder();
Character iterativeCharacter;
for (int i = 0; i < 10; i++) {
iterativeCharacter = Integer.toString(saltGenerator.nextInt(96) + 32).charAt(0);
saltString.append(iterativeCharacter);
}
savedSettingsEditor.putString("Salt", saltString.toString());
savedSettingsEditor.apply();
} else {
d("Salt", savedSettings.getString("Salt", ""));
}
}
@RequiresApi(api = Build.VERSION_CODES.N)
private void testingDateAndTime(){
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
String dateToday = simpleDateFormat.format(calendar.getTime());
d("dateToday", dateToday);
Integer dateTodayYear = 1000*Character.getNumericValue(dateToday.codePointAt(0))+100*Character.getNumericValue(dateToday.codePointAt(1))+10*Character.getNumericValue(dateToday.codePointAt(2))+Character.getNumericValue(dateToday.codePointAt(3));
calendar.add(Calendar.DATE, 5);
String dateLater = simpleDateFormat.format(calendar.getTime());
d("dateDeadline", dateLater);
Integer dateLaterYear = 1000*Character.getNumericValue(dateLater.codePointAt(0))+100*Character.getNumericValue(dateLater.codePointAt(1))+10*Character.getNumericValue(dateLater.codePointAt(2))+Character.getNumericValue(dateLater.codePointAt(3));
d("dateTest", dateTodayYear.toString());
d("dateTest", dateLaterYear.toString());
if(dateTodayYear <= dateLaterYear){
d("dateTest", "Release from experiment!");
simpleDateFormat = new SimpleDateFormat("mm");
dateToday = simpleDateFormat.format(calendar.getTime());
d("dateToday", dateToday);
Integer dateTodayMonth = 10*Character.getNumericValue(dateToday.codePointAt(0))+Character.getNumericValue(dateToday.codePointAt(1));
calendar.add(Calendar.DATE, 5);
dateLater = simpleDateFormat.format(calendar.getTime());
d("dateDeadline", dateLater);
Integer dateLaterMonth = 10*Character.getNumericValue(dateLater.codePointAt(0))+Character.getNumericValue(dateLater.codePointAt(1));
d("dateTest", dateTodayMonth.toString());
d("dateTest", dateLaterMonth.toString());
if(dateTodayMonth <= dateLaterMonth){
d("dateTest", "Release from experiment!");
simpleDateFormat = new SimpleDateFormat("dd");
dateToday = simpleDateFormat.format(calendar.getTime());
d("dateToday", dateToday);
Integer dateTodayDay = 10*Character.getNumericValue(dateToday.codePointAt(0))+Character.getNumericValue(dateToday.codePointAt(1));
calendar.add(Calendar.DATE, 5);
dateLater = simpleDateFormat.format(calendar.getTime());
d("dateDeadline", dateLater);
Integer dateLaterDay = 10*Character.getNumericValue(dateLater.codePointAt(0))+Character.getNumericValue(dateLater.codePointAt(1));
d("dateTest", dateTodayDay.toString());
d("dateTest", dateLaterDay.toString());
if(dateTodayDay <= dateLaterDay){
d("dateTest", "Release from experiment!");
}
}
}
}
private void resetBoxJSON() {
// Load Box Json and initialise to internal storage
InputStream inputStream = getResources().openRawResource(R.raw.box);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
String boxesData = "";
int ctr;
try {
ctr = inputStream.read();
while (ctr != -1) {
byteArrayOutputStream.write(ctr);
ctr = inputStream.read();
}
inputStream.close();
boxesData = byteArrayOutputStream.toString();
} catch (Exception e){
e.printStackTrace();
}
FileStoreReader.saveFile(this, "boxData.json", boxesData);
d("file", FileStoreReader.readFile(this, "boxData.json"));
}
// Image permission check
private void askForWriteAccess() {
if (ContextCompat.checkSelfPermission(MainActivity.this,
......
......@@ -209,10 +209,10 @@ public class NewBox extends AppCompatActivity {
tempEditTextForStringRetrieval = findViewById(R.id.BlurbEditText);
box.put("Blurb", tempEditTextForStringRetrieval.getText().toString());
box.put("Locked", true);
box.put("Locked", false); // TODO: change back to true when requesting a new box
if (imageUploaded) box.put("Image", imageUploadResultUri);
else box.put("Image", "");
else box.put("Image", JSONObject.NULL);
tempEditTextForStringRetrieval = findViewById(R.id.DescriptionEditText);
box.put("Description", tempEditTextForStringRetrieval.getText().toString());
......
......@@ -69,7 +69,7 @@
"Instructions": "Null for now",
"Description:": "Null for now"
},{
"Title": "Limit yourself to 4 cups of coffees per day; 10 cans of coke/fanta; 2 energy drinks",
"Title": "Limit yourself to 4 cups of coffees per day; 10 cans of Coke/ Fanta; 2 energy drinks",
"Instructions": "Null for now",
"Description:": "Null for now"
},{
......
......@@ -2,9 +2,9 @@
<resources>
<string-array name="meecs" >
<item>Select a MEECS...</item>
<item>Movement</item>
<item>Eating</item>
<item>Engaging</item>
<item>Move</item>
<item>Eat</item>
<item>Engage</item>
<item>Cogitate</item>
<item>Sleep</item>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment