RingActivity.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package com.jyc.threegames.activity;
  2. import android.app.Dialog;
  3. import android.app.ProgressDialog;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.content.res.AssetFileDescriptor;
  8. import android.media.AudioManager;
  9. import android.media.MediaPlayer;
  10. import android.media.Ringtone;
  11. import android.media.RingtoneManager;
  12. import android.net.Uri;
  13. import android.os.Bundle;
  14. import android.util.Log;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17. import androidx.annotation.NonNull;
  18. import androidx.appcompat.app.AlertDialog;
  19. import com.jyc.threegames.App;
  20. import com.jyc.threegames.R;
  21. import com.jyc.threegames.activity.base.BaseActivity;
  22. import com.jyc.threegames.bean.result.ResGameInfo;
  23. import com.jyc.threegames.controller.GameController;
  24. import com.jyc.threegames.net.SimpleRequest;
  25. import java.io.IOException;
  26. import butterknife.BindView;
  27. import butterknife.OnClick;
  28. import timber.log.Timber;
  29. public class RingActivity extends BaseActivity {
  30. private static final String PARAM_GAME_INFO = "info";
  31. public static final int TYPE_GAME = 0;
  32. public static final int TYPE_SCALE = 1;
  33. @BindView(R.id.label)
  34. TextView mTVLabel;
  35. private ResGameInfo mGameInfo;
  36. private boolean mGoToGameOrScale = false;
  37. private int mCurrentVolume = 0;
  38. private Ringtone mRingTone;
  39. @Override
  40. protected void init(Bundle instance) {
  41. super.init(instance);
  42. App.CAN_PLAY_GAME = false;
  43. if (instance == null)
  44. mGameInfo = getIntent().getParcelableExtra(PARAM_GAME_INFO);
  45. else
  46. mGameInfo = instance.getParcelable(PARAM_GAME_INFO);
  47. if (mGameInfo == null){
  48. finish();
  49. return;
  50. }
  51. mTVLabel.setText(getLabel());
  52. maxVolume();
  53. playRing();
  54. }
  55. @Override
  56. protected void onSaveInstanceState(@NonNull Bundle outState) {
  57. super.onSaveInstanceState(outState);
  58. outState.putParcelable(PARAM_GAME_INFO, mGameInfo);
  59. }
  60. @Override
  61. protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
  62. super.onRestoreInstanceState(savedInstanceState);
  63. mGameInfo = savedInstanceState.getParcelable(PARAM_GAME_INFO);
  64. }
  65. @Override
  66. protected int getRootLayout() {
  67. return R.layout.activity_ring;
  68. }
  69. @Override
  70. protected void onDestroy() {
  71. if (!mGoToGameOrScale)
  72. App.CAN_PLAY_GAME = true;
  73. stopPlay();
  74. restoreVolume();
  75. super.onDestroy();
  76. }
  77. @Override
  78. public void onBackPressed() { }
  79. @OnClick(R.id.now)
  80. public void clickNow(){
  81. mGoToGameOrScale = true;
  82. if (mGameInfo.playGameType == ResGameInfo.GAME_TYPE_GAME){
  83. GameActivity.LAUNCH_GAME(this, mGameInfo);
  84. finish();
  85. }
  86. }
  87. @OnClick(R.id.push_back)
  88. public void clickPushBack(){
  89. String[] items = new String[]{"5分鐘", "10分鐘", "15分鐘", "20分鐘", "25分鐘", "30分鐘"};
  90. new AlertDialog.Builder(this,0)
  91. .setTitle("請選擇延遲時間")
  92. .setItems(items, new DialogInterface.OnClickListener() {
  93. @Override
  94. public void onClick(DialogInterface dialogInterface, int i) {
  95. if (mGameInfo.playGameType == ResGameInfo.GAME_TYPE_GAME){
  96. dialogInterface.dismiss();
  97. Dialog loading = new ProgressDialog(RingActivity.this);
  98. loading.setTitle("正在延遲");
  99. loading.show();
  100. new SimpleRequest<>().request(RingActivity.this,
  101. GameController.getInstance().delayGame(mGameInfo.playGameId, mGameInfo.gameConfigId, getDelayMin(i)), "延遲失敗!請檢查網絡連接狀態", loading, new SimpleRequest.Executor<Object>() {
  102. @Override
  103. public void execute(Object obj) {
  104. Toast.makeText(RingActivity.this, "延遲成功", Toast.LENGTH_LONG).show();
  105. finish();
  106. }
  107. });
  108. }
  109. }
  110. }).create().show();
  111. }
  112. private int getDelayMin(int index){
  113. switch (index){
  114. case 1:
  115. return 10;
  116. case 2:
  117. return 15;
  118. case 3:
  119. return 20;
  120. case 4:
  121. return 25;
  122. case 5:
  123. return 30;
  124. case 0:
  125. default:
  126. return 5;
  127. }
  128. }
  129. private void playRing(){
  130. try {
  131. Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
  132. mRingTone = RingtoneManager.getRingtone(this, notification);
  133. mRingTone.setLooping(true);
  134. mRingTone.play();
  135. } catch (Exception e) {
  136. e.printStackTrace();
  137. Log.e("three_games", "播放鈴聲失敗");
  138. }
  139. }
  140. private void maxVolume(){
  141. mCurrentVolume = getAudioManager().getStreamVolume(AudioManager.STREAM_ALARM);
  142. getAudioManager().setStreamVolume(AudioManager.STREAM_ALARM, getAudioManager().getStreamMaxVolume(AudioManager.STREAM_ALARM), 0);
  143. }
  144. private void restoreVolume(){
  145. getAudioManager().setStreamVolume(AudioManager.STREAM_ALARM, mCurrentVolume, 0);
  146. }
  147. private AudioManager getAudioManager(){
  148. return (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  149. }
  150. private void stopPlay(){
  151. if (mRingTone != null && mRingTone.isPlaying()){
  152. mRingTone.stop();
  153. }
  154. }
  155. private String getLabel(){
  156. return mGameInfo.playGameType == ResGameInfo.GAME_TYPE_GAME ? "您需要做遊戲啦" : "您需要做量表啦";
  157. }
  158. public static void LAUNCH(Context context, ResGameInfo gameInfo){
  159. Intent intent = new Intent(context, RingActivity.class);
  160. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  161. intent.putExtra(PARAM_GAME_INFO, gameInfo);
  162. context.startActivity(intent);
  163. }
  164. }