Search in sources :

Example 16 with Account

use of android.accounts.Account in project android_frameworks_base by ParanoidAndroid.

the class SyncStorageEngine method readAndDeleteLegacyAccountInfoLocked.

/**
     * Load sync engine state from the old syncmanager database, and then
     * erase it.  Note that we don't deal with pending operations, active
     * sync, or history.
     */
private void readAndDeleteLegacyAccountInfoLocked() {
    // Look for old database to initialize from.
    File file = mContext.getDatabasePath("syncmanager.db");
    if (!file.exists()) {
        return;
    }
    String path = file.getPath();
    SQLiteDatabase db = null;
    try {
        db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
    }
    if (db != null) {
        final boolean hasType = db.getVersion() >= 11;
        // Copy in all of the status information, as well as accounts.
        if (DEBUG_FILE)
            Log.v(TAG, "Reading legacy sync accounts db");
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables("stats, status");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("_id", "status._id as _id");
        map.put("account", "stats.account as account");
        if (hasType) {
            map.put("account_type", "stats.account_type as account_type");
        }
        map.put("authority", "stats.authority as authority");
        map.put("totalElapsedTime", "totalElapsedTime");
        map.put("numSyncs", "numSyncs");
        map.put("numSourceLocal", "numSourceLocal");
        map.put("numSourcePoll", "numSourcePoll");
        map.put("numSourceServer", "numSourceServer");
        map.put("numSourceUser", "numSourceUser");
        map.put("lastSuccessSource", "lastSuccessSource");
        map.put("lastSuccessTime", "lastSuccessTime");
        map.put("lastFailureSource", "lastFailureSource");
        map.put("lastFailureTime", "lastFailureTime");
        map.put("lastFailureMesg", "lastFailureMesg");
        map.put("pending", "pending");
        qb.setProjectionMap(map);
        qb.appendWhere("stats._id = status.stats_id");
        Cursor c = qb.query(db, null, null, null, null, null, null);
        while (c.moveToNext()) {
            String accountName = c.getString(c.getColumnIndex("account"));
            String accountType = hasType ? c.getString(c.getColumnIndex("account_type")) : null;
            if (accountType == null) {
                accountType = "com.google";
            }
            String authorityName = c.getString(c.getColumnIndex("authority"));
            AuthorityInfo authority = this.getOrCreateAuthorityLocked(new Account(accountName, accountType), 0, /* legacy is single-user */
            authorityName, -1, false);
            if (authority != null) {
                int i = mSyncStatus.size();
                boolean found = false;
                SyncStatusInfo st = null;
                while (i > 0) {
                    i--;
                    st = mSyncStatus.valueAt(i);
                    if (st.authorityId == authority.ident) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    st = new SyncStatusInfo(authority.ident);
                    mSyncStatus.put(authority.ident, st);
                }
                st.totalElapsedTime = getLongColumn(c, "totalElapsedTime");
                st.numSyncs = getIntColumn(c, "numSyncs");
                st.numSourceLocal = getIntColumn(c, "numSourceLocal");
                st.numSourcePoll = getIntColumn(c, "numSourcePoll");
                st.numSourceServer = getIntColumn(c, "numSourceServer");
                st.numSourceUser = getIntColumn(c, "numSourceUser");
                st.numSourcePeriodic = 0;
                st.lastSuccessSource = getIntColumn(c, "lastSuccessSource");
                st.lastSuccessTime = getLongColumn(c, "lastSuccessTime");
                st.lastFailureSource = getIntColumn(c, "lastFailureSource");
                st.lastFailureTime = getLongColumn(c, "lastFailureTime");
                st.lastFailureMesg = c.getString(c.getColumnIndex("lastFailureMesg"));
                st.pending = getIntColumn(c, "pending") != 0;
            }
        }
        c.close();
        // Retrieve the settings.
        qb = new SQLiteQueryBuilder();
        qb.setTables("settings");
        c = qb.query(db, null, null, null, null, null, null);
        while (c.moveToNext()) {
            String name = c.getString(c.getColumnIndex("name"));
            String value = c.getString(c.getColumnIndex("value"));
            if (name == null)
                continue;
            if (name.equals("listen_for_tickles")) {
                setMasterSyncAutomatically(value == null || Boolean.parseBoolean(value), 0);
            } else if (name.startsWith("sync_provider_")) {
                String provider = name.substring("sync_provider_".length(), name.length());
                int i = mAuthorities.size();
                while (i > 0) {
                    i--;
                    AuthorityInfo authority = mAuthorities.valueAt(i);
                    if (authority.authority.equals(provider)) {
                        authority.enabled = value == null || Boolean.parseBoolean(value);
                        authority.syncable = 1;
                    }
                }
            }
        }
        c.close();
        db.close();
        (new File(path)).delete();
    }
}
Also used : Account(android.accounts.Account) HashMap(java.util.HashMap) SyncStatusInfo(android.content.SyncStatusInfo) SQLiteException(android.database.sqlite.SQLiteException) Cursor(android.database.Cursor) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) AtomicFile(android.util.AtomicFile) File(java.io.File) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder)

