Example usage for android.util SparseBooleanArray get

List of usage examples for android.util SparseBooleanArray get

Introduction

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

Prototype

public boolean get(int key) 

Source Link

Document

Gets the boolean mapped from the specified key, or false if no such mapping has been made.

Usage

From source file:com.btmura.android.reddit.app.CommentListController.java

private boolean isAllDeletable(ListView lv) {
    SparseBooleanArray checked = lv.getCheckedItemPositions();
    int count = adapter.getCount();
    for (int i = 0; i < count; i++) {
        if (checked.get(i) && (!isAuthor(i) || !hasThingId(i))) {
            return false;
        }/*from w  w w  .  j  ava 2 s.  c  o m*/
    }
    return true;
}

From source file:pl.bcichecki.rms.client.android.fragments.InboxMessagesListFragment.java

private Message getFirstCheckedItem() {
    if (getListView().getCheckedItemCount() != 1) {
        return null;
    }//  w ww  . j av  a  2s  . co  m

    SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
    for (int i = 0; i < getListAdapter().getCount(); i++) {
        if (checkedItemPositions.get(i)) {
            return (Message) getListAdapter().getItem(i);
        }
    }
    return null;
}

From source file:com.tinfoil.sms.sms.KeyExchangeManager.java

/**
 * The onClick action for when the exchange key message is pressed. Sends a
 * key exchange message for each contact that is selected.
 * @param view The View //from  w w w  .j  a va 2s .  c  om
 */
public void exchangeKey(View view) {
    if (runThread.getEntries() != null) {
        ListView list = (ListView) this.findViewById(R.id.key_exchange_list);
        SparseBooleanArray sba = list.getCheckedItemPositions();
        int i = 0;

        while (runThread.getEntries() != null && i < runThread.getEntries().size()) {
            if (sba.get(i)) {
                TrustedContact tc = dba.getRow(SMSUtility.format(runThread.getEntries().get(0).getNumber()));

                final Number number = tc
                        .getNumber(SMSUtility.format(runThread.getEntries().get(0).getNumber()));

                if (SMSUtility.checksharedSecret(number.getSharedInfo1())
                        && SMSUtility.checksharedSecret(number.getSharedInfo2())) {
                    respondKeyExchangeMessage(this, number, runThread.getEntries().get(i));
                } else {
                    requestSharedSecrets(this, number, tc.getName(), runThread.getEntries().get(i));
                }
            }

            i++;
        }
    }
}

From source file:com.Candy.sizer.CandySizer.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    final ImageView delButton = (ImageView) getView().findViewById(R.id.btn_delete);
    final ImageView profileButton = (ImageView) getView().findViewById(R.id.btn_profile);

    if (delButton == null) {
        return;//from  w ww. ja va  2 s .c  o m
    }

    // create arraylist of apps not to be removed
    final ArrayList<String> safetyList = new ArrayList<String>();
    safetyList.add("BackupRestoreConfirmation.apk");
    safetyList.add("CertInstaller.apk");
    safetyList.add("Contacts.apk");
    safetyList.add("ContactsProvider.apk");
    safetyList.add("DefaultContainerService.apk");
    safetyList.add("DownloadProvider.apk");
    safetyList.add("DrmProvider.apk");
    safetyList.add("MediaProvider.apk");
    safetyList.add("Mms.apk");
    safetyList.add("PackageInstaller.apk");
    safetyList.add("Phone.apk");
    safetyList.add("Settings.apk");
    safetyList.add("SettingsProvider.apk");
    safetyList.add("CandyOTA.apk");
    safetyList.add("Superuser.apk");
    safetyList.add("SystemUI.apk");
    safetyList.add("TelephonyProvider.apk");

    // create arraylist from /system/app content
    File system = new File(systemPath);
    String[] sysappArray = system.list();
    mSysApp = new ArrayList<String>(Arrays.asList(sysappArray));

    // remove "apps not to be removed" from list and sort list
    mSysApp.removeAll(safetyList);
    Collections.sort(mSysApp);

    // populate listview via arrayadapter
    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_multiple_choice,
            mSysApp);

    final ListView lv = (ListView) getView().findViewById(R.string.listsystem);
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    lv.setAdapter(adapter);

    // longclick an entry
    lv.setOnItemLongClickListener(new OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
            // create deletion dialog
            String item = lv.getAdapter().getItem(arg2).toString();
            showDialog(DELETE_DIALOG, item, adapter, 0);
            return false;
        }
    });
    // click button delete
    delButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // check which items are selected
            String item = null;
            int itemCounter = 0;
            SparseBooleanArray checked = lv.getCheckedItemPositions();
            for (int i = lv.getCount() - 1; i > 0; i--) {
                if (checked.get(i)) {
                    item = mSysApp.get(i);
                    itemCounter++;
                }
            }
            if (item == null) {
                toast(getResources().getString(R.string.sizer_message_noselect));
                return;
            } else {
                showDialog(DELETE_MULTIPLE_DIALOG, item, adapter, itemCounter);
            }
        }
    });
    // click button profile
    profileButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // call select dialog
            selectDialog(mSysApp, adapter);
        }
    });
}

