Skip to content

Commit

Permalink
Changed the way synchronizing is done. Added progress indicator.
Browse files Browse the repository at this point in the history
  • Loading branch information
hdweiss committed Dec 17, 2011
1 parent dbccaf7 commit 405b323
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 186 deletions.
4 changes: 3 additions & 1 deletion AndroidManifest.xml
Expand Up @@ -30,7 +30,7 @@
</receiver>

<service android:name=".Services.MobileOrgWidget$MobileOrgWidgetService" />
<service android:name=".Services.MobileOrgSyncService" >
<service android:name=".Services.SyncService" >
<intent-filter >
<action android:name="com.matburt.mobileorg.SYNC_SERVICE" />
</intent-filter>
Expand All @@ -51,6 +51,8 @@
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

<action android:name="com.matburt.mobileorg.SyncService.SyncService.action.SYNC_UPDATE" />
</intent-filter>
</activity>
<activity
Expand Down
148 changes: 82 additions & 66 deletions src/com/matburt/mobileorg/Gui/OutlineActivity.java
@@ -1,15 +1,15 @@
package com.matburt.mobileorg.Gui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
Expand All @@ -18,17 +18,17 @@
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.Toast;

import com.matburt.mobileorg.R;
import com.matburt.mobileorg.Parsing.MobileOrgApplication;
import com.matburt.mobileorg.Parsing.Node;
import com.matburt.mobileorg.Parsing.NodeEncryption;
import com.matburt.mobileorg.Parsing.OrgFile;
import com.matburt.mobileorg.Parsing.OrgFileParser;
import com.matburt.mobileorg.Services.SyncService;
import com.matburt.mobileorg.Settings.SettingsActivity;
import com.matburt.mobileorg.Settings.WizardActivity;
import com.matburt.mobileorg.Synchronizers.SyncManager;
import com.matburt.mobileorg.Synchronizers.Synchronizer;

public class OutlineActivity extends ListActivity
{
Expand All @@ -54,10 +54,9 @@ public class OutlineActivity extends ListActivity
*/
private int lastSelection = 0;

private final Handler syncHandler = new Handler();
private IOException syncError;
private ProgressDialog syncDialog;
private OutlineListAdapter outlineAdapter;
private SynchServiceReceiver syncReceiver;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -77,25 +76,37 @@ public void onCreate(Bundle savedInstanceState) {
}

registerForContextMenu(getListView());

this.syncReceiver = new SynchServiceReceiver();
}

@Override
public void onResume() {
super.onResume();

// If this is the case, the parser has invalidated nodes
if (this.depth != 1 && this.depth > this.appInst.nodestackSize()) {
this.setResult(RESULT_DONTPOP);
finish();
}
public void onResume() {
refreshDisplay();

IntentFilter serviceFilter = new IntentFilter(SyncService.SYNC_UPDATE);
registerReceiver(syncReceiver, serviceFilter);

super.onResume();
}

@Override
public void onPause() {
unregisterReceiver(this.syncReceiver);
super.onPause();
}

/**
* Refreshes the outline display. Should be called when the underlying
* data has been updated.
*/
private void refreshDisplay() {
// If this is the case, the parser/syncer has invalidated nodes
if (this.depth != 1 && this.depth > this.appInst.nodestackSize()) {
this.setResult(RESULT_DONTPOP);
finish();
}

outlineAdapter.notifyDataSetChanged();
getListView().setSelection(lastSelection);
}
Expand All @@ -111,7 +122,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_sync:
runSynchronizer();
runSync();
return true;

case R.id.menu_settings:
Expand Down Expand Up @@ -192,9 +203,19 @@ public void onListItemClick(ListView l, View v, int position, long id) {
}
}

public void showWizard() {
private void showWizard() {
startActivity(new Intent(this, WizardActivity.class));
}

private void runSync() {
this.syncDialog = new ProgressDialog(this);
this.syncDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
this.syncDialog.setCancelable(false);
this.syncDialog.setMessage("Started synchronization");
this.syncDialog.show();

startService(new Intent(this, SyncService.class));
}

private boolean runEditNewNodeActivity() {
Intent intent = new Intent(this, NodeEditActivity.class);
Expand Down Expand Up @@ -273,7 +294,47 @@ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
break;
}
}



private void showMessage(String message) {
this.syncDialog.setMessage(message);
}

private void stopSyncDialog() {
this.syncDialog.dismiss();
this.refreshDisplay();
}

private void showProgressDialog(int number, int total) {
int progress = ((40 / total) * number);
// TODO Fix progress bar
this.syncDialog.setProgress(60 + progress + 1);
}

private void showProgress(int total) {
this.syncDialog.setProgress(total);
}

// TODO Add the handling of thrown error messages from synchronizer
public class SynchServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int num = intent.getIntExtra(Synchronizer.SYNC_FILES, 0);
int totalfiles = intent.getIntExtra(Synchronizer.SYNC_FILES_TOTAL, 0);
int progress = intent.getIntExtra(Synchronizer.SYNC_PROGRESS, 0);
String message = intent.getStringExtra(Synchronizer.SYNC_MESSAGE);

if (intent.getBooleanExtra(Synchronizer.SYNC_DONE, false))
stopSyncDialog();
if (num > 0)
showProgressDialog(num, totalfiles);
if (message != null)
showMessage(message);
if (progress > 0)
showProgress(progress);
}
}

/**
* This calls startActivityForResult() with Encryption.DECRYPT_MESSAGE. The
* result is handled by onActivityResult() in this class, which calls a
Expand All @@ -291,62 +352,17 @@ private void runDecryptAndExpandNode(Node node) {
NodeEncryption.decrypt(this, rawData);
}
}

/**
* This function is called with the results of {@link #runDecryptAndExpandNode}.
* This function is called with the results of
* {@link #runDecryptAndExpandNode}.
*/
private void parseEncryptedNode(Intent data, Node node) {
OrgFileParser ofp = new OrgFileParser(getBaseContext(), appInst);

String decryptedData = data
.getStringExtra(NodeEncryption.EXTRA_DECRYPTED_MESSAGE);

ofp.parse(node, new BufferedReader(new StringReader(
decryptedData)));
}

private void runSynchronizer() {
final SyncManager synchman = new SyncManager(this, this.appInst);

if (!synchman.isConfigured()) {
Toast error = Toast.makeText((Context) this,
getString(R.string.error_synchronizer_not_configured),
Toast.LENGTH_LONG);
error.show();
this.runShowSettings();
return;
}

Thread syncThread = new Thread() {
public void run() {
try {
syncError = null;
synchman.sync();
} catch (IOException e) {
syncError = e;
} finally {
synchman.close();
}
syncHandler.post(syncUpdateResults);
}
};
syncThread.start();
syncDialog = ProgressDialog.show(this, "",
getString(R.string.sync_wait), true);
}

private final Runnable syncUpdateResults = new Runnable() {
public void run() {
postSynchronize();
}
};

private void postSynchronize() {
syncDialog.dismiss();
if (this.syncError != null) {
ErrorReporter.displayError(this, this.syncError.getMessage());
} else {
this.onResume();
}
ofp.parse(node, new BufferedReader(new StringReader(decryptedData)));
}
}

0 comments on commit 405b323

Please sign in to comment.