From 0ee660cc2741147febe7a910acbc55ade4b3c19a Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Fri, 4 Jan 2013 13:51:09 -0500 Subject: [PATCH] Do not turn off WIFI or BT while in use. Added if blocks to check if WIFI or BT is in used before changing their state. Also added a new receiver to monitor the number of connected BT devices Signed-off-by: Ricky Barrette --- LocationRinger/AndroidManifest.xml | 11 +++- .../receivers/BluetoothReceiver.java | 57 +++++++++++++++++++ .../services/RingerProcessingService.java | 35 ++++++++---- 3 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 LocationRinger/src/org/RickBarrette/android/LocationRinger/receivers/BluetoothReceiver.java diff --git a/LocationRinger/AndroidManifest.xml b/LocationRinger/AndroidManifest.xml index 7c3202d..10a7a27 100644 --- a/LocationRinger/AndroidManifest.xml +++ b/LocationRinger/AndroidManifest.xml @@ -2,8 +2,8 @@ + android:versionCode="219" + android:versionName="2a1fd40" > @@ -35,6 +35,7 @@ + + + + + + + \ No newline at end of file diff --git a/LocationRinger/src/org/RickBarrette/android/LocationRinger/receivers/BluetoothReceiver.java b/LocationRinger/src/org/RickBarrette/android/LocationRinger/receivers/BluetoothReceiver.java new file mode 100644 index 0000000..89b3e22 --- /dev/null +++ b/LocationRinger/src/org/RickBarrette/android/LocationRinger/receivers/BluetoothReceiver.java @@ -0,0 +1,57 @@ +/** + * BluetoothReceiver.java + * @date Jan 4, 2013 + * @author ricky barrette + * + * Copyright 2012 Richard Barrette + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +package org.RickBarrette.android.LocationRinger.receivers; + +import android.bluetooth.BluetoothDevice; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; + +/** + * This class will be used to maintain a log of connected devices + * @author ricky barrette + */ +public class BluetoothReceiver extends BroadcastReceiver { + + public static final String TAG = "BluetoothReceiver"; + public static final String NUMBER_CONNECTED = "number_connected"; + + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); + + SharedPreferences sp = context.getSharedPreferences(TAG, Context.MODE_MULTI_PROCESS); + Editor editor = sp.edit(); + int connected = sp.getInt(NUMBER_CONNECTED, 0); + + if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) + connected++; + + else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) + connected--; + + editor.putInt(NUMBER_CONNECTED, connected); + editor.apply(); + } + +} diff --git a/LocationRinger/src/org/RickBarrette/android/LocationRinger/services/RingerProcessingService.java b/LocationRinger/src/org/RickBarrette/android/LocationRinger/services/RingerProcessingService.java index 35942e5..d6ecabf 100644 --- a/LocationRinger/src/org/RickBarrette/android/LocationRinger/services/RingerProcessingService.java +++ b/LocationRinger/src/org/RickBarrette/android/LocationRinger/services/RingerProcessingService.java @@ -11,6 +11,7 @@ import java.util.Map.Entry; import org.RickBarrette.android.LocationRinger.Constraints; import org.RickBarrette.android.LocationRinger.Log; import org.RickBarrette.android.LocationRinger.db.RingerDatabase; +import org.RickBarrette.android.LocationRinger.receivers.BluetoothReceiver; import org.RickBarrette.android.LocationRinger.receivers.GetLocationWidget; import org.RickBarrette.android.LocationRinger.ui.SettingsActivity; @@ -24,6 +25,8 @@ import android.database.Cursor; import android.location.Location; import android.media.AudioManager; import android.media.RingtoneManager; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; import android.net.Uri; import android.net.wifi.WifiManager; import android.os.IBinder; @@ -105,18 +108,30 @@ public class RingerProcessingService extends Service { setStreamVolume(values.getAsInteger(RingerDatabase.KEY_ALARM_VOLUME), AudioManager.STREAM_ALARM); /* - * wifi & bluetooth + * wifi + * + * only change wifi state IF NOT connected */ - if (values.containsKey(RingerDatabase.KEY_WIFI)) - if (mWifiManager != null) - mWifiManager.setWifiEnabled(RingerDatabase.parseBoolean(values.getAsString(RingerDatabase.KEY_WIFI))); + ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); + NetworkInfo wifiNetInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); - if (values.containsKey(RingerDatabase.KEY_BT)) - if (mBluetoothAdapter != null) - if (RingerDatabase.parseBoolean(values.getAsString(RingerDatabase.KEY_BT))) - mBluetoothAdapter.enable(); - else - mBluetoothAdapter.disable(); + if (! wifiNetInfo.isConnected()) + if (values.containsKey(RingerDatabase.KEY_WIFI)) + if (mWifiManager != null) + mWifiManager.setWifiEnabled(RingerDatabase.parseBoolean(values.getAsString(RingerDatabase.KEY_WIFI))); + + /* + * bluetooth + * + * only updated bt state IF NOT connected + */ + if(getSharedPreferences(BluetoothReceiver.TAG, Context.MODE_MULTI_PROCESS).getInt(BluetoothReceiver.NUMBER_CONNECTED, 0) < 1) + if (values.containsKey(RingerDatabase.KEY_BT)) + if (mBluetoothAdapter != null) + if (RingerDatabase.parseBoolean(values.getAsString(RingerDatabase.KEY_BT))) + mBluetoothAdapter.enable(); + else + mBluetoothAdapter.disable(); /* * airplane mode