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

The checkboxes are now working. Apply the filter on all syncing methods. Add a button to launch a sync from MainActivity

This commit is contained in:
Loic Blot 2015-03-22 00:24:59 +01:00
parent 810567c237
commit 24f72073a4
8 changed files with 161 additions and 42 deletions

View File

@ -46,7 +46,7 @@
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/mail_button_accounts"
android:id="@+id/main_button_accounts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/main_tv_accounts"
@ -63,7 +63,7 @@
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/main_tv_accounts"
android:layout_alignStart="@+id/main_tv_accounts"
android:layout_below="@+id/mail_button_accounts"
android:layout_below="@+id/main_button_accounts"
android:layout_marginTop="18dp"
android:text="@string/ma_title_change_settings"
android:textAppearance="?android:attr/textAppearanceLarge" />
@ -79,6 +79,29 @@
android:background="@drawable/standard_button"
style="@style/StandardButton"
android:text="@string/ma_button_goto_settings" />
<TextView
android:id="@+id/tv_launchsync"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/main_tv_accounts"
android:layout_alignStart="@+id/main_tv_accounts"
android:layout_below="@+id/main_button_settings"
android:layout_marginTop="18dp"
android:text="@string/ma_title_sync_all"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/main_button_sync"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_launchsync"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:onClick="syncAllMessages"
android:background="@drawable/standard_button"
style="@style/StandardButton"
android:text="@string/ma_button_sync_accounts_now" />
<ImageView
android:id="@+id/imageView1"
@ -86,7 +109,7 @@
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignStart="@+id/textView1"
android:layout_below="@+id/main_button_settings"
android:layout_below="@+id/main_button_sync"
android:layout_marginTop="33dp"
android:src="@drawable/next_arrow" />

View File

@ -136,6 +136,8 @@
<string name="ma_content_welcome">Welcome to ownCloud SMS application. This application let you synchronize your SMS to your ownCloud instance using SMS app.</string>
<string name="ma_thanksto_people">ownCloud developers\n\
Contributors and issue\'s reporters</string>
<string name="ma_title_sync_all">Synchronize all messages</string>
<string name="ma_button_sync_accounts_now">Launch synchronization now</string>
<!-- Notifications -->
<string name="sync_title">Sync process</string>

View File

@ -28,15 +28,19 @@ import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.util.Log;
import java.util.List;
import fr.unix_experience.owncloud_sms.R;
import fr.unix_experience.owncloud_sms.defines.DefaultPrefs;
import fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs;
public class GeneralSettingsActivity extends PreferenceActivity {
private static final boolean ALWAYS_SIMPLE_PREFS = false;
@ -73,11 +77,7 @@ public class GeneralSettingsActivity extends PreferenceActivity {
mContext = getBaseContext();
// Bind the summaries of EditText/List/Dialog/Ringtone preferences to
// their values. When their values change, their summaries are updated
// to reflect the new value, per the Android Design guidelines.
bindPreferenceSummaryToValue(findPreference("sync_frequency"));
//bindPreferenceSummaryToValue(findPreference("slow_sync_frequency"));
bindPreferences();
}
/** {@inheritDoc} */
@ -114,9 +114,9 @@ public class GeneralSettingsActivity extends PreferenceActivity {
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
String prefKey = preference.getKey();
String stringValue = value.toString();
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
@ -127,9 +127,6 @@ public class GeneralSettingsActivity extends PreferenceActivity {
.setSummary(index >= 0 ? listPreference.getEntries()[index]
: null);
String prefKey = preference.getKey();
Account[] myAccountList = mAccountMgr.getAccountsByType(mAccountType);
// Handle sync frequency change
@ -161,16 +158,6 @@ public class GeneralSettingsActivity extends PreferenceActivity {
}
}
}
// 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"))) {
long syncFreq = Long.parseLong((String)value);
@ -201,10 +188,22 @@ public class GeneralSettingsActivity extends PreferenceActivity {
}
}
}*/
} else if (preference instanceof CheckBoxPreference) {
String prefKey = preference.getKey();
Boolean boolValue = (Boolean)value;
// Network types allowed for sync
if(prefKey.equals(new String("sync_wifi")) || prefKey.equals("sync_2g") ||
prefKey.equals(new String("sync_3g")) || prefKey.equals("sync_gprs") ||
prefKey.equals("sync_4g") || prefKey.equals("sync_others")) {
Log.d("FUCK",prefKey + " " + boolValue.toString());
OCSMSSharedPrefs prefs = new OCSMSSharedPrefs(mContext);
prefs.putBoolean(prefKey, boolValue);
}
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
//preference.setSummary(boolValue);
}
return true;
}
@ -219,7 +218,24 @@ public class GeneralSettingsActivity extends PreferenceActivity {
*
* @see #sBindPreferenceSummaryToValueListener
*/
private static void bindPreferenceSummaryToValue(Preference preference) {
private static void bindPreferenceBooleanToValue(Preference preference, Boolean defValue) {
// Set the listener to watch for value changes.
preference
.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(
preference,
PreferenceManager.getDefaultSharedPreferences(
preference.getContext()).getBoolean(
preference.getKey(),
defValue
)
);
}
private static void bindPreferenceStringToValue(Preference preference) {
// Set the listener to watch for value changes.
preference
.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
@ -251,8 +267,29 @@ public class GeneralSettingsActivity extends PreferenceActivity {
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference("sync_frequency"));
bindPreferenceStringToValue(findPreference("sync_frequency"));
bindPreferenceBooleanToValue(findPreference("sync_wifi"), DefaultPrefs.syncWifi);
bindPreferenceBooleanToValue(findPreference("sync_4g"), DefaultPrefs.sync4G);
bindPreferenceBooleanToValue(findPreference("sync_3g"), DefaultPrefs.sync3G);
bindPreferenceBooleanToValue(findPreference("sync_gprs"), DefaultPrefs.syncGPRS);
bindPreferenceBooleanToValue(findPreference("sync_2g"), DefaultPrefs.sync2G);
bindPreferenceBooleanToValue(findPreference("sync_others"), DefaultPrefs.syncOthers);
//bindPreferenceSummaryToValue(findPreference("slow_sync_frequency"));
}
}
private void bindPreferences() {
mContext = getBaseContext();
// Bind the summaries of EditText/List/Dialog/Ringtone preferences to
// their values. When their values change, their summaries are updated
// to reflect the new value, per the Android Design guidelines.
bindPreferenceStringToValue(findPreference("sync_frequency"));
bindPreferenceBooleanToValue(findPreference("sync_wifi"), DefaultPrefs.syncWifi);
bindPreferenceBooleanToValue(findPreference("sync_4g"), DefaultPrefs.sync4G);
bindPreferenceBooleanToValue(findPreference("sync_3g"), DefaultPrefs.sync3G);
bindPreferenceBooleanToValue(findPreference("sync_gprs"), DefaultPrefs.syncGPRS);
bindPreferenceBooleanToValue(findPreference("sync_2g"), DefaultPrefs.sync2G);
bindPreferenceBooleanToValue(findPreference("sync_others"), DefaultPrefs.syncOthers);
}
}

View File

@ -28,7 +28,13 @@ package fr.unix_experience.owncloud_sms.activities;
import java.util.List;
import java.util.Vector;
import org.json.JSONArray;
import fr.unix_experience.owncloud_sms.R;
import fr.unix_experience.owncloud_sms.engine.ConnectivityMonitor;
import fr.unix_experience.owncloud_sms.engine.SmsFetcher;
import fr.unix_experience.owncloud_sms.engine.ASyncTask.SyncTask;
import fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
@ -39,6 +45,7 @@ import android.provider.Settings;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -151,6 +158,20 @@ public class MainActivity extends Activity {
public void openAddAccount(View view) {
startActivity(new Intent(Settings.ACTION_ADD_ACCOUNT));
}
public void syncAllMessages(View view) {
ConnectivityMonitor cMon = new ConnectivityMonitor(getApplicationContext());
if (cMon.isValid()) {
// Now fetch messages since last stored date
JSONArray smsList = new SmsFetcher(getApplicationContext())
.bufferizeMessagesSinceDate((long) 0);
if (smsList != null) {
new SyncTask(getApplicationContext(), smsList).execute();
}
}
}
public void openGooglePlayStore(View view) {
Intent intent;

View File

@ -34,12 +34,11 @@ public class ConnectivityChanged extends BroadcastReceiver implements ASyncTask
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");
checkMessagesToSend(context);
checkMessagesAndSend(context);
}
// No data available and previous dataConnectionState was true
else if (dataConnectionAvailable == true && !cMon.isValid()) {
@ -48,7 +47,7 @@ public class ConnectivityChanged extends BroadcastReceiver implements ASyncTask
}
}
private void checkMessagesToSend(Context context) {
private void checkMessagesAndSend(Context context) {
// Get last message synced from preferences
Long lastMessageSynced = (new OCSMSSharedPrefs(context)).getLastMessageDate();
Log.d(TAG,"Synced Last:" + lastMessageSynced);
@ -56,7 +55,10 @@ public class ConnectivityChanged extends BroadcastReceiver implements ASyncTask
// Now fetch messages since last stored date
JSONArray smsList = new SmsFetcher(context).bufferizeMessagesSinceDate(lastMessageSynced);
if (smsList != null) {
ConnectivityMonitor cMon = new ConnectivityMonitor(context);
// Synchronize if network is valid and there are SMS
if (cMon.isValid() && smsList != null) {
new SyncTask(context, smsList).execute();
}
}

View File

@ -16,12 +16,10 @@ 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 fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs;
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 {
@ -42,21 +40,21 @@ public class ConnectivityMonitor {
// Load the connectivity manager to determine on which network we are connected
NetworkInfo netInfo = _cMgr.getActiveNetworkInfo();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(_context);
OCSMSSharedPrefs prefs = new OCSMSSharedPrefs(_context);
// Check
switch (netInfo.getType()) {
case ConnectivityManager.TYPE_WIFI:
return prefs.getBoolean("sync_wifi", DefaultPrefs.syncWifi);
return prefs.syncInWifi();
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);
return prefs.syncIn2G();
case TelephonyManager.NETWORK_TYPE_GPRS:
return prefs.getBoolean("sync_gprs", DefaultPrefs.syncGPRS);
return prefs.syncInGPRS();
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
@ -64,14 +62,14 @@ public class ConnectivityMonitor {
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
case TelephonyManager.NETWORK_TYPE_HSPAP:
return prefs.getBoolean("sync_3g", DefaultPrefs.sync3G);
return prefs.syncIn3G();
case TelephonyManager.NETWORK_TYPE_LTE:
return prefs.getBoolean("sync_4g", DefaultPrefs.sync3G);
return prefs.syncIn4G();
default:
return prefs.getBoolean("sync_others", DefaultPrefs.syncOthers);
return prefs.syncInOtherModes();
}
default:
return prefs.getBoolean("sync_others", DefaultPrefs.syncOthers);
return prefs.syncInOtherModes();
}

View File

@ -20,6 +20,7 @@ package fr.unix_experience.owncloud_sms.observers;
import org.json.JSONArray;
import fr.unix_experience.owncloud_sms.engine.ASyncTask;
import fr.unix_experience.owncloud_sms.engine.ConnectivityMonitor;
import fr.unix_experience.owncloud_sms.engine.OCSMSOwnCloudClient;
import fr.unix_experience.owncloud_sms.engine.SmsFetcher;
import fr.unix_experience.owncloud_sms.enums.MailboxID;
@ -46,7 +47,10 @@ public class SmsObserver extends ContentObserver implements ASyncTask {
SmsFetcher fetcher = new SmsFetcher(_context);
JSONArray smsList = fetcher.getLastMessage(MailboxID.ALL);
if (smsList != null) {
ConnectivityMonitor cMon = new ConnectivityMonitor(_context);
// Synchronize if network is valid and there are SMS
if (cMon.isValid() && smsList != null) {
new SyncTask(_context, smsList).execute();
}
}

View File

@ -18,8 +18,10 @@ package fr.unix_experience.owncloud_sms.prefs;
*/
import fr.unix_experience.owncloud_sms.R;
import fr.unix_experience.owncloud_sms.defines.DefaultPrefs;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class OCSMSSharedPrefs {
@ -29,6 +31,12 @@ public class OCSMSSharedPrefs {
_sPrefs = _context.getSharedPreferences(_context.getString(R.string.shared_preference_file), Context.MODE_PRIVATE);
}
public void putBoolean(String prefKey, Boolean boolValue) {
Editor edit = _sPrefs.edit();
edit.putBoolean(prefKey, boolValue);
edit.commit();
}
public void setLastMessageDate(Long msgDate) {
SharedPreferences.Editor editor = _sPrefs.edit();
editor.putLong(_context.getString(R.string.pref_lastmsgdate), msgDate);
@ -39,6 +47,30 @@ public class OCSMSSharedPrefs {
return _sPrefs.getLong(_context.getString(R.string.pref_lastmsgdate), 0);
}
public Boolean syncInWifi() {
return _sPrefs.getBoolean("sync_wifi", DefaultPrefs.syncWifi);
}
public Boolean syncIn2G() {
return _sPrefs.getBoolean("sync_2g", DefaultPrefs.sync2G);
}
public Boolean syncInGPRS() {
return _sPrefs.getBoolean("sync_gprs", DefaultPrefs.syncGPRS);
}
public Boolean syncIn3G() {
return _sPrefs.getBoolean("sync_3g", DefaultPrefs.sync3G);
}
public Boolean syncIn4G() {
return _sPrefs.getBoolean("sync_4g", DefaultPrefs.sync4G);
}
public Boolean syncInOtherModes() {
return _sPrefs.getBoolean("sync_others", DefaultPrefs.syncOthers);
}
private SharedPreferences _sPrefs;
private Context _context;
}