From source file:com.btmura.android.reddit.app.CommentListController.java

private void fillCheckedInfo(SparseBooleanArray checkedPositions, String[] checkedThingIds,
        boolean[] hasChildren) {
    int count = adapter.getCount();
    int j = 0;//from   w w  w .j  av a  2s  .  co m
    for (int i = 0; i < count; i++) {
        if (checkedPositions.get(i)) {
            checkedThingIds[j] = getThingId(i);
            hasChildren[j] = CommentLogic.hasChildren(this, i);
            j++;
        }
    }
}

From source file:com.Candy.sizer.CandySizer.java

private void showDialog(int id, final String item, final ArrayAdapter<String> adapter, int itemCounter) {
    // startup dialog
    final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());

    if (id == STARTUP_DIALOG) {
        // create warning dialog
        alert.setMessage(R.string.sizer_message_startup).setTitle(R.string.caution).setCancelable(true)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // action for ok
                        dialog.cancel();
                    }/*  w ww  . ja v a 2s .c  o m*/
                });
        // delete dialog
    } else if (id == DELETE_DIALOG) {
        alert.setMessage(R.string.sizer_message_delete)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // action for ok
                        // call delete
                        new CandySizer.SlimDeleter().execute(item);
                        // remove list entry
                        adapter.remove(item);
                        adapter.notifyDataSetChanged();
                    }
                }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // action for cancel
                        dialog.cancel();
                    }
                });
    } else if (id == DELETE_MULTIPLE_DIALOG) {
        String message;
        if (itemCounter == 1) {
            message = getResources().getString(R.string.sizer_message_delete_multi_one);
        } else {
            message = getResources().getString(R.string.sizer_message_delete_multi);
        }
        alert.setMessage(message).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                final ListView lv = (ListView) getView().findViewById(R.string.listsystem);
                ArrayList<String> itemsList = new ArrayList<String>();
                SparseBooleanArray checked = lv.getCheckedItemPositions();
                for (int i = lv.getCount() - 1; i > 0; i--) {
                    if (checked.get(i)) {
                        String appName = mSysApp.get(i);
                        itemsList.add(appName);
                        // remove list entry
                        lv.setItemChecked(i, false);
                        adapter.remove(appName);
                    }
                }
                adapter.notifyDataSetChanged();
                new CandySizer.SlimDeleter().execute(itemsList.toArray(new String[itemsList.size()]));
            }
        }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // action for cancel
                dialog.cancel();
            }
        });
    } else if (id == REBOOT_DIALOG) {
        // create warning dialog
        alert.setMessage(R.string.reboot).setTitle(R.string.caution).setCancelable(true)
                .setPositiveButton(R.string.reboot_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // action for ok
                        try {
                            dos.writeBytes("reboot");
                            dos.flush();
                            dos.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }).setNegativeButton(R.string.reboot_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // action for cancel
                        dialog.cancel();
                    }
                });
    }
    // show warning dialog
    alert.show();
}

From source file:org.flerda.android.honeypad.NoteListFragment.java

public int getCheckedItemCount() {
    if (mIsV11) {
        return mLV.getCheckedItemCount();
    } else {/* w  ww  .j av  a 2s.c o m*/
        /** Slow count **/
        SparseBooleanArray sba = mLV.getCheckedItemPositions();
        int count = 0;
        int size = sba.size();
        for (int i = 0; i < size; i++) {
            int key = sba.keyAt(i);
            if (sba.get(key)) {
                count++;
            }
        }
        return count;
    }
}

From source file:org.creativecommons.thelist.activities.CategoryListActivity.java

public void saveUserCategories() {
    SparseBooleanArray positions = mGridView.getCheckedItemPositions();
    int length = positions.size();
    //Array of user selected categories
    List<Integer> userCategories = new ArrayList<>();

    for (int i = 0; i < length; i++) {
        int itemPosition = positions.keyAt(i);
        boolean value = positions.get(itemPosition);

        if (value) {
            CategoryListItem catItem = (CategoryListItem) mGridView.getItemAtPosition(itemPosition);
            int catId = catItem.getCategoryID();
            userCategories.add(catId);/*from   ww w. j a v a2 s  .  c  om*/
            Log.v(TAG, "ITEM ADDED");
        }
    }

    Intent intent;

    if (mCurrentUser.isTempUser()) { //TEMP USER
        //Save user categories to shared preferences
        mSharedPref.saveSharedPreference(SharedPreferencesMethods.CATEGORY_PREFERENCE_KEY,
                userCategories.toString());

        if (mSharedPref.getUserItemCount() == 0) {
            intent = new Intent(CategoryListActivity.this, RandomActivity.class);
            startActivity(intent);
        } else {
            intent = new Intent(CategoryListActivity.this, DrawerActivity.class);
            startActivity(intent);
        }
    } else {
        intent = new Intent(CategoryListActivity.this, DrawerActivity.class);
        startActivity(intent);
    }
}

From source file:com.lithidsw.wallbox.app.randomizer.RandomizerFrag.java

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    fa = super.getActivity();
    mUtils = new Utils(fa);
    menuHelper = new MenuHelper(fa);
    ll = (LinearLayout) inflater.inflate(R.layout.randomizer_frag, container, false);
    prefs = fa.getSharedPreferences(C.PREF, Context.MODE_PRIVATE);

    adapter = new RandomizerGridAdapter(fa, mGalItems);
    mGridView = (GridView) ll.findViewById(R.id.grid_view);
    mTextView = (TextView) ll.findViewById(R.id.no_content);
    mProgressBar = (ProgressBar) ll.findViewById(R.id.progress);
    mGridView.setAdapter(adapter);//from   www.j  a v a 2s.  c o m

    mGridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
    mGridView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        @Override
        public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) {

        }

        @Override
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            actionMode.setTitle("Choose threads");
            mRemoveList.clear();
            MenuInflater inflater = actionMode.getMenuInflater();
            inflater.inflate(R.menu.randomizer_selection_menu, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
            final SparseBooleanArray checked = mGridView.getCheckedItemPositions();
            switch (menuItem.getItemId()) {
            case R.id.action_delete:
                int size = checked.size();
                for (int i = 0; i < size; i++) {
                    int key = checked.keyAt(i);
                    boolean value = checked.get(key);
                    if (value) {
                        new TableHelper(fa).deleteSinglePaper(mGalItems.get(key)[1]);
                    }
                }
                actionMode.finish();
                new GalLoader().execute();
                break;
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode actionMode) {

        }
    });
    setHasOptionsMenu(true);

    if (!prefs.getBoolean(C.PREF_RANDOMIZER_FIRST_RUN_MAIN, false)) {
        prefs.edit().putBoolean(C.PREF_RANDOMIZER_FIRST_RUN_MAIN, true).commit();
        String title = getResources().getString(R.string.main_title_randomize);
        String message = getResources().getString(R.string.randomizer_description);
        new CustomDialogs().openFirstDialog(fa, title, message);
    }
    return ll;
}

From source file:net.kazzz.NFCTagWriter.java

/**
 * ISO15693 Tag????????/*from   w ww.  j  av  a2  s  .  c o m*/
 * 
 * 
 * @param btnWrite ?????
 * @param editWrite ??
 * @param listMemBlock?
 * @param useNDEFNDEF????
 */
private void prepareISO15693(final Button btnWrite, final EditText editWrite, final ListView listMemBlock) {
    final CheckBox chkUseNDEF = (CheckBox) findViewById(R.id.chk_useNDEF);

    //??
    ISO15693Tag ft = new ISO15693Tag(this.mNfcTag);
    try {
        SystemInformationResponse sysInfo = ft.getSystemInformation();
        if (sysInfo == null || sysInfo.hasError()) {
            throw new ISO15693Exception(
                    "ISO15693 ??????????? : "
                            + sysInfo.getErrorCode());
        }

        final MemorySizeInfo memInfo = sysInfo.getMemoryInfo();
        if (memInfo == null || memInfo.getNumberOfBlocks() == 0) {
            throw new ISO15693Exception("ISO15693 ?????????");
        }

        chkUseNDEF.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                listMemBlock.setEnabled(!chkUseNDEF.isChecked());
                listMemBlock.setClickable(!chkUseNDEF.isChecked());
                //NDEF??NDEF?????
                if (chkUseNDEF.isChecked()) {
                    btnWrite.setEnabled(true);
                    editWrite.setEnabled(true);
                    //NDEF????10byte?
                    editWrite.setHint((memInfo.getBlockSize() * memInfo.getNumberOfBlocks()) - 10
                            + "??");
                } else {
                    if (editWrite.getText().length() > 0) {
                        btnWrite.setEnabled(true);
                        editWrite.setEnabled(true);
                    } else {
                        btnWrite.setEnabled(false);
                        editWrite.setEnabled(false);
                    }
                    editWrite.setHint(memInfo.getBlockSize() + "??");
                }
            }
        });

        ReadMultipleBlocksResponse rbResp = ft.readMultipleBlocks((byte) 0, memInfo.getBlockSize(),
                memInfo.getNumberOfBlocks());
        if (rbResp == null || rbResp.hasError()) {
            throw new ISO15693Exception(
                    "ISO15693 ReadMultipleBlock?????" + sysInfo.getErrorCode());
        }

        //??????
        ViewHolder[] holders = new ViewHolder[memInfo.getNumberOfBlocks()];
        for (byte i = 0; i < memInfo.getNumberOfBlocks(); i++) {
            BlockData bd = rbResp.getBlockDatas()[i];

            ViewHolder holder = new ViewHolder();
            holder.blockName = "block " + i;
            holder.isWritable = !bd.isLocked();
            holder.accessMode = bd.isLocked() ? "(Lock)" : "(UnLock)";

            //??
            if (bd.getData() != null && bd.getData().length > 0) {
                holder.data = bd.getData();
            } else {
                holder.data = new byte[memInfo.getBlockSize()];
                for (int b = 0; b < holder.data.length; b++) {
                    holder.data[b] = 0x00; //??
                }
            }
            holders[i] = holder;
        }
        final LayoutInflater layoutInflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        //ArrayAdapter? (??
        ArrayAdapter<ViewHolder> adapter = new ArrayAdapter<ViewHolder>(this,
                android.R.layout.simple_list_item_multiple_choice, holders) {

            /* (non-Javadoc)
             * @see android.widget.BaseAdapter#isEnabled(int)
             */
            @Override
            public boolean isEnabled(int position) {
                SparseBooleanArray arry = listMemBlock.getCheckedItemPositions();
                if (arry.indexOfValue(true) != -1 && !arry.get(position) //??????? 
                        && (!arry.get(position - 1) && !arry.get(position + 1))) //????? 
                {
                    //???????
                    return false;
                }
                return super.isEnabled(position);
            }

            /* (non-Javadoc)
             * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
             */
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                if (null == convertView) {
                    convertView = layoutInflater.inflate(android.R.layout.simple_list_item_multiple_choice,
                            null);
                }

                ViewHolder holder = this.getItem(position);

                if (holder != null) {

                    StringBuilder sb = new StringBuilder();
                    sb.append(holder.blockName).append(" ").append(holder.accessMode).append("\n");
                    if (holder.data != null && holder.data.length > 0) {
                        //UTF-8?
                        Charset utfEncoding = Charset.forName("UTF-8");
                        sb.append(" : ").append(new String(holder.data, utfEncoding).trim());
                    }
                    ((TextView) convertView).setText(sb.toString());
                }
                return convertView;
            }

        };

        listMemBlock.setAdapter(adapter);

        // ?????  
        //listMemBlock.setItemsCanFocus(false);  
        //?
        listMemBlock.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        listMemBlock.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //?????
                SparseBooleanArray arry = listMemBlock.getCheckedItemPositions();

                ByteBuffer buff = ByteBuffer.allocate(arry.size() * memInfo.getBlockSize());
                int writableBlockCount = 0;
                for (int i = 0; i < arry.size(); i++) {
                    int key = arry.keyAt(i);
                    //?????
                    Object o = parent.getItemAtPosition(key);
                    if (o != null && o instanceof ViewHolder) {
                        ViewHolder holder = (ViewHolder) o;
                        if (holder.isWritable) {
                            writableBlockCount++;
                            if (holder.data != null && holder.data.length > 0) {
                                buff.put(holder.data);
                            }
                        }
                    }
                }
                //UTF-8?
                StringBuilder sb = new StringBuilder();
                Charset utfEncoding = Charset.forName("UTF-8");
                sb.append(new String(buff.array(), utfEncoding).trim());

                editWrite.setHint(memInfo.getBlockSize() * writableBlockCount + "??");

                if (writableBlockCount > 0) {
                    editWrite.setText(sb.toString().trim());
                    editWrite.setTag(arry);
                    //????????????
                    btnWrite.setEnabled(true);
                    editWrite.setEnabled(true);
                } else {
                    btnWrite.setEnabled(false);
                    editWrite.setEnabled(false);
                }

            }
        });
    } catch (ISO15693Exception e) {
        e.printStackTrace();
        Log.e(TAG, e.toString());
    }
}