Search in sources :

Example 11 with VisibleForTesting

use of android.support.annotation.VisibleForTesting in project k-9 by k9mail.

the class MessagingController method clearFolderSynchronous.

@VisibleForTesting
protected void clearFolderSynchronous(Account account, String folderName, MessagingListener listener) {
    LocalFolder localFolder = null;
    try {
        localFolder = account.getLocalStore().getFolder(folderName);
        localFolder.open(Folder.OPEN_MODE_RW);
        localFolder.clearAllMessages();
    } catch (UnavailableStorageException e) {
        Timber.i("Failed to clear folder because storage is not available - trying again later.");
        throw new UnavailableAccountException(e);
    } catch (Exception e) {
        Timber.e(e, "clearFolder failed");
        addErrorMessage(account, null, e);
    } finally {
        closeFolder(localFolder);
    }
    listFoldersSynchronous(account, false, listener);
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) CertificateValidationException(com.fsck.k9.mail.CertificateValidationException) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) IOException(java.io.IOException) MessagingException(com.fsck.k9.mail.MessagingException) AuthenticationFailedException(com.fsck.k9.mail.AuthenticationFailedException) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 12 with VisibleForTesting

use of android.support.annotation.VisibleForTesting in project k-9 by k9mail.

the class MessagingController method refreshRemoteSynchronous.

@VisibleForTesting
void refreshRemoteSynchronous(final Account account, final MessagingListener listener) {
    List<LocalFolder> localFolders = null;
    try {
        Store store = account.getRemoteStore();
        List<? extends Folder> remoteFolders = store.getPersonalNamespaces(false);
        LocalStore localStore = account.getLocalStore();
        Set<String> remoteFolderNames = new HashSet<>();
        List<LocalFolder> foldersToCreate = new LinkedList<>();
        localFolders = localStore.getPersonalNamespaces(false);
        Set<String> localFolderNames = new HashSet<>();
        for (Folder localFolder : localFolders) {
            localFolderNames.add(localFolder.getName());
        }
        for (Folder remoteFolder : remoteFolders) {
            if (!localFolderNames.contains(remoteFolder.getName())) {
                LocalFolder localFolder = localStore.getFolder(remoteFolder.getName());
                foldersToCreate.add(localFolder);
            }
            remoteFolderNames.add(remoteFolder.getName());
        }
        localStore.createFolders(foldersToCreate, account.getDisplayCount());
        localFolders = localStore.getPersonalNamespaces(false);
        /*
             * Clear out any folders that are no longer on the remote store.
             */
        for (Folder localFolder : localFolders) {
            String localFolderName = localFolder.getName();
            //        special placeholder folder "-NONE-".
            if (K9.FOLDER_NONE.equals(localFolderName)) {
                localFolder.delete(false);
            }
            if (!account.isSpecialFolder(localFolderName) && !remoteFolderNames.contains(localFolderName)) {
                localFolder.delete(false);
            }
        }
        localFolders = localStore.getPersonalNamespaces(false);
        for (MessagingListener l : getListeners(listener)) {
            l.listFolders(account, localFolders);
        }
        for (MessagingListener l : getListeners(listener)) {
            l.listFoldersFinished(account);
        }
    } catch (Exception e) {
        for (MessagingListener l : getListeners(listener)) {
            l.listFoldersFailed(account, "");
        }
        addErrorMessage(account, null, e);
    } finally {
        if (localFolders != null) {
            for (Folder localFolder : localFolders) {
                closeFolder(localFolder);
            }
        }
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) LocalStore(com.fsck.k9.mailstore.LocalStore) Store(com.fsck.k9.mail.Store) Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store) LocalStore(com.fsck.k9.mailstore.LocalStore) Folder(com.fsck.k9.mail.Folder) LocalFolder(com.fsck.k9.mailstore.LocalFolder) LinkedList(java.util.LinkedList) CertificateValidationException(com.fsck.k9.mail.CertificateValidationException) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) IOException(java.io.IOException) MessagingException(com.fsck.k9.mail.MessagingException) AuthenticationFailedException(com.fsck.k9.mail.AuthenticationFailedException) HashSet(java.util.HashSet) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 13 with VisibleForTesting

use of android.support.annotation.VisibleForTesting in project k-9 by k9mail.

the class SettingsImporter method parseSettings.

