RingActivity.java 7.1 KB

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