tuttelikz

HearingFrequenciesFragment [FirebasePresent]

Nov 29th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.81 KB | None | 0 0
  1. package com.soyalab.askaruly.lor;
  2.  
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.graphics.Color;
  7. import android.icu.text.MessagePattern;
  8. import android.media.AudioFormat;
  9. import android.media.AudioManager;
  10. import android.media.AudioTrack;
  11. import android.os.Bundle;
  12. import android.support.annotation.Nullable;
  13. import android.support.v4.app.Fragment;
  14. import android.support.v7.app.AlertDialog;
  15. import android.util.Log;
  16. import android.view.LayoutInflater;
  17. import android.view.MotionEvent;
  18. import android.view.View;
  19. import android.view.ViewGroup;
  20. import android.widget.Button;
  21. import android.widget.ImageView;
  22. import android.widget.SeekBar;
  23.  
  24. import com.gitonway.lee.niftymodaldialogeffects.lib.Effectstype;
  25. import com.gitonway.lee.niftymodaldialogeffects.lib.NiftyDialogBuilder;
  26. import com.gitonway.lee.niftymodaldialogeffects.lib.effects.Fall;
  27. import com.gitonway.lee.niftymodaldialogeffects.lib.effects.Shake;
  28. import com.google.firebase.auth.FirebaseAuth;
  29. import com.hanks.htextview.base.HTextView;
  30. import com.sdsmdg.harjot.crollerTest.Croller;
  31. import com.skyfishjy.library.RippleBackground;
  32.  
  33.  
  34. public class HearingFrequencyFragment extends Fragment {
  35.  
  36. public static HearingFrequencyFragment newInstance() {
  37. return new HearingFrequencyFragment();
  38. }
  39.  
  40. Thread t; //audio processing thread
  41. int sr = 44100; //sampling rate
  42. boolean isRunning = true; //audio on off
  43. private AudioManager audioManager = null;
  44. Croller croller;
  45. int sliderval = 0;
  46. int crolprogress = 0;
  47. boolean test1going = true;
  48. boolean test2going = false;
  49. boolean noReset = true;
  50. int percentageHearing = 0;
  51. /*public double fr = 440.f;*/
  52. public double fr;
  53. public int frHigher;
  54. public int frLower;
  55. public int counter = 0;
  56. public int frCounter = 0;
  57. public int highCount = 0;
  58. public int max_frequency = 0;
  59. public int min_frequency = 0;
  60. ImageView finishBut;
  61. Button logoutButton;
  62. //Button resetButton;
  63. FirebaseAuth mAuth;
  64. FirebaseAuth.AuthStateListener mAuthListener;
  65.  
  66. boolean testFinished = false;
  67.  
  68. @Nullable
  69. @Override
  70. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  71. View view = inflater.inflate(R.layout.fragment_hearing_frequency, container, false);
  72.  
  73. return view;
  74. }
  75.  
  76. @Override
  77. public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
  78. super.onViewCreated(view, savedInstanceState);
  79.  
  80. //resetButton = (Button)view.findViewById(R.id.resetBtn);
  81. finishBut = (ImageView)view.findViewById(R.id.finishButton);
  82. croller = (Croller)view.findViewById(R.id.croller);
  83. audioManager = (AudioManager)getActivity().getSystemService(Context.AUDIO_SERVICE);
  84.  
  85.  
  86. final RippleBackground rippleBackground=(RippleBackground)view.findViewById(R.id.contents);
  87. final HTextView animText = (HTextView)view.findViewById(R.id.animatedText);
  88. final HTextView animText2 = (HTextView)view.findViewById(R.id.animatedText2);
  89. final HTextView instructionText = (HTextView) getActivity().findViewById(R.id.instruction_text);
  90. final ImageView kulak=(ImageView)view.findViewById(R.id.centerImage);
  91.  
  92. croller.setProgress(0);
  93. croller.setIsContinuous(false);
  94.  
  95. /* resetButton.setOnClickListener(new View.OnClickListener() {
  96. @Override
  97. public void onClick(View view) {
  98. counter = 0;
  99. croller.setProgress(counter);
  100. //trackButPress = trackButPress-1;
  101.  
  102. test1going = true;
  103. test2going = false;
  104. instructionText.animateText("Test 1. Press until start hearing");
  105. kulak.setEnabled(true);
  106.  
  107. animText.animateText("");
  108. animText2.animateText("");
  109.  
  110. }
  111. });*/
  112.  
  113. kulak.setOnTouchListener(new View.OnTouchListener() {
  114. @Override
  115. public boolean onTouch(View v, MotionEvent event) {
  116. String both;
  117. String both2;
  118.  
  119. switch (event.getAction()) {
  120. case MotionEvent.ACTION_DOWN:
  121.  
  122. if (test1going == true && test2going == false) {
  123.  
  124.  
  125. isRunning = true;
  126. Log.d("ADebugTag", "highCount/2: " + Double.toString(highCount/2));
  127. Log.d("ADebugTag", "highCount: " + Double.toString(highCount));
  128. //highCount = counter;
  129.  
  130. counter = 0;
  131.  
  132. croller.setProgressPrimaryColor(Color.parseColor("#ffb3ff"));
  133. //croller.setProgress(crolprogress);
  134. croller.setProgress(0);
  135. croller.setStartOffset(0);
  136. rippleBackground.startRippleAnimation();
  137.  
  138. t = new Thread() {
  139. public void run() {
  140. int buffsize = AudioTrack.getMinBufferSize(sr,
  141. AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  142. AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
  143. sr, AudioFormat.CHANNEL_OUT_MONO,
  144. AudioFormat.ENCODING_PCM_16BIT, buffsize,
  145. AudioTrack.MODE_STREAM);
  146.  
  147. short samples[] = new short[buffsize]; //audio synthesis
  148. int amp = 10000;
  149.  
  150. double twopi = 8.*Math.atan(1.);
  151. double ph = 0.0;
  152. audioTrack.play(); //audio running
  153.  
  154. while(isRunning){ //synthesis loop
  155.  
  156. sliderval = counter;
  157. frLower = 100*sliderval;
  158.  
  159. for(int i=0; i < buffsize; i++){
  160. samples[i] = (short) (amp*Math.sin(ph));
  161. ph += twopi*frLower/sr;
  162. }
  163.  
  164. audioTrack.write(samples, 0, buffsize);
  165.  
  166. getActivity().runOnUiThread(new Runnable() {
  167. @Override
  168. public void run() {
  169. crolprogress = counter/2;
  170. croller.setProgress(crolprogress);
  171. }
  172. });
  173. counter++;
  174. //highCount++;
  175. if (frLower >= 20000) {
  176. //counter = 0;
  177. isRunning = false;
  178. //rippleBackground.stopRippleAnimation();
  179. return;
  180. }
  181. }
  182.  
  183. audioTrack.stop();
  184. audioTrack.release();
  185.  
  186. }
  187. };
  188.  
  189. t.start();
  190.  
  191. test1going = false;
  192. test2going = true;
  193.  
  194. testFinished = true;
  195. return true;
  196.  
  197. }
  198.  
  199.  
  200. else if (test2going == true && test1going == false){
  201. //crolprogress = 0;
  202. testFinished = true;
  203. isRunning = true;
  204. counter = 1000;
  205. croller.setProgressPrimaryColor(Color.parseColor("#42f4d4"));
  206.  
  207. croller.setStartOffset(0);
  208. croller.setProgress(0);
  209.  
  210. rippleBackground.startRippleAnimation();
  211. t = new Thread() {
  212. public void run() {
  213. int buffsize = AudioTrack.getMinBufferSize(sr,
  214. AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  215. AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
  216. sr, AudioFormat.CHANNEL_OUT_MONO,
  217. AudioFormat.ENCODING_PCM_16BIT, buffsize,
  218. AudioTrack.MODE_STREAM);
  219.  
  220. short samples[] = new short[buffsize]; //audio synthesis
  221. int amp = 10000;
  222.  
  223. double twopi = 8.*Math.atan(1.);
  224. double ph = 0.0;
  225. audioTrack.play(); //audio running
  226. while(isRunning){ //synthesis loop
  227. sliderval = counter;
  228. frHigher = sliderval;
  229.  
  230. Log.d("ADebugTag", "highCount/2: " + Double.toString(frHigher));
  231.  
  232. for(int i=0; i < buffsize; i++){
  233. samples[i] = (short) (amp*Math.sin(ph));
  234. ph += twopi*frHigher/sr;
  235. }
  236.  
  237. audioTrack.write(samples, 0, buffsize);
  238.  
  239. getActivity().runOnUiThread(new Runnable() {
  240. @Override
  241. public void run() {
  242. crolprogress = Math.abs(counter/10);
  243. croller.setProgress(crolprogress);
  244. }
  245. });
  246. counter = counter-10;
  247. /* if (counter % 10 == 0) {
  248. frHigher = counter;
  249. }*/
  250.  
  251. if (frHigher <= 10) {
  252. isRunning = false;
  253. return;
  254. }
  255. }
  256. highCount = counter;
  257.  
  258. audioTrack.stop();
  259. audioTrack.release();
  260.  
  261. }
  262. };
  263.  
  264. t.start();
  265.  
  266. test1going = true;
  267. test2going = false;
  268.  
  269. //String maxfreq = String.valueOf(max_frequency);
  270. //String minfreq = String.valueOf(min_frequency);
  271.  
  272.  
  273. return true;
  274.  
  275.  
  276. }
  277.  
  278. else {
  279.  
  280. }
  281.  
  282. case MotionEvent.ACTION_UP:
  283. rippleBackground.stopRippleAnimation();
  284. if (test1going == false && test2going == true) {
  285. instructionText.animateText("Test 2. Press until stop hearing");
  286. }
  287. else if (testFinished == true) {
  288. /* if (testFinished == true) {
  289. instructionText.animateText("Test is complete.");
  290. kulak.setEnabled(false);
  291. testFinished = true;
  292. }*/
  293.  
  294. instructionText.animateText("Test is complete.");
  295. kulak.setEnabled(false);
  296. testFinished = true;
  297.  
  298. max_frequency = frLower;
  299. min_frequency = frHigher;
  300. percentageHearing = (max_frequency-min_frequency)/200;
  301.  
  302. /*String alert1Min = "Min Frequency: " + String.valueOf(min_frequency);
  303. String alert2Max = "Max Frequency: " + String.valueOf(max_frequency);
  304. String alertPercentage = "Percentage: " + String.valueOf(percentageHearing);*/
  305.  
  306. String alertHearing = "You hear in: " + String.valueOf(min_frequency) + " - " + String.valueOf(max_frequency) + " Hz range";
  307. //String alert2Max = "Max Frequency: " + String.valueOf(max_frequency);
  308. String alertPercentage = "Acuity: " + String.valueOf(percentageHearing) + "%";
  309. //String alert3 = "No. of losses: " + counterneg;
  310. //alertDialog.setMessage(alert1 +"\n"+ alert2 +"\n"+ alert3);
  311.  
  312. final NiftyDialogBuilder dialogBuilder=NiftyDialogBuilder.getInstance(getActivity());
  313.  
  314. dialogBuilder
  315. .withTitle("Hearing Performance") //.withTitle(null) no title
  316. .withTitleColor("#FFFFFF") //def
  317. .withDividerColor("#11000000") //def
  318. .withMessage(alertHearing + "\n\n" + alertPercentage) //.withMessage(null) no Msg
  319. .withMessageColor("#FFFFFFFF") //def | withMessageColor(int resid)
  320. .withDialogColor("#FFE74C3C") //def | withDialogColor(int resid)
  321. //.withIcon(getResources().getDrawable(R.drawable.icon))
  322. .withDuration(700) //def
  323. .withEffect(Effectstype.Fall) //def Effectstype.Slidetop
  324. .withButton1Text("MAIN MENU") //def gone
  325. .withButton2Text("SHARE") //def gone
  326. .isCancelableOnTouchOutside(true) //def | isCancelable(true)
  327. //.setCustomView(R.layout.custom_view,v.getContext()) //.setCustomView(View or ResId,context)
  328. .setButton1Click(new View.OnClickListener() {
  329. @Override
  330. public void onClick(View v) {
  331. //Toast.makeText(v.getContext(), "i'm btn1", Toast.LENGTH_SHORT).show();
  332.  
  333.  
  334. String maxfreq = String.valueOf(max_frequency);
  335. String minfreq = String.valueOf(min_frequency);
  336. Intent intent = new Intent(getActivity(),HearingFreqShare.class);
  337. Bundle bundle = new Bundle();
  338. bundle.putString("maxfreq",maxfreq);
  339. bundle.putString("minfreq",minfreq);
  340. intent.putExtras(bundle);
  341. startActivity(intent);
  342.  
  343.  
  344.  
  345. /* counter = 0;
  346. croller.setProgress(counter);
  347. test1going = true;
  348. test2going = false;
  349. instructionText.animateText("Test 1. Press until start hearing");
  350. kulak.setEnabled(true);
  351. animText.animateText("");
  352. animText2.animateText("");
  353. dialogBuilder.dismiss();*/
  354.  
  355.  
  356.  
  357. /* counter = 0;
  358. croller.setProgress(counter);
  359. //trackButPress = trackButPress-1;
  360. test1going = true;
  361. test2going = false;
  362. instructionText.animateText("Test 1. Press until start hearing");
  363. kulak.setEnabled(true);
  364.  
  365. animText.animateText("");
  366. animText2.animateText("");*/
  367.  
  368. }
  369. })
  370. .setButton2Click(new View.OnClickListener() {
  371. @Override
  372. public void onClick(View v) {
  373. String maxfreq = String.valueOf(max_frequency);
  374. String minfreq = String.valueOf(min_frequency);
  375. String perc = String.valueOf(percentageHearing);
  376. Intent intent = new Intent(getActivity(),HearingFreqShare.class);
  377. Bundle bundle = new Bundle();
  378. bundle.putString("maxfreq",maxfreq);
  379. bundle.putString("minfreq",minfreq);
  380. bundle.putString("perc",perc);
  381. intent.putExtras(bundle);
  382. startActivity(intent);
  383. }
  384. })
  385.  
  386. .show();
  387.  
  388. /* else {
  389. instructionText.animateText("Test 1. Press until start hearing");
  390. }*/
  391.  
  392. }
  393.  
  394. else {
  395.  
  396. }
  397.  
  398. if (test2going == false && test1going == true) {
  399.  
  400. both2 = "Min: " + String.valueOf(min_frequency) + " Hz";
  401. both = "Max: " + String.valueOf(max_frequency) + " Hz";
  402.  
  403. animText.animateText(both2);
  404. animText2.animateText(both);
  405. }
  406.  
  407. isRunning = false;
  408. t = null;
  409.  
  410.  
  411. return true;
  412. }
  413.  
  414. return false;
  415. }
  416. });
  417.  
  418. finishBut.setOnClickListener(new View.OnClickListener(){
  419. public void onClick(View view) {
  420. if (testFinished == true) {
  421. String maxfreq = String.valueOf(max_frequency);
  422. String minfreq = String.valueOf(min_frequency);
  423. Intent intent = new Intent(getActivity(),ResultActivity.class);
  424. Bundle bundle = new Bundle();
  425. bundle.putString("maxfreq",maxfreq);
  426. bundle.putString("minfreq",minfreq);
  427. intent.putExtras(bundle);
  428. startActivity(intent);
  429. }
  430. else {
  431. AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
  432. alertDialog.setTitle("Alert");
  433. alertDialog.setMessage("Please finish both tests");
  434. alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
  435. new DialogInterface.OnClickListener() {
  436. public void onClick(DialogInterface dialog, int which) {
  437. dialog.dismiss();
  438. }
  439. });
  440. alertDialog.show();
  441. }
  442. }
  443. });
  444.  
  445. }
  446. }
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455. /*
  456.  
  457.  
  458. private SeekBar volumeSeekbar = null;
  459. volumeSeekbar = (SeekBar)view.findViewById(R.id.volumeControl);
  460.  
  461. volumeSeekbar.setMax(audioManager
  462. .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
  463. volumeSeekbar.setProgress(audioManager
  464. .getStreamVolume(AudioManager.STREAM_MUSIC));
  465.  
  466. volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
  467. {
  468. @Override
  469. public void onStopTrackingTouch(SeekBar arg0)
  470. {
  471. }
  472.  
  473. @Override
  474. public void onStartTrackingTouch(SeekBar arg0)
  475. {
  476. }
  477.  
  478. @Override
  479. public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
  480. {
  481. audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
  482. progress, 0);
  483.  
  484. }
  485. });*/
Advertisement
Add Comment
Please, Sign In to add comment