Advertisement
Guest User

Untitled

a guest
Jan 9th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.85 KB | None | 0 0
  1. public class MainActivity extends ActionBarActivity {
  2.  
  3. boolean periodWasActivated;
  4. int periodLocation;
  5. int commaLocation;
  6. String defaultMessage = "Android, please tell me the answer to the following question for me please as best as you can";
  7. String theAnswer = "";
  8. boolean hasChanged = false;
  9. boolean questionSelected = false;
  10.  
  11.  
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. // System.out.println(getLastCharInString("//// Should say E then Q /////"));
  17. // System.out.println(getLastCharInString("THE"));
  18. // System.out.println(getLastCharInString("Q"));
  19. // System.out.println(getLastCharInString("////"));
  20. //initalize the vies
  21. hidingIllusionLoader();
  22. final EditText e_petition = (EditText) findViewById(R.id.petition);
  23. final EditText e_question = (EditText) findViewById(R.id.question);
  24. //DTYOP
  25.  
  26. Button b_ask = (Button) findViewById(R.id.askButton);
  27. Button b_reset = (Button) findViewById(R.id.resetButton);
  28. Button b_help = (Button) findViewById(R.id.helpButton);
  29.  
  30. e_question.setOnFocusChangeListener(new View.OnFocusChangeListener() {
  31. @Override
  32. public void onFocusChange(View v, boolean hasFocus) {
  33. questionSelected = true;
  34. }
  35. });
  36.  
  37. e_petition.setOnFocusChangeListener(new View.OnFocusChangeListener() {
  38. @Override
  39. public void onFocusChange(View v, boolean hasFocus) {
  40. questionSelected = false;
  41. }
  42. });
  43.  
  44. b_ask.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View v) {
  47.  
  48. //TOAST or change the debug fields to what the answer is
  49. //aka the string between the . and , in the petition code
  50. //theAnswer
  51. instantiateAnswer();
  52.  
  53. //createDialog();
  54. loadingIllusionLoader();
  55. // int tuirtle = 5;
  56. // tuirtle = 4;
  57. }
  58. });
  59. b_reset.setOnClickListener(new View.OnClickListener() {
  60. @Override
  61. public void onClick(View v) {
  62. resetTheVariables();
  63. //resets the edit text fields
  64.  
  65.  
  66. }
  67. });
  68. b_help.setOnClickListener(new View.OnClickListener() {
  69. @Override
  70. public void onClick(View v) {
  71.  
  72. //gives the user assistance on using the app
  73. //doesnt give how to secretly use it
  74. createHelpDialog();
  75. // Toast thetoast = Toast.makeText(getApplicationContext(), "First, create a petition above and end it with a colon. The petition should say something along the lines of, Android, please tell me the answer to the following question. Then, give it a question to process and answer you with. If Android likes you, then it will give you an answer.", Toast.LENGTH_LONG);
  76. // thetoast.show();
  77.  
  78. }
  79. });
  80.  
  81. //On Text Changed Listeners
  82.  
  83. e_petition.addTextChangedListener(new TextWatcher() {
  84. @Override
  85. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  86. // System.out.println("beforeTextChanged");
  87. }
  88.  
  89. @Override
  90. public void onTextChanged(CharSequence s, int start, int before, int count) {
  91. System.out.println("ontextchanged");
  92. //do nothing if no period was activated
  93. boolean pastThePeriod = periodLocation <= e_petition.getText().length();
  94. if(!pastThePeriod)
  95. {
  96. periodWasActivated = false;
  97. }
  98. if(e_petition.getText().toString().isEmpty())
  99. {
  100. theAnswer = "";
  101. }
  102. if(pastThePeriod) {
  103. if (!periodWasActivated) {
  104. return;
  105. }
  106.  
  107.  
  108. //if activated PAST the first character
  109. if (periodWasActivated && !hasChanged && pastThePeriod) {
  110. hasChanged = true;
  111.  
  112. String current = e_petition.getText().toString();
  113. String charToBeAddedToAnswer = getLastCharInString(current);
  114.  
  115.  
  116. System.out.println("charForAnswer: " + charToBeAddedToAnswer);
  117.  
  118. e_petition.setText(replaceCharsInStringWithDefaultString(current));
  119. e_petition.setSelection(e_petition.getText().length());
  120. //PROBLEM
  121. //W
  122. if(theAnswer.isEmpty())
  123. {
  124. System.out.println("fucknuggets");
  125. hasChanged = false;
  126. appendingTheStringProperly(" ");
  127. return;
  128. }
  129. appendingTheStringProperly(charToBeAddedToAnswer);
  130. hasChanged = false;
  131. }
  132.  
  133.  
  134. }
  135. }
  136.  
  137. @Override
  138. public void afterTextChanged(Editable s) {
  139. // System.out.println("afterTextChanged");
  140. boolean pasttheperiodint = periodLocation <= e_petition.getText().length();
  141. }
  142. });
  143. }
  144.  
  145. public void createDialog()
  146. {
  147.  
  148. // Toast theToast = Toast.makeText(getApplicationContext(), theAnswer, Toast.LENGTH_LONG);
  149. // theToast.show();
  150. final EditText e_petition = (EditText) findViewById(R.id.petition);
  151.  
  152.  
  153.  
  154. final EditText e_question = (EditText) findViewById(R.id.question);
  155. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  156. // System.out.println("THE ANSWER TO DIALOG: "+theAnswer);
  157. builder.setMessage(theAnswer);
  158. builder.setTitle(getRandomTitleResponse());
  159.  
  160. //buttons
  161. builder.setPositiveButton("Thanks", new DialogInterface.OnClickListener() {
  162. @Override
  163. public void onClick(DialogInterface dialog, int which) {
  164. Toast newToast = Toast.makeText(getApplicationContext(), ";)", Toast.LENGTH_SHORT);
  165. if(!theAnswer.isEmpty())
  166. newToast.show();
  167. }
  168. });
  169. //loadingIllusionLoader();
  170.  
  171. resetTheVariables();
  172. e_petition.requestFocus();
  173. builder.create();
  174. builder.show();
  175. }
  176.  
  177. public void loadingIllusionLoader()
  178. {
  179. final ProgressBar theCircleLoader = (ProgressBar) findViewById(R.id.progressBar2);
  180. final ProgressBar theBarLoader = (ProgressBar) findViewById(R.id.progressBar);
  181. theCircleLoader.setVisibility(View.VISIBLE);
  182. int timeToRest = (int) (Math.random() * 1500) + 500;
  183.  
  184. CountDownTimer theTimer = new CountDownTimer(timeToRest, 500)
  185.  
  186. {
  187. @Override public void onTick( long millisUntilFinished ) {
  188.  
  189. theBarLoader.setProgress( theCircleLoader.getProgress() + 100 );
  190. }
  191. @Override public void onFinish() {
  192. theCircleLoader.setVisibility(View.INVISIBLE);
  193. // theBarLoader.setVisibility(View.INVISIBLE);
  194. createDialog();
  195. }
  196. };
  197. theTimer.start();
  198.  
  199.  
  200. //UNCOMMENT if timer doesnt work
  201. // final Handler newHandle = new Handler();
  202. // newHandle.postDelayed(new Runnable() {
  203. // @Override
  204. // public void run() {
  205. // createDialog();
  206. // hidingIllusionLoader();
  207. // newHandle.removeCallbacksAndMessages(null);
  208. ////fug
  209. // }
  210. // }, timeToRest);
  211. }
  212.  
  213. public void hidingIllusionLoader()
  214. {
  215. ProgressBar theCircleLoader = (ProgressBar) findViewById(R.id.progressBar2);
  216.  
  217. ProgressBar theBarLoader = (ProgressBar) findViewById(R.id.progressBar);
  218. theCircleLoader.setVisibility(View.INVISIBLE);
  219. theBarLoader.setVisibility(View.INVISIBLE);
  220. }
  221.  
  222. public void createHelpDialog()
  223. {
  224. // Toast theToast = Toast.makeText(getApplicationContext(), theAnswer, Toast.LENGTH_LONG);
  225. // theToast.show();
  226. final EditText e_petition = (EditText) findViewById(R.id.petition);
  227.  
  228.  
  229.  
  230. final EditText e_question = (EditText) findViewById(R.id.question);
  231. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  232. // System.out.println("THE ANSWER TO DIALOG: "+theAnswer);
  233. builder.setMessage("First, create a petition above and end it with a colon. The petition should say something along the lines of, Android, please tell me the answer to the following question. Then, give it a question to process and answer you with. If Android likes you, then it will give you an answer.");
  234. builder.setTitle("How this works:");
  235.  
  236. //buttons
  237. builder.setPositiveButton("OK. Got it!", new DialogInterface.OnClickListener() {
  238. @Override
  239. public void onClick(DialogInterface dialog, int which) {
  240. Toast newToast = Toast.makeText(getApplicationContext(), ":)", Toast.LENGTH_SHORT);
  241.  
  242. newToast.show();
  243. }
  244. });
  245. //loadingIllusionLoader();
  246.  
  247. e_petition.requestFocus();
  248. builder.create();
  249. builder.show();
  250. }
  251. public void resetTheVariables()
  252. {
  253. final EditText e_petition = (EditText) findViewById(R.id.petition);
  254. final EditText e_question = (EditText) findViewById(R.id.question);
  255. e_petition.setText("", TextView.BufferType.EDITABLE);
  256. e_question.setText("", TextView.BufferType.EDITABLE);
  257. theAnswer = "";
  258. periodWasActivated = false;
  259. e_petition.setSelection(e_petition.getText().length());
  260. }
  261. public void appendingTheStringProperly(String roflStringInput)
  262. {
  263. if(theAnswer.isEmpty())
  264. {
  265. theAnswer = roflStringInput;
  266. return;
  267.  
  268. }
  269. System.out.println("******");
  270. System.out.println(theAnswer);
  271. theAnswer = theAnswer + roflStringInput;
  272. System.out.println(theAnswer);
  273. return;
  274. }
  275. public String replaceCharsInStringWithDefaultString(String thestring)
  276. {
  277. //only use this string if period activation is true
  278. //defaultMessage
  279. //periodLocation
  280. int lengthOfPetitionString = thestring.length();
  281.  
  282. char charToBePutIn = defaultMessage.charAt(lengthOfPetitionString - 1);
  283.  
  284. String newstring = removeJustTheLastCharInString(thestring);
  285. newstring = newstring + charToBePutIn;
  286.  
  287. return newstring;
  288. }
  289.  
  290. public String removeJustTheLastCharInString(String thestring)
  291. {
  292. if(thestring.length() == 1)
  293. {
  294. return "";
  295. }
  296. return thestring.substring(0, thestring.length() - 1);
  297. }
  298.  
  299. public String getLastCharInString(String thestring)
  300. {
  301. if(thestring.length() == 1)
  302. {
  303. return thestring;
  304. }
  305. return thestring.substring(thestring.length() - 1, thestring.length());
  306. }
  307.  
  308. @Override
  309. public boolean onKeyUp(int keyCode, KeyEvent event) {
  310.  
  311. System.out.println("onKeyUp");
  312. final EditText e_question = (EditText) findViewById(R.id.question);
  313. final EditText e_petition = (EditText) findViewById(R.id.petition);
  314.  
  315. switch (keyCode) {
  316. case KeyEvent.KEYCODE_COMMA:
  317. periodWasActivated = false;
  318. return true;
  319. case KeyEvent.KEYCODE_SEMICOLON:
  320. //changes focus onto the question box
  321. e_question.requestFocus();
  322. return true;
  323. case KeyEvent.KEYCODE_PERIOD:
  324.  
  325. periodWasActivated = true;
  326. periodLocation = e_petition.getText().toString().length();
  327. System.out.println("Period Activated @: " + periodLocation);
  328.  
  329. // if (periodWasActivated && (periodLocation == 1)) {
  330. // e_petition.setText("A", TextView.BufferType.EDITABLE);
  331. // e_petition.setSelection(e_petition.getText().length());
  332. // return true;
  333. // }
  334.  
  335. //
  336. String current = e_petition.getText().toString();
  337. System.out.println("Key up current @: " + current);
  338. e_petition.setText(replaceCharsInStringWithDefaultString(current));
  339. e_petition.setSelection(e_petition.getText().length());
  340. return true;
  341. // case KeyEvent.KEYCODE_DEL:
  342. // return true;
  343. case KeyEvent.KEYCODE_SLASH:
  344. if( questionSelected) {
  345. instantiateAnswer();
  346. // createDialog();
  347. loadingIllusionLoader();
  348.  
  349.  
  350. return true;
  351. }
  352. return super.onKeyUp(keyCode, event);
  353. default:
  354. // System.out.println("keycode");
  355. // char c = (char) keyCode;
  356. // System.out.println(c);
  357. return super.onKeyUp(keyCode, event);
  358. }
  359. }
  360. public String getRandomEmptyResponse() {
  361. double randomizationDouble = Math.random() * 10;
  362. String answer = "Try again.";
  363. if (randomizationDouble <= 1) {
  364. answer= "Why do you want to know the answer to this question?";
  365. return answer;
  366. }
  367. if (randomizationDouble <= 2) {
  368. answer= "Petition is not worded properly";
  369. return answer;
  370. }
  371. if (randomizationDouble <= 3) {
  372. answer= "Insufficient Funds in Account";
  373. return answer;
  374. }
  375. if (randomizationDouble <= 4) {
  376. answer= "System.out.println(Tell user question is in invalid format)";
  377. return answer;
  378. }
  379. if (randomizationDouble <= 5) {
  380. answer= "Processors cannot validate such a request.";
  381. return answer;
  382. }
  383. if (randomizationDouble <= 6) {
  384. answer= "Don't really want to tell you.";
  385. return answer;
  386. }
  387. if (randomizationDouble <= 7) {
  388. answer= "Wow. Just... wow.";
  389. return answer;
  390. }
  391. if (randomizationDouble <= 8) {
  392. answer= "I don't care enough to answer YOUR question.";
  393. return answer;
  394. }
  395. if (randomizationDouble <= 9) {
  396. answer= "Your question isn't even useful for anyone to know.";
  397. return answer;
  398. }
  399. if (randomizationDouble <= 10) {
  400. answer= "Try again in a moment...";
  401. return answer;
  402. }
  403. if (randomizationDouble <= 11) {
  404. answer= "Give me back to who was showing you this in the first place. I like them better.";
  405. return answer;
  406. }
  407. return answer;
  408. }
  409.  
  410. public String getRandomTitleResponse() {
  411. double randomizationDouble = Math.random() * 10;
  412. String answer = "I got the answer you need!";
  413.  
  414. final EditText e_question = (EditText) findViewById(R.id.question);
  415.  
  416. if (randomizationDouble <= 1) {
  417. answer= "Processed the question:";
  418. return answer;
  419. }
  420. if (randomizationDouble <= 2) {
  421. answer= "Here you go:";
  422. return answer;
  423. }
  424. if (randomizationDouble <= 3) {
  425. answer= e_question.getText().toString();
  426. return answer;
  427. }
  428. if (randomizationDouble <= 4) {
  429. answer= e_question.getText().toString();;
  430. return answer;
  431. }
  432. if (randomizationDouble <= 5) {
  433. answer= e_question.getText().toString();;
  434. return answer;
  435. }
  436. if (randomizationDouble <= 6) {
  437. answer= e_question.getText().toString();;
  438. return answer;
  439. }
  440. if (randomizationDouble <= 7) {
  441. answer= "Thank you for a proper question:";
  442. return answer;
  443. }
  444. if (randomizationDouble <= 8) {
  445. answer= "Good question:";
  446. return answer;
  447. }
  448. if (randomizationDouble <= 9) {
  449. answer= "Useful Question:";
  450. return answer;
  451. }
  452. if (randomizationDouble <= 10) {
  453. answer= "Processed";
  454. return answer;
  455. }
  456. if (randomizationDouble <= 11) {
  457. answer= "Got your answer";
  458. return answer;
  459. }
  460. return answer;
  461. }
  462.  
  463. public void instantiateAnswer(){
  464. final EditText e_petition = (EditText) findViewById(R.id.petition);
  465. if (theAnswer.isEmpty()) {
  466. theAnswer = getRandomEmptyResponse();
  467. }
  468. //check if the LAST char in answer is a comma, get rid of it
  469. if (theAnswer.endsWith(",")) {
  470. theAnswer = removeJustTheLastCharInString(theAnswer);
  471. }
  472.  
  473.  
  474.  
  475. }
  476. @Override
  477. public boolean onCreateOptionsMenu(Menu menu) {
  478. // Inflate the menu; this adds items to the action bar if it is present.
  479. // getMenuInflater().inflate(R.menu.menu_main, menu);
  480. return true;
  481. }
  482.  
  483. @Override
  484. public boolean onOptionsItemSelected(MenuItem item) {
  485. // Handle action bar item clicks here. The action bar will
  486. // automatically handle clicks on the Home/Up button, so long
  487. // as you specify a parent activity in AndroidManifest.xml.
  488. int id = item.getItemId();
  489.  
  490. //noinspection SimplifiableIfStatement
  491. if (id == R.id.action_settings) {
  492. return true;
  493. }
  494.  
  495. return super.onOptionsItemSelected(item);
  496. }
  497. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement