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()) {
// Now fetch messages since last stored date
final JSONArray smsList = new SmsFetcher(ctx)
.bufferizeMessagesSinceDate((long) 0);
.bufferMessagesSinceDate((long) 0);
if (smsList != null) {
final OCSMSNotificationManager nMgr = new OCSMSNotificationManager(ctx);

View File

@ -1,36 +1,26 @@
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.AccountManager;
import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.Spinner;
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.adapters.ContactListAdapter;
import fr.unix_experience.owncloud_sms.engine.ASyncContactLoad;
import fr.unix_experience.owncloud_sms.engine.OCSMSOwnCloudClient;
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));
// Init view
objects = new ArrayList<String>();
objects = new ArrayList<>();
setContentView(R.layout.restore_activity_contactlist);
_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/>.
*/
import org.json.JSONArray;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.BroadcastReceiver;
@ -26,6 +24,10 @@ import android.content.Context;
import android.content.Intent;
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.engine.ASyncSMSSync;
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 {
@Override
public void onReceive(final Context context, final Intent intent) {
public void onReceive(Context context, Intent intent) {
// No account: abort
final Account[] myAccountList = AccountManager.get(context).
Account[] myAccountList = AccountManager.get(context).
getAccountsByType(context.getString(R.string.account_type));
if (myAccountList.length == 0) {
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()) {
Log.d(TAG,"ConnectivityChanges.onReceive: pushOnReceive is disabled");
Log.d(ConnectivityChanged.TAG,"ConnectivityChanges.onReceive: pushOnReceive is disabled");
return;
}
// 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");
if (cMon.isValid() && !ConnectivityChanged.dataConnectionAvailable) {
ConnectivityChanged.dataConnectionAvailable = true;
Log.d(ConnectivityChanged.TAG,"ConnectivityChanged.onReceive, data conn available");
checkMessagesAndSend(context);
}
// No data available and previous dataConnectionState was true
else if (dataConnectionAvailable == true && !cMon.isValid()) {
dataConnectionAvailable = false;
Log.d(TAG,"ConnectivityChanges.onReceive: data conn is off");
else if (ConnectivityChanged.dataConnectionAvailable && !cMon.isValid()) {
ConnectivityChanged.dataConnectionAvailable = false;
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
final Long lastMessageSynced = (new OCSMSSharedPrefs(context)).getLastMessageDate();
Log.d(TAG,"Synced Last:" + lastMessageSynced);
Long lastMessageSynced = (new OCSMSSharedPrefs(context)).getLastMessageDate();
Log.d(ConnectivityChanged.TAG,"Synced Last:" + lastMessageSynced);
// 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
if (cMon.isValid() && smsList != null) {
if (cMon.get().isValid() && (smsList != null)) {
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/>.
*/
import fr.unix_experience.owncloud_sms.observers.SmsObserver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@ -25,15 +24,17 @@ import android.net.Uri;
import android.os.Handler;
import android.util.Log;
import fr.unix_experience.owncloud_sms.observers.SmsObserver;
public class IncomingSms extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (_mboxObserver == null) {
Log.d(TAG,"_mboxObserver == null");
_mboxObserver = new SmsObserver(new Handler(), context);
if (IncomingSms._mboxObserver == null) {
Log.d(IncomingSms.TAG,"_mboxObserver == null");
IncomingSms._mboxObserver = new SmsObserver(new Handler(), context);
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() {
_jsonDataDump = new JSONArray();
bufferizeMailboxMessages(MailboxID.INBOX);
bufferizeMailboxMessages(MailboxID.SENT);
bufferizeMailboxMessages(MailboxID.DRAFTS);
bufferMailboxMessages(MailboxID.INBOX);
bufferMailboxMessages(MailboxID.SENT);
bufferMailboxMessages(MailboxID.DRAFTS);
return _jsonDataDump;
}
private void bufferizeMailboxMessages(MailboxID mbID) {
private void bufferMailboxMessages(MailboxID mbID) {
String mbURI = mapMailboxIDToURI(mbID);
if (_context == null || mbURI == null) {
@ -60,16 +60,10 @@ public class SmsFetcher {
// We generate a ID list for this message box
String existingIDs = buildExistingMessagesString(mbID);
Cursor c = null;
if (existingIDs.length() > 0) {
c = (new SmsDataProvider(_context)).query(mbURI, "_id NOT IN (" + existingIDs + ")");
}
else {
c = (new SmsDataProvider(_context)).query(mbURI);
}
// Reading mailbox
Cursor c = new SmsDataProvider(_context).queryNonExistingMessages(mbURI, existingIDs);
// Reading mailbox
if (c != null && c.getCount() > 0) {
c.moveToFirst();
do {
@ -78,43 +72,45 @@ public class SmsFetcher {
try {
for(int idx=0;idx<c.getColumnCount();idx++) {
String colName = c.getColumnName(idx);
// Id column is must be an integer
if (colName.equals(new String("_id")) ||
colName.equals(new String("type"))) {
entry.put(colName, c.getInt(idx));
}
// Seen and read must be pseudo boolean
else if (colName.equals(new String("read")) ||
colName.equals(new String("seen"))) {
entry.put(colName, c.getInt(idx) > 0 ? "true" : "false");
}
else {
// Special case for date, we need to record last without searching
if (colName.equals(new String("date"))) {
final Long tmpDate = c.getLong(idx);
if (tmpDate > _lastMsgDate) {
_lastMsgDate = tmpDate;
}
}
entry.put(colName, c.getString(idx));
}
switch (colName) {
case "_id":
case "type":
entry.put(colName, c.getInt(idx));
break;
// Seen and read must be pseudo boolean
case "read":
case "seen":
entry.put(colName, c.getInt(idx) > 0 ? "true" : "false");
break;
default:
// Special case for date, we need to record last without searching
if (colName.equals("date")) {
final Long tmpDate = c.getLong(idx);
if (tmpDate > _lastMsgDate) {
_lastMsgDate = tmpDate;
}
}
entry.put(colName, c.getString(idx));
break;
}
}
// Mailbox ID is required by server
entry.put("mbox", mbID.ordinal());
_jsonDataDump.put(entry);
} catch (JSONException e) {
Log.e(TAG, "JSON Exception when reading SMS Mailbox", e);
c.close();
}
}
while(c.moveToNext());
Log.d(TAG, c.getCount() + " messages read from " + mbURI);
c.close();
}
}
@ -143,21 +139,23 @@ public class SmsFetcher {
String colName = c.getColumnName(idx);
// Id column is must be an integer
if (colName.equals(new String("_id"))) {
entry.put(colName, c.getInt(idx));
}
// Seen and read must be pseudo boolean
else if (colName.equals(new String("read")) ||
colName.equals(new String("seen"))) {
entry.put(colName, c.getInt(idx) > 0 ? "true" : "false");
}
else if (colName.equals(new String("type"))) {
mboxId = c.getInt(idx);
entry.put(colName, c.getInt(idx));
}
else {
entry.put(colName, c.getString(idx));
}
switch (colName) {
case "_id":
entry.put(colName, c.getInt(idx));
break;
// Seen and read must be pseudo boolean
case "read":
case "seen":
entry.put(colName, c.getInt(idx) > 0 ? "true" : "false");
break;
case "type":
mboxId = c.getInt(idx);
entry.put(colName, c.getInt(idx));
break;
default:
entry.put(colName, c.getString(idx));
break;
}
}
/*
@ -179,16 +177,16 @@ public class SmsFetcher {
}
// Used by ConnectivityChanged Event
public JSONArray bufferizeMessagesSinceDate(Long sinceDate) {
public JSONArray bufferMessagesSinceDate(Long sinceDate) {
_jsonDataDump = new JSONArray();
bufferizeMessagesSinceDate(MailboxID.INBOX, sinceDate);
bufferizeMessagesSinceDate(MailboxID.SENT, sinceDate);
bufferizeMessagesSinceDate(MailboxID.DRAFTS, sinceDate);
bufferMessagesSinceDate(MailboxID.INBOX, sinceDate);
bufferMessagesSinceDate(MailboxID.SENT, sinceDate);
bufferMessagesSinceDate(MailboxID.DRAFTS, sinceDate);
return _jsonDataDump;
}
// Used by ConnectivityChanged Event
public void bufferizeMessagesSinceDate(MailboxID mbID, Long sinceDate) {
public void bufferMessagesSinceDate(MailboxID mbID, Long sinceDate) {
String mbURI = mapMailboxIDToURI(mbID);
if (_context == null || mbURI == null) {
@ -208,29 +206,27 @@ public class SmsFetcher {
String colName = c.getColumnName(idx);
// Id column is must be an integer
if (colName.equals(new String("_id")) ||
colName.equals(new String("type"))) {
entry.put(colName, c.getInt(idx));
// bufferize Id for future use
if (colName.equals(new String("_id"))) {
}
}
// Seen and read must be pseudo boolean
else if (colName.equals(new String("read")) ||
colName.equals(new String("seen"))) {
entry.put(colName, c.getInt(idx) > 0 ? "true" : "false");
}
else {
// Special case for date, we need to record last without searching
if (colName.equals(new String("date"))) {
final Long tmpDate = c.getLong(idx);
if (tmpDate > _lastMsgDate) {
_lastMsgDate = tmpDate;
}
}
entry.put(colName, c.getString(idx));
}
switch (colName) {
case "_id":
case "type":
entry.put(colName, c.getInt(idx));
break;
// Seen and read must be pseudo boolean
case "read":
case "seen":
entry.put(colName, c.getInt(idx) > 0 ? "true" : "false");
break;
default:
// Special case for date, we need to record last without searching
if (colName.equals("date")) {
final Long tmpDate = c.getLong(idx);
if (tmpDate > _lastMsgDate) {
_lastMsgDate = tmpDate;
}
}
entry.put(colName, c.getString(idx));
break;
}
}
// Mailbox ID is required by server
@ -288,7 +284,7 @@ public class SmsFetcher {
sb.append(",");
}
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) {
return query(Uri.parse(mailBox),
new String[] { "read", "date", "address", "seen", "body", "_id", "type", },