1
0
mirror of https://github.com/nerzhul/ownCloud-SMS-App.git synced 2025-06-28 18:26:22 +00:00

Add a new preference to select on which network synchronize

This commit is contained in:
Loic Blot 2015-03-21 21:51:10 +01:00
parent b8506fffd1
commit 810567c237
7 changed files with 127 additions and 11 deletions

View File

@ -28,7 +28,7 @@
<resources>
<!-- Translation version, reference for translators -->
<string name="translation_version">5</string>
<string name="translation_version">6</string>
<!-- System strings, do not translate -->
<string name="app_name">ownCloud-SMS</string>
@ -95,6 +95,13 @@
<item>1440</item>
<item>-1</item>
</string-array>
<string name="pref_sync_wifi">Synchronize in Wi-Fi</string>
<string name="pref_sync_4g">Synchronize in 4G</string>
<string name="pref_sync_3g">Synchronize in 3G</string>
<string name="pref_sync_gprs">Synchronize in 2.5G (GPRS)</string>
<string name="pref_sync_2g">Synchronize in 2G</string>
<string name="pref_sync_others">Synchronize in other modes</string>
<string name="title_activity_login">Sign in</string>

View File

@ -2,12 +2,49 @@
<PreferenceCategory
android:title="@string/summary_global_pref_to_general_prefs">
<ListPreference
android:defaultValue="10"
android:defaultValue="15"
android:entries="@array/pref_sync_frequency_titles"
android:entryValues="@array/pref_sync_frequency_values"
android:key="sync_frequency"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="@string/pref_title_sync_frequency" />
<CheckBoxPreference android:id="@+id/checkbox_sync_wifi"
android:defaultValue="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:key="sync_wifi"
android:title="@string/pref_sync_wifi" />
<CheckBoxPreference android:id="@+id/checkbox_sync_4g"
android:defaultValue="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:key="sync_4g"
android:title="@string/pref_sync_4g" />
<CheckBoxPreference android:id="@+id/checkbox_sync_3g"
android:defaultValue="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:key="sync_3g"
android:title="@string/pref_sync_3g" />
<CheckBoxPreference android:id="@+id/checkbox_sync_2g"
android:defaultValue="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:key="sync_2g"
android:title="@string/pref_sync_2g" />
<CheckBoxPreference android:id="@+id/checkbox_sync_gprs"
android:defaultValue="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:key="sync_gprs"
android:title="@string/pref_sync_gprs" />
<CheckBoxPreference android:id="@+id/checkbox_sync_others"
android:defaultValue="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:key="sync_others"
android:title="@string/pref_sync_others" />
</PreferenceCategory>
</PreferenceScreen>

View File

