Search in sources :

Example 21 with PeriodicSync

use of android.content.PeriodicSync in project platform_frameworks_base by android.

the class SyncStorageEngine method parsePeriodicSync.

/**
     * Parse a periodic sync from accounts.xml. Sets the bundle to be empty.
     */
private PeriodicSync parsePeriodicSync(XmlPullParser parser, AuthorityInfo authorityInfo) {
    // Gets filled in later.
    Bundle extras = new Bundle();
    String periodValue = parser.getAttributeValue(null, "period");
    String flexValue = parser.getAttributeValue(null, "flex");
    final long period;
    long flextime;
    try {
        period = Long.parseLong(periodValue);
    } catch (NumberFormatException e) {
        Slog.e(TAG, "error parsing the period of a periodic sync", e);
        return null;
    } catch (NullPointerException e) {
        Slog.e(TAG, "the period of a periodic sync is null", e);
        return null;
    }
    try {
        flextime = Long.parseLong(flexValue);
    } catch (NumberFormatException e) {
        flextime = calculateDefaultFlexTime(period);
        Slog.e(TAG, "Error formatting value parsed for periodic sync flex: " + flexValue + ", using default: " + flextime);
    } catch (NullPointerException expected) {
        flextime = calculateDefaultFlexTime(period);
        Slog.d(TAG, "No flex time specified for this sync, using a default. period: " + period + " flex: " + flextime);
    }
    PeriodicSync periodicSync;
    periodicSync = new PeriodicSync(authorityInfo.target.account, authorityInfo.target.provider, extras, period, flextime);
    authorityInfo.periodicSyncs.add(periodicSync);
    return periodicSync;
}
Also used : Bundle(android.os.Bundle) PeriodicSync(android.content.PeriodicSync)

Example 22 with PeriodicSync

use of android.content.PeriodicSync in project platform_frameworks_base by android.

the class SyncStorageEngine method restoreAllPeriodicSyncs.

/**
     * Restore all periodic syncs read from persisted files. Used to restore periodic syncs
     * after an OS update.
     */
boolean restoreAllPeriodicSyncs() {
    if (mPeriodicSyncAddedListener == null) {
        return false;
    }
    synchronized (mAuthorities) {
        for (int i = 0; i < mAuthorities.size(); i++) {
            AuthorityInfo authority = mAuthorities.valueAt(i);
            for (PeriodicSync periodicSync : authority.periodicSyncs) {
                mPeriodicSyncAddedListener.onPeriodicSyncAdded(authority.target, periodicSync.extras, periodicSync.period, periodicSync.flexTime);
            }
            authority.periodicSyncs.clear();
        }
        writeAccountInfoLocked();
    }
    return true;
}
Also used : PeriodicSync(android.content.PeriodicSync)

Example 23 with PeriodicSync

use of android.content.PeriodicSync in project android_frameworks_base by DirtyUnicorns.

the class SyncStorageEngine method parsePeriodicSync.

/**
     * Parse a periodic sync from accounts.xml. Sets the bundle to be empty.
     */
private PeriodicSync parsePeriodicSync(XmlPullParser parser, AuthorityInfo authorityInfo) {
    // Gets filled in later.
    Bundle extras = new Bundle();
    String periodValue = parser.getAttributeValue(null, "period");
    String flexValue = parser.getAttributeValue(null, "flex");
    final long period;
    long flextime;
    try {
        period = Long.parseLong(periodValue);
    } catch (NumberFormatException e) {
        Slog.e(TAG, "error parsing the period of a periodic sync", e);
        return null;
    } catch (NullPointerException e) {
        Slog.e(TAG, "the period of a periodic sync is null", e);
        return null;
    }
    try {
        flextime = Long.parseLong(flexValue);
    } catch (NumberFormatException e) {
        flextime = calculateDefaultFlexTime(period);
        Slog.e(TAG, "Error formatting value parsed for periodic sync flex: " + flexValue + ", using default: " + flextime);
    } catch (NullPointerException expected) {
        flextime = calculateDefaultFlexTime(period);
        Slog.d(TAG, "No flex time specified for this sync, using a default. period: " + period + " flex: " + flextime);
    }
    PeriodicSync periodicSync;
    periodicSync = new PeriodicSync(authorityInfo.target.account, authorityInfo.target.provider, extras, period, flextime);
    authorityInfo.periodicSyncs.add(periodicSync);
    return periodicSync;
}
Also used : Bundle(android.os.Bundle) PeriodicSync(android.content.PeriodicSync)

Example 24 with PeriodicSync

use of android.content.PeriodicSync in project android_frameworks_base by DirtyUnicorns.

the class SyncStorageEngine method restoreAllPeriodicSyncs.

/**
     * Restore all periodic syncs read from persisted files. Used to restore periodic syncs
     * after an OS update.
     */
boolean restoreAllPeriodicSyncs() {
    if (mPeriodicSyncAddedListener == null) {
        return false;
    }
    synchronized (mAuthorities) {
        for (int i = 0; i < mAuthorities.size(); i++) {
            AuthorityInfo authority = mAuthorities.valueAt(i);
            for (PeriodicSync periodicSync : authority.periodicSyncs) {
                mPeriodicSyncAddedListener.onPeriodicSyncAdded(authority.target, periodicSync.extras, periodicSync.period, periodicSync.flexTime);
            }
            authority.periodicSyncs.clear();
        }
        writeAccountInfoLocked();
    }
    return true;
}
Also used : PeriodicSync(android.content.PeriodicSync)

