Permalink
Browse files

Keep a connection on when screen dims out

  • Loading branch information...
poretsky committed Oct 10, 2017
1 parent 8796b2a commit 51e8720debe972efc0288ed04234abcb18bce070
@@ -95,5 +95,9 @@
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<!-- WAKE_LOCK is needed to keep connection alive when screen dims out -->
<uses-permission android:name="android.permission.WAKE_LOCK" >
</uses-permission>
</manifest>
@@ -39,7 +39,8 @@
MY_PERMISSIONS_REQUEST_INTERNET = 3,
MY_PERMISSIONS_REQUEST_VIBRATE = 4,
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 5,
MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 6;
MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 6,
MY_PERMISSIONS_REQUEST_WAKE_LOCK = 7;
public static boolean setupPermission(Context context, Activity activity, int permission) {
String stringPermission;
@@ -70,6 +71,10 @@ public static boolean setupPermission(Context context, Activity activity, int pe
stringPermission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
errormessage = context.getString(R.string.permission_filerx);
break;
case MY_PERMISSIONS_REQUEST_WAKE_LOCK:
stringPermission = Manifest.permission.WAKE_LOCK;
errormessage = context.getString(R.string.permission_wake_lock);
break;
default :
Log.e(AppInfo.TAG, String.format("Unknown permission %d", permission));
return false;
@@ -83,6 +83,8 @@
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
@@ -167,6 +169,7 @@
SoundPool audioIcons;
ComponentName mediaButtonEventReceiver;
NotificationManager notificationManager;
WakeLock wakeLock;
static final String MESSAGE_NOTIFICATION_TAG = "incoming_message";
@@ -213,6 +216,8 @@ protected void onCreate(Bundle savedInstanceState) {
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mediaButtonEventReceiver = new ComponentName(getPackageName(), MediaButtonEventReceiver.class.getName());
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
wakeLock = ((PowerManager)getSystemService(Context.POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wakeLock.setReferenceCounted(false);
channelsAdapter = new ChannelListAdapter(this.getBaseContext());
filesAdapter = new FileListAdapter(this, this, accessibilityAssistant);
@@ -323,15 +328,18 @@ protected void onStart() {
if (ttsWrapper == null)
ttsWrapper = TTSWrapper.getInstance(this);
// Bind to LocalService
Intent intent = new Intent(getApplicationContext(), TeamTalkService.class);
mConnection = new TeamTalkConnection(this);
Log.d(TAG, "Binding TeamTalk service");
if(!bindService(intent, mConnection, Context.BIND_AUTO_CREATE))
Log.e(TAG, "Failed to bind to TeamTalk service");
else
mConnection.setBound(true);
if (mConnection == null)
mConnection = new TeamTalkConnection(this);
if (!mConnection.isBound()) {
// Bind to LocalService
Intent intent = new Intent(getApplicationContext(), TeamTalkService.class);
Log.d(TAG, "Binding TeamTalk service");
if(!bindService(intent, mConnection, Context.BIND_AUTO_CREATE))
Log.e(TAG, "Failed to bind to TeamTalk service");
else
mConnection.setBound(true);
}
}
@Override
@@ -385,7 +393,7 @@ public void onResume() {
protected void onStop() {
super.onStop();
if(stats_timer != null) {
if (stats_timer != null) {
stats_timer.cancel();
stats_timer = null;
}
@@ -399,7 +407,7 @@ protected void onStop() {
}
// Cleanup resources
if(isFinishing()) {
if (isFinishing()) {
if (audioIcons != null) {
audioIcons.release();
audioIcons = null;
@@ -409,23 +417,13 @@ protected void onStop() {
ttsWrapper = null;
}
notificationManager.cancelAll();
}
if(ttservice != null) {
ttservice.setOnVoiceTransmissionToggleListener(null);
ttservice.unregisterConnectionListener(this);
ttservice.unregisterCommandListener(this);
ttservice.unregisterUserListener(this);
ttservice.unregisterClientListener(this);
filesAdapter.setTeamTalkService(null);
mediaAdapter.clearTeamTalkService(ttservice);
}
// Unbind from the service
if(mConnection.isBound()) {
Log.d(TAG, "Unbinding TeamTalk service");
unbindService(mConnection);
mConnection.setBound(false);
// Unbind from the service
if(mConnection.isBound()) {
Log.d(TAG, "Unbinding TeamTalk service");
unbindService(mConnection);
mConnection.setBound(false);
}
}
}
@@ -1391,13 +1389,17 @@ public void onServiceConnected(TeamTalkService service) {
Toast.makeText(this, R.string.err_init_sound_output, Toast.LENGTH_LONG).show();
}
audioManager.registerMediaButtonEventReceiver(mediaButtonEventReceiver);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
audioManager.setMode(prefs.getBoolean(Preferences.PREF_SOUNDSYSTEM_VOICEPROCESSING, false)?
AudioManager.MODE_IN_COMMUNICATION : AudioManager.MODE_NORMAL);
audioManager.setSpeakerphoneOn(prefs.getBoolean(Preferences.PREF_SOUNDSYSTEM_SPEAKERPHONE, false));
if ((!prefs.getBoolean("keep_screen_on_checkbox", false))
&& Permissions.setupPermission(getBaseContext(), this, Permissions.MY_PERMISSIONS_REQUEST_WAKE_LOCK))
wakeLock.acquire();
if (prefs.getBoolean(Preferences.PREF_SOUNDSYSTEM_VOICEACTIVATION, false)) {
ttservice.enableVoiceActivation(true);
ttclient.setVoiceActivationLevel(5);
@@ -1422,7 +1424,16 @@ public void onServiceConnected(TeamTalkService service) {
@Override
public void onServiceDisconnected(TeamTalkService service) {
if (wakeLock.isHeld())
wakeLock.release();
audioManager.unregisterMediaButtonEventReceiver(mediaButtonEventReceiver);
service.setOnVoiceTransmissionToggleListener(null);
service.unregisterConnectionListener(this);
service.unregisterCommandListener(this);
service.unregisterUserListener(this);
service.unregisterClientListener(this);
filesAdapter.setTeamTalkService(null);
mediaAdapter.clearTeamTalkService(service);
}
@Override
@@ -1437,6 +1448,9 @@ public void onRequestPermissionsResult(int requestCode,
break;
case Permissions.MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE :
break;
case Permissions.MY_PERMISSIONS_REQUEST_WAKE_LOCK:
wakeLock.acquire();
break;
}
}
@@ -267,5 +267,6 @@
<string name="permission_filetx">Выкладывание и импортирование файлов будет недоступно</string>
<string name="permission_internet">Клиент не сможет подключаться к серверам</string>
<string name="permission_vibrate">Виброотклик кнопки включения передачи будет недоступен</string>
<string name="permission_wake_lock">Соединение с сервером может быть потеряно при выключении экрана</string>
</resources>
@@ -277,5 +277,6 @@
<string name="permission_filetx">Upload and importing files will be unavailable</string>
<string name="permission_internet">Client will be unable to connect to servers</string>
<string name="permission_vibrate">Vibrate during push-to-talk will be unavailable</string>
<string name="permission_wake_lock">Server connection can be lost when screen dims out</string>
</resources>

0 comments on commit 51e8720

Please sign in to comment.