Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //AmUtil.java
- package com.joythis.android.motivator;
- import android.app.Activity;
- import android.util.Log;
- import android.widget.Toast;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.nio.charset.StandardCharsets;
- import java.util.Random;
- public class AmUtil {
- Activity mA;
- public AmUtil(Activity pA){
- this.mA = pA;
- }//AmUtil
- public void fb(String pStrMsg){
- Toast t = Toast.makeText(
- this.mA,
- pStrMsg,
- Toast.LENGTH_LONG
- );
- t.show();
- }//fb
- public static int randomInt(
- int pMin,
- int pMax
- ){
- Random r = new Random();
- int iAmplitude =
- Math.max(pMin, pMax) -
- Math.min(pMin, pMax) + 1;
- int iJump = r.nextInt(iAmplitude);
- int iDestination = Math.min(pMin, pMax)+iJump;
- return iDestination;
- }//randomInt
- public boolean writeToTextFile(
- String pFileName,
- String pContent
- ){
- try {
- FileOutputStream fos =
- this.mA.openFileOutput(
- pFileName,
- this.mA.MODE_PRIVATE
- );
- if (fos!=null){
- OutputStreamWriter osw =
- new OutputStreamWriter(
- fos,
- StandardCharsets.UTF_8
- );
- if (osw!=null){
- osw.write(pContent);
- osw.close();
- fos.close();
- return true;
- }
- }//if
- }
- catch (Exception e){
- Log.e(
- "@AmUtil",
- e.toString()
- );
- }
- return false;
- }//writeToTextFile
- public String readTextFromFile(
- String pFileName
- ){
- try{
- FileInputStream fis =
- this.mA.openFileInput(pFileName);
- if (fis!=null){
- InputStreamReader isr =
- new InputStreamReader(
- fis,
- StandardCharsets.UTF_8
- );
- if (isr!=null){
- String strAll="";
- int i;
- char c;
- while((i=isr.read())!=-1){
- c = (char)i;
- strAll+=c;
- }//while
- isr.close();
- fis.close();
- return strAll;
- }//if
- }//if
- }//try
- catch (Exception e){
- Log.e(
- "@AmUtil",
- e.toString()
- );
- }//catch
- return "";
- }//readTextFromFile
- }//AmUtil
- *************************************************************************
- // MainActivity.java
- package com.joythis.android.motivator;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.SeekBar;
- import android.widget.TextView;
- import android.widget.Button;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.nio.charset.StandardCharsets;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- //data members
- Context mContext;
- AmUtil mUtil;
- // for correspondence with whatever relevant in the layout
- TextView mTvAbout, mTvMsgs;
- SeekBar mSbProb; // the user picked probability (of a positive message)
- Button mBtnGetMsg;
- // trio for respecting MVC for ListView
- ListView mLvMsgs;
- //the data
- ArrayList<String> mAlMsgs;
- //the broker
- ArrayAdapter<String> mAd;
- String[] maPositive, maNegative;
- //behavior (click) handler
- View.OnClickListener mClickHandler =
- new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case R.id.idBtnGetMsg:
- if(mTvMsgs!=null) {
- displayMsgInTextView();
- //boolean bOK = writeMessagesToFile();
- boolean bOK = mUtil.writeToTextFile(
- MESSAGES_FILE,
- mTvMsgs.getText().toString()
- );
- String strMsg = "";
- if (bOK)
- strMsg = mContext.getResources().getString(
- R.string.strFileWriteOK
- );
- else
- strMsg = mContext.getResources().getString(
- R.string.strFileWriteFAIL
- );
- mUtil.fb(strMsg);
- }
- if(mLvMsgs!=null)
- displayMessageInListView();
- break;
- }//switch
- }//onClick
- };//mClickHandler
- void displayMessageInListView(){
- int iUserPickedProbabilityOfPositiveMsg =
- mSbProb.getProgress(); //0..100
- int iRandom = AmUtil.randomInt(
- 0,
- mSbProb.getMax()
- );
- boolean bDecidePositive =
- iRandom<iUserPickedProbabilityOfPositiveMsg;
- String strMsg = "";
- if (bDecidePositive) {
- strMsg = maPositive[
- AmUtil.randomInt(
- 0,
- maPositive.length - 1
- )
- ];
- }
- else
- {
- strMsg = maNegative[
- AmUtil.randomInt(
- 0,
- maNegative.length-1
- )
- ];
- }
- //???? add strMsg to the ListView
- //mAlMsgs.add(strMsg); // inserts at the end
- mAlMsgs.add(0, strMsg);
- mAd.notifyDataSetChanged();
- }//displayMessageInListView
- void displayMsgInTextView(){
- //String strP1 = maPositive[0];
- //String strN3 = maNegative[2];
- int iUserPickedProbabilityOfPositiveMsg =
- mSbProb.getProgress(); //0..100
- int iRandom = AmUtil.randomInt(
- 0,
- mSbProb.getMax()
- );
- boolean bDecidePositive =
- iRandom<iUserPickedProbabilityOfPositiveMsg;
- String strMsg = "";
- if (bDecidePositive) {
- strMsg = maPositive[
- AmUtil.randomInt(
- 0,
- maPositive.length - 1
- )
- ];
- }
- else
- {
- strMsg = maNegative[
- AmUtil.randomInt(
- 0,
- maNegative.length-1
- )
- ];
- }
- String strPast = mTvMsgs.getText().toString();
- mTvMsgs.setText(strMsg+"\n"+strPast);
- }//displayMsg
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //setContentView(R.layout.activity_main);
- setContentView(R.layout.rl_motivator_v1);
- //setContentView(R.layout.cl_motivator_v1);
- //setContentView(R.layout.rl_motivator_v2);
- init();
- }//onCreate
- void init(){
- //inits
- mContext = this;
- mTvAbout = findViewById(R.id.idTvAbout);
- mSbProb = findViewById(R.id.idSbProb);
- mBtnGetMsg = findViewById(R.id.idBtnGetMsg);
- mUtil = new AmUtil(this);
- //caution!
- mTvMsgs = findViewById(R.id.idTvMsgs);
- mLvMsgs = findViewById(R.id.idLvMsgs);
- if (mLvMsgs!=null) {
- mAlMsgs = new ArrayList<>();
- mAd = new ArrayAdapter<>(
- mContext,
- android.R.layout.simple_list_item_1,
- mAlMsgs
- );
- mLvMsgs.setAdapter(mAd);
- }//if
- maPositive = this.getResources().
- getStringArray(R.array.saPositiveMsgs);
- maNegative = this.getResources().
- getStringArray(R.array.saNegativeMsgs);
- //quality control
- //set behavior
- //associate mBtnGetMsg to some behavior handler
- mBtnGetMsg.setOnClickListener(
- mClickHandler
- );
- /*
- testing randomInt
- */
- /*
- for(int idx=0; idx<100; idx+=1){
- int iRandom = AmUtil.randomInt(555, 777);
- mTvMsgs.setText(
- String.valueOf(iRandom)
- +"\n"+
- mTvMsgs.getText().toString()
- );
- }
- */
- //serviu para vermos a path da Private Internal Storage desta app
- //writeMessagesToFile();
- if (mTvMsgs!=null){
- //String strMsgs = readMessagesFromFile();
- String strMsgs = mUtil.readTextFromFile(
- MESSAGES_FILE
- );
- mTvMsgs.setText(strMsgs);
- }
- }//init
- //2022-11-02
- public final static String MESSAGES_FILE =
- "MESSAGES3.TXT";
- boolean writeMessagesToFile(){
- /*
- todas as apps têm um espaço de storage
- dedicado a si, no dispositivo
- só a própria app (as suas Activity(ies))
- pode ler e escrever nesse espaço privado dedicado
- O nome desse espaço de storage é "Private Internal Storage"
- */
- File thePrivateInternalStorage = this.getFilesDir();
- String strCuriousAboutWhere =
- thePrivateInternalStorage.toString();
- //mUtil.fb(strCuriousAboutWhere); //comment
- if(thePrivateInternalStorage!=null){
- try {
- FileOutputStream fos =
- this.openFileOutput(
- MESSAGES_FILE,
- Context.MODE_PRIVATE
- );
- if (fos!=null){
- OutputStreamWriter osw =
- new OutputStreamWriter(
- fos,
- StandardCharsets.UTF_8
- );
- if (osw!=null){
- String strContent =
- mTvMsgs.getText().toString();
- osw.write(
- strContent
- );
- osw.close();
- fos.close();
- return true;
- }//if
- }//if
- }//try
- catch (Exception e){
- String strMsg = e.toString();
- Log.e(
- "@MainActivity",
- strMsg
- );
- }//catch
- }//
- return false;
- }//writeMessagesToFile
- String readMessagesFromFile(){
- try {
- FileInputStream fis =
- this.openFileInput(
- MESSAGES_FILE
- );
- if (fis!=null){
- InputStreamReader isr =
- new InputStreamReader(
- fis,
- StandardCharsets.UTF_8
- );
- if (isr!=null){
- String strAll = "";
- int i;
- char c;
- while((i=isr.read())!=-1){
- c=(char)i;
- strAll+=c;
- }//while
- isr.close();
- fis.close();
- return strAll;
- }//if
- }//if
- }//try
- catch (Exception e){
- String strMsg = e.toString();
- Log.e(
- "@MainActivity",
- strMsg
- );
- }
- return "";
- }//readMessagesFromFile
- }//class MainActivity
Advertisement
Add Comment
Please, Sign In to add comment