Search in sources :

Example 11 with StorageEditor

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

the class Account method move.

public void move(Preferences preferences, boolean moveUp) {
    String[] uuids = preferences.getStorage().getString("accountUuids", "").split(",");
    StorageEditor editor = preferences.getStorage().edit();
    String[] newUuids = new String[uuids.length];
    if (moveUp) {
        for (int i = 0; i < uuids.length; i++) {
            if (i > 0 && uuids[i].equals(mUuid)) {
                newUuids[i] = newUuids[i - 1];
                newUuids[i - 1] = mUuid;
            } else {
                newUuids[i] = uuids[i];
            }
        }
    } else {
        for (int i = uuids.length - 1; i >= 0; i--) {
            if (i < uuids.length - 1 && uuids[i].equals(mUuid)) {
                newUuids[i] = newUuids[i + 1];
                newUuids[i + 1] = mUuid;
            } else {
                newUuids[i] = uuids[i];
            }
        }
    }
    String accountUuids = Utility.combine(newUuids, ',');
    editor.putString("accountUuids", accountUuids);
    editor.commit();
    preferences.loadAccounts();
}
Also used : StorageEditor(com.fsck.k9.preferences.StorageEditor)

Example 12 with StorageEditor

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

the class MessageList method onToggleTheme.

private void onToggleTheme() {
    if (K9.getK9MessageViewTheme() == K9.Theme.DARK) {
        K9.setK9MessageViewThemeSetting(K9.Theme.LIGHT);
    } else {
        K9.setK9MessageViewThemeSetting(K9.Theme.DARK);
    }
    new Thread(new Runnable() {

        @Override
        public void run() {
            Context appContext = getApplicationContext();
            Preferences prefs = Preferences.getPreferences(appContext);
            StorageEditor editor = prefs.getStorage().edit();
            K9.save(editor);
            editor.commit();
        }
    }).start();
    recreate();
}
Also used : Context(android.content.Context) Preferences(com.fsck.k9.Preferences) StorageEditor(com.fsck.k9.preferences.StorageEditor)

Example 13 with StorageEditor

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

the class FontSizeSettings method saveSettings.

/**
     * Update the global FontSize object and permanently store the (possibly
     * changed) font size settings.
     */
private void saveSettings() {
    FontSizes fontSizes = K9.getFontSizes();
    fontSizes.setAccountName(Integer.parseInt(mAccountName.getValue()));
    fontSizes.setAccountDescription(Integer.parseInt(mAccountDescription.getValue()));
    fontSizes.setFolderName(Integer.parseInt(mFolderName.getValue()));
    fontSizes.setFolderStatus(Integer.parseInt(mFolderStatus.getValue()));
    fontSizes.setMessageListSubject(Integer.parseInt(mMessageListSubject.getValue()));
    fontSizes.setMessageListSender(Integer.parseInt(mMessageListSender.getValue()));
    fontSizes.setMessageListDate(Integer.parseInt(mMessageListDate.getValue()));
    fontSizes.setMessageListPreview(Integer.parseInt(mMessageListPreview.getValue()));
    fontSizes.setMessageViewSender(Integer.parseInt(mMessageViewSender.getValue()));
    fontSizes.setMessageViewTo(Integer.parseInt(mMessageViewTo.getValue()));
    fontSizes.setMessageViewCC(Integer.parseInt(mMessageViewCC.getValue()));
    fontSizes.setMessageViewAdditionalHeaders(Integer.parseInt(mMessageViewAdditionalHeaders.getValue()));
    fontSizes.setMessageViewSubject(Integer.parseInt(mMessageViewSubject.getValue()));
    fontSizes.setMessageViewDate(Integer.parseInt(mMessageViewDate.getValue()));
    fontSizes.setMessageViewContentAsPercent(scaleToInt(mMessageViewContentSlider.getValue()));
    fontSizes.setMessageComposeInput(Integer.parseInt(mMessageComposeInput.getValue()));
    Storage storage = Preferences.getPreferences(this).getStorage();
    StorageEditor editor = storage.edit();
    fontSizes.save(editor);
    editor.commit();
}
Also used : Storage(com.fsck.k9.preferences.Storage) StorageEditor(com.fsck.k9.preferences.StorageEditor)

Example 14 with StorageEditor

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

the class OpenPgpAppSelectDialog method persistOpenPgpProviderSetting.

private void persistOpenPgpProviderSetting(String selectedPackage) {
    K9.setOpenPgpProvider(selectedPackage);
    StorageEditor editor = Preferences.getPreferences(this).getStorage().edit();
    K9.save(editor);
    editor.commit();
}
Also used : StorageEditor(com.fsck.k9.preferences.StorageEditor)

Example 15 with StorageEditor

use of com.fsck.k9.preferences.StorageEditor 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 + Account.ACCOUNT_DESCRIPTION_KEY, accountName);
    if (account.incoming == null) {
        // We don't import accounts without incoming server settings
        throw new InvalidSettingValueException();
    }
    // Write incoming server settings (storeUri)
    ServerSettings incoming = new ImportedServerSettings(account.incoming);
    String storeUri = RemoteStore.createStoreUri(incoming);
    putString(editor, accountKeyPrefix + Account.STORE_URI_KEY, Base64.encode(storeUri));
    // Mark account as disabled if the AuthType isn't EXTERNAL and the
    // settings file didn't contain a password
    boolean createAccountDisabled = AuthType.EXTERNAL != incoming.authenticationType && (incoming.password == null || incoming.password.isEmpty());
    if (account.outgoing == null && !ServerSettings.Type.WebDAV.name().equals(account.incoming.type)) {
        // All account types except WebDAV need to provide outgoing server settings
        throw new InvalidSettingValueException();
    }
    if (account.outgoing != null) {
        // Write outgoing server settings (transportUri)
        ServerSettings outgoing = new ImportedServerSettings(account.outgoing);
        String transportUri = Transport.createTransportUri(outgoing);
        putString(editor, accountKeyPrefix + Account.TRANSPORT_URI_KEY, Base64.encode(transportUri));
        /*
             * 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.
             */
        boolean outgoingPasswordNeeded = AuthType.EXTERNAL != outgoing.authenticationType && !(ServerSettings.Type.WebDAV == outgoing.type) && outgoing.username != null && !outgoing.username.isEmpty() && (outgoing.password == null || outgoing.password.isEmpty());
        createAccountDisabled = outgoingPasswordNeeded || createAccountDisabled;
    }
    // Write key to mark account as disabled if necessary
    if (createAccountDisabled) {
        editor.putBoolean(accountKeyPrefix + "enabled", false);
    }
    // Validate account settings
    Map<String, Object> validatedSettings = AccountSettings.validate(contentVersion, account.settings.settings, !mergeImportedAccount);
    // Upgrade account settings to current content version
    if (contentVersion != Settings.VERSION) {
        AccountSettings.upgrade(contentVersion, validatedSettings);
    }
    // Convert account settings to the string representation used in preference storage
    Map<String, String> stringSettings = AccountSettings.convert(validatedSettings);
    // Merge account settings if necessary
    Map<String, String> writeSettings;
    if (mergeImportedAccount) {
        writeSettings = new HashMap<>(AccountSettings.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 = Account.generateAccountNumber(prefs);
        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);
}
Also used : Account(com.fsck.k9.Account) 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) Map(java.util.Map)

Aggregations

StorageEditor (com.fsck.k9.preferences.StorageEditor)14 Preferences (com.fsck.k9.Preferences)6 Storage (com.fsck.k9.preferences.Storage)6 Account (com.fsck.k9.Account)4 Intent (android.content.Intent)2 SharedPreferences (android.content.SharedPreferences)2 NetworkType (com.fsck.k9.mail.NetworkType)2 InvalidSettingValueException (com.fsck.k9.preferences.Settings.InvalidSettingValueException)2 ArrayList (java.util.ArrayList)2 Context (android.content.Context)1 Toast (android.widget.Toast)1 BACKGROUND_OPS (com.fsck.k9.K9.BACKGROUND_OPS)1 Folder (com.fsck.k9.mail.Folder)1 ServerSettings (com.fsck.k9.mail.ServerSettings)1 LocalFolder (com.fsck.k9.mailstore.LocalFolder)1 LocalStore (com.fsck.k9.mailstore.LocalStore)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1