Created
November 11, 2015 04:11
-
-
Save jlwatkins/d56ae7ebe5dc053a0e56 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 { | |
// Constant Variable Declarations | |
private static final String MUTE_STATUS = "MUTE_STATUS"; | |
private static final String TAG = "mute"; | |
// Variable Declarations | |
private AudioManager mAudioManager; | |
private TelephonyManager mTelephonyManager; | |
protected SharedPreferences mSharedPrefs; | |
protected boolean muteSound; | |
// UI Declarations | |
private MenuItem mMuteMenuItem; | |
// Custom Phone State Listener Implementation | |
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); | |
// Setting up system services and volume | |
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(); | |
} | |
/** | |
* Retreieve the current mute status. true = muted | |
*/ | |
public boolean getMuteStatus() { | |
// Get mute status via shared preferences and default is false | |
muteSound = mSharedPrefs.getBoolean(MUTE_STATUS, false); | |
return muteSound; | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
// register phone listener to telephony manager | |
tManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); | |
} | |
@Override | |
protected void onPause() { | |
// unregister phone listener to telephony manager | |
tManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE); | |
// might want to mute here???? | |
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: // Selected mute/unmute button | |
changeMuteStatus(); | |
break; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
/** | |
* Switch mute status | |
*/ | |
protected void changeMuteStatus() { | |
// Switch mute status | |
if (getMuteStatus()) { | |
unMute(); | |
} else { | |
mute(); | |
} | |
} | |
/** | |
* Set the mute status, also storing in shared preferences | |
*/ | |
protected void setMuteStatus(boolean muted) { | |
mSharedPrefs.edit().putBoolean(MUTE_STATUS, muted).apply(); | |
muteSound = muted; | |
} | |
/** | |
* Update the mute menu item to be the opposite of the mute status | |
*/ | |
protected void updateMuteMenuItem() { | |
// Just to be safe null check (view not displayed yet possibly) | |
if(mMuteMenuItem == null) { | |
return; | |
} | |
// if we are muted display unmute and vice versa | |
if(getMuteStatus()) { | |
mMuteMenuItem.setTitle("UNMUTE"); | |
} else { | |
mMuteMenuItem.setTitle("MUTE") | |
} | |
} | |
/** | |
* Unmute the sound. This method changes mute status, changes audio manager, updates | |
* the menu view item and also displays a toast message as well as a verbose log statement. | |
*/ | |
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"); | |
} | |
/** | |
* Mute the sound. This method changes mute status, changes audio manager, updates | |
* the menu view item and also displays a toast message as well as a verbose log statement. | |
*/ | |
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"); | |
} | |
/** | |
* This function simply stops the background service timer | |
*/ | |
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 | |
mute(); | |
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