Skip to content

Commit

Permalink
Add option to run on wifi only
Browse files Browse the repository at this point in the history
  • Loading branch information
markwinter committed Sep 23, 2014
1 parent 4c876c8 commit 2ee717f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
19 changes: 19 additions & 0 deletions app/src/main/java/im/tox/antox/fragments/SettingsFragment.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package im.tox.antox.fragments;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
Expand All @@ -15,6 +18,7 @@

import im.tox.antox.R;
import im.tox.antox.activities.LoginActivity;
import im.tox.antox.data.AntoxDB;
import im.tox.antox.data.UserDB;
import im.tox.antox.tox.ToxDoService;
import im.tox.antox.tox.ToxSingleton;
Expand Down Expand Up @@ -258,5 +262,20 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
// Start service
getActivity().startService(service);
}

if(key.equals("wifi_only")) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
final ConnectivityManager connManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

boolean wifiOnly = sharedPreferences.getBoolean("wifi_only", true);

// Set all offline as we wont receive callbacks for them by not doing doTox()
if(wifiOnly && !mWifi.isConnected()) {
AntoxDB antoxDB = new AntoxDB(getActivity());
antoxDB.setAllOffline();
antoxDB.close();
}
}
}
}
30 changes: 26 additions & 4 deletions app/src/main/java/im/tox/antox/tox/ToxDoService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package im.tox.antox.tox;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;

import im.tox.jtoxcore.ToxException;
Expand Down Expand Up @@ -31,10 +36,27 @@ public void onCreate() {
@Override
public void run() {
while(keepRunning) {
try {
Thread.sleep(toxSingleton.jTox.doToxInterval());
toxSingleton.jTox.doTox();
} catch (Exception e) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
final ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final boolean wifiOnly = preferences.getBoolean("wifi_only", true);
final NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if(wifiOnly && !mWifi.isConnected()) {

try {
// Sleep for 10 seconds before checking again
Thread.sleep(10000);
} catch (Exception e) {
}

} else {

try {
Thread.sleep(toxSingleton.jTox.doToxInterval());
toxSingleton.jTox.doTox();
} catch (Exception e) {
}

}
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings_activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@
<string name="pref_title_enable_notifications">Enable notifications</string>
<string name="pref_title_new_message_notifications">Message notifications</string>
<string name="pref_title_new_friend_notifications">Friend request notifications</string>
<string name="pref_title_action_messages">Action messages</string>

<!-- Other -->
<string name="pref_header_other">Other</string>
<string name="pref_title_connection_status_messages">Online/Offline Messages</string>
<string name="pref_title_wifi_only">Wi-Fi Only</string>

<string name="pref_title_language">Language</string>
<string-array name="pref_language_list_titles" translatable="false">
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/res/xml/pref_other.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
android:negativeButtonText="@null"
android:positiveButtonText="@null" />

<CheckBoxPreference
android:key="wifi_only"
android:title="@string/pref_title_wifi_only"
android:defaultValue="true" />

<CheckBoxPreference
android:key="action_messages"
android:title="@string/pref_title_action_messages"
android:title="@string/pref_title_connection_status_messages"
android:defaultValue="true" />

<PreferenceScreen
Expand Down

0 comments on commit 2ee717f

Please sign in to comment.