Skip to content

Commit

Permalink
Keep a connection on when screen dims out
Browse files Browse the repository at this point in the history
  • Loading branch information
poretsky committed Oct 11, 2017
1 parent 8796b2a commit 51e8720
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 31 deletions.
6 changes: 5 additions & 1 deletion TeamTalkAndroid/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public class Permissions {
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;
Expand Down Expand Up @@ -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;
Expand Down
72 changes: 43 additions & 29 deletions TeamTalkAndroid/src/main/java/dk/bearware/gui/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -167,6 +169,7 @@ public class MainActivity
SoundPool audioIcons;
ComponentName mediaButtonEventReceiver;
NotificationManager notificationManager;
WakeLock wakeLock;

static final String MESSAGE_NOTIFICATION_TAG = "incoming_message";

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -399,7 +407,7 @@ protected void onStop() {
}

// Cleanup resources
if(isFinishing()) {
if (isFinishing()) {
if (audioIcons != null) {
audioIcons.release();
audioIcons = null;
Expand All @@ -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);
}
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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;
}
}

Expand Down
1 change: 1 addition & 0 deletions TeamTalkAndroid/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
1 change: 1 addition & 0 deletions TeamTalkAndroid/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.