Skip to content

Instantly share code, notes, and snippets.

@jlwatkins
Created November 11, 2015 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlwatkins/d55ffc20e2c58df83744 to your computer and use it in GitHub Desktop.
Save jlwatkins/d55ffc20e2c58df83744 to your computer and use it in GitHub Desktop.
public class BaseActivity extends AppCompatActivity {
// Variable Declarations
private AudioManager mAudioManager;
private TelephonyManager mTelephonyManager;
private static final String = "MUTE_STATUS";
protected SharedPreferences mSharedPrefs;
protected boolean muteSound;
final String TAG = "mute";
// UI Declarations
private MenuItem mMuteMenuItem;
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
mSharedPrefs = getSharedPreferences("Whatever", MODE_PRIVATE);
muteSound = getMuteStatus();
// Setup UI items
mMuteMenuItem = (MenuItem) findViewById(R.id.muteMenu);
updateMuteMenuItem();
}
public boolean getMuteStatus() {
muteSound = mSharedPrefs.getBoolean(MUTE_STATUS, false);
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:
changeMuteStatus();
break;
}
return super.onOptionsItemSelected(item);
}
protected void changeMuteStatus() {
if (getMuteStatus()) {
unMute();
} else {
mute();
}
}
protected void setMuteStatus(boolean muted) {
mSharedPrefs.edit().putBoolean(MUTE_STATUS, muted).apply();
muteSound = muted;
}
protected void updateMuteMenuItem() {
// Just to be safe null check
if(mMuteMenuItem == null) {
return;
}
// if we are muted display unmute and vice versa
if(getMuteStatus()) {
mMuteMenuItem.setTitle("UNMUTE");
} else {
mMuteMenuItem.setTitle("MUTE")
}
}
protected void unMute() {
setMuteStatus(false);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, userVolumeOnStart.userVolume, 0);
updateMuteMenuItem();
Toast.makeText(this, "UNMUTED", Toast.LENGTH_SHORT).show();
Log.v(TAG,"UNMUTED");
}
protected void mute() {
setMuteStatus(true);
userVolumeOnStart.userVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
updateMuteMenuItem();
Toast.makeText(this, "MUTED", Toast.LENGTH_SHORT).show();
Log.v(TAG,"MUTED");
}
protected void stopTimer() {
Log.v(TAG, "Timer Stopping");
TwentySeconds.stopTimer();
}
protected void onPhoneCallInterrupt() {
Log.v(TAG, "Phone Call Interruption");
// Stop the timer
stopTimer();
// If we are unmuted mute
if(getMuteStatus()) {
changeMuteStatus();
}
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