Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AutoSeekBar.java
- ----------------
- package com.example.mohamadpc.horserace;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Color;
- import android.graphics.drawable.BitmapDrawable;
- import android.graphics.drawable.Drawable;
- import android.util.AttributeSet;
- import java.util.Random;
- /**
- * Created by MOHAMADPC on 18/09/2017.
- */
- public class autoSeekBar extends android.support.v7.widget.AppCompatSeekBar implements Runnable {
- private static Random rnd = new Random();
- // speed parameters: time to wait after each step
- private static int minTime = 150;
- private static int maxTime = 400;
- // progress changes in one step
- private static int step = 1;
- // auto or manual progress
- private boolean isAuto;
- public autoSeekBar(Context context, AttributeSet attrs) {
- super(context, attrs);
- this.setBackgroundColor(Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
- this.setProgressDrawable(getBackground());
- this.setMax(20);
- isAuto = false;
- }
- public autoSeekBar(Context context, int imageId) {
- this(context, null);
- Drawable thumb = getContext().getDrawable(imageId);
- Bitmap b = ((BitmapDrawable) thumb).getBitmap();
- Bitmap bitmapResized = Bitmap.createScaledBitmap(b, 50, 50, false);
- thumb = new BitmapDrawable(getResources(), bitmapResized);
- setThumb(thumb);
- }
- public autoSeekBar(Context context) {
- this(context, null);
- }
- public void autoStart() {
- this.setProgress(0);
- isAuto = true;
- new Thread(this).start();
- }
- @Override
- public void run() {
- while (isAuto) {
- setProgress(Math.min(getProgress() + step, getMax()));
- if (getProgress() == getMax()) {
- isAuto = false;
- ((MainActivity) getContext()).horseFinished(this);
- } else {
- try {
- Thread.sleep(minTime + rnd.nextInt(maxTime - minTime + 1));
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- MainActivity.java
- -----------------
- package com.example.mohamadpc.horserace;
- import android.content.ActivityNotFoundException;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.speech.RecognizerIntent;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.View;
- import android.widget.LinearLayout;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.Locale;
- import java.util.Vector;
- public class MainActivity extends AppCompatActivity {
- //codes for startActivityForResult
- final int AUDIO_NUMBER_RESULT = 100;
- final int AUDIO_RUN_RESULT = 101;
- LinearLayout llBackGround;
- private int horsesCount = -1;
- private autoSeekBar[] myHorses;
- private boolean isRacing;
- Vector<autoSeekBar> arrivedHorses = new Vector<autoSeekBar>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- }
- public void setPointer() {
- llBackGround = (LinearLayout) findViewById(R.id.llyBackGround);
- llBackGround.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (horsesCount == -1) {
- getNumberBySpeech();
- } else {
- if (!isRacing) {
- if (arrivedHorses.size() == horsesCount) {
- clearLayout();
- } else {
- getCommandBySpeech();
- }
- }
- }
- }
- });
- isRacing = false;
- }
- private void getNumberBySpeech() {
- try {
- Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
- RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
- intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
- getResources().getString(R.string.speech_prompt) + "\n" + getResources().getString(R.string.tracks_number_prompt));
- startActivityForResult(intent, AUDIO_NUMBER_RESULT);
- } catch (ActivityNotFoundException e) {
- String appPackageName = "com.google.android.googlequicksearchbox";
- try {
- startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
- } catch (ActivityNotFoundException ee) {
- startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
- }
- Toast.makeText(this, "No such activity", Toast.LENGTH_SHORT).show();
- }
- }
- private void getCommandBySpeech() {
- try {
- Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
- RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
- intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
- getResources().getString(R.string.speech_prompt) + "\n" + getResources().getString(R.string.run_command_prompt));
- startActivityForResult(intent, AUDIO_RUN_RESULT);
- } catch (ActivityNotFoundException e) {
- String appPackageName = "com.google.android.googlequicksearchbox";
- try {
- startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
- } catch (ActivityNotFoundException ee) {
- startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
- }
- Toast.makeText(this, "No such activity", Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (resultCode == RESULT_OK) {
- String strRes = "";
- Bundle bundle = data.getExtras();
- final ArrayList<String> matches = bundle.getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
- switch (requestCode) {
- case AUDIO_NUMBER_RESULT:
- for (String item : matches) {
- try {
- Log.d("Speech_Number", item);
- this.horsesCount = Integer.parseInt(item);
- break;
- } catch (Exception e) {
- }
- }
- if (this.horsesCount > 0) {
- setLayout();
- }
- break;
- case AUDIO_RUN_RESULT:
- for (String item : matches) {
- Log.d("Speech_Number", item);
- if (item.toUpperCase().equals(getResources().getString(R.string.run_command_string).toUpperCase())) {
- startRace();
- }
- }
- break;
- }
- }
- }
- private void startRace() {
- arrivedHorses.clear();
- for (int i = 0; i < myHorses.length; i += 1) {
- myHorses[i].autoStart();
- }
- isRacing = true;
- }
- private void setLayout() {
- myHorses = new autoSeekBar[horsesCount];
- for (int i = 0; i < myHorses.length; i += 1) {
- myHorses[i] = newHorse();
- llBackGround.addView(myHorses[i]);
- }
- isRacing = false;
- }
- private void clearLayout() {
- arrivedHorses.clear();
- for (int i = 0; i < myHorses.length; i += 1) {
- llBackGround.removeView(myHorses[i]);
- myHorses[i] = null;
- }
- myHorses = null;
- horsesCount = -1;
- isRacing = false;
- }
- private autoSeekBar newHorse() {
- autoSeekBar newHorse;
- if (isRTL()) {
- newHorse = new autoSeekBar(this, R.drawable.rtl_horse);
- } else {
- newHorse = new autoSeekBar(this, R.drawable.ltr_horse);
- }
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
- params.setMargins(0, 10, 0, 0);
- newHorse.setLayoutParams(params);
- return newHorse;
- }
- public void horseFinished(autoSeekBar horse) {
- arrivedHorses.add(horse);
- if (arrivedHorses.size() == horsesCount) {
- double step = (double) myHorses[0].getMax() / myHorses.length;
- for (int i = 0; i < arrivedHorses.size(); i += 1) {
- arrivedHorses.get(i).setProgress((int) ((arrivedHorses.size() - i) * step));
- }
- isRacing = false;
- }
- }
- private boolean isRTL() {
- return isRTL(Locale.getDefault());
- }
- private boolean isRTL(Locale locale) {
- final int directionality = Character.getDirectionality(locale.getDisplayName().charAt(0));
- return directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT ||
- directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC;
- }
- }
- strings.xml
- -----------
- <resources>
- <string name="app_name">HorseRace</string>
- <string name="speech_prompt">waiting for your words!</string>
- <string name="tracks_number_prompt">Say Number of horses to race</string>
- <string name="run_command_prompt">Say Run to start</string>
- <string name="run_command_string">go</string>
- </resources>
- AndroidManifest.xml
- -------------------
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.mohamadpc.horserace">
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.RECORD_AUDIO" />
- <uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT" />
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement