Example usage for android.util SparseArray SparseArray

List of usage examples for android.util SparseArray SparseArray

Introduction

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

Prototype

public SparseArray() 

Source Link

Document

Creates a new SparseArray containing no mappings.

Usage

From source file:atownsend.swipeopenhelper.SwipeOpenItemTouchHelper.java

public void restoreInstanceState(Bundle savedInstanceState) {
    openedPositions = savedInstanceState.getSparseParcelableArray(OPENED_STATES);
    if (openedPositions == null) {
        openedPositions = new SparseArray<>();
    }//from   w ww . j  av  a  2  s .c o m
}

From source file:com.taobao.weex.ui.component.list.BasicListComponent.java

/**
 * ViewType will be classified into {HashMap<Integer,ArrayList<Integer>> mViewTypes}
 *
 * @param component/*w w w  .  j  a va 2  s  .c  o m*/
 */
private void bindViewType(WXComponent component) {
    int id = generateViewType(component);

    if (mViewTypes == null) {
        mViewTypes = new SparseArray<>();
    }

    ArrayList<WXComponent> mTypes = mViewTypes.get(id);

    if (mTypes == null) {
        mTypes = new ArrayList<>();
        mViewTypes.put(id, mTypes);
    }
    mTypes.add(component);
}

From source file:de.ub0r.android.callmeter.data.RuleMatcher.java

/**
 * Load {@link Rule}s and {@link Plan}s.
 *
 * @param context {@link Context}/*  w  ww. j  av a 2 s.  co m*/
 */
private static void load(final Context context) {
    Log.d(TAG, "load()");
    if (rules != null && plans != null) {
        return;
    }
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    stripLeadingZeros = prefs.getBoolean(Preferences.PREFS_STRIP_LEADING_ZEROS, false);
    intPrefix = prefs.getString(Preferences.PREFS_INT_PREFIX, "");
    zeroPrefix = !intPrefix.equals("+44") && !intPrefix.equals("+49");

    final ContentResolver cr = context.getContentResolver();

    // load rules
    rules = new ArrayList<Rule>();
    Cursor cursor = cr.query(DataProvider.Rules.CONTENT_URI, DataProvider.Rules.PROJECTION,
            DataProvider.Rules.ACTIVE + ">0", null, DataProvider.Rules.ORDER);
    if (cursor != null && cursor.moveToFirst()) {
        do {
            rules.add(new Rule(cr, cursor, -1));
        } while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }

    // load plans
    plans = new SparseArray<Plan>();
    cursor = cr.query(DataProvider.Plans.CONTENT_URI, DataProvider.Plans.PROJECTION,
            DataProvider.Plans.WHERE_REALPLANS, null, null);
    if (cursor != null && cursor.moveToFirst()) {
        do {
            final int i = cursor.getInt(DataProvider.Plans.INDEX_ID);
            plans.put(i, new Plan(cr, cursor));
        } while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    // update parent references
    int l = plans.size();
    for (int i = 0; i < l; i++) {
        Plan p = plans.valueAt(i);
        p.parent = plans.get(p.ppid);
    }
}

From source file:de.eidottermihi.rpicheck.activity.MainActivity.java

@Override
public void onQueryFinished(QueryBean result) {
    currentDevice.setLastQueryData(result);
    // update and reset pullToRefresh
    swipeRefreshLayout.setRefreshing(false);
    if (result.getException() == null) {
        // update view data
        this.updateQueryDataInView(result);
        // update entry in allDevices-Map
        if (allDevices != null) {
            allDevices.put(this.currentDevice.getId(), this.currentDevice);
        } else {/*from   w ww . ja  va 2s .  c o  m*/
            allDevices = new SparseArray<>();
            allDevices.put(this.currentDevice.getId(), this.currentDevice);
        }
    } else {
        this.handleQueryException(result.getException());
    }

}

From source file:dev.ukanth.ufirewall.Api.java

/**
 * @param ctx application context (mandatory)
 * @return a list of applications//  w ww  .  j a v  a 2 s  . co m
 */
public static List<PackageInfoData> getApps(Context ctx, GetAppList appList) {

    initSpecial();
    if (applications != null && applications.size() > 0) {
        // return cached instance
        return applications;
    }

    final SharedPreferences defaultPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    final boolean enableVPN = defaultPrefs.getBoolean("enableVPN", false);
    final boolean enableLAN = defaultPrefs.getBoolean("enableLAN", false);
    final boolean enableRoam = defaultPrefs.getBoolean("enableRoam", true);

    final SharedPreferences prefs = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);

    final String savedPkg_wifi_uid = prefs.getString(PREF_WIFI_PKG_UIDS, "");
    final String savedPkg_3g_uid = prefs.getString(PREF_3G_PKG_UIDS, "");
    final String savedPkg_roam_uid = prefs.getString(PREF_ROAMING_PKG_UIDS, "");
    final String savedPkg_vpn_uid = prefs.getString(PREF_VPN_PKG_UIDS, "");
    final String savedPkg_lan_uid = prefs.getString(PREF_LAN_PKG_UIDS, "");

    List<Integer> selected_wifi = new ArrayList<Integer>();
    List<Integer> selected_3g = new ArrayList<Integer>();
    List<Integer> selected_roam = new ArrayList<Integer>();
    List<Integer> selected_vpn = new ArrayList<Integer>();
    List<Integer> selected_lan = new ArrayList<Integer>();

    selected_wifi = getListFromPref(savedPkg_wifi_uid);
    selected_3g = getListFromPref(savedPkg_3g_uid);

    if (enableRoam) {
        selected_roam = getListFromPref(savedPkg_roam_uid);
    }
    if (enableVPN) {
        selected_vpn = getListFromPref(savedPkg_vpn_uid);
    }
    if (enableLAN) {
        selected_lan = getListFromPref(savedPkg_lan_uid);
    }
    //revert back to old approach

    //always use the defaul preferences to store cache value - reduces the application usage size
    final SharedPreferences cachePrefs = ctx.getSharedPreferences("AFWallPrefs", Context.MODE_PRIVATE);

    int count = 0;
    try {
        final PackageManager pkgmanager = ctx.getPackageManager();
        final List<ApplicationInfo> installed = pkgmanager
                .getInstalledApplications(PackageManager.GET_META_DATA);
        SparseArray<PackageInfoData> syncMap = new SparseArray<PackageInfoData>();
        final Editor edit = cachePrefs.edit();
        boolean changed = false;
        String name = null;
        String cachekey = null;
        final String cacheLabel = "cache.label.";
        PackageInfoData app = null;
        ApplicationInfo apinfo = null;

        for (int i = 0; i < installed.size(); i++) {
            //for (final ApplicationInfo apinfo : installed) {
            count = count + 1;
            apinfo = installed.get(i);

            if (appList != null) {
                appList.doProgress(count);
            }

            boolean firstseen = false;
            app = syncMap.get(apinfo.uid);
            // filter applications which are not allowed to access the Internet
            if (app == null && PackageManager.PERMISSION_GRANTED != pkgmanager
                    .checkPermission(Manifest.permission.INTERNET, apinfo.packageName)) {
                continue;
            }
            // try to get the application label from our cache - getApplicationLabel() is horribly slow!!!!
            cachekey = cacheLabel + apinfo.packageName;
            name = prefs.getString(cachekey, "");
            if (name.length() == 0) {
                // get label and put on cache
                name = pkgmanager.getApplicationLabel(apinfo).toString();
                edit.putString(cachekey, name);
                changed = true;
                firstseen = true;
            }
            if (app == null) {
                app = new PackageInfoData();
                app.uid = apinfo.uid;
                app.names = new ArrayList<String>();
                app.names.add(name);
                app.appinfo = apinfo;
                app.pkgName = apinfo.packageName;
                syncMap.put(apinfo.uid, app);
            } else {
                app.names.add(name);
            }
            app.firstseen = firstseen;
            // check if this application is selected
            if (!app.selected_wifi && Collections.binarySearch(selected_wifi, app.uid) >= 0) {
                app.selected_wifi = true;
            }
            if (!app.selected_3g && Collections.binarySearch(selected_3g, app.uid) >= 0) {
                app.selected_3g = true;
            }
            if (enableRoam && !app.selected_roam && Collections.binarySearch(selected_roam, app.uid) >= 0) {
                app.selected_roam = true;
            }
            if (enableVPN && !app.selected_vpn && Collections.binarySearch(selected_vpn, app.uid) >= 0) {
                app.selected_vpn = true;
            }
            if (enableLAN && !app.selected_lan && Collections.binarySearch(selected_lan, app.uid) >= 0) {
                app.selected_lan = true;
            }

        }

        List<PackageInfoData> specialData = new ArrayList<PackageInfoData>();
        specialData.add(new PackageInfoData(SPECIAL_UID_ANY, ctx.getString(R.string.all_item),
                "dev.afwall.special.any"));
        specialData.add(new PackageInfoData(SPECIAL_UID_KERNEL, ctx.getString(R.string.kernel_item),
                "dev.afwall.special.kernel"));
        specialData.add(new PackageInfoData(SPECIAL_UID_TETHER, ctx.getString(R.string.tethering_item),
                "dev.afwall.special.tether"));
        //specialData.add(new PackageInfoData(SPECIAL_UID_DNSPROXY, ctx.getString(R.string.dnsproxy_item), "dev.afwall.special.dnsproxy"));
        specialData.add(new PackageInfoData(SPECIAL_UID_NTP, ctx.getString(R.string.ntp_item),
                "dev.afwall.special.ntp"));
        specialData
                .add(new PackageInfoData("root", ctx.getString(R.string.root_item), "dev.afwall.special.root"));
        specialData.add(new PackageInfoData("media", "Media server", "dev.afwall.special.media"));
        specialData.add(new PackageInfoData("vpn", "VPN networking", "dev.afwall.special.vpn"));
        specialData.add(new PackageInfoData("shell", "Linux shell", "dev.afwall.special.shell"));
        specialData.add(new PackageInfoData("gps", "GPS", "dev.afwall.special.gps"));
        specialData.add(new PackageInfoData("adb", "ADB (Android Debug Bridge)", "dev.afwall.special.adb"));

        if (specialApps == null) {
            specialApps = new HashMap<String, Integer>();
        }
        for (int i = 0; i < specialData.size(); i++) {
            app = specialData.get(i);
            specialApps.put(app.pkgName, app.uid);
            //default DNS/NTP
            if (app.uid != -1 && syncMap.get(app.uid) == null) {
                // check if this application is allowed
                if (!app.selected_wifi && Collections.binarySearch(selected_wifi, app.uid) >= 0) {
                    app.selected_wifi = true;
                }
                if (!app.selected_3g && Collections.binarySearch(selected_3g, app.uid) >= 0) {
                    app.selected_3g = true;
                }
                if (enableRoam && !app.selected_roam && Collections.binarySearch(selected_roam, app.uid) >= 0) {
                    app.selected_roam = true;
                }
                if (enableVPN && !app.selected_vpn && Collections.binarySearch(selected_vpn, app.uid) >= 0) {
                    app.selected_vpn = true;
                }
                if (enableLAN && !app.selected_lan && Collections.binarySearch(selected_lan, app.uid) >= 0) {
                    app.selected_lan = true;
                }
                syncMap.put(app.uid, app);
            }
        }

        if (changed) {
            edit.commit();
        }
        /* convert the map into an array */
        applications = new ArrayList<PackageInfoData>();
        for (int i = 0; i < syncMap.size(); i++) {
            applications.add(syncMap.valueAt(i));
        }

        return applications;
    } catch (Exception e) {
        alert(ctx, ctx.getString(R.string.error_common) + e);
    }
    return null;
}

