PageRenderTime 1355ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/src/edu/PrimozRezek/iManager/android/Alarm/FizicnaBudilkaActivity.java

https://github.com/primoz990/PJ-testGuthub
Java | 212 lines | 150 code | 51 blank | 11 comment | 3 complexity | 8ce86eaa7de077623a0cdfed8af53b7c MD5 | raw file
  1. package edu.PrimozRezek.iManager.android.Alarm;
  2. import java.io.BufferedWriter;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import edu.PrimozRezek.iManager.android.R;
  7. import android.app.Activity;
  8. import android.content.BroadcastReceiver;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.content.IntentFilter;
  12. import android.content.SharedPreferences;
  13. import android.content.res.AssetFileDescriptor;
  14. import android.graphics.drawable.GradientDrawable.Orientation;
  15. import android.hardware.Sensor;
  16. import android.hardware.SensorEvent;
  17. import android.hardware.SensorEventListener;
  18. import android.hardware.SensorManager;
  19. import android.media.MediaPlayer;
  20. import android.net.Uri;
  21. import android.os.Bundle;
  22. import android.os.Environment;
  23. import android.os.Handler;
  24. import android.os.Message;
  25. import android.os.PowerManager;
  26. import android.os.SystemClock;
  27. import android.os.PowerManager.WakeLock;
  28. import android.util.Log;
  29. import android.view.Window;
  30. import android.view.WindowManager;
  31. import android.widget.TextView;
  32. public class FizicnaBudilkaActivity extends Activity
  33. {
  34. TextView txtViewStevilo;
  35. //vklop ekrana
  36. PowerManager powMaN;
  37. WakeLock wakeL;
  38. Window window;
  39. //TextView txtViev1;
  40. public SensorManager SenMan;
  41. public MediaPlayer mPlayer = null;
  42. double gravityX=0;
  43. double gravityY=0;
  44. double gravityZ=0;
  45. String rezultati="";
  46. //ZVOK: http://developer.android.com/guide/topics/media/audio-capture.html
  47. //nastavitve
  48. int stevecUdarcev=0;;
  49. int STEVILO_UDARCEV;
  50. public SensorEventListener SenLis = new SensorEventListener() {
  51. @Override
  52. public void onSensorChanged(SensorEvent event)
  53. {
  54. if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER)
  55. {
  56. double alpha = 0.8;
  57. gravityX = alpha * gravityX + (1 - alpha) * event.values[0];
  58. gravityY = alpha * gravityY + (1 - alpha) * event.values[1];
  59. gravityZ = alpha * gravityZ + (1 - alpha) * event.values[2];
  60. double Gx= event.values[0] - gravityX;
  61. double Gy = event.values[1] - gravityY;
  62. double Gz = event.values[2] - gravityZ;
  63. //txtViev1.setText("Gx: "+(int)Gx+"\nGy: "+(int)Gy+"\nGz: "+(int)Gz);
  64. rezultati+="Gx: "+(int)Gx+" Gy: "+(int)Gy+" Gz: "+(int)Gz+"\n";
  65. //if((Gx>3 && Gy>3) || (Gx<-3 && Gy<-3) || (Gx<-3 && Gy>3) || (Gx>3 && Gy<-3))
  66. if(Gx>4)
  67. {
  68. stevecUdarcev++;
  69. if(stevecUdarcev<STEVILO_UDARCEV)
  70. {
  71. txtViewStevilo.setText((STEVILO_UDARCEV-stevecUdarcev)+" ");//prikaziStevilo();
  72. }
  73. else
  74. {
  75. wakeL.release();
  76. System.exit(0);
  77. }
  78. }
  79. }
  80. }
  81. @Override
  82. public void onAccuracyChanged(Sensor sensor, int accuracy) {
  83. }
  84. };
  85. public void prikaziStevilo()
  86. {
  87. txtViewStevilo.setText(STEVILO_UDARCEV-stevecUdarcev);
  88. }
  89. @Override
  90. public void onCreate(Bundle savedInstanceState)
  91. {
  92. super.onCreate(savedInstanceState);
  93. setContentView(R.layout.fizicna_budilka);
  94. txtViewStevilo = (TextView) findViewById(R.id.textViewKolikoKratSe);
  95. SenMan = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  96. SenMan.registerListener(SenLis, SenMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
  97. //vklop ekrana
  98. powMaN = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
  99. wakeL = powMaN.newWakeLock((PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "YourServie");
  100. wakeL.acquire();
  101. //odklep ekrana
  102. window = getWindow();
  103. window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
  104. //preberem nastavitve
  105. SharedPreferences bud_ali_odst = getSharedPreferences("BUDILKA_ALI_ODSTEVALNIK", 0);
  106. String kajIzkljapljamo = bud_ali_odst.getString("ali_budilka_ali_odstevalnik", "budilka");
  107. SharedPreferences zadnje_nastavitve = getSharedPreferences("NASTAVITVE_APP_LAST_SET", 0);
  108. STEVILO_UDARCEV = zadnje_nastavitve.getInt("st_udarcev_"+kajIzkljapljamo, 0); //preberemo st_udarcev za budilko ali za odstevalnik
  109. Uri potdomuzike22 = Uri.parse(zadnje_nastavitve.getString("glasba_"+kajIzkljapljamo, "napaka")); //preberemo izbrano glasbo za budliko ali odstevalnik
  110. txtViewStevilo.setText(STEVILO_UDARCEV+"");
  111. startPlaying(potdomuzike22);
  112. }
  113. @Override
  114. protected void onResume()
  115. {
  116. super.onResume(); //REGISTRIRAM SENZOR
  117. SenMan.registerListener(SenLis, SenMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
  118. }
  119. @Override
  120. protected void onStop()
  121. {
  122. SenMan.unregisterListener(SenLis);
  123. mPlayer.release();
  124. wakeL.release();
  125. super.onStop();
  126. }
  127. private void startPlaying(Uri potDomuzike)
  128. {
  129. mPlayer = new MediaPlayer();
  130. try {
  131. mPlayer.setDataSource(this, potDomuzike);
  132. mPlayer.setLooping(true);
  133. mPlayer.prepare();
  134. mPlayer.start();
  135. } catch (IOException e)
  136. {
  137. startPlayingDefault();
  138. }
  139. }
  140. //če ni izbrane glasbe predvajamo svojo
  141. private void startPlayingDefault()
  142. {
  143. mPlayer = new MediaPlayer();
  144. try
  145. {
  146. //http://androidforums.com/developer-101/205744-play-sound-assets.html
  147. AssetFileDescriptor descriptor = getAssets().openFd("paganini.mp3");
  148. mPlayer.setDataSource( descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength() );
  149. descriptor.close();
  150. mPlayer.setLooping(true);
  151. mPlayer.prepare();
  152. mPlayer.start();
  153. } catch (IllegalStateException e1)
  154. {
  155. Log.e("Napaka", "Pri nalaganju glasbe");
  156. } catch (IOException e2)
  157. {
  158. Log.e("Napaka", "Pri nalaganju glasbe");
  159. }
  160. }
  161. }