Search in sources :

Example 1 with PeriodicSync

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

the class TestContext method removePeriodicSyncs.

private void removePeriodicSyncs(SyncStorageEngine engine, Account account, int userId, String authority) {
    engine.setIsSyncable(account, userId, authority, engine.getIsSyncable(account, 0, authority));
    List<PeriodicSync> syncs = engine.getPeriodicSyncs(account, userId, authority);
    for (PeriodicSync sync : syncs) {
        engine.removePeriodicSync(sync.account, userId, sync.authority, sync.extras);
    }
}
Also used : PeriodicSync(android.content.PeriodicSync)

Example 2 with PeriodicSync

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

the class TestContext method testAuthorityPersistence.

@LargeTest
public void testAuthorityPersistence() throws Exception {
    final Account account1 = new Account("a@example.com", "example.type");
    final Account account2 = new Account("b@example.com", "example.type.2");
    final String authority1 = "testprovider1";
    final String authority2 = "testprovider2";
    final Bundle extras1 = new Bundle();
    extras1.putString("a", "1");
    final Bundle extras2 = new Bundle();
    extras2.putString("a", "2");
    extras2.putLong("b", 2);
    extras2.putInt("c", 1);
    extras2.putBoolean("d", true);
    extras2.putDouble("e", 1.2);
    extras2.putFloat("f", 4.5f);
    extras2.putParcelable("g", account1);
    final int period1 = 200;
    final int period2 = 1000;
    PeriodicSync sync1 = new PeriodicSync(account1, authority1, extras1, period1);
    PeriodicSync sync2 = new PeriodicSync(account1, authority1, extras2, period1);
    PeriodicSync sync3 = new PeriodicSync(account1, authority2, extras1, period1);
    PeriodicSync sync4 = new PeriodicSync(account1, authority2, extras2, period2);
    PeriodicSync sync5 = new PeriodicSync(account2, authority1, extras1, period1);
    MockContentResolver mockResolver = new MockContentResolver();
    SyncStorageEngine engine = SyncStorageEngine.newTestInstance(new TestContext(mockResolver, getContext()));
    removePeriodicSyncs(engine, account1, 0, authority1);
    removePeriodicSyncs(engine, account2, 0, authority1);
    removePeriodicSyncs(engine, account1, 0, authority2);
    removePeriodicSyncs(engine, account2, 0, authority2);
    engine.setMasterSyncAutomatically(false, 0);
    engine.setIsSyncable(account1, 0, authority1, 1);
    engine.setSyncAutomatically(account1, 0, authority1, true);
    engine.setIsSyncable(account2, 0, authority1, 1);
    engine.setSyncAutomatically(account2, 0, authority1, true);
    engine.setIsSyncable(account1, 0, authority2, 1);
    engine.setSyncAutomatically(account1, 0, authority2, false);
    engine.setIsSyncable(account2, 0, authority2, 0);
    engine.setSyncAutomatically(account2, 0, authority2, true);
    engine.addPeriodicSync(sync1.account, 0, sync1.authority, sync1.extras, sync1.period);
    engine.addPeriodicSync(sync2.account, 0, sync2.authority, sync2.extras, sync2.period);
    engine.addPeriodicSync(sync3.account, 0, sync3.authority, sync3.extras, sync3.period);
    engine.addPeriodicSync(sync4.account, 0, sync4.authority, sync4.extras, sync4.period);
    engine.addPeriodicSync(sync5.account, 0, sync5.authority, sync5.extras, sync5.period);
    engine.writeAllState();
    engine.clearAndReadState();
    List<PeriodicSync> syncs = engine.getPeriodicSyncs(account1, 0, authority1);
    assertEquals(2, syncs.size());
    assertEquals(sync1, syncs.get(0));
    assertEquals(sync2, syncs.get(1));
    syncs = engine.getPeriodicSyncs(account1, 0, authority2);
    assertEquals(2, syncs.size());
    assertEquals(sync3, syncs.get(0));
    assertEquals(sync4, syncs.get(1));
    syncs = engine.getPeriodicSyncs(account2, 0, authority1);
    assertEquals(1, syncs.size());
    assertEquals(sync5, syncs.get(0));
    assertEquals(true, engine.getSyncAutomatically(account1, 0, authority1));
    assertEquals(true, engine.getSyncAutomatically(account2, 0, authority1));
    assertEquals(false, engine.getSyncAutomatically(account1, 0, authority2));
    assertEquals(true, engine.getSyncAutomatically(account2, 0, authority2));
    assertEquals(1, engine.getIsSyncable(account1, 0, authority1));
    assertEquals(1, engine.getIsSyncable(account2, 0, authority1));
    assertEquals(1, engine.getIsSyncable(account1, 0, authority2));
    assertEquals(0, engine.getIsSyncable(account2, 0, authority2));
}
Also used : Account(android.accounts.Account) Bundle(android.os.Bundle) PeriodicSync(android.content.PeriodicSync) MockContentResolver(android.test.mock.MockContentResolver) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 3 with PeriodicSync

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

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

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

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

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

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