From source file:com.staggeredgrid.library.StaggeredGridView.java

@Override
public Parcelable onSaveInstanceState() {
    ListSavedState listState = (ListSavedState) super.onSaveInstanceState();
    GridListSavedState ss = new GridListSavedState(listState.getSuperState());

    // from the list state
    ss.selectedId = listState.selectedId;
    ss.firstId = listState.firstId;/*w w  w.j  a  v  a  2s.  c om*/
    ss.viewTop = listState.viewTop;
    ss.position = listState.position;
    ss.height = listState.height;

    // our state

    boolean haveChildren = getChildCount() > 0 && getCount() > 0;

    if (haveChildren && mFirstPosition > 0) {
        ss.columnCount = mColumnCount;
        ss.columnTops = mColumnTops;
        ss.positionData = mPositionData;
    } else {
        ss.columnCount = mColumnCount >= 0 ? mColumnCount : 0;
        ss.columnTops = new int[ss.columnCount];
        ss.positionData = new SparseArray<Object>();
    }

    return ss;
}

From source file:xyz.zpayh.hdimage.HDImageView.java

private void initialiseTileMap(Point maxTileDimensions) {
    mMappingMap = new SparseArray<>();
    int sampleSize = mMaxSampleSize;
    int xTiles = 1;
    int yTiles = 1;
    while (true) {
        int sTileWidth = getShowWidth() / xTiles;
        int sTileHeight = getShowHeight() / yTiles;
        int subTileWidth = sTileWidth / sampleSize;
        int subTileHeight = sTileHeight / sampleSize;

        while (subTileWidth + xTiles + 1 > maxTileDimensions.x
                || (subTileWidth > getWidth() * 1.25 && sampleSize < mMaxSampleSize)) {
            xTiles++;//from w  w  w . j  av a  2 s .c  om
            sTileWidth = getShowWidth() / xTiles;
            subTileWidth = sTileWidth / sampleSize;
        }

        while (subTileHeight + yTiles + 1 > maxTileDimensions.y
                || (subTileHeight > getHeight() * 1.25 && sampleSize < mMaxSampleSize)) {
            yTiles++;
            sTileHeight = getShowHeight() / yTiles;
            subTileHeight = sTileHeight / sampleSize;
        }
        List<Mapping> mappingGrid = new ArrayList<>(xTiles * yTiles);
        for (int x = 0; x < xTiles; x++) {
            for (int y = 0; y < yTiles; y++) {
                Mapping mapping = new Mapping();
                mapping.mSampleSize = sampleSize;
                mapping.mVisible = sampleSize == mMaxSampleSize;
                mapping.mSourceRect = new Rect(x * sTileWidth, y * sTileHeight,
                        x == xTiles - 1 ? getShowWidth() : (x + 1) * sTileWidth,
                        y == yTiles - 1 ? getShowHeight() : (y + 1) * sTileHeight);
                mapping.mViewRect = new Rect(0, 0, 0, 0);
                mapping.mFileSourceRect = new Rect(mapping.mSourceRect);
                mappingGrid.add(mapping);
            }
        }
        mMappingMap.put(sampleSize, mappingGrid);
        if (sampleSize == 1) {
            break;
        } else {
            sampleSize /= 2;
        }
    }
    if (BuildConfig.DEBUG) {
        for (int index = 0; index < mMappingMap.size(); index++) {
            Log.d(TAG,
                    "[sampleSize]" + mMappingMap.keyAt(index) + " [tiles]" + mMappingMap.valueAt(index).size());
        }
    }
}

From source file:nz.ac.otago.psyanlab.common.designer.ExperimentDesignerActivity.java

@Override
public void registerDialogueResultListener(int requestCode, DialogueResultListener listener) {
    if (mDialogueResultListeners == null) {
        mDialogueResultListeners = new SparseArray<DialogueResultListener>();
    }//from   w  w  w .ja  v a2s.  com
    mDialogueResultListeners.put(requestCode, listener);
}

From source file:com.mobicage.rogerthat.NewsListAdapter.java

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    if (position < (mMinPosition - BATCH_SIZE)) {
        mNewsItemsByPosition = new SparseArray<>();
        mNewsItemsById = new LongSparseArray<>();
        mMinPosition = -1;/* ww w .ja v  a2 s .  c om*/
        mMaxPosition = -1;
    }

    if (position < mMinPosition) {
        if (mFillCacheInFrontTask != null && mFillCacheInFrontTask.getStatus() != AsyncTask.Status.FINISHED) {
            mFillCacheInFrontTask.cancel(true);
            mFillCacheInFrontTask = null;
        }

        do {
            List<NewsItemIndex> newsItems = mActivity.newsPlugin.getNewsBefore(mActivity.getFeedName(),
                    mNewsItemsByPosition.get(mMinPosition).sortKey, BATCH_SIZE, mActivity.pinnedSearchQry);
            addNewsItemsToFront(newsItems);
        } while (position < mMinPosition);

    } else if (position > mMaxPosition) {
        if (mFillCacheInEndTask != null && mFillCacheInEndTask.getStatus() != AsyncTask.Status.FINISHED) {
            mFillCacheInEndTask.cancel(true);
            mFillCacheInEndTask = null;
        }

        if (mMinPosition < 0)
            mMinPosition = 0;

        do {
            NewsItemIndex newsItemIndex = mNewsItemsByPosition.get(mMaxPosition);
            long sortKey = newsItemIndex == null ? Long.MAX_VALUE : newsItemIndex.sortKey;
            List<NewsItemIndex> newsItems = mActivity.newsPlugin.getNewsAfter(mActivity.getFeedName(), sortKey,
                    BATCH_SIZE, mActivity.pinnedSearchQry);
            addNewsItemsToEnd(newsItems);
        } while (position > mMaxPosition);
    }

    if ((position - BACKGROUND_FETCH_THRESHOLD) <= mMinPosition && mMinPosition > 0
            && (mFillCacheInFrontTask == null
                    || mFillCacheInFrontTask.getStatus() == AsyncTask.Status.FINISHED)) {
        mFillCacheInFrontTask = new FillCacheInFrontTask();
        mFillCacheInFrontTask.execute(
                new FillCacheParams(mNewsItemsByPosition.get(mMinPosition).sortKey, mActivity.pinnedSearchQry));
    }
    if ((position + BACKGROUND_FETCH_THRESHOLD) >= mMaxPosition
            && (mFillCacheInEndTask == null || mFillCacheInEndTask.getStatus() == AsyncTask.Status.FINISHED)) {
        NewsItemIndex newsItemIndex = mNewsItemsByPosition.get(mMaxPosition);
        long sortKey = newsItemIndex == null ? Long.MAX_VALUE : newsItemIndex.sortKey;
        mFillCacheInEndTask = new FillCacheInEndTask();
        mFillCacheInEndTask.execute(new FillCacheParams(sortKey, mActivity.pinnedSearchQry));
    }

    String logMessage = "onBindViewHolder for:\n- position: " + position + "\n- mMinPosition: " + mMinPosition
            + "\n- mMaxPosition: " + mMaxPosition + "\n- newsitem size: " + mNewsItemsByPosition.size()
            + "\n- adapter size: " + getItemCount();
    NewsItemIndex ni = mNewsItemsByPosition.get(position);
    if (ni == null) {
        L.bug(logMessage);
    } else {
        L.d(logMessage);
        holder.setNewsItem(position, mActivity.newsStore.getNewsItem(ni.id), ni);
    }
}

