From 9b1bf6bdb329a69448f11f2a459196d7a7ef08e3 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Wed, 21 Oct 2015 16:43:13 +0200 Subject: [PATCH] Moving Bitmessage context into a foreground service (work in progress) --- app/src/main/AndroidManifest.xml | 3 +- .../dissem/apps/abit/MessageListActivity.java | 16 ++--- .../notification/AbstractNotification.java | 2 +- .../notification/NetworkNotification.java | 10 +++- .../abit/synchronization/SyncAdapter.java | 19 +----- .../abit/synchronization/SyncService.java | 60 ++++++++++++++++++- 6 files changed, 81 insertions(+), 29 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f43e1e6..f635f00 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -119,8 +119,7 @@ + android:exported="true"> diff --git a/app/src/main/java/ch/dissem/apps/abit/MessageListActivity.java b/app/src/main/java/ch/dissem/apps/abit/MessageListActivity.java index 70891ea..f488cdf 100644 --- a/app/src/main/java/ch/dissem/apps/abit/MessageListActivity.java +++ b/app/src/main/java/ch/dissem/apps/abit/MessageListActivity.java @@ -36,9 +36,9 @@ import java.util.ArrayList; import ch.dissem.apps.abit.listener.ActionBarListener; import ch.dissem.apps.abit.listener.ListSelectionListener; -import ch.dissem.apps.abit.notification.NetworkNotification; import ch.dissem.apps.abit.service.Singleton; import ch.dissem.apps.abit.synchronization.Authenticator; +import ch.dissem.apps.abit.synchronization.SyncService; import ch.dissem.bitmessage.BitmessageContext; import ch.dissem.bitmessage.entity.BitmessageAddress; import ch.dissem.bitmessage.entity.Plaintext; @@ -70,7 +70,7 @@ public class MessageListActivity extends AppCompatActivity public static final String ACTION_SHOW_INBOX = "ch.dissem.abit.ShowInbox"; private static final Logger LOG = LoggerFactory.getLogger(MessageListActivity.class); - private static final long SYNC_FREQUENCY = 15 * 60; // seconds + private static final long SYNC_FREQUENCY = 15;// FIXME * 60; // seconds private static final int ADD_IDENTITY = 1; /** @@ -253,14 +253,14 @@ public class MessageListActivity extends AppCompatActivity .withOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(IDrawerItem drawerItem, CompoundButton buttonView, boolean isChecked) { - // TODO: warn user, option to restrict to WiFi - if (isChecked && !bmc.isRunning()) { - bmc.startup(); - new NetworkNotification(MessageListActivity.this).show(); - } else if (bmc.isRunning()) bmc.shutdown(); + if (isChecked) { + startService(new Intent(MessageListActivity.this, SyncService.class)); + } else { + stopService(new Intent(MessageListActivity.this, SyncService.class)); + } } }) - .withChecked(bmc.isRunning()) + .withChecked(SyncService.isRunning()) ) .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() { @Override diff --git a/app/src/main/java/ch/dissem/apps/abit/notification/AbstractNotification.java b/app/src/main/java/ch/dissem/apps/abit/notification/AbstractNotification.java index 60bb86d..60eb282 100644 --- a/app/src/main/java/ch/dissem/apps/abit/notification/AbstractNotification.java +++ b/app/src/main/java/ch/dissem/apps/abit/notification/AbstractNotification.java @@ -10,7 +10,7 @@ import android.content.Context; public abstract class AbstractNotification { protected final Context ctx; protected final NotificationManager manager; - public Notification notification; + protected Notification notification; public AbstractNotification(Context ctx) { diff --git a/app/src/main/java/ch/dissem/apps/abit/notification/NetworkNotification.java b/app/src/main/java/ch/dissem/apps/abit/notification/NetworkNotification.java index 99f78d6..a013602 100644 --- a/app/src/main/java/ch/dissem/apps/abit/notification/NetworkNotification.java +++ b/app/src/main/java/ch/dissem/apps/abit/notification/NetworkNotification.java @@ -1,6 +1,7 @@ package ch.dissem.apps.abit.notification; import android.annotation.SuppressLint; +import android.app.Notification; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; @@ -19,6 +20,8 @@ import ch.dissem.bitmessage.utils.Property; * Shows the network status (as long as the client is connected as a full node) */ public class NetworkNotification extends AbstractNotification { + public static final int ONGOING_NOTIFICATION_ID = 2; + private final BitmessageContext bmc; private NotificationCompat.Builder builder; @@ -31,6 +34,11 @@ public class NetworkNotification extends AbstractNotification { .setVisibility(NotificationCompat.VISIBILITY_PUBLIC); } + public Notification getNotification() { + update(); + return notification; + } + @SuppressLint("StringFormatMatches") private boolean update() { boolean running = bmc.isRunning(); @@ -82,6 +90,6 @@ public class NetworkNotification extends AbstractNotification { @Override protected int getNotificationId() { - return 2; + return ONGOING_NOTIFICATION_ID; } } diff --git a/app/src/main/java/ch/dissem/apps/abit/synchronization/SyncAdapter.java b/app/src/main/java/ch/dissem/apps/abit/synchronization/SyncAdapter.java index c12d30c..efc1333 100644 --- a/app/src/main/java/ch/dissem/apps/abit/synchronization/SyncAdapter.java +++ b/app/src/main/java/ch/dissem/apps/abit/synchronization/SyncAdapter.java @@ -30,22 +30,9 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter { /** * Set up the sync adapter */ - public SyncAdapter(Context context, boolean autoInitialize) { - super(context, autoInitialize); - bmc = Singleton.getBitmessageContext(context); - } - - /** - * Set up the sync adapter. This form of the - * constructor maintains compatibility with Android 3.0 - * and later platform versions - */ - public SyncAdapter( - Context context, - boolean autoInitialize, - boolean allowParallelSyncs) { - super(context, autoInitialize, allowParallelSyncs); - bmc = Singleton.getBitmessageContext(context); + public SyncAdapter(Context context, BitmessageContext bitmessageContext) { + super(context, true); + bmc = bitmessageContext; } @Override diff --git a/app/src/main/java/ch/dissem/apps/abit/synchronization/SyncService.java b/app/src/main/java/ch/dissem/apps/abit/synchronization/SyncService.java index 4beaa04..f744299 100644 --- a/app/src/main/java/ch/dissem/apps/abit/synchronization/SyncService.java +++ b/app/src/main/java/ch/dissem/apps/abit/synchronization/SyncService.java @@ -1,20 +1,44 @@ package ch.dissem.apps.abit.synchronization; import android.app.Service; +import android.content.Context; import android.content.Intent; import android.os.IBinder; +import ch.dissem.apps.abit.MessageListActivity; +import ch.dissem.apps.abit.listener.MessageListener; +import ch.dissem.apps.abit.notification.NetworkNotification; +import ch.dissem.apps.abit.repository.AndroidAddressRepository; +import ch.dissem.apps.abit.repository.AndroidInventory; +import ch.dissem.apps.abit.repository.AndroidMessageRepository; +import ch.dissem.apps.abit.repository.SqlHelper; +import ch.dissem.apps.abit.service.Singleton; +import ch.dissem.bitmessage.BitmessageContext; +import ch.dissem.bitmessage.networking.DefaultNetworkHandler; +import ch.dissem.bitmessage.ports.MemoryNodeRegistry; +import ch.dissem.bitmessage.security.sc.SpongySecurity; + +import static ch.dissem.apps.abit.notification.NetworkNotification.ONGOING_NOTIFICATION_ID; + /** * Define a Service that returns an IBinder for the * sync adapter class, allowing the sync adapter framework to call * onPerformSync(). */ public class SyncService extends Service { + private static MessageListener messageListener = null; + private static BitmessageContext bmc = null; // Storage for an instance of the sync adapter private static SyncAdapter syncAdapter = null; // Object to use as a thread-safe lock private static final Object syncAdapterLock = new Object(); + private static volatile boolean running = false; + + public static boolean isRunning() { + return running; + } + /* * Instantiate the sync adapter object. */ @@ -26,12 +50,46 @@ public class SyncService extends Service { * Disallow parallel syncs */ synchronized (syncAdapterLock) { + final Context ctx = getApplicationContext(); + if (bmc == null) { +// messageListener = new MessageListener(ctx); +// SqlHelper sqlHelper = new SqlHelper(ctx); +// bmc = new BitmessageContext.Builder() +// .security(new SpongySecurity()) +// .nodeRegistry(new MemoryNodeRegistry()) +// .inventory(new AndroidInventory(sqlHelper)) +// .addressRepo(new AndroidAddressRepository(sqlHelper)) +// .messageRepo(new AndroidMessageRepository(sqlHelper, ctx)) +// .networkHandler(new DefaultNetworkHandler()) +// .listener(messageListener) +// .build(); + // FIXME: this needs to change once I figured out how to get rid of those singletons + messageListener = Singleton.getMessageListener(ctx); + bmc = Singleton.getBitmessageContext(ctx); + } if (syncAdapter == null) { - syncAdapter = new SyncAdapter(getApplicationContext(), true); + syncAdapter = new SyncAdapter(ctx, bmc); } } } + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + // TODO: warn user, option to restrict to WiFi + running = true; + NetworkNotification networkNotification = new NetworkNotification(this); + startForeground(ONGOING_NOTIFICATION_ID, networkNotification.getNotification()); + bmc.startup(); + networkNotification.show(); + return Service.START_STICKY; + } + + @Override + public void onDestroy() { + bmc.shutdown(); + running = false; + } + /** * Return an object that allows the system to invoke * the sync adapter.