tuttelikz

MainActivity [FirebasePresent]

Nov 29th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.90 KB | None | 0 0
  1. package com.soyalab.askaruly.lor;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.graphics.Color;
  6. import android.media.AudioFormat;
  7. import android.media.AudioManager;
  8. import android.media.AudioTrack;
  9. import android.support.annotation.NonNull;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.os.Bundle;
  12. import android.util.Log;
  13. import android.view.MotionEvent;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.ImageView;
  17. import android.widget.SeekBar;
  18.  
  19. import com.google.firebase.auth.FirebaseAuth;
  20. import com.hanks.htextview.base.HTextView;
  21. import com.sdsmdg.harjot.crollerTest.Croller;
  22. import com.skyfishjy.library.RippleBackground;
  23.  
  24. import static java.lang.Math.abs;
  25.  
  26. public class MainActivity extends AppCompatActivity {
  27.  
  28.     Thread t; //audio processing thread
  29.     int sr = 44100; //sampling rate
  30.     boolean isRunning = true; //audio on off
  31.     //private SeekBar volumeSeekbar = null;
  32.     private SeekBar volumeSeekbar = null;
  33.     private AudioManager audioManager = null;
  34.     Croller croller;
  35.     int sliderval = 0;
  36.     int crolprogress = 0;
  37.     int trackButPress = 0;
  38.  
  39.  
  40.     /*public double fr = 440.f;*/
  41.     public double fr;
  42.     public int frHigher;
  43.     public int frLower;
  44.     public int counter = 0;
  45.     public int frCounter = 0;
  46.     public int highCount = 0;
  47.     public int max_frequency = 0;
  48.     public int min_frequency = 0;
  49.     ImageView finishBut;
  50.     Button logoutButton;
  51.     Button resetButton;
  52.     FirebaseAuth mAuth;
  53.     FirebaseAuth.AuthStateListener mAuthListener;
  54.  
  55.     @Override
  56.     protected void onStart() {
  57.         super.onStart();
  58.         mAuth.addAuthStateListener(mAuthListener);
  59.     }
  60.  
  61.     @Override
  62.     protected void onCreate(Bundle savedInstanceState) {
  63.         super.onCreate(savedInstanceState);
  64.         setContentView(R.layout.activity_main);
  65.  
  66.         logoutButton = (Button)findViewById(R.id.logoutBtn);
  67.         resetButton = (Button)findViewById(R.id.resetBtn);
  68.         mAuth = FirebaseAuth.getInstance();
  69.  
  70.         mAuthListener = new FirebaseAuth.AuthStateListener() {
  71.             @Override
  72.             public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  73.                 if (firebaseAuth.getCurrentUser() == null) {
  74.  
  75.                     startActivity(new Intent(MainActivity.this,EnterActivity.class));
  76.  
  77.                 }
  78.             }
  79.         };
  80.  
  81.         logoutButton.setOnClickListener(new View.OnClickListener() {
  82.             @Override
  83.             public void onClick(View view) {
  84.                 mAuth.signOut();
  85.             }
  86.         });
  87.  
  88.  
  89.         final RippleBackground rippleBackground=(RippleBackground)findViewById(R.id.contents);
  90.         final HTextView animText = (HTextView)findViewById(R.id.animatedText);
  91.         final HTextView animText2 = (HTextView)findViewById(R.id.animatedText2);
  92.         final HTextView instructionText = (HTextView)findViewById(R.id.instruction_text);
  93.  
  94.  
  95.         finishBut = (ImageView) findViewById(R.id.finishButton);
  96.         ImageView kulak=(ImageView)findViewById(R.id.centerImage);
  97.         //final TextView instructionText = (TextView)findViewById(R.id.instruction_text);
  98.         //initControls();
  99.  
  100.         croller = (Croller) findViewById(R.id.croller);
  101.         croller.setProgress(0);
  102.  
  103.  
  104.         resetButton.setOnClickListener(new View.OnClickListener() {
  105.             @Override
  106.             public void onClick(View view) {
  107.                 counter = 0;
  108.                 croller.setProgress(counter);
  109.                 //trackButPress = trackButPress-1;
  110.                 if ((trackButPress % 2) == 0) {
  111.                     //instructionText.setText(R.string.evenPress);
  112.                     instructionText.animateText("Test 1. Press until start hearing");
  113.                 }
  114.                 else {
  115.                     //instructionText.setText(R.string.oddPress);
  116.                     instructionText.animateText("Test 2. Press until stop hearing");
  117.                 }
  118.             }
  119.         });
  120.  
  121.         volumeSeekbar = (SeekBar)findViewById(R.id.volumeControl);
  122.         audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  123.         volumeSeekbar.setMax(audioManager
  124.                 .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
  125.         volumeSeekbar.setProgress(audioManager
  126.                 .getStreamVolume(AudioManager.STREAM_MUSIC));
  127.  
  128.         kulak.setOnTouchListener(new View.OnTouchListener() {
  129.             @Override
  130.             public boolean onTouch(View v, MotionEvent event) {
  131.                 String both;
  132.                 String both2;
  133.  
  134.                 switch (event.getAction()) {
  135.                     case MotionEvent.ACTION_DOWN:
  136.                         if ((trackButPress % 2) == 0) {
  137.  
  138.                             isRunning = true;
  139.                             counter = 0;
  140.                             croller.setProgressPrimaryColor(Color.parseColor("#42f4d4"));
  141.  
  142.                             croller.setStartOffset(0);
  143.                             croller.setProgress(0);
  144.  
  145.  
  146.                             rippleBackground.startRippleAnimation();
  147.                             t = new Thread() {
  148.                                 public void run() {
  149.                                     int buffsize = AudioTrack.getMinBufferSize(sr,
  150.                                             AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  151.                                     AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
  152.                                             sr, AudioFormat.CHANNEL_OUT_MONO,
  153.                                             AudioFormat.ENCODING_PCM_16BIT, buffsize,
  154.                                             AudioTrack.MODE_STREAM);
  155.  
  156.                                     short samples[] = new short[buffsize];  //audio synthesis
  157.                                     int amp = 10000;
  158.  
  159.                                     double twopi = 8.*Math.atan(1.);
  160.                                     double ph = 0.0;
  161.                                     audioTrack.play();      //audio running
  162.                                     while(isRunning){   //synthesis loop
  163.                                         //frHigher = 2*counter;
  164.                                         frHigher = 2*frCounter;
  165.  
  166.                                         Log.d("ADebugTag", "highCount/2: " + Double.toString(frHigher));
  167.  
  168.                                         for(int i=0; i < buffsize; i++){
  169.                                             samples[i] = (short) (amp*Math.sin(ph));
  170.  
  171.                                             ph += twopi*frHigher/sr;
  172.  
  173.                                             volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
  174.                                             {
  175.                                                 @Override
  176.                                                 public void onStopTrackingTouch(SeekBar arg0)
  177.                                                 {
  178.                                                 }
  179.  
  180.                                                 @Override
  181.                                                 public void onStartTrackingTouch(SeekBar arg0)
  182.                                                 {
  183.                                                 }
  184.  
  185.                                                 @Override
  186.                                                 public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
  187.                                                 {
  188.                                                     audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
  189.                                                             progress, 0);
  190.  
  191.                                                 }
  192.                                             });
  193.                                         }
  194.                                         audioTrack.write(samples, 0, buffsize);
  195.  
  196.                                         runOnUiThread(new Runnable() {
  197.                                             @Override
  198.                                             public void run() {
  199.  
  200.                                                 croller.setProgress(counter);
  201.                                             }
  202.                                         });
  203.                                         counter++;
  204.                                         if (counter %10 == 0) {
  205.                                             frCounter = counter;
  206.                                         }
  207.  
  208.                                         if (frHigher >= 200) {
  209.                                             isRunning = false;
  210.                                             return;
  211.                                         }
  212.                                     }
  213.                                     highCount = counter;
  214.  
  215.                                     audioTrack.stop();
  216.                                     audioTrack.release();
  217.  
  218.                                 }
  219.                             };
  220.  
  221.                             t.start();
  222.  
  223.                             trackButPress++;
  224.  
  225.                             return true;
  226.                         }
  227.  
  228.                         else {
  229.                             isRunning = true;
  230.                             Log.d("ADebugTag", "highCount/2: " + Double.toString(highCount/2));
  231.  
  232.                             highCount = counter;
  233.                             Log.d("ADebugTag", "highCount: " + Double.toString(highCount));
  234.                             counter = 0;
  235.  
  236.                             croller.setProgressPrimaryColor(Color.parseColor("#ffb3ff"));
  237.                             //croller.setProgress(crolprogress);
  238.                             croller.setProgress(0);
  239.                             croller.setStartOffset(0);
  240.                             rippleBackground.startRippleAnimation();
  241.  
  242.  
  243.                             t = new Thread() {
  244.                                 public void run() {
  245.                                     int buffsize = AudioTrack.getMinBufferSize(sr,
  246.                                             AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  247.                                     AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
  248.                                             sr, AudioFormat.CHANNEL_OUT_MONO,
  249.                                             AudioFormat.ENCODING_PCM_16BIT, buffsize,
  250.                                             AudioTrack.MODE_STREAM);
  251.  
  252.                                     short samples[] = new short[buffsize];  //audio synthesis
  253.                                     int amp = 10000;
  254.  
  255.                                     double twopi = 8.*Math.atan(1.);
  256.                                     double ph = 0.0;
  257.                                     audioTrack.play();      //audio running
  258.  
  259.                                     while(isRunning){   //synthesis loop
  260.  
  261.                                         sliderval = counter;
  262.                                         frLower =  100*sliderval;
  263.  
  264.                                         for(int i=0; i < buffsize; i++){
  265.                                             samples[i] = (short) (amp*Math.sin(ph));
  266.                                             ph += twopi*frLower/sr;
  267.                                             volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
  268.                                             {
  269.                                                 @Override
  270.                                                 public void onStopTrackingTouch(SeekBar arg0)
  271.                                                 {
  272.                                                 }
  273.  
  274.                                                 @Override
  275.                                                 public void onStartTrackingTouch(SeekBar arg0)
  276.                                                 {
  277.                                                 }
  278.  
  279.                                                 @Override
  280.                                                 public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
  281.                                                 {
  282.                                                     audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
  283.                                                             progress, 0);
  284.  
  285.                                                 }
  286.                                             });
  287.                                         }
  288.                                         audioTrack.write(samples, 0, buffsize);
  289.  
  290.                                         runOnUiThread(new Runnable() {
  291.                                             @Override
  292.                                             public void run() {
  293.  
  294.                                                 crolprogress = counter/2;
  295.                                                 croller.setProgress(crolprogress);
  296.                                             }
  297.                                         });
  298.                                         counter++;
  299.                                         highCount++;
  300.                                         if (frLower >= 20000) {
  301.                                             //counter = 0;
  302.                                             isRunning = false;
  303.                                             //rippleBackground.stopRippleAnimation();
  304.                                             return;
  305.                                         }
  306.                                     }
  307.  
  308.                                     audioTrack.stop();
  309.                                     audioTrack.release();
  310.  
  311.                                     //////////////////////////////
  312.  
  313.                                     //////////////////////////////
  314.  
  315.                                 }
  316.                             };
  317.  
  318.                             t.start();
  319.                             trackButPress++;
  320.  
  321.                             return true;
  322.                         }
  323.  
  324.                     case MotionEvent.ACTION_UP:
  325.                         rippleBackground.stopRippleAnimation();
  326.                         if ((trackButPress % 2) == 0) {
  327.                             //instructionText.setText(R.string.evenPress);
  328.                             instructionText.animateText("Test 1. Press until start hearing");
  329.                         }
  330.                         else {
  331.                             //instructionText.setText(R.string.oddPress);
  332.                             instructionText.animateText("Test 2. Press until stop hearing");
  333.                         }
  334.  
  335.                         max_frequency = frLower;
  336.                         min_frequency = frHigher;
  337.                         if (trackButPress%2 == 0) {
  338.  
  339.                             both2 = "Min: " + String.valueOf(min_frequency) + " Hz";
  340.                             both = "Max: " + String.valueOf(max_frequency) + " Hz";
  341.  
  342.                             animText.animateText(both2);
  343.                             animText2.animateText(both);
  344.                         }
  345.  
  346.                         isRunning = false;
  347.                         t = null;
  348.  
  349.  
  350.                         return true;
  351.                 }
  352.  
  353.                 return false;
  354.             }
  355.         });
  356.     }
  357.  
  358.  
  359.  
  360.  
  361. /*    private void initControls()
  362.     {
  363.         try
  364.         {
  365.             volumeSeekbar = (SeekBar)findViewById(R.id.volumeControl);
  366.             audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  367.             volumeSeekbar.setMax(audioManager
  368.                     .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
  369.             volumeSeekbar.setProgress(audioManager
  370.                     .getStreamVolume(AudioManager.STREAM_MUSIC));
  371.  
  372.  
  373.             volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
  374.             {
  375.                 @Override
  376.                 public void onStopTrackingTouch(SeekBar arg0)
  377.                 {
  378.                 }
  379.  
  380.                 @Override
  381.                 public void onStartTrackingTouch(SeekBar arg0)
  382.                 {
  383.                 }
  384.  
  385.                 @Override
  386.                 public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
  387.                 {
  388.                     audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
  389.                             progress, 0);
  390.  
  391.                 }
  392.             });
  393.  
  394.         }
  395.         catch (Exception e)
  396.         {
  397.             e.printStackTrace();
  398.         }
  399.     }*/
  400.  
  401.  
  402.     public void lastpage(View v) {
  403.  
  404.         String maxfreq = String.valueOf(max_frequency);
  405.         String minfreq = String.valueOf(min_frequency);
  406.         Intent intent = new Intent(MainActivity.this,ResultActivity.class);
  407.         Bundle bundle = new Bundle();
  408.         bundle.putString("maxfreq",maxfreq);
  409.         bundle.putString("minfreq",minfreq);
  410.         intent.putExtras(bundle);
  411.         startActivity(intent);
  412.  
  413.  
  414. /*        Intent initi = new Intent(MainActivity.this, ResultActivity.class);
  415.  
  416.         startActivity(initi);*/
  417.     }
  418.  
  419. }
Advertisement
Add Comment
Please, Sign In to add comment