Some sync adapter fixes and changes - still doesn't work

This commit is contained in:
Christian Basler 2015-10-12 14:44:01 +02:00
parent 348fa8daed
commit 67c06b9884
10 changed files with 82 additions and 104 deletions

View File

@ -103,9 +103,10 @@
</intent-filter>
</activity>
<!-- Synchronization -->
<provider
android:name=".synchronization.StubProvider"
android:authorities="ch.dissem.bitmessage.provider"
android:authorities="ch.dissem.apps.abit.provider"
android:exported="false"
android:syncable="true" />
<service android:name=".synchronization.AuthenticatorService">

View File

@ -2,27 +2,15 @@ package ch.dissem.apps.abit;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.ContentResolver;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import ch.dissem.apps.abit.listeners.ActionBarListener;
import ch.dissem.apps.abit.listeners.ListSelectionListener;
import ch.dissem.apps.abit.service.Singleton;
import ch.dissem.apps.abit.synchronization.Authenticator;
import ch.dissem.apps.abit.synchronization.SyncAdapter;
import ch.dissem.bitmessage.BitmessageContext;
import ch.dissem.bitmessage.entity.BitmessageAddress;
import ch.dissem.bitmessage.entity.Plaintext;
import ch.dissem.bitmessage.entity.Streamable;
import ch.dissem.bitmessage.entity.valueobject.Label;
import android.widget.CompoundButton;
import com.mikepenz.community_material_typeface_library.CommunityMaterial;
import com.mikepenz.google_material_typeface_library.GoogleMaterial;
@ -34,10 +22,11 @@ import com.mikepenz.materialdrawer.accountswitcher.AccountHeaderBuilder;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileSettingDrawerItem;
import com.mikepenz.materialdrawer.model.SecondaryDrawerItem;
import com.mikepenz.materialdrawer.model.SwitchDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IProfile;
import com.mikepenz.materialdrawer.model.interfaces.Nameable;
import com.mikepenz.materialdrawer.model.interfaces.OnCheckedChangeListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -45,6 +34,17 @@ import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.util.ArrayList;
import ch.dissem.apps.abit.listeners.ActionBarListener;
import ch.dissem.apps.abit.listeners.ListSelectionListener;
import ch.dissem.apps.abit.service.Singleton;
import ch.dissem.apps.abit.synchronization.Authenticator;
import ch.dissem.bitmessage.BitmessageContext;
import ch.dissem.bitmessage.entity.BitmessageAddress;
import ch.dissem.bitmessage.entity.Plaintext;
import ch.dissem.bitmessage.entity.valueobject.Label;
import static ch.dissem.apps.abit.synchronization.StubProvider.AUTHORITY;
/**
* An activity representing a list of Messages. This activity
@ -69,10 +69,9 @@ 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 int ADD_IDENTITY = 1;
private Account account;
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
@ -82,7 +81,6 @@ public class MessageListActivity extends AppCompatActivity
private AccountHeader accountHeader;
private BitmessageContext bmc;
private Label selectedLabel;
private Menu menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -119,39 +117,22 @@ public class MessageListActivity extends AppCompatActivity
onItemSelected(getIntent().getSerializableExtra(EXTRA_SHOW_MESSAGE));
}
account = createSyncAccount(this);
getContentResolver().setSyncAutomatically(account, SyncAdapter.AUTHORITY, true);
createSyncAccount();
}
private Account createSyncAccount(Context context) {
// Create the account type and default account
Account newAccount = new Account(Authenticator.ACCOUNT_NAME, Authenticator.ACCOUNT_TYPE);
// Get an instance of the Android account manager
AccountManager accountManager = (AccountManager) context.getSystemService(ACCOUNT_SERVICE);
/*
* Add the account and account type, no password or user data
* If successful, return the Account object, otherwise report an error.
*/
if (accountManager.addAccountExplicitly(newAccount, null, null)) {
/*
* If you don't set android:syncable="true" in
* in your <provider> element in the manifest,
* then call context.setIsSyncable(account, AUTHORITY, 1)
* here.
*/
} else {
/*
* The account exists or some other error occurred. Log this, report it,
* or handle it internally.
*/
LOG.error("Couldn't add account");
}
return newAccount;
}
private void createSyncAccount() {
// Create account, if it's missing. (Either first run, or user has deleted account.)
Account account = new Account(Authenticator.ACCOUNT_NAME, Authenticator.ACCOUNT_TYPE);
@Override
protected void onResume() {
super.onResume();
if (AccountManager.get(this).addAccountExplicitly(account, null, null)) {
// Inform the system that this account supports sync
ContentResolver.setIsSyncable(account, AUTHORITY, 1);
// Inform the system that this account is eligible for auto sync when the network is up
ContentResolver.setSyncAutomatically(account, AUTHORITY, true);
// Recommend a schedule for automatic synchronization. The system may modify this based
// on other scheduled syncs and network utilization.
ContentResolver.addPeriodicSync(account, AUTHORITY, new Bundle(), SYNC_FREQUENCY);
}
}
private void changeList(AbstractItemListFragment<?> listFragment) {
@ -253,12 +234,24 @@ public class MessageListActivity extends AppCompatActivity
.withAccountHeader(accountHeader)
.withDrawerItems(drawerItems)
.addStickyDrawerItems(
new SecondaryDrawerItem()
new PrimaryDrawerItem()
.withName(R.string.subscriptions)
.withIcon(CommunityMaterial.Icon.cmd_rss_box),
new SecondaryDrawerItem()
new PrimaryDrawerItem()
.withName(R.string.settings)
.withIcon(GoogleMaterial.Icon.gmd_settings)
.withIcon(GoogleMaterial.Icon.gmd_settings),
new SwitchDrawerItem()
.withName(R.string.full_node)
.withIcon(CommunityMaterial.Icon.cmd_cloud_outline)
.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();
else if (bmc.isRunning()) bmc.shutdown();
}
})
.withChecked(bmc.isRunning())
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
@ -288,6 +281,8 @@ public class MessageListActivity extends AppCompatActivity
case R.string.settings:
startActivity(new Intent(MessageListActivity.this, SettingsActivity.class));
break;
case R.string.full_node:
return true;
}
}
return false;
@ -297,36 +292,6 @@ public class MessageListActivity extends AppCompatActivity
.build();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
this.menu = menu;
updateMenu();
return true;
}
private void updateMenu() {
boolean running = bmc.isRunning();
menu.findItem(R.id.sync_enabled).setVisible(running);
menu.findItem(R.id.sync_disabled).setVisible(!running);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.sync_disabled:
bmc.startup();
updateMenu();
return true;
case R.id.sync_enabled:
bmc.shutdown();
updateMenu();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* Callback method from {@link ListSelectionListener}
* indicating that the item with the given ID was selected.

View File

@ -1,6 +1,5 @@
package ch.dissem.apps.abit.service;
import android.app.NotificationManager;
import android.content.Context;
import ch.dissem.apps.abit.listeners.MessageListener;

View File

@ -10,6 +10,8 @@ import android.net.Uri;
* all methods
*/
public class StubProvider extends ContentProvider {
public static final String AUTHORITY = "ch.dissem.apps.abit.provider";
/*
* Always return true, indicating that the
* provider loaded correctly.
@ -18,6 +20,7 @@ public class StubProvider extends ContentProvider {
public boolean onCreate() {
return true;
}
/*
* Return no type for MIME type
*/
@ -25,6 +28,7 @@ public class StubProvider extends ContentProvider {
public String getType(Uri uri) {
return null;
}
/*
* query() always returns no results
*
@ -38,6 +42,7 @@ public class StubProvider extends ContentProvider {
String sortOrder) {
return null;
}
/*
* insert() always returns null (no URI)
*/
@ -45,6 +50,7 @@ public class StubProvider extends ContentProvider {
public Uri insert(Uri uri, ContentValues values) {
return null;
}
/*
* delete() always returns "no rows affected" (0)
*/
@ -52,6 +58,7 @@ public class StubProvider extends ContentProvider {
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
/*
* update() always returns "no rows affected" (0)
*/

View File

@ -25,17 +25,32 @@ import ch.dissem.bitmessage.BitmessageContext;
public class SyncAdapter extends AbstractThreadedSyncAdapter {
private final static Logger LOG = LoggerFactory.getLogger(SyncAdapter.class);
public static final String AUTHORITY = "ch.dissem.bitmessage.provider";
private final BitmessageContext bmc;
public SyncAdapter(Context context) {
super(context, true, false);
/**
* 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);
}
@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
LOG.info("Synchronizing Bitmessage");
// If the Bitmessage context acts as a full node, synchronization isn't necessary
if (bmc.isRunning()) return;
@ -52,7 +67,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
String portString = trustedNode.substring(index + 1);
trustedNode = trustedNode.substring(0, index);
try {
port = Integer.parseInt(portString);// FIXME
port = Integer.parseInt(portString);
} catch (NumberFormatException e) {
LOG.error("Invalid port " + portString);
// TODO: show error as notification

View File

@ -27,7 +27,7 @@ public class SyncService extends Service {
*/
synchronized (syncAdapterLock) {
if (syncAdapter == null) {
syncAdapter = new SyncAdapter(getApplicationContext());
syncAdapter = new SyncAdapter(getApplicationContext(), true);
}
}
}

View File

@ -5,5 +5,5 @@
android:id="@+id/send"
app:showAsAction="always"
android:icon="@drawable/ic_action_send"
android:title="@string/disable_sync"/>`
android:title="@string/send"/>`
</menu>

View File

@ -1,14 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/sync_enabled"
app:showAsAction="always"
android:icon="@drawable/ic_action_notification_sync"
android:title="@string/disable_sync"/>
<item
android:id="@+id/sync_disabled"
app:showAsAction="always"
android:icon="@drawable/ic_action_notification_sync_disabled"
android:title="@string/enable_sync"/>
</menu>

View File

@ -5,8 +5,6 @@
<string name="wifi_only">Nur WLAN</string>
<string name="wifi_only_summary">Nicht mit Mobilfunknetz verbinden</string>
<string name="bitmessage_active">Bitmessage ist aktiv</string>
<string name="disable_sync">Synchronisieren ausschalten</string>
<string name="enable_sync">Synchronisieren einschalten</string>
<string name="subject">Betreff</string>
<string name="to">An</string>
<string name="title_message_detail">Nachricht</string>
@ -37,4 +35,7 @@
<string name="sync_timeout_summary">Timeout in Sekunden</string>
<string name="trusted_node">Vertrauenswürdiger Knoten</string>
<string name="trusted_node_summary">Diese Adresse wird für die Synchronisation verwendet</string>
<string name="full_node">Aktiver Knoten</string>
<string name="send">Senden</string>
<string name="write_message">Schreiben</string>
</resources>

View File

@ -2,8 +2,6 @@
<string name="app_name">Abit</string>
<string name="title_message_detail">Message Detail</string>
<string name="title_subscription_detail">Subscription Detail</string>
<string name="disable_sync">Disable Sync</string>
<string name="enable_sync">Enable Sync</string>
<string name="bitmessage_active">Bitmessage active</string>
<string name="wifi_mode">Wi-Fi Connection Mode</string>
<string name="settings">Settings</string>
@ -38,4 +36,6 @@
<string name="sync_timeout">Synchronization Timeout</string>
<string name="sync_timeout_summary">Timeout in seconds</string>
<string name="write_message">Write message</string>
<string name="full_node">Full node</string>
<string name="send">Send</string>
</resources>