Search in sources :

Example 81 with SyncAdapterType

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

the class SyncManager method computeSyncable.

public int computeSyncable(Account account, int userId, String authority, boolean checkAccountAccess) {
    final int status = getIsSyncable(account, userId, authority);
    if (status == AuthorityInfo.NOT_SYNCABLE) {
        return AuthorityInfo.NOT_SYNCABLE;
    }
    final SyncAdapterType type = SyncAdapterType.newKey(authority, account.type);
    final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo = mSyncAdapters.getServiceInfo(type, userId);
    if (syncAdapterInfo == null) {
        return AuthorityInfo.NOT_SYNCABLE;
    }
    final int owningUid = syncAdapterInfo.uid;
    final String owningPackage = syncAdapterInfo.componentName.getPackageName();
    try {
        if (ActivityManagerNative.getDefault().getAppStartMode(owningUid, owningPackage) == ActivityManager.APP_START_MODE_DISABLED) {
            Slog.w(TAG, "Not scheduling job " + syncAdapterInfo.uid + ":" + syncAdapterInfo.componentName + " -- package not allowed to start");
            return AuthorityInfo.NOT_SYNCABLE;
        }
    } catch (RemoteException e) {
    /* ignore - local call */
    }
    if (checkAccountAccess && !canAccessAccount(account, owningPackage, owningUid)) {
        Log.w(TAG, "Access to " + account + " denied for package " + owningPackage + " in UID " + syncAdapterInfo.uid);
        return AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS;
    }
    return status;
}
Also used : SyncAdapterType(android.content.SyncAdapterType) RemoteException(android.os.RemoteException) EndPoint(com.android.server.content.SyncStorageEngine.EndPoint) RegisteredServicesCache(android.content.pm.RegisteredServicesCache)

Example 82 with SyncAdapterType

use of android.content.SyncAdapterType in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class ManageAccountsSettings method requestOrCancelSyncForAccounts.

private void requestOrCancelSyncForAccounts(boolean sync) {
    final int userId = mUserHandle.getIdentifier();
    SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypesAsUser(userId);
    Bundle extras = new Bundle();
    extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
    int count = getPreferenceScreen().getPreferenceCount();
    // For each account
    for (int i = 0; i < count; i++) {
        Preference pref = getPreferenceScreen().getPreference(i);
        if (pref instanceof AccountPreference) {
            Account account = ((AccountPreference) pref).getAccount();
            // For all available sync authorities, sync those that are enabled for the account
            for (int j = 0; j < syncAdapters.length; j++) {
                SyncAdapterType sa = syncAdapters[j];
                if (syncAdapters[j].accountType.equals(mAccountType) && ContentResolver.getSyncAutomaticallyAsUser(account, sa.authority, userId)) {
                    if (sync) {
                        ContentResolver.requestSyncAsUser(account, sa.authority, userId, extras);
                    } else {
                        ContentResolver.cancelSyncAsUser(account, sa.authority, userId);
                    }
                }
            }
        }
    }
}
Also used : Account(android.accounts.Account) AccountPreference(com.android.settings.AccountPreference) Preference(android.support.v7.preference.Preference) Bundle(android.os.Bundle) SyncAdapterType(android.content.SyncAdapterType) AccountPreference(com.android.settings.AccountPreference)

Example 83 with SyncAdapterType

use of android.content.SyncAdapterType in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class ManageAccountsSettings method showSyncState.

/**
     * Shows the sync state of the accounts. Note: it must be called after the accounts have been
     * loaded, @see #showAccountsIfNeeded().
     */