Example 25 with PeriodicSync

use of android.content.PeriodicSync in project android_frameworks_base by ResurrectionRemix.

the class SyncStorageEngine method readAccountInfoLocked.

/**
     * Read all account information back in to the initial engine state.
     */
private void readAccountInfoLocked() {
    int highestAuthorityId = -1;
    FileInputStream fis = null;
    try {
        fis = mAccountInfoFile.openRead();
        if (Log.isLoggable(TAG_FILE, Log.VERBOSE)) {
            Slog.v(TAG_FILE, "Reading " + mAccountInfoFile.getBaseFile());
        }
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(fis, StandardCharsets.UTF_8.name());
        int eventType = parser.getEventType();
        while (eventType != XmlPullParser.START_TAG && eventType != XmlPullParser.END_DOCUMENT) {
            eventType = parser.next();
        }
        if (eventType == XmlPullParser.END_DOCUMENT) {
            Slog.i(TAG, "No initial accounts");
            return;
        }
        String tagName = parser.getName();
        if ("accounts".equals(tagName)) {
            String listen = parser.getAttributeValue(null, XML_ATTR_LISTEN_FOR_TICKLES);
            String versionString = parser.getAttributeValue(null, "version");
            int version;
            try {
                version = (versionString == null) ? 0 : Integer.parseInt(versionString);
            } catch (NumberFormatException e) {
                version = 0;
            }
            if (version < 3) {
                mGrantSyncAdaptersAccountAccess = true;
            }
            String nextIdString = parser.getAttributeValue(null, XML_ATTR_NEXT_AUTHORITY_ID);
            try {
                int id = (nextIdString == null) ? 0 : Integer.parseInt(nextIdString);
                mNextAuthorityId = Math.max(mNextAuthorityId, id);
            } catch (NumberFormatException e) {
            // don't care
            }
            String offsetString = parser.getAttributeValue(null, XML_ATTR_SYNC_RANDOM_OFFSET);
            try {
                mSyncRandomOffset = (offsetString == null) ? 0 : Integer.parseInt(offsetString);
            } catch (NumberFormatException e) {
                mSyncRandomOffset = 0;
            }
            if (mSyncRandomOffset == 0) {
                Random random = new Random(System.currentTimeMillis());
                mSyncRandomOffset = random.nextInt(86400);
            }
            mMasterSyncAutomatically.put(0, listen == null || Boolean.parseBoolean(listen));
            eventType = parser.next();
            AuthorityInfo authority = null;
            PeriodicSync periodicSync = null;
            do {
                if (eventType == XmlPullParser.START_TAG) {
                    tagName = parser.getName();
                    if (parser.getDepth() == 2) {
                        if ("authority".equals(tagName)) {
                            authority = parseAuthority(parser, version);
                            periodicSync = null;
                            if (authority != null) {
                                if (authority.ident > highestAuthorityId) {
                                    highestAuthorityId = authority.ident;
                                }
                            } else {
                                EventLog.writeEvent(0x534e4554, "26513719", -1, "Malformed authority");
                            }
                        } else if (XML_TAG_LISTEN_FOR_TICKLES.equals(tagName)) {
                            parseListenForTickles(parser);
                        }
                    } else if (parser.getDepth() == 3) {
                        if ("periodicSync".equals(tagName) && authority != null) {
                            periodicSync = parsePeriodicSync(parser, authority);
                        }
                    } else if (parser.getDepth() == 4 && periodicSync != null) {
                        if ("extra".equals(tagName)) {
                            parseExtra(parser, periodicSync.extras);
                        }
                    }
                }
                eventType = parser.next();
            } while (eventType != XmlPullParser.END_DOCUMENT);
        }
    } catch (XmlPullParserException e) {
        Slog.w(TAG, "Error reading accounts", e);
        return;
    } catch (java.io.IOException e) {
        if (fis == null)
            Slog.i(TAG, "No initial accounts");
        else
            Slog.w(TAG, "Error reading accounts", e);
        return;
    } finally {
        mNextAuthorityId = Math.max(highestAuthorityId + 1, mNextAuthorityId);
        if (fis != null) {
            try {
                fis.close();
            } catch (java.io.IOException e1) {
            }
        }
    }
    maybeMigrateSettingsForRenamedAuthorities();
}
Also used : Random(java.util.Random) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) PeriodicSync(android.content.PeriodicSync) FileInputStream(java.io.FileInputStream)

Aggregations

PeriodicSync (android.content.PeriodicSync)31 Bundle (android.os.Bundle)11 FileInputStream (java.io.FileInputStream)5 ArrayList (java.util.ArrayList)5 Random (java.util.Random)5 XmlPullParser (org.xmlpull.v1.XmlPullParser)5 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)5 Account (android.accounts.Account)4 MockContentResolver (android.test.mock.MockContentResolver)3 Test (org.junit.Test)3 MediumTest (android.test.suitebuilder.annotation.MediumTest)2 Implementation (org.robolectric.annotation.Implementation)2 LargeTest (android.test.suitebuilder.annotation.LargeTest)1 AtomicFile (com.android.internal.os.AtomicFile)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1