From b2ea4070699a33503b32d6311a07c9986953058d Mon Sep 17 00:00:00 2001 From: "Wood, Jacob G" Date: Tue, 15 Jun 2021 10:46:27 -0400 Subject: [PATCH 01/10] Created button that simply checks if a file exists and creates and writes to it if not. --- .../activities/MainActivity.java | 17 +++++++++- .../models/Weapon.java | 34 ++++++++++++++++++- app/src/main/res/layout/activity_main.xml | 8 +++++ app/src/main/res/values-fr/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 5 files changed, 59 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/borderlandsdpscalculator/borderlands3dpscalculator/activities/MainActivity.java b/app/src/main/java/com/borderlandsdpscalculator/borderlands3dpscalculator/activities/MainActivity.java index 2799b43..fc05420 100644 --- a/app/src/main/java/com/borderlandsdpscalculator/borderlands3dpscalculator/activities/MainActivity.java +++ b/app/src/main/java/com/borderlandsdpscalculator/borderlands3dpscalculator/activities/MainActivity.java @@ -3,6 +3,7 @@ import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.text.Editable; @@ -30,6 +31,7 @@ public class MainActivity extends AppCompatActivity { TextView mTextViewDPSSustained; Button mButton; + Button mButtonSaveWeapon; private Weapon mWeapon = new Weapon(); @@ -51,6 +53,7 @@ protected void onCreate(Bundle savedInstanceState) { mTextViewTimeSpentReloading = findViewById(R.id.textViewTimeSpentReloading); mButton = findViewById(R.id.autoScanButton); + mButtonSaveWeapon = findViewById(R.id.saveWeapon); initListeners(); } @@ -86,6 +89,13 @@ public void onClick(View v) { startActivityForResult(intent, 1); } }); + + mButtonSaveWeapon.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + saveWeapon(); + } + }); } @Override @@ -105,7 +115,8 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { } private void updateWeapon() { - if (!mEditTextDamage.getText().toString().equals("") && !mEditTextReloadTime.getText().toString().equals("") && !mEditTextFireRate.getText().toString().equals("") && !mEditTextMagazineSize.getText().toString().equals("")) { + if (!mEditTextDamage.getText().toString().equals("") && !mEditTextReloadTime.getText().toString().equals("") + && !mEditTextFireRate.getText().toString().equals("") && !mEditTextMagazineSize.getText().toString().equals("")) { int damage = Integer.parseInt(mEditTextDamage.getText().toString()); float reloadTime = Float.parseFloat(mEditTextReloadTime.getText().toString()); float fireRate = Float.parseFloat(mEditTextFireRate.getText().toString()); @@ -129,4 +140,8 @@ protected void updateTextViews() { mTextViewTimeSpentReloading.setText(String.valueOf(mWeapon.getTimeSpentReloading())); mTextViewTimeSpentShooting.setText(String.valueOf(mWeapon.getTimeSpentShooting())); } + + private void saveWeapon(){ + mWeapon.saveWeapon(getApplicationContext()); + } } diff --git a/app/src/main/java/com/borderlandsdpscalculator/borderlands3dpscalculator/models/Weapon.java b/app/src/main/java/com/borderlandsdpscalculator/borderlands3dpscalculator/models/Weapon.java index 8c763b7..8bbdb48 100644 --- a/app/src/main/java/com/borderlandsdpscalculator/borderlands3dpscalculator/models/Weapon.java +++ b/app/src/main/java/com/borderlandsdpscalculator/borderlands3dpscalculator/models/Weapon.java @@ -1,5 +1,11 @@ package com.borderlandsdpscalculator.borderlands3dpscalculator.models; +import android.content.Context; +import android.util.Log; + +import java.io.File; +import java.io.IOException; +import java.io.OutputStreamWriter; import java.io.Serializable; import androidx.annotation.NonNull; @@ -19,7 +25,7 @@ public Weapon() { @Override public String toString() { return "damage " + mDamage + ", accuracy " + mAccuracy + ", handling " - + mHandling + ", reloadTime " + mReloadTime + ", fireRate" + mFireRate + ", magazineSize" + mMagazineSize; + + mHandling + ", reloadTime " + mReloadTime + ", fireRate " + mFireRate + ", magazineSize" + mMagazineSize; } public Weapon(int damage, float accuracy, int handling, float reloadTime, float fireRate, int magazineSize) { @@ -121,6 +127,32 @@ public float getDPSSustained() { return mDamage * mMagazineSize / (mMagazineSize / mFireRate + mReloadTime); } + private void writeToFile(String data,Context context) { + try { + OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("weapons.txt", Context.MODE_PRIVATE)); + outputStreamWriter.write(data); + outputStreamWriter.close(); + } + catch (IOException e) { + Log.e("Exception", "File write failed: " + e.toString()); + } + } + // code to handle saving the weapon stats to a file. + public void saveWeapon(Context context) { + File file = new File(context.getFilesDir(), "weapons.txt"); + if(file.exists()){ + Log.d("File Exists", "True"); + if (!file.canRead()) + Log.d("File Log:", "File is read-restricted"); + } + else { + writeToFile("success", context); + Log.d("File Exists", "False"); + Log.d("File Location", String.valueOf(file)); + } + + } + private int mDamage; private float mAccuracy; private int mHandling; diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 97a2991..0eb611b 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -137,6 +137,14 @@ android:layout_height="wrap_content" android:text="@string/auto_mode" /> +