private void showSyncState() {
    // Catch any delayed delivery of update messages
    if (getActivity() == null || getActivity().isFinishing())
        return;
    final int userId = mUserHandle.getIdentifier();
    // iterate over all the preferences, setting the state properly for each
    List<SyncInfo> currentSyncs = ContentResolver.getCurrentSyncsAsUser(userId);
    // true if sync on any account failed
    boolean anySyncFailed = false;
    Date date = new Date();
    // only track userfacing sync adapters when deciding if account is synced or not
    final SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypesAsUser(userId);
    HashSet<String> userFacing = new HashSet<String>();
    for (int k = 0, n = syncAdapters.length; k < n; k++) {
        final SyncAdapterType sa = syncAdapters[k];
        if (sa.isUserVisible()) {
            userFacing.add(sa.authority);
        }
    }
    for (int i = 0, count = getPreferenceScreen().getPreferenceCount(); i < count; i++) {
        Preference pref = getPreferenceScreen().getPreference(i);
        if (!(pref instanceof AccountPreference)) {
            continue;
        }
        AccountPreference accountPref = (AccountPreference) pref;
        Account account = accountPref.getAccount();
        int syncCount = 0;
        long lastSuccessTime = 0;
        boolean syncIsFailing = false;
        final ArrayList<String> authorities = accountPref.getAuthorities();
        boolean syncingNow = false;
        if (authorities != null) {
            for (String authority : authorities) {
                SyncStatusInfo status = ContentResolver.getSyncStatusAsUser(account, authority, userId);
                boolean syncEnabled = isSyncEnabled(userId, account, authority);
                boolean authorityIsPending = ContentResolver.isSyncPending(account, authority);
                boolean activelySyncing = isSyncing(currentSyncs, account, authority);
                boolean lastSyncFailed = status != null && syncEnabled && status.lastFailureTime != 0 && status.getLastFailureMesgAsInt(0) != ContentResolver.SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS;
                if (lastSyncFailed && !activelySyncing && !authorityIsPending) {
                    syncIsFailing = true;
                    anySyncFailed = true;
                }
                syncingNow |= activelySyncing;
                if (status != null && lastSuccessTime < status.lastSuccessTime) {
                    lastSuccessTime = status.lastSuccessTime;
                }
                syncCount += syncEnabled && userFacing.contains(authority) ? 1 : 0;
            }
        } else {
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                Log.v(TAG, "no syncadapters found for " + account);
            }
        }
        if (syncIsFailing) {
            accountPref.setSyncStatus(AccountPreference.SYNC_ERROR, true);
        } else if (syncCount == 0) {
            accountPref.setSyncStatus(AccountPreference.SYNC_DISABLED, true);
        } else if (syncCount > 0) {
            if (syncingNow) {
                accountPref.setSyncStatus(AccountPreference.SYNC_IN_PROGRESS, true);
            } else {
                accountPref.setSyncStatus(AccountPreference.SYNC_ENABLED, true);
                if (lastSuccessTime > 0) {
                    accountPref.setSyncStatus(AccountPreference.SYNC_ENABLED, false);
                    date.setTime(lastSuccessTime);
                    final String timeString = formatSyncDate(date);
                    accountPref.setSummary(getResources().getString(R.string.last_synced, timeString));
                }
            }
        } else {
            accountPref.setSyncStatus(AccountPreference.SYNC_DISABLED, true);
        }
    }
    mErrorInfoView.setVisibility(anySyncFailed ? View.VISIBLE : View.GONE);
}
Also used : Account(android.accounts.Account) SyncStatusInfo(android.content.SyncStatusInfo) SyncAdapterType(android.content.SyncAdapterType) Date(java.util.Date) AccountPreference(com.android.settings.AccountPreference) Preference(android.support.v7.preference.Preference) SyncInfo(android.content.SyncInfo) AccountPreference(com.android.settings.AccountPreference) HashSet(java.util.HashSet)

Example 84 with SyncAdapterType

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

the class AuthenticatorHelper method buildAccountTypeToAuthoritiesMap.

private void buildAccountTypeToAuthoritiesMap() {
    mAccountTypeToAuthorities.clear();
    SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypesAsUser(mUserHandle.getIdentifier());
    for (int i = 0, n = syncAdapters.length; i < n; i++) {
        final SyncAdapterType sa = syncAdapters[i];
        ArrayList<String> authorities = mAccountTypeToAuthorities.get(sa.accountType);
        if (authorities == null) {
            authorities = new ArrayList<String>();
            mAccountTypeToAuthorities.put(sa.accountType, authorities);
        }
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "Added authority " + sa.authority + " to accountType " + sa.accountType);
        }
        authorities.add(sa.authority);
    }
}
Also used : SyncAdapterType(android.content.SyncAdapterType)

