Example usage for android.util ArraySet ArraySet

List of usage examples for android.util ArraySet ArraySet

Introduction

In this page you can find the example usage for android.util ArraySet ArraySet.

Prototype

@UnsupportedAppUsage
public ArraySet(Collection<E> set) 

Source Link

Usage

From source file:com.android.tv.settings.MainFragment.java

private void updateAccounts() {
    if (mAccountsGroup == null) {
        return;//from w  w  w .  ja  v a2  s . c  om
    }

    final Set<String> touchedAccounts = new ArraySet<>(mAccountsGroup.getPreferenceCount());

    final AccountManager am = AccountManager.get(getContext());
    final AuthenticatorDescription[] authTypes = am.getAuthenticatorTypes();
    final ArrayList<String> allowableAccountTypes = new ArrayList<>(authTypes.length);
    final Context themedContext = getPreferenceManager().getContext();

    for (AuthenticatorDescription authDesc : authTypes) {
        final Context targetContext;
        try {
            targetContext = getContext().createPackageContext(authDesc.packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "Authenticator description with bad package name", e);
            continue;
        } catch (SecurityException e) {
            Log.e(TAG, "Security exception loading package resources", e);
            continue;
        }

        // Main title text comes from the authenticator description (e.g. "Google").
        String authTitle = null;
        try {
            authTitle = targetContext.getString(authDesc.labelId);
            if (TextUtils.isEmpty(authTitle)) {
                authTitle = null; // Handled later when we add the row.
            }
        } catch (Resources.NotFoundException e) {
            Log.e(TAG, "Authenticator description with bad label id", e);
        }

        // There exist some authenticators which aren't intended to be user-facing.
        // If the authenticator doesn't have a title or an icon, don't present it to
        // the user as an option.
        if (authTitle != null || authDesc.iconId != 0) {
            allowableAccountTypes.add(authDesc.type);
        }

        Account[] accounts = am.getAccountsByType(authDesc.type);
        if (accounts == null || accounts.length == 0) {
            continue; // No point in continuing; there aren't any accounts to show.
        }

        // Icon URI to be displayed for each account is based on the type of authenticator.
        Drawable authImage = null;
        try {
            authImage = targetContext.getDrawable(authDesc.iconId);
        } catch (Resources.NotFoundException e) {
            Log.e(TAG, "Authenticator has bad resources", e);
        }

        // Display an entry for each installed account we have.
        for (final Account account : accounts) {
            final String key = "account_pref:" + account.type + ":" + account.name;
            Preference preference = findPreference(key);
            if (preference == null) {
                preference = new Preference(themedContext);
            }
            preference.setTitle(authTitle != null ? authTitle : account.name);
            preference.setIcon(authImage);
            preference.setSummary(authTitle != null ? account.name : null);
            preference.setFragment(AccountSyncFragment.class.getName());
            AccountSyncFragment.prepareArgs(preference.getExtras(), account);

            touchedAccounts.add(key);
            preference.setKey(key);

            mAccountsGroup.addPreference(preference);
        }
    }

    for (int i = 0; i < mAccountsGroup.getPreferenceCount();) {
        final Preference preference = mAccountsGroup.getPreference(i);
        final String key = preference.getKey();
        if (touchedAccounts.contains(key) || TextUtils.equals(KEY_ADD_ACCOUNT, key)) {
            i++;
        } else {
            mAccountsGroup.removePreference(preference);
        }
    }

    // Never allow restricted profile to add accounts.
    final Preference addAccountPref = findPreference(KEY_ADD_ACCOUNT);
    if (addAccountPref != null) {
        addAccountPref.setOrder(Integer.MAX_VALUE);
        if (isRestricted()) {
            addAccountPref.setVisible(false);
        } else {
            Intent i = new Intent().setComponent(new ComponentName("com.android.tv.settings",
                    "com.android.tv.settings.accounts.AddAccountWithTypeActivity"));
            i.putExtra(AddAccountWithTypeActivity.EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY,
                    allowableAccountTypes.toArray(new String[allowableAccountTypes.size()]));

            // If there are available account types, show the "add account" button.
            addAccountPref.setVisible(!allowableAccountTypes.isEmpty());
            addAccountPref.setIntent(i);
        }
    }
}

From source file:com.android.tv.settings.MainFragment.java

private void updateAccessories() {
    if (mAccessoriesGroup == null) {
        return;/*from  w w w  .  j a  v a2  s . com*/
    }

    if (mBtAdapter == null) {
        mAccessoriesGroup.setVisible(false);
        mAccessoriesGroup.removeAll();
        return;
    }

    final Set<BluetoothDevice> bondedDevices = mBtAdapter.getBondedDevices();
    final Set<String> connectedBluetoothAddresses = BluetoothConnectionsManager.getConnectedSet(getContext());
    final Context themedContext = getPreferenceManager().getContext();

    final Set<String> touchedKeys = new ArraySet<>(bondedDevices.size() + 1);
    if (mAddAccessory != null) {
        touchedKeys.add(mAddAccessory.getKey());
    }

    for (final BluetoothDevice device : bondedDevices) {
        final String desc = connectedBluetoothAddresses.contains(device.getAddress())
                ? getString(R.string.accessory_connected)
                : null;

        final String key = "BluetoothDevice:" + device.getAddress();
        touchedKeys.add(key);
        Preference preference = mAccessoriesGroup.findPreference(key);
        if (preference == null) {
            preference = new Preference(themedContext);
            preference.setKey(key);
        }
        preference.setTitle(device.getName());
        preference.setSummary(desc);
        final int deviceImgId = AccessoryUtils.getImageIdForDevice(device);
        preference.setIcon(deviceImgId);
        preference.setFragment(BluetoothAccessoryFragment.class.getName());
        BluetoothAccessoryFragment.prepareArgs(preference.getExtras(), device.getAddress(), device.getName(),
                deviceImgId);
        mAccessoriesGroup.addPreference(preference);
    }

    for (int i = 0; i < mAccessoriesGroup.getPreferenceCount();) {
        final Preference preference = mAccessoriesGroup.getPreference(i);
        if (touchedKeys.contains(preference.getKey())) {
            i++;
        } else {
            mAccessoriesGroup.removePreference(preference);
        }
    }
}

From source file:com.androidinspain.deskclock.Utils.java

/**
 * {@link ArraySet} is @hide prior to {@link Build.VERSION_CODES#M}.
 *///from   w  w  w  .j av a 2  s.c  o  m
@SuppressLint("NewApi")
public static <E> ArraySet<E> newArraySet(Collection<E> collection) {
    final ArraySet<E> arraySet = new ArraySet<>(collection.size());
    arraySet.addAll(collection);
    return arraySet;
}

From source file:com.android.deskclock.Utils.java

public static <E> ArraySet<E> newArraySet(Collection<E> collection) {
    final ArraySet<E> arraySet = new ArraySet<>(collection.size());
    arraySet.addAll(collection);//  w  ww .j  a  v  a2s .  c  o m
    return arraySet;
}