Created
November 11, 2015 02:22
-
-
Save jlwatkins/b071d245b754caae29de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BaseActivity extends AppCompatActivity { | |
private AudioManager mAudioManager; | |
private TelephonyManager mTelephonyManager; | |
private boolean muteSound; | |
final String TAG = "mute"; | |
private PhoneStateListener phoneStateListener = new PhoneStateListener() { | |
@Override | |
public void onCallStateChanged(int state, String incomingNumber) { | |
if (state == TelephonyManager.CALL_STATE_RINGING) { //Incoming call: Pause music | |
Toast.makeText(BaseActivity.this, "INCOMING CALL", Toast.LENGTH_SHORT).show(); | |
Log.v(TAG, "INCOMING CALL"); | |
onPhoneCallInterrupt(); | |
} else if (state == TelephonyManager.CALL_STATE_IDLE) { | |
//Not in call: Play music | |
Log.v(TAG, "NOT IN A CALL"); | |
} else if (state == TelephonyManager.CALL_STATE_OFFHOOK) { //A call is dialing, active or on hold | |
Log.v(TAG, "CALL IS ACTIVE!"); | |
Toast.makeText(BaseActivity.this, "DIALING", Toast.LENGTH_SHORT).show(); | |
onPhoneCallInterrupt(); | |
} | |
} | |
}; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_base); | |
setVolumeControlStream(AudioManager.STREAM_MUSIC); | |
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); | |
mTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); | |
// Recommend storing this in shared preferences and loading from there | |
muteSound = false; | |
} | |
public boolean isMuted() { | |
return muteSound; | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
tManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); | |
} | |
@Override | |
protected void onPause() { | |
tManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE); | |
super.onPause(); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_base, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case R.id.muteMenu: | |
if (isMuted()) { //If it's odd UNMUTE | |
unMute(item); | |
} else { //If its even MUTE | |
mute(item); | |
} | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
protected void unMute(MenuItem item) { | |
muteSound = false; | |
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, userVolumeOnStart.userVolume, 0); | |
item.setTitle("MUTE"); | |
Toast.makeText(BaseActivity.this, "UNMUTED", Toast.LENGTH_SHORT).show(); | |
Log.v(TAG, variableForMute.x + "UNMUTED"); | |
} | |
protected void mute(MenuItem item) { | |
muteSound = true; | |
userVolumeOnStart.userVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING); | |
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0); | |
item.setTitle("UNMUTE"); | |
Toast.makeText(BaseActivity.this, "MUTED", Toast.LENGTH_SHORT).show(); | |
Log.v(TAG, variableForMute.x + "MUTED"); | |
} | |
protected void stopTimer() { | |
Log.v(TAG, "Timer Stopping"); | |
TwentySeconds.stopTimer(); | |
} | |
protected void onPhoneCallInterrupt() { | |
Log.v(TAG, "Phone Call Interruption"); | |
stopTimer(); | |
if(this instanceOf Main_Menu.class) { | |
// Do nothing we are already in main menu | |
} else { | |
Intent i = new Intent(this, Main_Menu.class); | |
startActivity(i); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment