Example usage for android.util ArrayMap ArrayMap

List of usage examples for android.util ArrayMap ArrayMap

Introduction

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

Prototype

public ArrayMap() 

Source Link

Document

Create a new empty ArrayMap.

Usage

From source file:com.appsimobile.appsii.compat.MapCompat.java

public static <K, V> Map<K, V> createMap() {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return new ArrayMap<>();
    }/*w  w w  .ja  v  a 2s  . c  om*/
    return new android.support.v4.util.ArrayMap<>();
}

From source file:com.android.tests.lib.UnitTest.java

@Test
public void exceptions() {
    try {/* w  w w . j  a v  a 2 s  . c  om*/
        ArrayMap map = new ArrayMap();
        map.isEmpty();
        fail();
    } catch (RuntimeException e) {
        assertEquals(RuntimeException.class, e.getClass());
        assertTrue(e.getMessage().contains("isEmpty"));
        assertTrue(e.getMessage().contains("not mocked"));
        assertTrue(e.getMessage().contains("androidstudio/not-mocked"));
    }

    try {
        Debug.getThreadAllocCount();
        fail();
    } catch (RuntimeException e) {
        assertEquals(RuntimeException.class, e.getClass());
        assertTrue(e.getMessage().contains("getThreadAllocCount"));
        assertTrue(e.getMessage().contains("not mocked"));
        assertTrue(e.getMessage().contains("androidstudio/not-mocked"));
    }

}

From source file:com.android.tests.lib.LibUnitTest.java

@Test
public void exceptions() {
    try {/*from  w  ww.  j a v a 2 s  .  co m*/
        ArrayMap map = new ArrayMap();
        map.isEmpty();
        fail();
    } catch (RuntimeException e) {
        assertEquals(RuntimeException.class, e.getClass());
        assertTrue(e.getMessage().contains("isEmpty"));
        assertTrue(e.getMessage().contains("not mocked"));
    }

    try {
        Debug.getThreadAllocCount();
        fail();
    } catch (RuntimeException e) {
        assertEquals(RuntimeException.class, e.getClass());
        assertTrue(e.getMessage().contains("getThreadAllocCount"));
        assertTrue(e.getMessage().contains("not mocked"));
    }

}

From source file:com.android.settings.security.SecurityFeatureProviderImpl.java

@VisibleForTesting
void updatePreferencesToRunOnWorkerThread(Context context, PreferenceScreen preferenceScreen,
        DashboardCategory dashboardCategory) {

    int tilesCount = (dashboardCategory != null) ? dashboardCategory.getTilesCount() : 0;
    Map<String, IContentProvider> providerMap = new ArrayMap<>();
    for (int i = 0; i < tilesCount; i++) {
        Tile tile = dashboardCategory.getTile(i);
        // If the tile does not have a key or appropriate meta data, skip it.
        if (TextUtils.isEmpty(tile.key) || (tile.metaData == null)) {
            continue;
        }//  w  w  w .  j a v a2 s  . co  m
        Preference matchingPref = preferenceScreen.findPreference(tile.key);
        // If the tile does not have a matching preference, skip it.
        if (matchingPref == null) {
            continue;
        }
        // Check if the tile has content providers for dynamically updatable content.
        final String iconUri = tile.metaData.getString(TileUtils.META_DATA_PREFERENCE_ICON_URI, null);
        final String summaryUri = tile.metaData.getString(TileUtils.META_DATA_PREFERENCE_SUMMARY_URI, null);
        if (!TextUtils.isEmpty(iconUri)) {
            String packageName = null;
            if (tile.intent != null) {
                Intent intent = tile.intent;
                if (!TextUtils.isEmpty(intent.getPackage())) {
                    packageName = intent.getPackage();
                } else if (intent.getComponent() != null) {
                    packageName = intent.getComponent().getPackageName();
                }
            }
            Pair<String, Integer> icon = TileUtils.getIconFromUri(context, packageName, iconUri, providerMap);
            if (icon != null) {
                sIconCache.put(iconUri, icon);
                // Icon is only returned if the icon belongs to Settings or the target app.
                // setIcon must be called on the UI thread.
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            matchingPref.setIcon(context.getPackageManager()
                                    .getResourcesForApplication(icon.first /* package name */)
                                    .getDrawable(icon.second /* res id */, context.getTheme()));
                        } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
                            // Intentionally ignored. If icon resources cannot be found, do not
                            // update.
                        }
                    }
                });
            }
        }
        if (!TextUtils.isEmpty(summaryUri)) {
            String summary = TileUtils.getTextFromUri(context, summaryUri, providerMap,
                    TileUtils.META_DATA_PREFERENCE_SUMMARY);
            sSummaryCache.put(summaryUri, summary);
            // setSummary must be called on UI thread.
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    // Only update the summary if it has actually changed.
                    if (summary == null) {
                        if (matchingPref.getSummary() != null) {
                            matchingPref.setSummary(summary);
                        }
                    } else if (!summary.equals(matchingPref.getSummary())) {
                        matchingPref.setSummary(summary);
                    }
                }
            });
        }
    }
}

From source file:com.android.systemui.qs.QSDragPanel.java

public void setTiles(final Collection<QSTile<?>> tilesCollection) {
    // we try to be as efficient as possible here because this can happen while the user
    // is in edit mode, or maybe even while tiles are animating
    // step 1: stop all animations
    // step 2: remove tiles no longer to be used, cache ones that are still valid
    // step 3: remove empty viewpager pages
    // step 4: generate new tiles, re-add cached ones

    if (DEBUG_TILES) {
        Log.i(TAG, "setTiles() called with tiles = [" + tilesCollection + "]");
    }/*w ww. j  av  a  2  s. com*/
    if (mLastDragRecord != null && mRecords.indexOf(mLastDragRecord) == -1) {
        // the last removed record might be stored in mLastDragRecord if we just shifted
        // re-add it to the list so we'll clean it up below
        mRecords.add(mLastDragRecord);
        mLastDragRecord = null;
    }

    // step kinda-1
    if (mDraggingRecord != null) {
        // dragging record might be animating back, force it to finished position
        mDraggingRecord.tileView.animate().cancel();
    }

    int currentViewPagerPage = mViewPager.getCurrentItem();
    int removedPages = 0;

    Map<QSTile<?>, DragTileRecord> cachedRecords = new ArrayMap<>();
    ListIterator<TileRecord> iterator = mRecords.listIterator(mRecords.size());

    int recordsRemoved = 0;
    // cleanup current records
    while (iterator.hasPrevious()) { // mRecords
        DragTileRecord dr = (DragTileRecord) iterator.previous();

        // step 1
        dr.tileView.animate().cancel();

        // step 2
        if (tilesCollection.contains(dr.tile)) {
            if (DEBUG_TILES) {
                Log.i(TAG, "caching tile: " + dr.tile);
            }
            cachedRecords.put(dr.tile, dr);
        } else {
            if (dr.page >= 0) {
                if (DEBUG_TILES) {
                    Log.w(TAG, "removed dr.tileView: " + dr.tileView + " from page: " + dr.page
                            + " (dest page: " + dr.destinationPage + ")");
                }

                removeTileView(dr.tileView);
            }
            if (DEBUG_TILES) {
                Log.i(TAG, "removing tile: " + dr.tile);
            }

            // remove record
            iterator.remove();
            recordsRemoved++;

            dr.page = -1;
            dr.destinationPage = -1;
        }
    }

    // at this point cachedRecords should have all retained tiles, no new or old tiles
    int delta = tilesCollection.size() - cachedRecords.size() - recordsRemoved;
    if (DEBUG_TILES) {
        Log.i(TAG, "record map delta: " + delta);
    }

    // step 3
    final Iterator<QSPage> pageIterator = mPages.iterator();
    while (pageIterator.hasNext()) {
        final QSPage page = pageIterator.next();
        final int viewpagerIndex = page.getPageIndex() + (mEditing ? 1 : 0);
        final int childCount = page.getChildCount();

        if (DEBUG_TILES) {
            Log.d(TAG, "page " + viewpagerIndex + " has " + childCount);
        }
        if (page.getPageIndex() >= getCurrentMaxPageCount() - 1) {
            if (DEBUG_TILES) {
                Log.d(TAG, "page : " + page + " has " + childCount + " children");
            }
            if (childCount == 0) {
                removedPages++;

                page.removeAllViews();
                mPagerAdapter.startUpdate(mViewPager);
                mPagerAdapter.destroyItem(mViewPager, viewpagerIndex, page);
                mPagerAdapter.finishUpdate(mViewPager);
                mPagerAdapter.notifyDataSetChanged();
            }
        }
    }

    if (removedPages > 0) {
        // even though we explicitly destroy old pages, without this call,
        // the viewpager doesn't seem to want to pick up the fact that we have less pages
        // and allows "empty" scrolls to the right where there is no page.
        if (DEBUG_TILES) {
            Log.d(TAG, "re-setting adapter, page: " + currentViewPagerPage);
        }
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setCurrentItem(Math.min(currentViewPagerPage, mPagerAdapter.getCount()), false);
        mPagerAdapter.notifyDataSetChanged();
    }

    // step 4
    mRecords.ensureCapacity(tilesCollection.size());
    int runningCount = 0;

    final Iterator<QSTile<?>> newTileIterator = tilesCollection.iterator();
    while (newTileIterator.hasNext()) {
        QSTile<?> tile = newTileIterator.next();
        if (tile instanceof CustomQSTile) {
            if (((CustomQSTile) tile).isUserRemoved() || ((CustomQSTile) tile).getTile() == null) {
                // tile not published yet
                continue;
            }
        }
        final int tileDestPage = getPagesForCount(runningCount + 1) - 1;

        if (DEBUG_TILES) {
            Log.d(TAG, "tile at : " + runningCount + ": " + tile + " to dest page: " + tileDestPage);
        }
        DragTileRecord record;
        if (!cachedRecords.containsKey(tile)) {
            if (DEBUG_TILES) {
                Log.d(TAG, "tile at: " + runningCount + " not cached, adding it to records");
            }
            record = makeRecord(tile);
            record.destinationPage = tileDestPage;
            mRecords.add(runningCount, record);
            mPagerAdapter.notifyDataSetChanged();
        } else {
            record = cachedRecords.get(tile);
            if (DEBUG_TILES) {
                Log.d(TAG, "tile at : " + runningCount + ": cached, restoring: " + record);
            }

            mPages.get(record.page).removeView(record.tileView);

            record.page = -1;
            record.destinationPage = tileDestPage;

            mRecords.remove(record);
            mRecords.add(runningCount, record);
            mPagerAdapter.notifyDataSetChanged();
        }
        if (record.page == -1) {
            // add the view
            mPages.get(record.destinationPage).addView(record.tileView);
            record.page = record.destinationPage;
            if (DEBUG_TILES) {
                Log.d(TAG, "added view " + record);
            }
        }
        runningCount++;
    }

    if (isShowingDetail()) {
        mDetail.bringToFront();
    }
    mPagerAdapter.notifyDataSetChanged();

    refreshAllTiles();
    requestLayout();
}

From source file:android.content.pm.PackageParser.java

private static PackageLite parseClusterPackageLite(File packageDir, int flags) throws PackageParserException {
    final File[] files = packageDir.listFiles();
    if (ArrayUtils.isEmpty(files)) {
        throw new PackageParserException(INSTALL_PARSE_FAILED_NOT_APK, "No packages found in split");
    }/*from www .j  a  v  a  2  s.  c  o  m*/

    String packageName = null;
    int versionCode = 0;

    final ArrayMap<String, ApkLite> apks = new ArrayMap<>();
    for (File file : files) {
        if (isApkFile(file)) {
            final ApkLite lite = parseApkLite(file, flags);

            // Assert that all package names and version codes are
            // consistent with the first one we encounter.
            if (packageName == null) {
                packageName = lite.packageName;
                versionCode = lite.versionCode;
            } else {
                if (!packageName.equals(lite.packageName)) {
                    throw new PackageParserException(INSTALL_PARSE_FAILED_BAD_MANIFEST, "Inconsistent package "
                            + lite.packageName + " in " + file + "; expected " + packageName);
                }
                if (versionCode != lite.versionCode) {
                    throw new PackageParserException(INSTALL_PARSE_FAILED_BAD_MANIFEST, "Inconsistent version "
                            + lite.versionCode + " in " + file + "; expected " + versionCode);
                }
            }

            // Assert that each split is defined only once
            if (apks.put(lite.splitName, lite) != null) {
                throw new PackageParserException(INSTALL_PARSE_FAILED_BAD_MANIFEST,
                        "Split name " + lite.splitName + " defined more than once; most recent was " + file);
            }
        }
    }

    final ApkLite baseApk = apks.remove(null);
    if (baseApk == null) {
        throw new PackageParserException(INSTALL_PARSE_FAILED_BAD_MANIFEST,
                "Missing base APK in " + packageDir);
    }

    // Always apply deterministic ordering based on splitName
    final int size = apks.size();

    String[] splitNames = null;
    String[] splitCodePaths = null;
    int[] splitRevisionCodes = null;
    if (size > 0) {
        splitNames = new String[size];
        splitCodePaths = new String[size];
        splitRevisionCodes = new int[size];

        splitNames = apks.keySet().toArray(splitNames);
        Arrays.sort(splitNames, sSplitNameComparator);

        for (int i = 0; i < size; i++) {
            splitCodePaths[i] = apks.get(splitNames[i]).codePath;
            splitRevisionCodes[i] = apks.get(splitNames[i]).revisionCode;
        }
    }

    final String codePath = packageDir.getAbsolutePath();
    return new PackageLite(codePath, baseApk, splitNames, splitCodePaths, splitRevisionCodes);
}

From source file:android.app.Activity.java

LoaderManagerImpl getLoaderManager(String who, boolean started, boolean create) {
    if (mAllLoaderManagers == null) {
        mAllLoaderManagers = new ArrayMap<String, LoaderManagerImpl>();
    }//from   w  w  w . j a v  a  2 s .  c o  m
    LoaderManagerImpl lm = mAllLoaderManagers.get(who);
    if (lm == null) {
        if (create) {
            lm = new LoaderManagerImpl(who, this, started);
            mAllLoaderManagers.put(who, lm);
        }
    } else {
        lm.updateActivity(this);
    }
    return lm;
}

From source file:android.content.pm.PackageParser.java

private boolean parseKeySets(Package owner, Resources res, XmlPullParser parser, AttributeSet attrs,
        String[] outError) throws XmlPullParserException, IOException {
    // we've encountered the 'key-sets' tag
    // all the keys and keysets that we want must be defined here
    // so we're going to iterate over the parser and pull out the things we want
    int outerDepth = parser.getDepth();
    int currentKeySetDepth = -1;
    int type;/*from   w  ww.j  a v  a  2s.  c o m*/
    String currentKeySet = null;
    ArrayMap<String, PublicKey> publicKeys = new ArrayMap<String, PublicKey>();
    ArraySet<String> upgradeKeySets = new ArraySet<String>();
    ArrayMap<String, ArraySet<String>> definedKeySets = new ArrayMap<String, ArraySet<String>>();
    ArraySet<String> improperKeySets = new ArraySet<String>();
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
        if (type == XmlPullParser.END_TAG) {
            if (parser.getDepth() == currentKeySetDepth) {
                currentKeySet = null;
                currentKeySetDepth = -1;
            }
            continue;
        }
        String tagName = parser.getName();
        if (tagName.equals("key-set")) {
            if (currentKeySet != null) {
                outError[0] = "Improperly nested 'key-set' tag at " + parser.getPositionDescription();
                mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
                return false;
            }
            final TypedArray sa = res.obtainAttributes(attrs,
                    com.android.internal.R.styleable.AndroidManifestKeySet);
            final String keysetName = sa
                    .getNonResourceString(com.android.internal.R.styleable.AndroidManifestKeySet_name);
            definedKeySets.put(keysetName, new ArraySet<String>());
            currentKeySet = keysetName;
            currentKeySetDepth = parser.getDepth();
            sa.recycle();
        } else if (tagName.equals("public-key")) {
            if (currentKeySet == null) {
                outError[0] = "Improperly nested 'key-set' tag at " + parser.getPositionDescription();
                mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
                return false;
            }
            final TypedArray sa = res.obtainAttributes(attrs,
                    com.android.internal.R.styleable.AndroidManifestPublicKey);
            final String publicKeyName = sa
                    .getNonResourceString(com.android.internal.R.styleable.AndroidManifestPublicKey_name);
            final String encodedKey = sa
                    .getNonResourceString(com.android.internal.R.styleable.AndroidManifestPublicKey_value);
            if (encodedKey == null && publicKeys.get(publicKeyName) == null) {
                outError[0] = "'public-key' " + publicKeyName + " must define a public-key value"
                        + " on first use at " + parser.getPositionDescription();
                mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
                sa.recycle();
                return false;
            } else if (encodedKey != null) {
                PublicKey currentKey = parsePublicKey(encodedKey);
                if (currentKey == null) {
                    Slog.w(TAG,
                            "No recognized valid key in 'public-key' tag at " + parser.getPositionDescription()
                                    + " key-set " + currentKeySet
                                    + " will not be added to the package's defined key-sets.");
                    sa.recycle();
                    improperKeySets.add(currentKeySet);
                    XmlUtils.skipCurrentTag(parser);
                    continue;
                }
                if (publicKeys.get(publicKeyName) == null || publicKeys.get(publicKeyName).equals(currentKey)) {

                    /* public-key first definition, or matches old definition */
                    publicKeys.put(publicKeyName, currentKey);
                } else {
                    outError[0] = "Value of 'public-key' " + publicKeyName
                            + " conflicts with previously defined value at " + parser.getPositionDescription();
                    mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
                    sa.recycle();
                    return false;
                }
            }
            definedKeySets.get(currentKeySet).add(publicKeyName);
            sa.recycle();
            XmlUtils.skipCurrentTag(parser);
        } else if (tagName.equals("upgrade-key-set")) {
            final TypedArray sa = res.obtainAttributes(attrs,
                    com.android.internal.R.styleable.AndroidManifestUpgradeKeySet);
            String name = sa
                    .getNonResourceString(com.android.internal.R.styleable.AndroidManifestUpgradeKeySet_name);
            upgradeKeySets.add(name);
            sa.recycle();
            XmlUtils.skipCurrentTag(parser);
        } else if (RIGID_PARSER) {
            outError[0] = "Bad element under <key-sets>: " + parser.getName() + " at " + mArchiveSourcePath
                    + " " + parser.getPositionDescription();
            mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
            return false;
        } else {
            Slog.w(TAG, "Unknown element under <key-sets>: " + parser.getName() + " at " + mArchiveSourcePath
                    + " " + parser.getPositionDescription());
            XmlUtils.skipCurrentTag(parser);
            continue;
        }
    }
    Set<String> publicKeyNames = publicKeys.keySet();
    if (publicKeyNames.removeAll(definedKeySets.keySet())) {
        outError[0] = "Package" + owner.packageName + " AndroidManifext.xml "
                + "'key-set' and 'public-key' names must be distinct.";
        mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
        return false;
    }
    owner.mKeySetMapping = new ArrayMap<String, ArraySet<PublicKey>>();
    for (ArrayMap.Entry<String, ArraySet<String>> e : definedKeySets.entrySet()) {
        final String keySetName = e.getKey();
        if (e.getValue().size() == 0) {
            Slog.w(TAG,
                    "Package" + owner.packageName + " AndroidManifext.xml " + "'key-set' " + keySetName
                            + " has no valid associated 'public-key'."
                            + " Not including in package's defined key-sets.");
            continue;
        } else if (improperKeySets.contains(keySetName)) {
            Slog.w(TAG,
                    "Package" + owner.packageName + " AndroidManifext.xml " + "'key-set' " + keySetName
                            + " contained improper 'public-key'"
                            + " tags. Not including in package's defined key-sets.");
            continue;
        }
        owner.mKeySetMapping.put(keySetName, new ArraySet<PublicKey>());
        for (String s : e.getValue()) {
            owner.mKeySetMapping.get(keySetName).add(publicKeys.get(s));
        }
    }
    if (owner.mKeySetMapping.keySet().containsAll(upgradeKeySets)) {
        owner.mUpgradeKeySets = upgradeKeySets;
    } else {
        outError[0] = "Package" + owner.packageName + " AndroidManifext.xml "
                + "does not define all 'upgrade-key-set's .";
        mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
        return false;
    }
    return true;
}