@VisibleForTesting
static Imported parseSettings(InputStream inputStream, boolean globalSettings, List<String> accountUuids, boolean overview) throws SettingsImportExportException {
    if (!overview && accountUuids == null) {
        throw new IllegalArgumentException("Argument 'accountUuids' must not be null.");
    }
    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        //factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        InputStreamReader reader = new InputStreamReader(inputStream);
        xpp.setInput(reader);
        Imported imported = null;
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                if (SettingsExporter.ROOT_ELEMENT.equals(xpp.getName())) {
                    imported = parseRoot(xpp, globalSettings, accountUuids, overview);
                } else {
                    Timber.w("Unexpected start tag: %s", xpp.getName());
                }
            }
            eventType = xpp.next();
        }
        if (imported == null || (overview && imported.globalSettings == null && imported.accounts == null)) {
            throw new SettingsImportExportException("Invalid import data");
        }
        return imported;
    } catch (Exception e) {
        throw new SettingsImportExportException(e);
    }
}
Also used : XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) InputStreamReader(java.io.InputStreamReader) XmlPullParser(org.xmlpull.v1.XmlPullParser) InvalidSettingValueException(com.fsck.k9.preferences.Settings.InvalidSettingValueException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 14 with VisibleForTesting

use of android.support.annotation.VisibleForTesting in project android_frameworks_base by ResurrectionRemix.

the class StubProvider method clearCacheAndBuildRoots.

@VisibleForTesting
public void clearCacheAndBuildRoots() {
    Log.d(TAG, "Resetting storage.");
    removeChildrenRecursively(getContext().getCacheDir());
    mStorage.clear();
    mSimulateReadErrorIds.clear();
    mPrefs = getContext().getSharedPreferences("com.android.documentsui.stubprovider.preferences", Context.MODE_PRIVATE);
    Collection<String> rootIds = mPrefs.getStringSet("roots", null);
    if (rootIds == null) {
        rootIds = Arrays.asList(new String[] { ROOT_0_ID, ROOT_1_ID });
    }
    mRoots.clear();
    for (String rootId : rootIds) {
        // Make a subdir in the cache dir for each root.
        final File file = new File(getContext().getCacheDir(), rootId);
        if (file.mkdir()) {
            Log.i(TAG, "Created new root directory @ " + file.getPath());
        }
        final RootInfo rootInfo = new RootInfo(file, getSize(rootId));
        if (rootId.equals(ROOT_1_ID)) {
            rootInfo.setSearchEnabled(false);
        }
        mStorage.put(rootInfo.document.documentId, rootInfo.document);
        mRoots.put(rootId, rootInfo);
    }
}
Also used : File(java.io.File) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 15 with VisibleForTesting

use of android.support.annotation.VisibleForTesting in project android_frameworks_base by ResurrectionRemix.

the class RootsCache method getMatchingRoots.

@VisibleForTesting
static List<RootInfo> getMatchingRoots(Collection<RootInfo> roots, State state) {
    final List<RootInfo> matching = new ArrayList<>();
    for (RootInfo root : roots) {
        if (DEBUG)
            Log.d(TAG, "Evaluating " + root);
        if (state.action == State.ACTION_CREATE && !root.supportsCreate()) {
            if (DEBUG)
                Log.d(TAG, "Excluding read-only root because: ACTION_CREATE.");
            continue;
        }
        if (state.action == State.ACTION_PICK_COPY_DESTINATION && !root.supportsCreate()) {
            if (DEBUG)
                Log.d(TAG, "Excluding read-only root because: ACTION_PICK_COPY_DESTINATION.");
            continue;
        }
        if (state.action == State.ACTION_OPEN_TREE && !root.supportsChildren()) {
            if (DEBUG)
                Log.d(TAG, "Excluding root !supportsChildren because: ACTION_OPEN_TREE.");
            continue;
        }
        if (!state.showAdvanced && root.isAdvanced()) {
            if (DEBUG)
                Log.d(TAG, "Excluding root because: unwanted advanced device.");
            continue;
        }
        if (state.localOnly && !root.isLocalOnly()) {
            if (DEBUG)
                Log.d(TAG, "Excluding root because: unwanted non-local device.");
            continue;
        }
        if (state.directoryCopy && root.isDownloads()) {
            if (DEBUG)
                Log.d(TAG, "Excluding downloads root because: unsupported directory copy.");
            continue;
        }
        if (state.action == State.ACTION_OPEN && root.isEmpty()) {
            if (DEBUG)
                Log.d(TAG, "Excluding empty root because: ACTION_OPEN.");
            continue;
        }
        if (state.action == State.ACTION_GET_CONTENT && root.isEmpty()) {
            if (DEBUG)
                Log.d(TAG, "Excluding empty root because: ACTION_GET_CONTENT.");
            continue;
        }
        final boolean overlap = MimePredicate.mimeMatches(root.derivedMimeTypes, state.acceptMimes) || MimePredicate.mimeMatches(state.acceptMimes, root.derivedMimeTypes);
        if (!overlap) {
            if (DEBUG)
                Log.d(TAG, "Excluding root because: unsupported content types > " + state.acceptMimes);
            continue;
        }
        if (state.excludedAuthorities.contains(root.authority)) {
            if (DEBUG)
                Log.d(TAG, "Excluding root because: owned by calling package.");
            continue;
        }
        if (DEBUG)
            Log.d(TAG, "Including " + root);
        matching.add(root);
    }
    return matching;
}
Also used : RootInfo(com.android.documentsui.model.RootInfo) ArrayList(java.util.ArrayList) VisibleForTesting(android.support.annotation.VisibleForTesting)

Aggregations

VisibleForTesting (android.support.annotation.VisibleForTesting)63 File (java.io.File)24 FileNotFoundException (java.io.FileNotFoundException)15 Intent (android.content.Intent)11 IOException (java.io.IOException)11 MessagingException (com.fsck.k9.mail.MessagingException)8 ArrayList (java.util.ArrayList)8 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)6 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)6 UnavailableStorageException (com.fsck.k9.mailstore.UnavailableStorageException)6 Shared.getQuantityString (com.android.documentsui.Shared.getQuantityString)5 RootInfo (com.android.documentsui.model.RootInfo)5 LocalFolder (com.fsck.k9.mailstore.LocalFolder)5 LocalStore (com.fsck.k9.mailstore.LocalStore)5 HashMap (java.util.HashMap)5 SuppressLint (android.annotation.SuppressLint)4 LocalMessage (com.fsck.k9.mailstore.LocalMessage)4 Bundle (android.os.Bundle)3 Nullable (android.support.annotation.Nullable)3 Folder (com.fsck.k9.mail.Folder)3