@ -23,6 +23,8 @@ import android.annotation.TargetApi;
import android.content.ContentResolver;
import android.content.Context;
import android.content.PeriodicSync;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
@ -31,12 +33,14 @@ import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import java.util.List;
import fr.unix_experience.owncloud_sms.R;
public class GeneralSettingsActivity extends PreferenceActivity {
private static final boolean ALWAYS_SIMPLE_PREFS = false;
private static Context mContext;
static AccountManager mAccountMgr;
static String mAccountAuthority;
static String mSlowSyncAccountAuthority;
@ -66,6 +70,8 @@ public class GeneralSettingsActivity extends PreferenceActivity {
// In the simplified UI, fragments are not used at all and we instead
// use the older PreferenceActivity APIs.
addPreferencesFromResource(R.xml.pref_data_sync);
mContext = getBaseContext();
// Bind the summaries of EditText/List/Dialog/Ringtone preferences to
// their values. When their values change, their summaries are updated
@ -124,12 +130,13 @@ public class GeneralSettingsActivity extends PreferenceActivity {
String prefKey = preference.getKey();
Account[] myAccountList = mAccountMgr.getAccountsByType(mAccountType);
// Handle sync frequency change
if (prefKey.equals(new String("sync_frequency"))) {
long syncFreq = Long.parseLong((String)value);
if (prefKey.equals("sync_frequency")) {
long syncFreq = Long.parseLong(stringValue);
// Get ownCloud SMS account list
Account[] myAccountList = mAccountMgr.getAccountsByType(mAccountType);
for (int i = 0; i < myAccountList.length; i++) {
// And get all authorities for this account
List<PeriodicSync> syncList = ContentResolver.getPeriodicSyncs(myAccountList[i], mAccountAuthority);
@ -153,8 +160,19 @@ public class GeneralSettingsActivity extends PreferenceActivity {
mAccountAuthority, b, syncFreq * 60);
}
}
}
// Network types allowed for sync
else if(prefKey.equals("sync_wifi") || prefKey.equals("sync_2g") ||
prefKey.equals("sync_3g") || prefKey.equals("sync_gprs") ||
prefKey.equals("sync_4g") || prefKey.equals("sync_others")) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
Editor edit = prefs.edit();
edit.putBoolean(prefKey, Boolean.parseBoolean(stringValue));
edit.commit();
}
// Slow Sync frequency
} /*else if (prefKey.equals(new String("slow_sync_frequency"))) {
/*else if (prefKey.equals(new String("slow_sync_frequency"))) {
long syncFreq = Long.parseLong((String)value);
// Get ownCloud SMS account list

View File

@ -46,6 +46,7 @@ import com.owncloud.android.lib.common.OwnCloudCredentialsFactory;
import fr.unix_experience.owncloud_sms.R;
import fr.unix_experience.owncloud_sms.authenticators.OwnCloudAuthenticator;
import fr.unix_experience.owncloud_sms.defines.DefaultPrefs;
import fr.unix_experience.owncloud_sms.enums.LoginReturnCode;
/**
@ -282,7 +283,7 @@ public class LoginActivity extends Activity {
Bundle b = new Bundle();
b.putInt("synctype", 1);
ContentResolver.addPeriodicSync(account, getString(R.string.account_authority), b, 15 * 60);
ContentResolver.addPeriodicSync(account, getString(R.string.account_authority), b, DefaultPrefs.syncInterval * 60);
// Then it's finished
finish();

View File

@ -33,11 +33,13 @@ public class ConnectivityChanged extends BroadcastReceiver implements ASyncTask
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityMonitor cMon = new ConnectivityMonitor(context);
Log.d(TAG, "test");
// If data is available and previous dataConnectionState was false, then we need to sync
if (cMon.isValid() && dataConnectionAvailable == false) {
dataConnectionAvailable = true;
Log.d(TAG,"ConnectivityChanged.onReceive, data conn available");
checkMessagesToSent(context);
checkMessagesToSend(context);
}
// No data available and previous dataConnectionState was true
else if (dataConnectionAvailable == true && !cMon.isValid()) {
@ -46,7 +48,7 @@ public class ConnectivityChanged extends BroadcastReceiver implements ASyncTask
}
}
private void checkMessagesToSent(Context context) {
private void checkMessagesToSend(Context context) {
// Get last message synced from preferences
Long lastMessageSynced = (new OCSMSSharedPrefs(context)).getLastMessageDate();
Log.d(TAG,"Synced Last:" + lastMessageSynced);

View File

@ -0,0 +1,11 @@
package fr.unix_experience.owncloud_sms.defines;
public class DefaultPrefs {
public final static Integer syncInterval = 15;
public final static Boolean syncWifi = true;
public final static Boolean sync2G = true;
public final static Boolean syncGPRS = true;
public final static Boolean sync3G = true;
public final static Boolean sync4G = true;
public final static Boolean syncOthers = true;
}

View File

@ -16,8 +16,13 @@ package fr.unix_experience.owncloud_sms.engine;
* You should have received a copy of the GNU Affero General Public License
*/
import fr.unix_experience.owncloud_sms.defines.DefaultPrefs;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
public class ConnectivityMonitor {
public ConnectivityMonitor(Context context) {
@ -28,13 +33,48 @@ public class ConnectivityMonitor {
public boolean isValid() {
if (_cMgr == null) {
_cMgr = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
}
}
final android.net.NetworkInfo niWiFi = _cMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo niMobile = _cMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (niWiFi.isAvailable() || niMobile.isAvailable()) {
return true;
// Load the connectivity manager to determine on which network we are connected
NetworkInfo netInfo = _cMgr.getActiveNetworkInfo();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(_context);
// Check
switch (netInfo.getType()) {
case ConnectivityManager.TYPE_WIFI:
return prefs.getBoolean("sync_wifi", DefaultPrefs.syncWifi);
case ConnectivityManager.TYPE_MOBILE:
switch (netInfo.getSubtype()) {
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return prefs.getBoolean("sync_2g", DefaultPrefs.sync2G);
case TelephonyManager.NETWORK_TYPE_GPRS:
return prefs.getBoolean("sync_gprs", DefaultPrefs.syncGPRS);
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
case TelephonyManager.NETWORK_TYPE_HSPAP:
return prefs.getBoolean("sync_3g", DefaultPrefs.sync3G);
case TelephonyManager.NETWORK_TYPE_LTE:
return prefs.getBoolean("sync_4g", DefaultPrefs.sync3G);
default:
return prefs.getBoolean("sync_others", DefaultPrefs.syncOthers);
}
default:
return prefs.getBoolean("sync_others", DefaultPrefs.syncOthers);
}
}
return false;