Example 17 with Account

use of android.accounts.Account in project android_frameworks_base by ParanoidAndroid.

the class AccountManagerServiceTest method testUserdata.

public void testUserdata() throws Exception {
    Account a11 = new Account("account1", "type1");
    Bundle u11 = new Bundle();
    u11.putString("a", "a_a11");
    u11.putString("b", "b_a11");
    u11.putString("c", "c_a11");
    Account a12 = new Account("account1", "type2");
    Bundle u12 = new Bundle();
    u12.putString("a", "a_a12");
    u12.putString("b", "b_a12");
    u12.putString("c", "c_a12");
    mAms.addAccountExplicitly(a11, "p11", u11);
    mAms.addAccountExplicitly(a12, "p12", u12);
    assertEquals("a_a11", mAms.getUserData(a11, "a"));
    assertEquals("b_a11", mAms.getUserData(a11, "b"));
    assertEquals("c_a11", mAms.getUserData(a11, "c"));
    assertEquals("a_a12", mAms.getUserData(a12, "a"));
    assertEquals("b_a12", mAms.getUserData(a12, "b"));
    assertEquals("c_a12", mAms.getUserData(a12, "c"));
    mAms.setUserData(a11, "b", "b_a11b");
    mAms.setUserData(a12, "c", null);
    assertEquals("a_a11", mAms.getUserData(a11, "a"));
    assertEquals("b_a11b", mAms.getUserData(a11, "b"));
    assertEquals("c_a11", mAms.getUserData(a11, "c"));
    assertEquals("a_a12", mAms.getUserData(a12, "a"));
    assertEquals("b_a12", mAms.getUserData(a12, "b"));
    assertNull(mAms.getUserData(a12, "c"));
}
Also used : Account(android.accounts.Account) Bundle(android.os.Bundle)

Example 18 with Account

use of android.accounts.Account in project android_frameworks_base by ParanoidAndroid.

the class TestContext method testAuthorityRenaming.

@MediumTest
public void testAuthorityRenaming() throws Exception {
    final Account account1 = new Account("acc1", "type1");
    final Account account2 = new Account("acc2", "type2");
    final String authorityContacts = "contacts";
    final String authorityCalendar = "calendar";
    final String authorityOther = "other";
    final String authorityContactsNew = "com.android.contacts";
    final String authorityCalendarNew = "com.android.calendar";
    MockContentResolver mockResolver = new MockContentResolver();
    final TestContext testContext = new TestContext(mockResolver, getContext());
    byte[] accountsFileData = ("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n" + "<accounts>\n" + "<authority id=\"0\" account=\"acc1\" type=\"type1\" authority=\"contacts\" />\n" + "<authority id=\"1\" account=\"acc1\" type=\"type1\" authority=\"calendar\" />\n" + "<authority id=\"2\" account=\"acc1\" type=\"type1\" authority=\"other\" />\n" + "<authority id=\"3\" account=\"acc2\" type=\"type2\" authority=\"contacts\" />\n" + "<authority id=\"4\" account=\"acc2\" type=\"type2\" authority=\"calendar\" />\n" + "<authority id=\"5\" account=\"acc2\" type=\"type2\" authority=\"other\" />\n" + "<authority id=\"6\" account=\"acc2\" type=\"type2\" enabled=\"false\"" + " authority=\"com.android.calendar\" />\n" + "<authority id=\"7\" account=\"acc2\" type=\"type2\" enabled=\"false\"" + " authority=\"com.android.contacts\" />\n" + "</accounts>\n").getBytes();
    File syncDir = new File(new File(testContext.getFilesDir(), "system"), "sync");
    syncDir.mkdirs();
    AtomicFile accountInfoFile = new AtomicFile(new File(syncDir, "accounts.xml"));
    FileOutputStream fos = accountInfoFile.startWrite();
    fos.write(accountsFileData);
    accountInfoFile.finishWrite(fos);
    SyncStorageEngine engine = SyncStorageEngine.newTestInstance(testContext);
    assertEquals(false, engine.getSyncAutomatically(account1, 0, authorityContacts));
    assertEquals(false, engine.getSyncAutomatically(account1, 0, authorityCalendar));
    assertEquals(true, engine.getSyncAutomatically(account1, 0, authorityOther));
    assertEquals(true, engine.getSyncAutomatically(account1, 0, authorityContactsNew));
    assertEquals(true, engine.getSyncAutomatically(account1, 0, authorityCalendarNew));
    assertEquals(false, engine.getSyncAutomatically(account2, 0, authorityContacts));
    assertEquals(false, engine.getSyncAutomatically(account2, 0, authorityCalendar));
    assertEquals(true, engine.getSyncAutomatically(account2, 0, authorityOther));
    assertEquals(false, engine.getSyncAutomatically(account2, 0, authorityContactsNew));
    assertEquals(false, engine.getSyncAutomatically(account2, 0, authorityCalendarNew));
}
Also used : Account(android.accounts.Account) AtomicFile(com.android.internal.os.AtomicFile) FileOutputStream(java.io.FileOutputStream) MockContentResolver(android.test.mock.MockContentResolver) File(java.io.File) AtomicFile(com.android.internal.os.AtomicFile) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 19 with Account

