Search in sources :

Example 46 with Preferences

use of com.fsck.k9.Preferences in project sms-backup-plus by jberkel.

the class App method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    setupStrictMode();
    gcmAvailable = GooglePlayServices.isAvailable(this);
    preferences = new Preferences(this);
    preferences.migrate();
    backupJobs = new BackupJobs(this);
    if (gcmAvailable) {
        setBroadcastReceiversEnabled(false);
    } else {
        Log.v(TAG, "Google Play Services not available, forcing use of old scheduler");
        preferences.setUseOldScheduler(true);
    }
    K9MailLib.setDebugStatus(new K9MailLib.DebugStatus() {

        @Override
        public boolean enabled() {
            return preferences.isAppLogDebug();
        }

        @Override
        public boolean debugSensitive() {
            return false;
        }
    });
    if (gcmAvailable && DEBUG) {
        getContentResolver().registerContentObserver(Consts.SMS_PROVIDER, true, new LoggingContentObserver());
        getContentResolver().registerContentObserver(Consts.CALLLOG_PROVIDER, true, new LoggingContentObserver());
    }
    register(this);
}
Also used : BackupJobs(com.zegoggles.smssync.service.BackupJobs) Preferences(com.zegoggles.smssync.preferences.Preferences) K9MailLib(com.fsck.k9.mail.K9MailLib)

Example 47 with Preferences

use of com.fsck.k9.Preferences in project sms-backup-plus by jberkel.

the class MessageConverterTest method testConvertMessagesSeenFlagFromMessageStatusWithSMS.

@Test
public void testConvertMessagesSeenFlagFromMessageStatusWithSMS() throws Exception {
    MatrixCursor cursor = new MatrixCursor(new String[] { Telephony.TextBasedSmsColumns.ADDRESS, Telephony.TextBasedSmsColumns.READ });
    cursor.addRow(new Object[] { "foo", "0" });
    cursor.addRow(new Object[] { "foo", "1" });
    cursor.moveToFirst();
    PersonRecord record = mock(PersonRecord.class);
    when(personLookup.lookupPerson(any(String.class))).thenReturn(record);
    when(record.getAddress(any(AddressStyle.class))).thenReturn(new Address("foo"));
    when(preferences.getMarkAsReadType()).thenReturn(MarkAsReadTypes.MESSAGE_STATUS);
    messageConverter = new MessageConverter(RuntimeEnvironment.application, preferences, "foo@example.com", personLookup, contactAccessor);
    ConversionResult res = messageConverter.convertMessages(cursor, DataType.SMS);
    assertThat(res.getMessages().get(0).isSet(Flag.SEEN)).isFalse();
    cursor.moveToNext();
    res = messageConverter.convertMessages(cursor, DataType.SMS);
    assertThat(res.getMessages().get(0).isSet(Flag.SEEN)).isTrue();
}
Also used : Address(com.fsck.k9.mail.Address) MatrixCursor(android.database.MatrixCursor) AddressStyle(com.zegoggles.smssync.preferences.AddressStyle) Test(org.junit.Test)

Example 48 with Preferences

use of com.fsck.k9.Preferences in project sms-backup-plus by jberkel.

the class MessageConverterTest method testConvertMessagesSeenFlagUnreadWithSMS.

@Test
public void testConvertMessagesSeenFlagUnreadWithSMS() throws Exception {
    MatrixCursor cursor = new MatrixCursor(new String[] { Telephony.TextBasedSmsColumns.ADDRESS, Telephony.TextBasedSmsColumns.READ });
    cursor.addRow(new Object[] { "foo", "0" });
    cursor.addRow(new Object[] { "foo", "1" });
    cursor.moveToFirst();
    PersonRecord record = mock(PersonRecord.class);
    when(personLookup.lookupPerson(any(String.class))).thenReturn(record);
    when(record.getAddress(any(AddressStyle.class))).thenReturn(new Address("foo"));
    when(preferences.getMarkAsReadType()).thenReturn(MarkAsReadTypes.UNREAD);
    messageConverter = new MessageConverter(RuntimeEnvironment.application, preferences, "foo@example.com", personLookup, contactAccessor);
    ConversionResult res = messageConverter.convertMessages(cursor, DataType.SMS);
    assertThat(res.getMessages().get(0).isSet(Flag.SEEN)).isFalse();
    cursor.moveToNext();
    res = messageConverter.convertMessages(cursor, DataType.SMS);
    assertThat(res.getMessages().get(0).isSet(Flag.SEEN)).isFalse();
}
Also used : Address(com.fsck.k9.mail.Address) MatrixCursor(android.database.MatrixCursor) AddressStyle(com.zegoggles.smssync.preferences.AddressStyle) Test(org.junit.Test)

Example 49 with Preferences

use of com.fsck.k9.Preferences in project k-9 by k9mail.

the class SettingsImporter method importAccount.

private static AccountDescriptionPair importAccount(Context context, StorageEditor editor, int contentVersion, ImportedAccount account, boolean overwrite) throws InvalidSettingValueException {
    AccountDescription original = new AccountDescription(account.name, account.uuid);
    Preferences prefs = Preferences.getPreferences(context);
    List<Account> accounts = prefs.getAccounts();
    String uuid = account.uuid;
    Account existingAccount = prefs.getAccount(uuid);
    boolean mergeImportedAccount = (overwrite && existingAccount != null);
    if (!overwrite && existingAccount != null) {
        // An account with this UUID already exists, but we're not allowed to overwrite it.
        // So generate a new UUID.
        uuid = UUID.randomUUID().toString();
    }
    // Make sure the account name is unique
    String accountName = account.name;
    if (isAccountNameUsed(accountName, accounts)) {
        // number >= 1 that results in an unused account name.
        for (int i = 1; i <= accounts.size(); i++) {
            accountName = account.name + " (" + i + ")";
            if (!isAccountNameUsed(accountName, accounts)) {
                break;
            }
        }
    }
    // Write account name
    String accountKeyPrefix = uuid + ".";
    putString(editor, accountKeyPrefix + AccountPreferenceSerializer.ACCOUNT_DESCRIPTION_KEY, accountName);
    if (account.incoming == null) {
        // We don't import accounts without incoming server settings
        throw new InvalidSettingValueException();
    }
    // Write incoming server settings
    ServerSettings incoming = createServerSettings(account.incoming);
    ServerSettingsSerializer serverSettingsSerializer = DI.get(ServerSettingsSerializer.class);
    String incomingServer = serverSettingsSerializer.serialize(incoming);
    putString(editor, accountKeyPrefix + AccountPreferenceSerializer.INCOMING_SERVER_SETTINGS_KEY, incomingServer);
    String incomingServerName = incoming.host;
    boolean incomingPasswordNeeded = AuthType.EXTERNAL != incoming.authenticationType && (incoming.password == null || incoming.password.isEmpty());
    String incomingServerType = ServerTypeConverter.toServerSettingsType(account.incoming.type);
    if (account.outgoing == null && !incomingServerType.equals(Protocols.WEBDAV)) {
        // All account types except WebDAV need to provide outgoing server settings
        throw new InvalidSettingValueException();
    }
    String outgoingServerName = null;
    boolean outgoingPasswordNeeded = false;
    if (account.outgoing != null) {
        // Write outgoing server settings
        ServerSettings outgoing = createServerSettings(account.outgoing);
        String outgoingServer = serverSettingsSerializer.serialize(outgoing);
        putString(editor, accountKeyPrefix + AccountPreferenceSerializer.OUTGOING_SERVER_SETTINGS_KEY, outgoingServer);
        /*
             * Mark account as disabled if the settings file contained a username but no password. However, no password
             * is required for the outgoing server for WebDAV accounts, because incoming and outgoing servers are 
             * identical for this account type. Nor is a password required if the AuthType is EXTERNAL.
             */
        String outgoingServerType = ServerTypeConverter.toServerSettingsType(outgoing.type);
        outgoingPasswordNeeded = AuthType.EXTERNAL != outgoing.authenticationType && !outgoingServerType.equals(Protocols.WEBDAV) && outgoing.username != null && !outgoing.username.isEmpty() && (outgoing.password == null || outgoing.password.isEmpty());
        outgoingServerName = outgoing.host;
    }
    boolean createAccountDisabled = incomingPasswordNeeded || outgoingPasswordNeeded;
    if (createAccountDisabled) {
        editor.putBoolean(accountKeyPrefix + "enabled", false);
    }
    // Validate account settings
    Map<String, Object> validatedSettings = AccountSettingsDescriptions.validate(contentVersion, account.settings.settings, !mergeImportedAccount);
    // Upgrade account settings to current content version
    if (contentVersion != Settings.VERSION) {
        AccountSettingsDescriptions.upgrade(contentVersion, validatedSettings);
    }
    // Convert account settings to the string representation used in preference storage
    Map<String, String> stringSettings = AccountSettingsDescriptions.convert(validatedSettings);
    // Merge account settings if necessary
    Map<String, String> writeSettings;
    if (mergeImportedAccount) {
        writeSettings = new HashMap<>(AccountSettingsDescriptions.getAccountSettings(prefs.getStorage(), uuid));
        writeSettings.putAll(stringSettings);
    } else {
        writeSettings = stringSettings;
    }
    // Write account settings
    for (Map.Entry<String, String> setting : writeSettings.entrySet()) {
        String key = accountKeyPrefix + setting.getKey();
        String value = setting.getValue();
        putString(editor, key, value);
    }
    // If it's a new account generate and write a new "accountNumber"
    if (!mergeImportedAccount) {
        int newAccountNumber = prefs.generateAccountNumber();
        putString(editor, accountKeyPrefix + "accountNumber", Integer.toString(newAccountNumber));
    }
    // Write identities
    if (account.identities != null) {
        importIdentities(editor, contentVersion, uuid, account, overwrite, existingAccount, prefs);
    } else if (!mergeImportedAccount) {
        // Require accounts to at least have one identity
        throw new InvalidSettingValueException();
    }
    // Write folder settings
    if (account.folders != null) {
        for (ImportedFolder folder : account.folders) {
            importFolder(editor, contentVersion, uuid, folder, mergeImportedAccount, prefs);
        }
    }
    // TODO: sync folder settings with localstore?
    AccountDescription imported = new AccountDescription(accountName, uuid);
    return new AccountDescriptionPair(original, imported, mergeImportedAccount, incomingPasswordNeeded, outgoingPasswordNeeded, incomingServerName, outgoingServerName);
}
Also used : Account(com.fsck.k9.Account) ServerSettingsSerializer(com.fsck.k9.ServerSettingsSerializer) InvalidSettingValueException(com.fsck.k9.preferences.Settings.InvalidSettingValueException) ServerSettings(com.fsck.k9.mail.ServerSettings) SharedPreferences(android.content.SharedPreferences) Preferences(com.fsck.k9.Preferences) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap)

Example 50 with Preferences

use of com.fsck.k9.Preferences in project k-9 by k9mail.

the class DatabaseUpgradeService method upgradeDatabases.

/**
 * Upgrade the accounts' databases.
 */
private void upgradeDatabases() {
    Preferences preferences = Preferences.getPreferences(this);
    List<Account> accounts = preferences.getAccounts();
    mProgressEnd = accounts.size();
    mProgress = 0;
    for (Account account : accounts) {
        mAccountUuid = account.getUuid();
        sendProgressBroadcast(mAccountUuid, mProgress, mProgressEnd);
        try {
            // Account.getLocalStore() is blocking and will upgrade the database if necessary
            DI.get(LocalStoreProvider.class).getInstance(account);
        } catch (Exception e) {
            Timber.e(e, "Error while upgrading database");
        }
        mProgress++;
    }
    K9.setDatabasesUpToDate(true);
    sendUpgradeCompleteBroadcast();
}
Also used : Account(com.fsck.k9.Account) Preferences(com.fsck.k9.Preferences) LocalStoreProvider(com.fsck.k9.mailstore.LocalStoreProvider)

Aggregations

Preferences (com.fsck.k9.Preferences)18 Account (com.fsck.k9.Account)17 Test (org.junit.Test)15 StorageEditor (com.fsck.k9.preferences.StorageEditor)9 Storage (com.fsck.k9.preferences.Storage)6 Map (java.util.Map)5 Preferences (org.apereo.portal.soffit.model.v1_0.Preferences)5 Preferences (org.orcid.jaxb.model.record_v2.Preferences)5 SharedPreferences (android.content.SharedPreferences)4 MessagingException (com.fsck.k9.mail.MessagingException)4 SearchAccount (com.fsck.k9.search.SearchAccount)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Intent (android.content.Intent)3 MatrixCursor (android.database.MatrixCursor)3 Uri (android.net.Uri)3 Address (com.fsck.k9.mail.Address)3 NetworkType (com.fsck.k9.mail.NetworkType)3 LocalStore (com.fsck.k9.mailstore.LocalStore)3 IOException (java.io.IOException)3