Search in sources :

Example 26 with PeriodicSync

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

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 27 with PeriodicSync

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

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 28 with PeriodicSync

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

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)

Example 29 with PeriodicSync

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

the class SyncManager method getPeriodicSyncs.

/**
     * Get a list of periodic syncs corresponding to the given target.
     */
public List<PeriodicSync> getPeriodicSyncs(EndPoint target) {
    List<SyncOperation> ops = getAllPendingSyncs();
    List<PeriodicSync> periodicSyncs = new ArrayList<PeriodicSync>();
    for (SyncOperation op : ops) {
        if (op.isPeriodic && op.target.matchesSpec(target)) {
            periodicSyncs.add(new PeriodicSync(op.target.account, op.target.provider, op.extras, op.periodMillis / 1000, op.flexMillis / 1000));
        }
    }
    return periodicSyncs;
}
Also used : ArrayList(java.util.ArrayList) PeriodicSync(android.content.PeriodicSync)

Example 30 with PeriodicSync

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

the class ShadowContentResolverTest method shouldAddPeriodicSync.

@Test
public void shouldAddPeriodicSync() {
    Bundle fooBar = new Bundle();
    fooBar.putString("foo", "bar");
    Bundle fooBaz = new Bundle();
    fooBaz.putString("foo", "baz");
    ContentResolver.addPeriodicSync(a, AUTHORITY, fooBar, 6000L);
    ContentResolver.addPeriodicSync(a, AUTHORITY, fooBaz, 6000L);
    ContentResolver.addPeriodicSync(b, AUTHORITY, fooBar, 6000L);
    ContentResolver.addPeriodicSync(b, AUTHORITY, fooBaz, 6000L);
    assertThat(ShadowContentResolver.getPeriodicSyncs(a, AUTHORITY)).containsExactly(new PeriodicSync(a, AUTHORITY, fooBar, 6000L), new PeriodicSync(a, AUTHORITY, fooBaz, 6000L));
    assertThat(ShadowContentResolver.getPeriodicSyncs(b, AUTHORITY)).containsExactly(new PeriodicSync(b, AUTHORITY, fooBar, 6000L), new PeriodicSync(b, AUTHORITY, fooBaz, 6000L));
    // If same extras, but different time, simply update the time.
    ContentResolver.addPeriodicSync(a, AUTHORITY, fooBar, 42L);
    ContentResolver.addPeriodicSync(b, AUTHORITY, fooBaz, 42L);
    assertThat(ShadowContentResolver.getPeriodicSyncs(a, AUTHORITY)).containsExactly(new PeriodicSync(a, AUTHORITY, fooBar, 42L), new PeriodicSync(a, AUTHORITY, fooBaz, 6000L));
    assertThat(ShadowContentResolver.getPeriodicSyncs(b, AUTHORITY)).containsExactly(new PeriodicSync(b, AUTHORITY, fooBar, 6000L), new PeriodicSync(b, AUTHORITY, fooBaz, 42L));
}
Also used : Bundle(android.os.Bundle) PeriodicSync(android.content.PeriodicSync) Test(org.junit.Test)

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