Example 85 with SyncAdapterType

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

the class SyncManager method scheduleSync.

/**
     * @param minDelayMillis The sync can't land before this delay expires.
     */
private void scheduleSync(Account requestedAccount, int userId, int reason, String requestedAuthority, Bundle extras, int targetSyncState, final long minDelayMillis) {
    final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
    if (extras == null) {
        extras = new Bundle();
    }
    if (isLoggable) {
        Log.d(TAG, "one-time sync for: " + requestedAccount + " " + extras.toString() + " " + requestedAuthority);
    }
    AccountAndUser[] accounts = null;
    if (requestedAccount != null) {
        if (userId != UserHandle.USER_ALL) {
            accounts = new AccountAndUser[] { new AccountAndUser(requestedAccount, userId) };
        } else {
            for (AccountAndUser runningAccount : mRunningAccounts) {
                if (requestedAccount.equals(runningAccount.account)) {
                    accounts = ArrayUtils.appendElement(AccountAndUser.class, accounts, runningAccount);
                }
            }
        }
    } else {
        accounts = mRunningAccounts;
    }
    if (ArrayUtils.isEmpty(accounts)) {
        if (isLoggable) {
            Slog.v(TAG, "scheduleSync: no accounts configured, dropping");
        }
        return;
    }
    final boolean uploadOnly = extras.getBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, false);
    final boolean manualSync = extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
    if (manualSync) {
        extras.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, true);
        extras.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, true);
    }
    final boolean ignoreSettings = extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, false);
    int source;
    if (uploadOnly) {
        source = SyncStorageEngine.SOURCE_LOCAL;
    } else if (manualSync) {
        source = SyncStorageEngine.SOURCE_USER;
    } else if (requestedAuthority == null) {
        source = SyncStorageEngine.SOURCE_POLL;
    } else {
        // This isn't strictly server, since arbitrary callers can (and do) request
        // a non-forced two-way sync on a specific url.
        source = SyncStorageEngine.SOURCE_SERVER;
    }
    for (AccountAndUser account : accounts) {
        // If userId is specified, do not sync accounts of other users
        if (userId >= UserHandle.USER_SYSTEM && account.userId >= UserHandle.USER_SYSTEM && userId != account.userId) {
            continue;
        }
        // Compile a list of authorities that have sync adapters.
        // For each authority sync each account that matches a sync adapter.
        final HashSet<String> syncableAuthorities = new HashSet<String>();
        for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapter : mSyncAdapters.getAllServices(account.userId)) {
            syncableAuthorities.add(syncAdapter.type.authority);
        }
        // syncable.
        if (requestedAuthority != null) {
            final boolean hasSyncAdapter = syncableAuthorities.contains(requestedAuthority);
            syncableAuthorities.clear();
            if (hasSyncAdapter)
                syncableAuthorities.add(requestedAuthority);
        }
        for (String authority : syncableAuthorities) {
            int isSyncable = computeSyncable(account.account, account.userId, authority);
            if (isSyncable == AuthorityInfo.NOT_SYNCABLE) {
                continue;
            }
            final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo = mSyncAdapters.getServiceInfo(SyncAdapterType.newKey(authority, account.account.type), account.userId);
            if (syncAdapterInfo == null) {
                continue;
            }
            final int owningUid = syncAdapterInfo.uid;
            if (isSyncable == AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS) {
                if (isLoggable) {
                    Slog.v(TAG, "    Not scheduling sync operation: " + "isSyncable == SYNCABLE_NO_ACCOUNT_ACCESS");
                }
                Bundle finalExtras = new Bundle(extras);
                String packageName = syncAdapterInfo.componentName.getPackageName();
                // If the app did not run and has no account access, done
                if (!mPackageManagerInternal.wasPackageEverLaunched(packageName, userId)) {
                    continue;
                }
                mAccountManagerInternal.requestAccountAccess(account.account, packageName, userId, new RemoteCallback((Bundle result) -> {
                    if (result != null && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) {
                        scheduleSync(account.account, userId, reason, authority, finalExtras, targetSyncState, minDelayMillis);
                    }
                }));
                continue;
            }
            final boolean allowParallelSyncs = syncAdapterInfo.type.allowParallelSyncs();
            final boolean isAlwaysSyncable = syncAdapterInfo.type.isAlwaysSyncable();
            if (isSyncable < 0 && isAlwaysSyncable) {
                mSyncStorageEngine.setIsSyncable(account.account, account.userId, authority, AuthorityInfo.SYNCABLE);
                isSyncable = AuthorityInfo.SYNCABLE;
            }
            if (targetSyncState != AuthorityInfo.UNDEFINED && targetSyncState != isSyncable) {
                continue;
            }
            if (!syncAdapterInfo.type.supportsUploading() && uploadOnly) {
                continue;
            }
            boolean syncAllowed = // Always allow if the isSyncable state is unknown.
            (isSyncable < 0) || ignoreSettings || (mSyncStorageEngine.getMasterSyncAutomatically(account.userId) && mSyncStorageEngine.getSyncAutomatically(account.account, account.userId, authority));
            if (!syncAllowed) {
                if (isLoggable) {
                    Log.d(TAG, "scheduleSync: sync of " + account + ", " + authority + " is not allowed, dropping request");
                }
                continue;
            }
            SyncStorageEngine.EndPoint info = new SyncStorageEngine.EndPoint(account.account, authority, account.userId);
            long delayUntil = mSyncStorageEngine.getDelayUntilTime(info);
            final String owningPackage = syncAdapterInfo.componentName.getPackageName();
            if (isSyncable == AuthorityInfo.NOT_INITIALIZED) {
                // Initialisation sync.
                Bundle newExtras = new Bundle();
                newExtras.putBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, true);
                if (isLoggable) {
                    Slog.v(TAG, "schedule initialisation Sync:" + ", delay until " + delayUntil + ", run by " + 0 + ", flexMillis " + 0 + ", source " + source + ", account " + account + ", authority " + authority + ", extras " + newExtras);
                }
                postScheduleSyncMessage(new SyncOperation(account.account, account.userId, owningUid, owningPackage, reason, source, authority, newExtras, allowParallelSyncs), minDelayMillis);
            } else if (targetSyncState == AuthorityInfo.UNDEFINED || targetSyncState == isSyncable) {
                if (isLoggable) {
                    Slog.v(TAG, "scheduleSync:" + " delay until " + delayUntil + ", source " + source + ", account " + account + ", authority " + authority + ", extras " + extras);
                }
                postScheduleSyncMessage(new SyncOperation(account.account, account.userId, owningUid, owningPackage, reason, source, authority, extras, allowParallelSyncs), minDelayMillis);
            }
        }
    }
}
Also used : Bundle(android.os.Bundle) SyncAdapterType(android.content.SyncAdapterType) EndPoint(com.android.server.content.SyncStorageEngine.EndPoint) RemoteCallback(android.os.RemoteCallback) EndPoint(com.android.server.content.SyncStorageEngine.EndPoint) EndPoint(com.android.server.content.SyncStorageEngine.EndPoint) AccountAndUser(android.accounts.AccountAndUser) RegisteredServicesCache(android.content.pm.RegisteredServicesCache) HashSet(java.util.HashSet)

Aggregations

SyncAdapterType (android.content.SyncAdapterType)132 Test (org.junit.Test)50 RegisteredServicesCache (android.content.pm.RegisteredServicesCache)33 EndPoint (com.android.server.content.SyncStorageEngine.EndPoint)30 UserInfo (android.content.pm.UserInfo)17 Account (android.accounts.Account)13 PackageManager (android.content.pm.PackageManager)13 UserHandle (android.os.UserHandle)13 ArrayList (java.util.ArrayList)13 AccountAndUser (android.accounts.AccountAndUser)12 RemoteException (android.os.RemoteException)11 AuthenticatorDescription (android.accounts.AuthenticatorDescription)9 Bundle (android.os.Bundle)8 HashSet (java.util.HashSet)8 SyncStatusInfo (android.content.SyncStatusInfo)7 Preference (android.support.v7.preference.Preference)7 PackageInfo (android.content.pm.PackageInfo)6 List (java.util.List)6 RemoteCallback (android.os.RemoteCallback)5 VisibleForTesting (android.support.annotation.VisibleForTesting)5