am_dot_com

DDM 2022-11-02

Nov 2nd, 2022 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.32 KB | None | 0 0
  1. //AmUtil.java
  2. package com.joythis.android.motivator;
  3.  
  4. import android.app.Activity;
  5. import android.util.Log;
  6. import android.widget.Toast;
  7.  
  8. import java.io.FileInputStream;
  9. import java.io.FileOutputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.OutputStreamWriter;
  12. import java.nio.charset.StandardCharsets;
  13. import java.util.Random;
  14.  
  15. public class AmUtil {
  16. Activity mA;
  17. public AmUtil(Activity pA){
  18. this.mA = pA;
  19. }//AmUtil
  20.  
  21. public void fb(String pStrMsg){
  22. Toast t = Toast.makeText(
  23. this.mA,
  24. pStrMsg,
  25. Toast.LENGTH_LONG
  26. );
  27. t.show();
  28. }//fb
  29.  
  30. public static int randomInt(
  31. int pMin,
  32. int pMax
  33. ){
  34. Random r = new Random();
  35. int iAmplitude =
  36. Math.max(pMin, pMax) -
  37. Math.min(pMin, pMax) + 1;
  38. int iJump = r.nextInt(iAmplitude);
  39. int iDestination = Math.min(pMin, pMax)+iJump;
  40. return iDestination;
  41. }//randomInt
  42.  
  43. public boolean writeToTextFile(
  44. String pFileName,
  45. String pContent
  46. ){
  47. try {
  48. FileOutputStream fos =
  49. this.mA.openFileOutput(
  50. pFileName,
  51. this.mA.MODE_PRIVATE
  52. );
  53. if (fos!=null){
  54. OutputStreamWriter osw =
  55. new OutputStreamWriter(
  56. fos,
  57. StandardCharsets.UTF_8
  58. );
  59. if (osw!=null){
  60. osw.write(pContent);
  61. osw.close();
  62. fos.close();
  63. return true;
  64. }
  65. }//if
  66. }
  67. catch (Exception e){
  68. Log.e(
  69. "@AmUtil",
  70. e.toString()
  71. );
  72. }
  73. return false;
  74. }//writeToTextFile
  75.  
  76. public String readTextFromFile(
  77. String pFileName
  78. ){
  79. try{
  80. FileInputStream fis =
  81. this.mA.openFileInput(pFileName);
  82. if (fis!=null){
  83. InputStreamReader isr =
  84. new InputStreamReader(
  85. fis,
  86. StandardCharsets.UTF_8
  87. );
  88. if (isr!=null){
  89. String strAll="";
  90. int i;
  91. char c;
  92. while((i=isr.read())!=-1){
  93. c = (char)i;
  94. strAll+=c;
  95. }//while
  96. isr.close();
  97. fis.close();
  98. return strAll;
  99. }//if
  100. }//if
  101. }//try
  102. catch (Exception e){
  103. Log.e(
  104. "@AmUtil",
  105. e.toString()
  106. );
  107. }//catch
  108. return "";
  109. }//readTextFromFile
  110. }//AmUtil
  111.  
  112.  
  113. *************************************************************************
  114. // MainActivity.java
  115. package com.joythis.android.motivator;
  116.  
  117. import androidx.appcompat.app.AppCompatActivity;
  118.  
  119. import android.content.Context;
  120. import android.os.Bundle;
  121. import android.util.Log;
  122. import android.view.View;
  123. import android.widget.ArrayAdapter;
  124. import android.widget.ListView;
  125. import android.widget.SeekBar;
  126. import android.widget.TextView;
  127. import android.widget.Button;
  128.  
  129. import java.io.File;
  130. import java.io.FileInputStream;
  131. import java.io.FileOutputStream;
  132. import java.io.InputStreamReader;
  133. import java.io.OutputStreamWriter;
  134. import java.nio.charset.StandardCharsets;
  135. import java.util.ArrayList;
  136.  
  137. public class MainActivity extends AppCompatActivity {
  138. //data members
  139. Context mContext;
  140.  
  141. AmUtil mUtil;
  142.  
  143. // for correspondence with whatever relevant in the layout
  144. TextView mTvAbout, mTvMsgs;
  145. SeekBar mSbProb; // the user picked probability (of a positive message)
  146. Button mBtnGetMsg;
  147.  
  148. // trio for respecting MVC for ListView
  149. ListView mLvMsgs;
  150. //the data
  151. ArrayList<String> mAlMsgs;
  152. //the broker
  153. ArrayAdapter<String> mAd;
  154.  
  155. String[] maPositive, maNegative;
  156.  
  157. //behavior (click) handler
  158. View.OnClickListener mClickHandler =
  159. new View.OnClickListener() {
  160. @Override
  161. public void onClick(View v) {
  162. switch(v.getId()){
  163. case R.id.idBtnGetMsg:
  164. if(mTvMsgs!=null) {
  165. displayMsgInTextView();
  166. //boolean bOK = writeMessagesToFile();
  167. boolean bOK = mUtil.writeToTextFile(
  168. MESSAGES_FILE,
  169. mTvMsgs.getText().toString()
  170. );
  171. String strMsg = "";
  172. if (bOK)
  173. strMsg = mContext.getResources().getString(
  174. R.string.strFileWriteOK
  175. );
  176. else
  177. strMsg = mContext.getResources().getString(
  178. R.string.strFileWriteFAIL
  179. );
  180. mUtil.fb(strMsg);
  181. }
  182.  
  183. if(mLvMsgs!=null)
  184. displayMessageInListView();
  185. break;
  186.  
  187.  
  188. }//switch
  189. }//onClick
  190. };//mClickHandler
  191.  
  192. void displayMessageInListView(){
  193. int iUserPickedProbabilityOfPositiveMsg =
  194. mSbProb.getProgress(); //0..100
  195.  
  196. int iRandom = AmUtil.randomInt(
  197. 0,
  198. mSbProb.getMax()
  199. );
  200.  
  201. boolean bDecidePositive =
  202. iRandom<iUserPickedProbabilityOfPositiveMsg;
  203.  
  204. String strMsg = "";
  205. if (bDecidePositive) {
  206. strMsg = maPositive[
  207. AmUtil.randomInt(
  208. 0,
  209. maPositive.length - 1
  210. )
  211. ];
  212. }
  213. else
  214. {
  215. strMsg = maNegative[
  216. AmUtil.randomInt(
  217. 0,
  218. maNegative.length-1
  219. )
  220. ];
  221. }
  222.  
  223. //???? add strMsg to the ListView
  224. //mAlMsgs.add(strMsg); // inserts at the end
  225. mAlMsgs.add(0, strMsg);
  226. mAd.notifyDataSetChanged();
  227. }//displayMessageInListView
  228.  
  229. void displayMsgInTextView(){
  230. //String strP1 = maPositive[0];
  231. //String strN3 = maNegative[2];
  232. int iUserPickedProbabilityOfPositiveMsg =
  233. mSbProb.getProgress(); //0..100
  234.  
  235. int iRandom = AmUtil.randomInt(
  236. 0,
  237. mSbProb.getMax()
  238. );
  239.  
  240. boolean bDecidePositive =
  241. iRandom<iUserPickedProbabilityOfPositiveMsg;
  242.  
  243. String strMsg = "";
  244. if (bDecidePositive) {
  245. strMsg = maPositive[
  246. AmUtil.randomInt(
  247. 0,
  248. maPositive.length - 1
  249. )
  250. ];
  251. }
  252. else
  253. {
  254. strMsg = maNegative[
  255. AmUtil.randomInt(
  256. 0,
  257. maNegative.length-1
  258. )
  259. ];
  260. }
  261.  
  262. String strPast = mTvMsgs.getText().toString();
  263. mTvMsgs.setText(strMsg+"\n"+strPast);
  264. }//displayMsg
  265.  
  266. @Override
  267. protected void onCreate(Bundle savedInstanceState) {
  268. super.onCreate(savedInstanceState);
  269. //setContentView(R.layout.activity_main);
  270. setContentView(R.layout.rl_motivator_v1);
  271. //setContentView(R.layout.cl_motivator_v1);
  272. //setContentView(R.layout.rl_motivator_v2);
  273.  
  274. init();
  275. }//onCreate
  276.  
  277. void init(){
  278. //inits
  279. mContext = this;
  280. mTvAbout = findViewById(R.id.idTvAbout);
  281. mSbProb = findViewById(R.id.idSbProb);
  282. mBtnGetMsg = findViewById(R.id.idBtnGetMsg);
  283.  
  284. mUtil = new AmUtil(this);
  285. //caution!
  286. mTvMsgs = findViewById(R.id.idTvMsgs);
  287.  
  288. mLvMsgs = findViewById(R.id.idLvMsgs);
  289.  
  290. if (mLvMsgs!=null) {
  291. mAlMsgs = new ArrayList<>();
  292. mAd = new ArrayAdapter<>(
  293. mContext,
  294. android.R.layout.simple_list_item_1,
  295. mAlMsgs
  296. );
  297. mLvMsgs.setAdapter(mAd);
  298. }//if
  299.  
  300. maPositive = this.getResources().
  301. getStringArray(R.array.saPositiveMsgs);
  302. maNegative = this.getResources().
  303. getStringArray(R.array.saNegativeMsgs);
  304.  
  305. //quality control
  306.  
  307. //set behavior
  308. //associate mBtnGetMsg to some behavior handler
  309. mBtnGetMsg.setOnClickListener(
  310. mClickHandler
  311. );
  312.  
  313. /*
  314. testing randomInt
  315. */
  316. /*
  317. for(int idx=0; idx<100; idx+=1){
  318. int iRandom = AmUtil.randomInt(555, 777);
  319. mTvMsgs.setText(
  320. String.valueOf(iRandom)
  321. +"\n"+
  322. mTvMsgs.getText().toString()
  323. );
  324. }
  325. */
  326.  
  327. //serviu para vermos a path da Private Internal Storage desta app
  328. //writeMessagesToFile();
  329.  
  330. if (mTvMsgs!=null){
  331. //String strMsgs = readMessagesFromFile();
  332. String strMsgs = mUtil.readTextFromFile(
  333. MESSAGES_FILE
  334. );
  335. mTvMsgs.setText(strMsgs);
  336. }
  337. }//init
  338.  
  339. //2022-11-02
  340. public final static String MESSAGES_FILE =
  341. "MESSAGES3.TXT";
  342.  
  343. boolean writeMessagesToFile(){
  344. /*
  345. todas as apps têm um espaço de storage
  346. dedicado a si, no dispositivo
  347. só a própria app (as suas Activity(ies))
  348. pode ler e escrever nesse espaço privado dedicado
  349. O nome desse espaço de storage é "Private Internal Storage"
  350. */
  351. File thePrivateInternalStorage = this.getFilesDir();
  352. String strCuriousAboutWhere =
  353. thePrivateInternalStorage.toString();
  354. //mUtil.fb(strCuriousAboutWhere); //comment
  355.  
  356. if(thePrivateInternalStorage!=null){
  357. try {
  358. FileOutputStream fos =
  359. this.openFileOutput(
  360. MESSAGES_FILE,
  361. Context.MODE_PRIVATE
  362. );
  363.  
  364. if (fos!=null){
  365. OutputStreamWriter osw =
  366. new OutputStreamWriter(
  367. fos,
  368. StandardCharsets.UTF_8
  369. );
  370.  
  371. if (osw!=null){
  372. String strContent =
  373. mTvMsgs.getText().toString();
  374. osw.write(
  375. strContent
  376. );
  377. osw.close();
  378. fos.close();
  379. return true;
  380. }//if
  381. }//if
  382. }//try
  383. catch (Exception e){
  384. String strMsg = e.toString();
  385. Log.e(
  386. "@MainActivity",
  387. strMsg
  388. );
  389. }//catch
  390. }//
  391. return false;
  392. }//writeMessagesToFile
  393.  
  394. String readMessagesFromFile(){
  395. try {
  396. FileInputStream fis =
  397. this.openFileInput(
  398. MESSAGES_FILE
  399. );
  400. if (fis!=null){
  401. InputStreamReader isr =
  402. new InputStreamReader(
  403. fis,
  404. StandardCharsets.UTF_8
  405. );
  406. if (isr!=null){
  407. String strAll = "";
  408. int i;
  409. char c;
  410. while((i=isr.read())!=-1){
  411. c=(char)i;
  412. strAll+=c;
  413. }//while
  414. isr.close();
  415. fis.close();
  416. return strAll;
  417. }//if
  418. }//if
  419. }//try
  420. catch (Exception e){
  421. String strMsg = e.toString();
  422. Log.e(
  423. "@MainActivity",
  424. strMsg
  425. );
  426. }
  427. return "";
  428. }//readMessagesFromFile
  429. }//class MainActivity
Advertisement
Add Comment
Please, Sign In to add comment