use of android.accounts.Account in project android_frameworks_base by ParanoidAndroid.

the class TestContext method testSyncableMigration.

@SmallTest
public void testSyncableMigration() throws Exception {
    final Account account = new Account("acc", "type");
    MockContentResolver mockResolver = new MockContentResolver();
    final TestContext testContext = new TestContext(mockResolver, getContext());
    byte[] accountsFileData = ("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n" + "<accounts>\n" + "<authority id=\"0\" account=\"acc\" authority=\"other1\" />\n" + "<authority id=\"1\" account=\"acc\" type=\"type\" authority=\"other2\" />\n" + "<authority id=\"2\" account=\"acc\" type=\"type\" syncable=\"false\"" + " authority=\"other3\" />\n" + "<authority id=\"3\" account=\"acc\" type=\"type\" syncable=\"true\"" + " authority=\"other4\" />\n" + "</accounts>\n").getBytes();
    File syncDir = new File(new File(testContext.getFilesDir(), "system"), "sync");
    syncDir.mkdirs();
    AtomicFile accountInfoFile = new AtomicFile(new File(syncDir, "accounts.xml"));
    FileOutputStream fos = accountInfoFile.startWrite();
    fos.write(accountsFileData);
    accountInfoFile.finishWrite(fos);
    SyncStorageEngine engine = SyncStorageEngine.newTestInstance(testContext);
    assertEquals(-1, engine.getIsSyncable(account, 0, "other1"));
    assertEquals(1, engine.getIsSyncable(account, 0, "other2"));
    assertEquals(0, engine.getIsSyncable(account, 0, "other3"));
    assertEquals(1, engine.getIsSyncable(account, 0, "other4"));
}
Also used : Account(android.accounts.Account) AtomicFile(com.android.internal.os.AtomicFile) FileOutputStream(java.io.FileOutputStream) MockContentResolver(android.test.mock.MockContentResolver) File(java.io.File) AtomicFile(com.android.internal.os.AtomicFile) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 20 with Account

use of android.accounts.Account 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)

Aggregations

Account (android.accounts.Account)548 Bundle (android.os.Bundle)108 AccountManager (android.accounts.AccountManager)78 Test (org.junit.Test)53 ArrayList (java.util.ArrayList)49 Intent (android.content.Intent)40 File (java.io.File)37 Cursor (android.database.Cursor)31 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)29 PersistableBundle (android.os.PersistableBundle)27 UserInfo (android.content.pm.UserInfo)22 MockContentResolver (android.test.mock.MockContentResolver)22 HashMap (java.util.HashMap)20 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)20 RemoteException (android.os.RemoteException)19 FileOutputStream (java.io.FileOutputStream)19 IOException (java.io.IOException)19 SmallTest (android.test.suitebuilder.annotation.SmallTest)18 HashSet (java.util.HashSet)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16