From source file:android.app.FragmentManager.java

boolean popBackStackState(Handler handler, String name, int id, int flags) {
    if (mBackStack == null) {
        return false;
    }/* ww w .  j av a 2  s.c  o m*/
    if (name == null && id < 0 && (flags & POP_BACK_STACK_INCLUSIVE) == 0) {
        int last = mBackStack.size() - 1;
        if (last < 0) {
            return false;
        }
        final BackStackRecord bss = mBackStack.remove(last);
        SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>();
        SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>();
        bss.calculateBackFragments(firstOutFragments, lastInFragments);
        bss.popFromBackStack(true, null, firstOutFragments, lastInFragments);
        reportBackStackChanged();
    } else {
        int index = -1;
        if (name != null || id >= 0) {
            // If a name or ID is specified, look for that place in
            // the stack.
            index = mBackStack.size() - 1;
            while (index >= 0) {
                BackStackRecord bss = mBackStack.get(index);
                if (name != null && name.equals(bss.getName())) {
                    break;
                }
                if (id >= 0 && id == bss.mIndex) {
                    break;
                }
                index--;
            }
            if (index < 0) {
                return false;
            }
            if ((flags & POP_BACK_STACK_INCLUSIVE) != 0) {
                index--;
                // Consume all following entries that match.
                while (index >= 0) {
                    BackStackRecord bss = mBackStack.get(index);
                    if ((name != null && name.equals(bss.getName())) || (id >= 0 && id == bss.mIndex)) {
                        index--;
                        continue;
                    }
                    break;
                }
            }
        }
        if (index == mBackStack.size() - 1) {
            return false;
        }
        final ArrayList<BackStackRecord> states = new ArrayList<BackStackRecord>();
        for (int i = mBackStack.size() - 1; i > index; i--) {
            states.add(mBackStack.remove(i));
        }
        final int LAST = states.size() - 1;
        SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>();
        SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>();
        for (int i = 0; i <= LAST; i++) {
            states.get(i).calculateBackFragments(firstOutFragments, lastInFragments);
        }
        BackStackRecord.TransitionState state = null;
        for (int i = 0; i <= LAST; i++) {
            if (DEBUG)
                Log.v(TAG, "Popping back stack state: " + states.get(i));
            state = states.get(i).popFromBackStack(i == LAST, state, firstOutFragments, lastInFragments);
        }
        reportBackStackChanged();
    }
    return true;
}