Commit da814310 authored by Alan McNatty's avatar Alan McNatty

Fixed syncing notifications by having the sync API return the time() from...

Fixed syncing notifications by having the sync API return the time() from server so we don't rely on handset knowning server time , etc, etc. Also added the sync.time to preferences for debugging purposes.

Also fixed the 'bug' where if we 'write' a journal if no default journal pick the first journal.
parent 3109a301
......@@ -32,7 +32,7 @@ require(dirname(dirname(dirname(__FILE__))) . '/init.php');
safe_require('artefact', 'file');
safe_require('artefact', 'blog');
$json = array();
$json = array( 'time' => time() );
if (!get_config('allowmobileuploads')) {
jsonreply(array('fail' => 'Mobile uploads disabled'));
......@@ -69,7 +69,7 @@ catch (AuthUnknownUserException $e) {
}
// Add in bits of sync data - let's start with notifications
$lastsync = time();
$lastsync = 0;
try {
$lastsync = param_variable('lastsync') + 0;
}
......@@ -91,7 +91,7 @@ $activity_arr = get_records_sql_array("select n.id, n.subject, n.message
where n.type=a.id and n.read=0 and " .
db_format_tsfield('ctime', '') . " >= ?
and n.usr= ? " . $notification_types_sql,
array($lastsync + 0, $USER->id));
array($lastsync, $USER->id));
if ( count($activity_arr) > 0 )
$json['activity'] = $activity_arr;
......
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -46,7 +46,7 @@
android:singleLine="false"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:paddingBottom="5dip"
android:paddingBottom="15dip"
style="@android:style/TextAppearance.Small">
</TextView>
</LinearLayout>
......@@ -61,7 +61,6 @@
android:id="@+id/ArtefactTitleLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView android:text="@string/upload_title_label"
......
......@@ -117,6 +117,10 @@
<string name="pref_sync_periodic_title">Periodic Sync</string>
<string name="pref_sync_periodic_summary">Enter the time in minutes to force a perioidc sync (0 or blank for none)</string>
<string name="pref_sync_time_key">sync.time</string>
<string name="pref_sync_time_title">Last Sync Time</string>
<string name="pref_sync_time_summary">The time, in seconds, received from the server of last sync.</string>
<string name="pref_menu_reset">Reset</string>
<string name="pref_menu_about">About</string>
<string name="pref_menu_scan">Scan</string>
......
......@@ -170,5 +170,12 @@
android:defaultValue="15"
android:numeric="integer"
/>
<EditTextPreference
android:key="@string/pref_sync_time_key"
android:title="@string/pref_sync_time_title"
android:summary="@string/pref_sync_time_summary"
android:defaultValue="0"
android:numeric="integer"
/>
</PreferenceCategory>
</PreferenceScreen>
\ No newline at end of file
......@@ -65,23 +65,6 @@ public class Utils {
// whether VERBOSE level logging is enabled
static final boolean VERBOSE = LogConfig.VERBOSE;
public static void runSyncNow(Context context) {
AccountManager mAccountManager = AccountManager.get(context);
Account account;
// TODO replicated from AuthenticatorActivity
Account[] mAccounts = mAccountManager.getAccountsByType(GlobalResources.ACCOUNT_TYPE);
if ( mAccounts.length > 0 ) {
// Just pick the first one .. support multiple accounts can come later.
account = mAccounts[0];
} else {
return;
}
ContentResolver.requestSync(account, GlobalResources.ACCOUNT_TYPE, null);
}
public static boolean canUpload(Context context) {
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
......@@ -258,7 +241,7 @@ public class Utils {
mNM.cancel(id);
}
public static long processSyncResults(JSONObject result, ContentProviderClient myProvider, Context context) {
public static long processSyncResults(JSONObject result, ContentProviderClient myProvider, Context context, String sync_key) {
// TODO Auto-generated method stub
long numUpdates = 0;
try {
......@@ -285,6 +268,21 @@ public class Utils {
long newItems = updateListPreferenceFromJSON(myProvider, syncObj.getJSONArray("folders"), "folder");
numUpdates += newItems;
}
if ( syncObj.has("time") ) {
// Save last sync time
String last_sync = syncObj.getString("time");
Log.v(TAG, "saving sync time as: " + last_sync);
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
// We save current time in seconds since 1970 in UTC!!
// TODO fix this - get the sync api to respond with the current server time which we can save here
// i.e. a syncObj.has("time") piece containing the epoch to store.
mPrefs.edit()
.putString(sync_key, last_sync)
.commit()
;
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
......@@ -293,15 +291,6 @@ public class Utils {
e.printStackTrace();
}
// Save last sync time
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
// We save current time in seconds since 1970 in UTC!!
mPrefs.edit()
.putLong("lastsync", System.currentTimeMillis()/1000)
.commit()
;
return numUpdates;
}
......
......@@ -67,7 +67,8 @@ public class ThreadedSyncAdapter extends AbstractThreadedSyncAdapter{
"");
String token = mPrefs.getString(mContext.getResources().getString(R.string.pref_auth_token_key).toString(),
"");
Long lastsync = mPrefs.getLong("lastsync", 0);
String sync_key = mContext.getResources().getString(R.string.pref_sync_time_key);
Long lastsync = Long.parseLong(mPrefs.getString(sync_key, "0"));
String syncNotifications = Utils.getSyncNotificationsPref(mContext);
......@@ -79,7 +80,7 @@ public class ThreadedSyncAdapter extends AbstractThreadedSyncAdapter{
if ( Utils.updateTokenFromResult(result, mContext) == null ) {
++syncResult.stats.numAuthExceptions;
} else if ( result.has("sync") ) {
syncResult.stats.numUpdates = Utils.processSyncResults(result, myProvider, mContext);
syncResult.stats.numUpdates = Utils.processSyncResults(result, myProvider, mContext, sync_key);
// OK sync success - now push any uploads
// Check if we have appropriate data access
......
......@@ -170,7 +170,7 @@ public class ArtefactExpandableListAdapterActivity extends Activity implements O
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case R.id.option_delete:
artefactData.deleteAllSavedArtefacts();
......@@ -184,25 +184,25 @@ public class ArtefactExpandableListAdapterActivity extends Activity implements O
startActivity(new Intent(this, AboutActivity.class));
break;
case R.id.option_pref:
Intent intent = new Intent(this, EditPreferences.class);
intent = new Intent(this, EditPreferences.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
break;
case R.id.option_account:
//i.putExtra(Settings.EXTRA_AUTHORITIES, new String[] {GlobalResources.ACCOUNT_TYPE});
startActivity(new Intent(Settings.ACTION_SYNC_SETTINGS).putExtra(Settings.EXTRA_AUTHORITIES, new String[] {GlobalResources.SYNC_AUTHORITY}));
break;
case R.id.option_camera:
startActivityForResult(Utils.makeCameraIntent(mContext), GlobalResources.REQ_CAMERA_RETURN);
break;
case R.id.option_gallery:
Intent i = new Intent(Intent.ACTION_PICK,
intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, GlobalResources.REQ_GALLERY_RETURN);
startActivityForResult(intent, GlobalResources.REQ_GALLERY_RETURN);
break;
case R.id.option_compose:
startActivity(new Intent(this, ArtifactSettingsActivity.class));
intent = new Intent(this, ArtifactSettingsActivity.class);
intent.putExtra("writejournal", true);
startActivity(intent);
break;
}
return true;
......
......@@ -76,6 +76,8 @@ public class ArtifactSettingsActivity extends Activity implements OnClickListene
// whether VERBOSE level logging is enabled
static final boolean VERBOSE = LogConfig.VERBOSE;
private boolean DEFAULT_TO_JOURNAL = false;
// application preferences
private SharedPreferences mPrefs;
......@@ -145,55 +147,61 @@ public class ArtifactSettingsActivity extends Activity implements OnClickListene
// InputMethodManager.HIDE_IMPLICIT_ONLY);
m_extras = getIntent().getExtras();
if ( m_extras == null ) {
if ( m_extras == null ) {
if ( DEBUG ) Log.d(TAG, "Nothing passed - write a journal post without attachment.");
setDefaultJournal();
setDefaultTag();
// Load a saved artefact
} else if ( m_extras.containsKey("artefact") ) {
if ( DEBUG ) Log.d(TAG, "Have a saved artefact to upload");
a = m_extras.getParcelable("artefact");
uris = new String[] { a.getFilename() };
((EditText)findViewById(R.id.txtArtefactTitle)).setText(a.getTitle());
((EditText)findViewById(R.id.txtArtefactTitle)).selectAll();
((EditText)findViewById(R.id.txtArtefactDescription)).setText(a.getDescription());
((EditText)findViewById(R.id.txtArtefactTags)).setText(a.getTags());
((EditText)findViewById(R.id.txtArtefactId)).setText(a.getId().toString());
((CheckBox)findViewById(R.id.txtArtefactIsDraft)).setChecked(a.getIsDraft());
((CheckBox)findViewById(R.id.txtArtefactAllowComments)).setChecked(a.getAllowComments());
setDefaultJournal();
} else {
DEFAULT_TO_JOURNAL = m_extras.containsKey("writejournal");
if ( DEBUG ) Log.d(TAG, "Have extras - default to journal? ... " + DEFAULT_TO_JOURNAL);
} else if ( m_extras.containsKey("uri") ) {
if ( DEBUG ) Log.d(TAG, "Have a new upload");
setDefaultJournal();
setDefaultTag();
uris = m_extras.getStringArray("uri");
// If single - show the title (with default) and description
if ( uris.length == 1 ) {
if ( DEBUG ) Log.d(TAG, "Have a single upload");
a = new Artefact(uris[0]);
setDefaultTitle(a.getBaseFilename(mContext));
} else if ( uris.length > 1 ) {
if ( DEBUG ) Log.d(TAG, "Have a multi upload");
if ( m_extras.containsKey("artefact") ) {
if ( DEBUG ) Log.d(TAG, "Have a saved artefact to upload");
a = m_extras.getParcelable("artefact");
uris = new String[] { a.getFilename() };
((EditText)findViewById(R.id.txtArtefactTitle)).setText(a.getTitle());
((EditText)findViewById(R.id.txtArtefactTitle)).selectAll();
((EditText)findViewById(R.id.txtArtefactDescription)).setText(a.getDescription());
((EditText)findViewById(R.id.txtArtefactTags)).setText(a.getTags());
((EditText)findViewById(R.id.txtArtefactId)).setText(a.getId().toString());
((CheckBox)findViewById(R.id.txtArtefactIsDraft)).setChecked(a.getIsDraft());
((CheckBox)findViewById(R.id.txtArtefactAllowComments)).setChecked(a.getAllowComments());
setDefaultJournal();
} else if ( m_extras.containsKey("uri") ) {
if ( DEBUG ) Log.d(TAG, "Have a new upload");
setDefaultJournal();
setDefaultTag();
uris = m_extras.getStringArray("uri");
// If single - show the title (with default) and description
if ( uris.length == 1 ) {
if ( DEBUG ) Log.d(TAG, "Have a single upload");
a = new Artefact(uris[0]);
setDefaultTitle(a.getBaseFilename(mContext));
} else if ( uris.length > 1 ) {
if ( DEBUG ) Log.d(TAG, "Have a multi upload");
} else {
if ( DEBUG ) Log.d(TAG, "Passed uri key, but no uri's - bogus link?");
// TODO show toast message but not finish? Maybe they want to write a Journal post and
// attach an file?
finish();
}
} else {
if ( DEBUG ) Log.d(TAG, "Passed uri key, but no uri's - bogus link?");
// TODO show toast message but not finish? Maybe they want to write a Journal post and
// attach an file?
finish();
setDefaultJournal();
setDefaultTag();
}
}
// Check acceptance of upload conditions
// Check acceptance of upload conditions
checkAcceptanceOfConditions();
// Check data connection
......@@ -247,6 +255,8 @@ public class ArtifactSettingsActivity extends Activity implements OnClickListene
} else {
if ( mPrefs.getBoolean(getResources().getString(R.string.pref_upload_journal_default_key), false) ) {
journal_id = mPrefs.getString(getResources().getString(R.string.pref_upload_journal_key), null);
} else if ( DEFAULT_TO_JOURNAL && journalKeys.length > 1 ) { // o - is upload file
journal_id = journalKeys[1];
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment