Search in sources :

Example 11 with PeriodicSync

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

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

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

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

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

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

use of android.content.PeriodicSync in project aware-client by denzilferreira.

the class Aware method isSyncEnabled.

/**
 * Checks if a specific sync adapter is enabled or not
 * @param authority
 * @returns
 */
public static boolean isSyncEnabled(Context context, String authority) {
    Account aware = Aware.getAWAREAccount(context);
    boolean isAutoSynchable = ContentResolver.getSyncAutomatically(aware, authority);
    boolean isSynchable = (ContentResolver.getIsSyncable(aware, authority) > 0);
    boolean isMasterSyncEnabled = ContentResolver.getMasterSyncAutomatically();
    List<PeriodicSync> periodicSyncs = ContentResolver.getPeriodicSyncs(aware, authority);
    if (Aware.DEBUG)
        Log.d(Aware.TAG, "Sync-Adapter Authority: " + authority + " syncable: " + isSynchable + " auto: " + isAutoSynchable + " Periodic: " + !periodicSyncs.isEmpty() + " global: " + isMasterSyncEnabled);
    for (PeriodicSync p : periodicSyncs) {
        if (Aware.DEBUG)
            Log.d(Aware.TAG, "Every: " + p.period / 60 + " minutes");
    }
    return isSynchable && isAutoSynchable && isMasterSyncEnabled;
}
Also used : Account(android.accounts.Account) PeriodicSync(android.content.PeriodicSync)

Example 15 with PeriodicSync

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

the class ShadowContentResolverTest method shouldRemovePeriodSync.

@Test
public void shouldRemovePeriodSync() {
    Bundle fooBar = new Bundle();
    fooBar.putString("foo", "bar");
    Bundle fooBaz = new Bundle();
    fooBaz.putString("foo", "baz");
    Bundle foo42 = new Bundle();
    foo42.putInt("foo", 42);
    assertThat(ShadowContentResolver.getPeriodicSyncs(b, AUTHORITY)).isEmpty();
    assertThat(ShadowContentResolver.getPeriodicSyncs(a, AUTHORITY)).isEmpty();
    ContentResolver.addPeriodicSync(a, AUTHORITY, fooBar, 6000L);
    ContentResolver.addPeriodicSync(a, AUTHORITY, fooBaz, 6000L);
    ContentResolver.addPeriodicSync(a, AUTHORITY, foo42, 6000L);
    ContentResolver.addPeriodicSync(b, AUTHORITY, fooBar, 6000L);
    ContentResolver.addPeriodicSync(b, AUTHORITY, fooBaz, 6000L);
    ContentResolver.addPeriodicSync(b, AUTHORITY, foo42, 6000L);
    assertThat(ShadowContentResolver.getPeriodicSyncs(a, AUTHORITY)).containsExactly(new PeriodicSync(a, AUTHORITY, fooBar, 6000L), new PeriodicSync(a, AUTHORITY, fooBaz, 6000L), new PeriodicSync(a, AUTHORITY, foo42, 6000L));
    ContentResolver.removePeriodicSync(a, AUTHORITY, fooBar);
    assertThat(ShadowContentResolver.getPeriodicSyncs(a, AUTHORITY)).containsExactly(new PeriodicSync(a, AUTHORITY, fooBaz, 6000L), new PeriodicSync(a, AUTHORITY, foo42, 6000L));
    ContentResolver.removePeriodicSync(a, AUTHORITY, fooBaz);
    assertThat(ShadowContentResolver.getPeriodicSyncs(a, AUTHORITY)).containsExactly(new PeriodicSync(a, AUTHORITY, foo42, 6000L));
    ContentResolver.removePeriodicSync(a, AUTHORITY, foo42);
    assertThat(ShadowContentResolver.getPeriodicSyncs(a, AUTHORITY)).isEmpty();
    assertThat(ShadowContentResolver.getPeriodicSyncs(b, AUTHORITY)).containsExactly(new PeriodicSync(b, AUTHORITY, fooBar, 6000L), new PeriodicSync(b, AUTHORITY, fooBaz, 6000L), new PeriodicSync(b, AUTHORITY, foo42, 6000L));
}
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