Enable and disable Keep Alive according to device Doze mode

This commit is contained in:
Erwan Croze 2016-12-08 16:51:06 +01:00
parent 1c7907e217
commit 32011c8f72
9 changed files with 152 additions and 94 deletions

View file

@ -0,0 +1,35 @@
package org.linphone;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.PowerManager;
import org.linphone.core.LinphoneCore;
import org.linphone.core.LinphoneCoreFactory;
import org.linphone.mediastream.Log;
/*
* Purpose of this receiver is to disable keep alives when device is on idle
* */
public class DozeReceiver extends android.content.BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm;
if (!LinphoneService.isReady()) return;
boolean isDebugEnabled = LinphonePreferences.instance().isDebugEnabled();
LinphoneCoreFactory.instance().enableLogCollection(isDebugEnabled);
LinphoneCoreFactory.instance().setDebugMode(isDebugEnabled, context.getString(R.string.app_name));
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
if (lc == null) return;
pm = (PowerManager) context.getSystemService(context.POWER_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
boolean dozeM = pm.isDeviceIdleMode();
Log.i("[DozeReceiver] Idle Mode: " + dozeM);
LinphoneManager.getInstance().setDozeModeEnabled(dozeM);
LinphoneManager.getInstance().updateNetworkReachability();
}
}
}

View file

@ -140,10 +140,13 @@ public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessag
private static boolean sExited; private static boolean sExited;
private boolean mAudioFocused; private boolean mAudioFocused;
private boolean echoTesterIsRunning; private boolean echoTesterIsRunning;
private boolean dozeModeEnabled;
private int mLastNetworkType=-1; private int mLastNetworkType=-1;
private ConnectivityManager mConnectivityManager; private ConnectivityManager mConnectivityManager;
private BroadcastReceiver mKeepAliveReceiver; private BroadcastReceiver mKeepAliveReceiver;
private BroadcastReceiver mDozeReceiver;
private IntentFilter mKeepAliveIntentFilter; private IntentFilter mKeepAliveIntentFilter;
private IntentFilter mDozeIntentFilter;
private Handler mHandler = new Handler(); private Handler mHandler = new Handler();
private WakeLock mIncallWakeLock; private WakeLock mIncallWakeLock;
private LinphoneAccountCreator accountCreator; private LinphoneAccountCreator accountCreator;
@ -188,6 +191,12 @@ public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessag
mConnectivityManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE); mConnectivityManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
mR = c.getResources(); mR = c.getResources();
mPendingChatFileMessage = new ArrayList<LinphoneChatMessage>(); mPendingChatFileMessage = new ArrayList<LinphoneChatMessage>();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
dozeModeEnabled = ((PowerManager)c.getSystemService(c.POWER_SERVICE)).isDeviceIdleMode();
} else {
dozeModeEnabled = false;
}
} }
private static final int LINPHONE_VOLUME_STREAM = STREAM_VOICE_CALL; private static final int LINPHONE_VOLUME_STREAM = STREAM_VOICE_CALL;
@ -663,6 +672,7 @@ public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessag
finally { finally {
try { try {
mServiceContext.unregisterReceiver(mKeepAliveReceiver); mServiceContext.unregisterReceiver(mKeepAliveReceiver);
mServiceContext.unregisterReceiver(mDozeReceiver);
} catch (Exception e) { } catch (Exception e) {
Log.e(e); Log.e(e);
} }
@ -679,6 +689,7 @@ public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessag
be sent by the system. be sent by the system.
*/ */
mServiceContext.registerReceiver(mKeepAliveReceiver, mKeepAliveIntentFilter); mServiceContext.registerReceiver(mKeepAliveReceiver, mKeepAliveIntentFilter);
mServiceContext.registerReceiver(mDozeReceiver, mDozeIntentFilter);
sExited = false; sExited = false;
} }
@ -798,9 +809,17 @@ public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessag
*/ */
mKeepAliveIntentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON); mKeepAliveIntentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
mKeepAliveIntentFilter.addAction(Intent.ACTION_SCREEN_OFF); mKeepAliveIntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
mKeepAliveReceiver = new KeepAliveReceiver(); mKeepAliveReceiver = new KeepAliveReceiver();
mServiceContext.registerReceiver(mKeepAliveReceiver, mKeepAliveIntentFilter); mServiceContext.registerReceiver(mKeepAliveReceiver, mKeepAliveIntentFilter);
mDozeIntentFilter = new IntentFilter(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
mDozeIntentFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
mDozeReceiver = new DozeReceiver();
mServiceContext.registerReceiver(mDozeReceiver, mDozeIntentFilter);
updateNetworkReachability(); updateNetworkReachability();
resetCameraFromPreferences(); resetCameraFromPreferences();
@ -870,7 +889,7 @@ public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessag
ConnectivityManager cm = (ConnectivityManager) mServiceContext.getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) mServiceContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo eventInfo = cm.getActiveNetworkInfo(); NetworkInfo eventInfo = cm.getActiveNetworkInfo();
if (eventInfo == null || eventInfo.getState() == NetworkInfo.State.DISCONNECTED) { if (eventInfo == null || eventInfo.getState() == NetworkInfo.State.DISCONNECTED || dozeModeEnabled) {
Log.i("No connectivity: setting network unreachable"); Log.i("No connectivity: setting network unreachable");
mLc.setNetworkReachable(false); mLc.setNetworkReachable(false);
} else if (eventInfo.getState() == NetworkInfo.State.CONNECTED){ } else if (eventInfo.getState() == NetworkInfo.State.CONNECTED){
@ -1495,6 +1514,10 @@ public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessag
dialog.show(); dialog.show();
} }
public void setDozeModeEnabled(boolean b) {
dozeModeEnabled = b;
}
@SuppressWarnings("serial") @SuppressWarnings("serial")
public static class LinphoneConfigException extends LinphoneException { public static class LinphoneConfigException extends LinphoneException {

View file

@ -182,7 +182,7 @@ public class PreferencesMigrator {
private void deleteAllOldPreferences() { private void deleteAllOldPreferences() {
Editor editor = mOldPrefs.edit(); Editor editor = mOldPrefs.edit();
editor.clear(); editor.clear();
editor.commit(); editor.apply();
} }
private String getString(int key) { private String getString(int key) {

View file

@ -117,7 +117,7 @@ public class SettingsFragment extends PreferencesListFragment {
private void removePreviousPreferencesFile() { private void removePreviousPreferencesFile() {
SharedPreferences.Editor editor = getPreferenceManager().getSharedPreferences().edit(); SharedPreferences.Editor editor = getPreferenceManager().getSharedPreferences().edit();
editor.clear(); editor.clear();
editor.commit(); editor.apply();
File dir = new File(getActivity().getFilesDir().getAbsolutePath() + "shared_prefs"); File dir = new File(getActivity().getFilesDir().getAbsolutePath() + "shared_prefs");
LinphoneUtils.recursiveFileRemoval(dir); LinphoneUtils.recursiveFileRemoval(dir);

@ -1 +1 @@
Subproject commit d63b53d1ee7fe9c3d44ae9d294063cb1c030e486 Subproject commit 141c0b6c0991fc79e8988961f2283936ca3c14f7