Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends ActionBarActivity {
- boolean periodWasActivated;
- int periodLocation;
- int commaLocation;
- String defaultMessage = "Android, please tell me the answer to the following question for me please as best as you can";
- String theAnswer = "";
- boolean hasChanged = false;
- boolean questionSelected = false;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // System.out.println(getLastCharInString("//// Should say E then Q /////"));
- // System.out.println(getLastCharInString("THE"));
- // System.out.println(getLastCharInString("Q"));
- // System.out.println(getLastCharInString("////"));
- //initalize the vies
- hidingIllusionLoader();
- final EditText e_petition = (EditText) findViewById(R.id.petition);
- final EditText e_question = (EditText) findViewById(R.id.question);
- //DTYOP
- Button b_ask = (Button) findViewById(R.id.askButton);
- Button b_reset = (Button) findViewById(R.id.resetButton);
- Button b_help = (Button) findViewById(R.id.helpButton);
- e_question.setOnFocusChangeListener(new View.OnFocusChangeListener() {
- @Override
- public void onFocusChange(View v, boolean hasFocus) {
- questionSelected = true;
- }
- });
- e_petition.setOnFocusChangeListener(new View.OnFocusChangeListener() {
- @Override
- public void onFocusChange(View v, boolean hasFocus) {
- questionSelected = false;
- }
- });
- b_ask.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //TOAST or change the debug fields to what the answer is
- //aka the string between the . and , in the petition code
- //theAnswer
- instantiateAnswer();
- //createDialog();
- loadingIllusionLoader();
- // int tuirtle = 5;
- // tuirtle = 4;
- }
- });
- b_reset.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- resetTheVariables();
- //resets the edit text fields
- }
- });
- b_help.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //gives the user assistance on using the app
- //doesnt give how to secretly use it
- createHelpDialog();
- // 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);
- // thetoast.show();
- }
- });
- //On Text Changed Listeners
- e_petition.addTextChangedListener(new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- // System.out.println("beforeTextChanged");
- }
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- System.out.println("ontextchanged");
- //do nothing if no period was activated
- boolean pastThePeriod = periodLocation <= e_petition.getText().length();
- if(!pastThePeriod)
- {
- periodWasActivated = false;
- }
- if(e_petition.getText().toString().isEmpty())
- {
- theAnswer = "";
- }
- if(pastThePeriod) {
- if (!periodWasActivated) {
- return;
- }
- //if activated PAST the first character
- if (periodWasActivated && !hasChanged && pastThePeriod) {
- hasChanged = true;
- String current = e_petition.getText().toString();
- String charToBeAddedToAnswer = getLastCharInString(current);
- System.out.println("charForAnswer: " + charToBeAddedToAnswer);
- e_petition.setText(replaceCharsInStringWithDefaultString(current));
- e_petition.setSelection(e_petition.getText().length());
- //PROBLEM
- //W
- if(theAnswer.isEmpty())
- {
- System.out.println("fucknuggets");
- hasChanged = false;
- appendingTheStringProperly(" ");
- return;
- }
- appendingTheStringProperly(charToBeAddedToAnswer);
- hasChanged = false;
- }
- }
- }
- @Override
- public void afterTextChanged(Editable s) {
- // System.out.println("afterTextChanged");
- boolean pasttheperiodint = periodLocation <= e_petition.getText().length();
- }
- });
- }
- public void createDialog()
- {
- // Toast theToast = Toast.makeText(getApplicationContext(), theAnswer, Toast.LENGTH_LONG);
- // theToast.show();
- final EditText e_petition = (EditText) findViewById(R.id.petition);
- final EditText e_question = (EditText) findViewById(R.id.question);
- AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
- // System.out.println("THE ANSWER TO DIALOG: "+theAnswer);
- builder.setMessage(theAnswer);
- builder.setTitle(getRandomTitleResponse());
- //buttons
- builder.setPositiveButton("Thanks", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast newToast = Toast.makeText(getApplicationContext(), ";)", Toast.LENGTH_SHORT);
- if(!theAnswer.isEmpty())
- newToast.show();
- }
- });
- //loadingIllusionLoader();
- resetTheVariables();
- e_petition.requestFocus();
- builder.create();
- builder.show();
- }
- public void loadingIllusionLoader()
- {
- final ProgressBar theCircleLoader = (ProgressBar) findViewById(R.id.progressBar2);
- final ProgressBar theBarLoader = (ProgressBar) findViewById(R.id.progressBar);
- theCircleLoader.setVisibility(View.VISIBLE);
- int timeToRest = (int) (Math.random() * 1500) + 500;
- CountDownTimer theTimer = new CountDownTimer(timeToRest, 500)
- {
- @Override public void onTick( long millisUntilFinished ) {
- theBarLoader.setProgress( theCircleLoader.getProgress() + 100 );
- }
- @Override public void onFinish() {
- theCircleLoader.setVisibility(View.INVISIBLE);
- // theBarLoader.setVisibility(View.INVISIBLE);
- createDialog();
- }
- };
- theTimer.start();
- //UNCOMMENT if timer doesnt work
- // final Handler newHandle = new Handler();
- // newHandle.postDelayed(new Runnable() {
- // @Override
- // public void run() {
- // createDialog();
- // hidingIllusionLoader();
- // newHandle.removeCallbacksAndMessages(null);
- ////fug
- // }
- // }, timeToRest);
- }
- public void hidingIllusionLoader()
- {
- ProgressBar theCircleLoader = (ProgressBar) findViewById(R.id.progressBar2);
- ProgressBar theBarLoader = (ProgressBar) findViewById(R.id.progressBar);
- theCircleLoader.setVisibility(View.INVISIBLE);
- theBarLoader.setVisibility(View.INVISIBLE);
- }
- public void createHelpDialog()
- {
- // Toast theToast = Toast.makeText(getApplicationContext(), theAnswer, Toast.LENGTH_LONG);
- // theToast.show();
- final EditText e_petition = (EditText) findViewById(R.id.petition);
- final EditText e_question = (EditText) findViewById(R.id.question);
- AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
- // System.out.println("THE ANSWER TO DIALOG: "+theAnswer);
- 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.");
- builder.setTitle("How this works:");
- //buttons
- builder.setPositiveButton("OK. Got it!", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast newToast = Toast.makeText(getApplicationContext(), ":)", Toast.LENGTH_SHORT);
- newToast.show();
- }
- });
- //loadingIllusionLoader();
- e_petition.requestFocus();
- builder.create();
- builder.show();
- }
- public void resetTheVariables()
- {
- final EditText e_petition = (EditText) findViewById(R.id.petition);
- final EditText e_question = (EditText) findViewById(R.id.question);
- e_petition.setText("", TextView.BufferType.EDITABLE);
- e_question.setText("", TextView.BufferType.EDITABLE);
- theAnswer = "";
- periodWasActivated = false;
- e_petition.setSelection(e_petition.getText().length());
- }
- public void appendingTheStringProperly(String roflStringInput)
- {
- if(theAnswer.isEmpty())
- {
- theAnswer = roflStringInput;
- return;
- }
- System.out.println("******");
- System.out.println(theAnswer);
- theAnswer = theAnswer + roflStringInput;
- System.out.println(theAnswer);
- return;
- }
- public String replaceCharsInStringWithDefaultString(String thestring)
- {
- //only use this string if period activation is true
- //defaultMessage
- //periodLocation
- int lengthOfPetitionString = thestring.length();
- char charToBePutIn = defaultMessage.charAt(lengthOfPetitionString - 1);
- String newstring = removeJustTheLastCharInString(thestring);
- newstring = newstring + charToBePutIn;
- return newstring;
- }
- public String removeJustTheLastCharInString(String thestring)
- {
- if(thestring.length() == 1)
- {
- return "";
- }
- return thestring.substring(0, thestring.length() - 1);
- }
- public String getLastCharInString(String thestring)
- {
- if(thestring.length() == 1)
- {
- return thestring;
- }
- return thestring.substring(thestring.length() - 1, thestring.length());
- }
- @Override
- public boolean onKeyUp(int keyCode, KeyEvent event) {
- System.out.println("onKeyUp");
- final EditText e_question = (EditText) findViewById(R.id.question);
- final EditText e_petition = (EditText) findViewById(R.id.petition);
- switch (keyCode) {
- case KeyEvent.KEYCODE_COMMA:
- periodWasActivated = false;
- return true;
- case KeyEvent.KEYCODE_SEMICOLON:
- //changes focus onto the question box
- e_question.requestFocus();
- return true;
- case KeyEvent.KEYCODE_PERIOD:
- periodWasActivated = true;
- periodLocation = e_petition.getText().toString().length();
- System.out.println("Period Activated @: " + periodLocation);
- // if (periodWasActivated && (periodLocation == 1)) {
- // e_petition.setText("A", TextView.BufferType.EDITABLE);
- // e_petition.setSelection(e_petition.getText().length());
- // return true;
- // }
- //
- String current = e_petition.getText().toString();
- System.out.println("Key up current @: " + current);
- e_petition.setText(replaceCharsInStringWithDefaultString(current));
- e_petition.setSelection(e_petition.getText().length());
- return true;
- // case KeyEvent.KEYCODE_DEL:
- // return true;
- case KeyEvent.KEYCODE_SLASH:
- if( questionSelected) {
- instantiateAnswer();
- // createDialog();
- loadingIllusionLoader();
- return true;
- }
- return super.onKeyUp(keyCode, event);
- default:
- // System.out.println("keycode");
- // char c = (char) keyCode;
- // System.out.println(c);
- return super.onKeyUp(keyCode, event);
- }
- }
- public String getRandomEmptyResponse() {
- double randomizationDouble = Math.random() * 10;
- String answer = "Try again.";
- if (randomizationDouble <= 1) {
- answer= "Why do you want to know the answer to this question?";
- return answer;
- }
- if (randomizationDouble <= 2) {
- answer= "Petition is not worded properly";
- return answer;
- }
- if (randomizationDouble <= 3) {
- answer= "Insufficient Funds in Account";
- return answer;
- }
- if (randomizationDouble <= 4) {
- answer= "System.out.println(Tell user question is in invalid format)";
- return answer;
- }
- if (randomizationDouble <= 5) {
- answer= "Processors cannot validate such a request.";
- return answer;
- }
- if (randomizationDouble <= 6) {
- answer= "Don't really want to tell you.";
- return answer;
- }
- if (randomizationDouble <= 7) {
- answer= "Wow. Just... wow.";
- return answer;
- }
- if (randomizationDouble <= 8) {
- answer= "I don't care enough to answer YOUR question.";
- return answer;
- }
- if (randomizationDouble <= 9) {
- answer= "Your question isn't even useful for anyone to know.";
- return answer;
- }
- if (randomizationDouble <= 10) {
- answer= "Try again in a moment...";
- return answer;
- }
- if (randomizationDouble <= 11) {
- answer= "Give me back to who was showing you this in the first place. I like them better.";
- return answer;
- }
- return answer;
- }
- public String getRandomTitleResponse() {
- double randomizationDouble = Math.random() * 10;
- String answer = "I got the answer you need!";
- final EditText e_question = (EditText) findViewById(R.id.question);
- if (randomizationDouble <= 1) {
- answer= "Processed the question:";
- return answer;
- }
- if (randomizationDouble <= 2) {
- answer= "Here you go:";
- return answer;
- }
- if (randomizationDouble <= 3) {
- answer= e_question.getText().toString();
- return answer;
- }
- if (randomizationDouble <= 4) {
- answer= e_question.getText().toString();;
- return answer;
- }
- if (randomizationDouble <= 5) {
- answer= e_question.getText().toString();;
- return answer;
- }
- if (randomizationDouble <= 6) {
- answer= e_question.getText().toString();;
- return answer;
- }
- if (randomizationDouble <= 7) {
- answer= "Thank you for a proper question:";
- return answer;
- }
- if (randomizationDouble <= 8) {
- answer= "Good question:";
- return answer;
- }
- if (randomizationDouble <= 9) {
- answer= "Useful Question:";
- return answer;
- }
- if (randomizationDouble <= 10) {
- answer= "Processed";
- return answer;
- }
- if (randomizationDouble <= 11) {
- answer= "Got your answer";
- return answer;
- }
- return answer;
- }
- public void instantiateAnswer(){
- final EditText e_petition = (EditText) findViewById(R.id.petition);
- if (theAnswer.isEmpty()) {
- theAnswer = getRandomEmptyResponse();
- }
- //check if the LAST char in answer is a comma, get rid of it
- if (theAnswer.endsWith(",")) {
- theAnswer = removeJustTheLastCharInString(theAnswer);
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- // getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement