1
0
mirror of https://github.com/nerzhul/ownCloud-SMS-App.git synced 2025-06-27 17:56:09 +00:00

Little refactoring

This commit is contained in:
Loic Blot 2015-11-03 22:32:51 +01:00
parent 8bdaa2a42e
commit 2576ca7468
6 changed files with 118 additions and 121 deletions

View File

@ -169,7 +169,7 @@ public class MainActivity extends Activity {
if (cMon.isValid()) { if (cMon.isValid()) {
// Now fetch messages since last stored date // Now fetch messages since last stored date
final JSONArray smsList = new SmsFetcher(ctx) final JSONArray smsList = new SmsFetcher(ctx)
.bufferizeMessagesSinceDate((long) 0); .bufferMessagesSinceDate((long) 0);
if (smsList != null) { if (smsList != null) {
final OCSMSNotificationManager nMgr = new OCSMSNotificationManager(ctx); final OCSMSNotificationManager nMgr = new OCSMSNotificationManager(ctx);

View File

@ -1,36 +1,26 @@
package fr.unix_experience.owncloud_sms.activities.remote_account; package fr.unix_experience.owncloud_sms.activities.remote_account;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Vector;
import android.accounts.Account; import android.accounts.Account;
import android.accounts.AccountManager; import android.accounts.AccountManager;
import android.app.Activity; import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.TextView; import android.widget.TextView;
import org.w3c.dom.Text; import java.util.ArrayList;
import java.util.Vector;
import fr.nrz.androidlib.adapters.AndroidAccountAdapter;
import fr.unix_experience.owncloud_sms.R; import fr.unix_experience.owncloud_sms.R;
import fr.unix_experience.owncloud_sms.adapters.ContactListAdapter; import fr.unix_experience.owncloud_sms.adapters.ContactListAdapter;
import fr.unix_experience.owncloud_sms.engine.ASyncContactLoad; import fr.unix_experience.owncloud_sms.engine.ASyncContactLoad;
import fr.unix_experience.owncloud_sms.engine.OCSMSOwnCloudClient;
public class ContactListActivity extends Activity implements ASyncContactLoad { public class ContactListActivity extends Activity implements ASyncContactLoad {
@ -55,7 +45,7 @@ public class ContactListActivity extends Activity implements ASyncContactLoad {
_accountMgr.getAccountsByType(getString(R.string.account_type)); _accountMgr.getAccountsByType(getString(R.string.account_type));
// Init view // Init view
objects = new ArrayList<String>(); objects = new ArrayList<>();
setContentView(R.layout.restore_activity_contactlist); setContentView(R.layout.restore_activity_contactlist);
_layout = (SwipeRefreshLayout) findViewById(R.id.contactlist_swipe_container); _layout = (SwipeRefreshLayout) findViewById(R.id.contactlist_swipe_container);

View File

@ -17,8 +17,6 @@ package fr.unix_experience.owncloud_sms.broadcast_receivers;
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
import org.json.JSONArray;
import android.accounts.Account; import android.accounts.Account;
import android.accounts.AccountManager; import android.accounts.AccountManager;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
@ -26,6 +24,10 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.util.Log; import android.util.Log;
import org.json.JSONArray;
import java.util.concurrent.atomic.AtomicReference;
import fr.unix_experience.owncloud_sms.R; import fr.unix_experience.owncloud_sms.R;
import fr.unix_experience.owncloud_sms.engine.ASyncSMSSync; import fr.unix_experience.owncloud_sms.engine.ASyncSMSSync;
import fr.unix_experience.owncloud_sms.engine.ConnectivityMonitor; import fr.unix_experience.owncloud_sms.engine.ConnectivityMonitor;
@ -35,49 +37,49 @@ import fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs;
public class ConnectivityChanged extends BroadcastReceiver implements ASyncSMSSync { public class ConnectivityChanged extends BroadcastReceiver implements ASyncSMSSync {
@Override @Override
public void onReceive(final Context context, final Intent intent) { public void onReceive(Context context, Intent intent) {
// No account: abort // No account: abort
final Account[] myAccountList = AccountManager.get(context). Account[] myAccountList = AccountManager.get(context).
getAccountsByType(context.getString(R.string.account_type)); getAccountsByType(context.getString(R.string.account_type));
if (myAccountList.length == 0) { if (myAccountList.length == 0) {
return; return;
} }
final ConnectivityMonitor cMon = new ConnectivityMonitor(context); ConnectivityMonitor cMon = new ConnectivityMonitor(context);
final OCSMSSharedPrefs prefs = new OCSMSSharedPrefs(context); OCSMSSharedPrefs prefs = new OCSMSSharedPrefs(context);
if (!prefs.pushOnReceive()) { if (!prefs.pushOnReceive()) {
Log.d(TAG,"ConnectivityChanges.onReceive: pushOnReceive is disabled"); Log.d(ConnectivityChanged.TAG,"ConnectivityChanges.onReceive: pushOnReceive is disabled");
return; return;
} }
// If data is available and previous dataConnectionState was false, then we need to sync // If data is available and previous dataConnectionState was false, then we need to sync
if (cMon.isValid() && dataConnectionAvailable == false) { if (cMon.isValid() && !ConnectivityChanged.dataConnectionAvailable) {
dataConnectionAvailable = true; ConnectivityChanged.dataConnectionAvailable = true;
Log.d(TAG,"ConnectivityChanged.onReceive, data conn available"); Log.d(ConnectivityChanged.TAG,"ConnectivityChanged.onReceive, data conn available");
checkMessagesAndSend(context); checkMessagesAndSend(context);
} }
// No data available and previous dataConnectionState was true // No data available and previous dataConnectionState was true
else if (dataConnectionAvailable == true && !cMon.isValid()) { else if (ConnectivityChanged.dataConnectionAvailable && !cMon.isValid()) {
dataConnectionAvailable = false; ConnectivityChanged.dataConnectionAvailable = false;
Log.d(TAG,"ConnectivityChanges.onReceive: data conn is off"); Log.d(ConnectivityChanged.TAG,"ConnectivityChanges.onReceive: data conn is off");
} }
} }
private void checkMessagesAndSend(final Context context) { private void checkMessagesAndSend(Context context) {
// Get last message synced from preferences // Get last message synced from preferences
final Long lastMessageSynced = (new OCSMSSharedPrefs(context)).getLastMessageDate(); Long lastMessageSynced = (new OCSMSSharedPrefs(context)).getLastMessageDate();
Log.d(TAG,"Synced Last:" + lastMessageSynced); Log.d(ConnectivityChanged.TAG,"Synced Last:" + lastMessageSynced);
// Now fetch messages since last stored date // Now fetch messages since last stored date
final JSONArray smsList = new SmsFetcher(context).bufferizeMessagesSinceDate(lastMessageSynced); JSONArray smsList = new SmsFetcher(context).bufferMessagesSinceDate(lastMessageSynced);
final ConnectivityMonitor cMon = new ConnectivityMonitor(context); AtomicReference<ConnectivityMonitor> cMon = new AtomicReference<>(new ConnectivityMonitor(context));
// Synchronize if network is valid and there are SMS // Synchronize if network is valid and there are SMS
if (cMon.isValid() && smsList != null) { if (cMon.get().isValid() && (smsList != null)) {
new SyncTask(context, smsList).execute(); new SyncTask(context, smsList).execute();
} }
} }

View File

@ -17,7 +17,6 @@ package fr.unix_experience.owncloud_sms.broadcast_receivers;
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
import fr.unix_experience.owncloud_sms.observers.SmsObserver;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -25,15 +24,17 @@ import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.util.Log; import android.util.Log;
import fr.unix_experience.owncloud_sms.observers.SmsObserver;
public class IncomingSms extends BroadcastReceiver { public class IncomingSms extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (_mboxObserver == null) { if (IncomingSms._mboxObserver == null) {
Log.d(TAG,"_mboxObserver == null"); Log.d(IncomingSms.TAG,"_mboxObserver == null");
_mboxObserver = new SmsObserver(new Handler(), context); IncomingSms._mboxObserver = new SmsObserver(new Handler(), context);
context.getContentResolver(). context.getContentResolver().
registerContentObserver(Uri.parse("content://sms"), true, _mboxObserver); registerContentObserver(Uri.parse("content://sms"), true, IncomingSms._mboxObserver);
} }
} }

View File

@ -39,13 +39,13 @@ public class SmsFetcher {
public JSONArray fetchAllMessages() { public JSONArray fetchAllMessages() {
_jsonDataDump = new JSONArray(); _jsonDataDump = new JSONArray();
bufferizeMailboxMessages(MailboxID.INBOX); bufferMailboxMessages(MailboxID.INBOX);
bufferizeMailboxMessages(MailboxID.SENT); bufferMailboxMessages(MailboxID.SENT);
bufferizeMailboxMessages(MailboxID.DRAFTS); bufferMailboxMessages(MailboxID.DRAFTS);
return _jsonDataDump; return _jsonDataDump;
} }
private void bufferizeMailboxMessages(MailboxID mbID) { private void bufferMailboxMessages(MailboxID mbID) {
String mbURI = mapMailboxIDToURI(mbID); String mbURI = mapMailboxIDToURI(mbID);
if (_context == null || mbURI == null) { if (_context == null || mbURI == null) {
@ -60,16 +60,10 @@ public class SmsFetcher {
// We generate a ID list for this message box // We generate a ID list for this message box
String existingIDs = buildExistingMessagesString(mbID); String existingIDs = buildExistingMessagesString(mbID);
Cursor c = null; Cursor c = new SmsDataProvider(_context).queryNonExistingMessages(mbURI, existingIDs);
if (existingIDs.length() > 0) {
c = (new SmsDataProvider(_context)).query(mbURI, "_id NOT IN (" + existingIDs + ")"); // Reading mailbox
}
else {
c = (new SmsDataProvider(_context)).query(mbURI);
}
// Reading mailbox
if (c != null && c.getCount() > 0) { if (c != null && c.getCount() > 0) {
c.moveToFirst(); c.moveToFirst();
do { do {
@ -78,43 +72,45 @@ public class SmsFetcher {
try { try {
for(int idx=0;idx<c.getColumnCount();idx++) { for(int idx=0;idx<c.getColumnCount();idx++) {
String colName = c.getColumnName(idx); String colName = c.getColumnName(idx);
// Id column is must be an integer // Id column is must be an integer
if (colName.equals(new String("_id")) || switch (colName) {
colName.equals(new String("type"))) { case "_id":
entry.put(colName, c.getInt(idx)); case "type":
} entry.put(colName, c.getInt(idx));
// Seen and read must be pseudo boolean break;
else if (colName.equals(new String("read")) || // Seen and read must be pseudo boolean
colName.equals(new String("seen"))) { case "read":
entry.put(colName, c.getInt(idx) > 0 ? "true" : "false"); case "seen":
} entry.put(colName, c.getInt(idx) > 0 ? "true" : "false");
else { break;
// Special case for date, we need to record last without searching default:
if (colName.equals(new String("date"))) { // Special case for date, we need to record last without searching
final Long tmpDate = c.getLong(idx); if (colName.equals("date")) {
if (tmpDate > _lastMsgDate) { final Long tmpDate = c.getLong(idx);
_lastMsgDate = tmpDate; if (tmpDate > _lastMsgDate) {
} _lastMsgDate = tmpDate;
} }
entry.put(colName, c.getString(idx)); }
} entry.put(colName, c.getString(idx));
break;
}
} }
// Mailbox ID is required by server // Mailbox ID is required by server
entry.put("mbox", mbID.ordinal()); entry.put("mbox", mbID.ordinal());
_jsonDataDump.put(entry); _jsonDataDump.put(entry);
} catch (JSONException e) { } catch (JSONException e) {
Log.e(TAG, "JSON Exception when reading SMS Mailbox", e); Log.e(TAG, "JSON Exception when reading SMS Mailbox", e);
c.close(); c.close();
} }
} }
while(c.moveToNext()); while(c.moveToNext());
Log.d(TAG, c.getCount() + " messages read from " + mbURI); Log.d(TAG, c.getCount() + " messages read from " + mbURI);
c.close(); c.close();
} }
} }
@ -143,21 +139,23 @@ public class SmsFetcher {
String colName = c.getColumnName(idx); String colName = c.getColumnName(idx);
// Id column is must be an integer // Id column is must be an integer
if (colName.equals(new String("_id"))) { switch (colName) {
entry.put(colName, c.getInt(idx)); case "_id":
} entry.put(colName, c.getInt(idx));
// Seen and read must be pseudo boolean break;
else if (colName.equals(new String("read")) || // Seen and read must be pseudo boolean
colName.equals(new String("seen"))) { case "read":
entry.put(colName, c.getInt(idx) > 0 ? "true" : "false"); case "seen":
} entry.put(colName, c.getInt(idx) > 0 ? "true" : "false");
else if (colName.equals(new String("type"))) { break;
mboxId = c.getInt(idx); case "type":
entry.put(colName, c.getInt(idx)); mboxId = c.getInt(idx);
} entry.put(colName, c.getInt(idx));
else { break;
entry.put(colName, c.getString(idx)); default:
} entry.put(colName, c.getString(idx));
break;
}
} }
/* /*
@ -179,16 +177,16 @@ public class SmsFetcher {
} }
// Used by ConnectivityChanged Event // Used by ConnectivityChanged Event
public JSONArray bufferizeMessagesSinceDate(Long sinceDate) { public JSONArray bufferMessagesSinceDate(Long sinceDate) {
_jsonDataDump = new JSONArray(); _jsonDataDump = new JSONArray();
bufferizeMessagesSinceDate(MailboxID.INBOX, sinceDate); bufferMessagesSinceDate(MailboxID.INBOX, sinceDate);
bufferizeMessagesSinceDate(MailboxID.SENT, sinceDate); bufferMessagesSinceDate(MailboxID.SENT, sinceDate);
bufferizeMessagesSinceDate(MailboxID.DRAFTS, sinceDate); bufferMessagesSinceDate(MailboxID.DRAFTS, sinceDate);
return _jsonDataDump; return _jsonDataDump;
} }
// Used by ConnectivityChanged Event // Used by ConnectivityChanged Event
public void bufferizeMessagesSinceDate(MailboxID mbID, Long sinceDate) { public void bufferMessagesSinceDate(MailboxID mbID, Long sinceDate) {
String mbURI = mapMailboxIDToURI(mbID); String mbURI = mapMailboxIDToURI(mbID);
if (_context == null || mbURI == null) { if (_context == null || mbURI == null) {
@ -208,29 +206,27 @@ public class SmsFetcher {
String colName = c.getColumnName(idx); String colName = c.getColumnName(idx);
// Id column is must be an integer // Id column is must be an integer
if (colName.equals(new String("_id")) || switch (colName) {
colName.equals(new String("type"))) { case "_id":
entry.put(colName, c.getInt(idx)); case "type":
entry.put(colName, c.getInt(idx));
// bufferize Id for future use break;
if (colName.equals(new String("_id"))) { // Seen and read must be pseudo boolean
} case "read":
} case "seen":
// Seen and read must be pseudo boolean entry.put(colName, c.getInt(idx) > 0 ? "true" : "false");
else if (colName.equals(new String("read")) || break;
colName.equals(new String("seen"))) { default:
entry.put(colName, c.getInt(idx) > 0 ? "true" : "false"); // Special case for date, we need to record last without searching
} if (colName.equals("date")) {
else { final Long tmpDate = c.getLong(idx);
// Special case for date, we need to record last without searching if (tmpDate > _lastMsgDate) {
if (colName.equals(new String("date"))) { _lastMsgDate = tmpDate;
final Long tmpDate = c.getLong(idx); }
if (tmpDate > _lastMsgDate) { }
_lastMsgDate = tmpDate; entry.put(colName, c.getString(idx));
} break;
} }
entry.put(colName, c.getString(idx));
}
} }
// Mailbox ID is required by server // Mailbox ID is required by server
@ -288,7 +284,7 @@ public class SmsFetcher {
sb.append(","); sb.append(",");
} }
sb.append(existingMessages.getInt(i)); sb.append(existingMessages.getInt(i));
} catch (JSONException e) { } catch (JSONException ignored) {
} }
} }

View File

@ -50,6 +50,14 @@ public class SmsDataProvider extends ContentProvider {
); );
} }
public Cursor queryNonExistingMessages(final String mailBox, final String existingIds) {
if (existingIds.length() > 0) {
return query(mailBox, "_id NOT IN (" + existingIds + ")");
}
return query(mailBox);
}
public Cursor query(final String mailBox, final String selection, final String[] selectionArgs) { public Cursor query(final String mailBox, final String selection, final String[] selectionArgs) {
return query(Uri.parse(mailBox), return query(Uri.parse(mailBox),
new String[] { "read", "date", "address", "seen", "body", "_id", "type", }, new String[] { "read", "date", "address", "seen", "body", "_id", "type", },