Example usage for android.util SparseArray size

List of usage examples for android.util SparseArray size

Introduction

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

Prototype

public int size() 

Source Link

Document

Returns the number of key-value mappings that this SparseArray currently stores.

Usage

From source file:com.dvn.vindecoder.ui.OCR.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PHOTO_REQUEST && resultCode == RESULT_OK) {
        launchMediaScanIntent();//from ww w .  ja  v  a 2  s .  com
        try {
            Bitmap bitmap = decodeBitmapUri(this, imageUri);
            if (detector.isOperational() && bitmap != null) {
                Frame frame = new Frame.Builder().setBitmap(bitmap).build();
                SparseArray<Barcode> barcodes = detector.detect(frame);
                for (int index = 0; index < barcodes.size(); index++) {
                    Barcode code = barcodes.valueAt(index);
                    scanResults.setText(scanResults.getText() + code.displayValue + "\n");

                    //Required only if you need to extract the type of barcode
                    int type = barcodes.valueAt(index).valueFormat;
                    switch (type) {
                    case Barcode.CONTACT_INFO:
                        Log.i(LOG_TAG, code.contactInfo.title);
                        break;
                    case Barcode.EMAIL:
                        Log.i(LOG_TAG, code.email.address);
                        break;
                    case Barcode.ISBN:
                        Log.i(LOG_TAG, code.rawValue);
                        break;
                    case Barcode.PHONE:
                        Log.i(LOG_TAG, code.phone.number);
                        break;
                    case Barcode.PRODUCT:
                        Log.i(LOG_TAG, code.rawValue);
                        break;
                    case Barcode.SMS:
                        Log.i(LOG_TAG, code.sms.message);
                        break;
                    case Barcode.TEXT:
                        Log.i(LOG_TAG, code.rawValue);
                        break;
                    case Barcode.URL:
                        Log.i(LOG_TAG, "url: " + code.url.url);
                        break;
                    case Barcode.WIFI:
                        Log.i(LOG_TAG, code.wifi.ssid);
                        break;
                    case Barcode.GEO:
                        Log.i(LOG_TAG, code.geoPoint.lat + ":" + code.geoPoint.lng);
                        break;
                    case Barcode.CALENDAR_EVENT:
                        Log.i(LOG_TAG, code.calendarEvent.description);
                        break;
                    case Barcode.DRIVER_LICENSE:
                        Log.i(LOG_TAG, code.driverLicense.licenseNumber);
                        break;
                    default:
                        Log.i(LOG_TAG, code.rawValue);
                        break;
                    }
                }
                if (barcodes.size() == 0) {
                    scanResults.setText("Scan Failed: Found nothing to scan");
                }
            } else {
                scanResults.setText("Could not set up the detector!");
            }
        } catch (Exception e) {
            Toast.makeText(this, "Failed to load Image", Toast.LENGTH_SHORT).show();
            Log.e(LOG_TAG, e.toString());
        }
    }
}

From source file:com.example.carrie.carrie_test1.scandrug.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scandrug);
    cameraView = (SurfaceView) findViewById(R.id.cameraView);
    cameraView.setZOrderMediaOverlay(true);
    holder = cameraView.getHolder();/*from   w  ww.  ja  v  a  2s.  c  o m*/
    barcode = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();
    if (!barcode.isOperational()) {
        Toast.makeText(getApplicationContext(), "Sorry ,Couldn't set up the detector", Toast.LENGTH_LONG)
                .show();
        this.finish();
    }

    cameraSource = new CameraSource.Builder(this, barcode).setFacing(CameraSource.CAMERA_FACING_BACK)
            .setRequestedFps(24).setAutoFocusEnabled(true).setRequestedPreviewSize(1920, 1024).build();
    cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                if (ContextCompat.checkSelfPermission(scandrug.this,
                        android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                    cameraSource.start(cameraView.getHolder());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {

        }
    });
    barcode.setProcessor(new Detector.Processor<Barcode>() {
        @Override
        public void release() {

        }

        @Override
        public void receiveDetections(Detector.Detections<Barcode> detections) {
            final SparseArray<Barcode> barcodes = detections.getDetectedItems();
            if (barcodes.size() > 0) {
                int count;
                Intent intent = new Intent();
                intent.putExtra("barcode", barcodes.valueAt(0));
                Log.d("code", barcodes.valueAt(0).displayValue);
                urlcode = barcodes.valueAt(0).displayValue;
                Log.d("code2", barcodes.valueAt(0).displayValue);
                geturl();
                setResult(RESULT_OK, intent);
                //                    cameraSource.stop();
                //                    addNormalDialogEvent();

            }
        }
    });

}

From source file:org.akvo.caddisfly.helper.TestConfigHelper.java

/**
 * Creates the json result containing the results for test.
 * @param testInfo information about the test
 * @param results the results for the test
 * @param color the color extracted//from ww  w.java  2s.c  o m
 * @param resultImageUrl the url of the image
 * @param groupingType type of grouping
 * @return the result in json format
 */
public static JSONObject getJsonResult(TestInfo testInfo, SparseArray<String> results, int color,
        String resultImageUrl, StripTest.GroupType groupingType) {

    JSONObject resultJson = new JSONObject();

    try {

        resultJson.put(SensorConstants.TYPE, SensorConstants.TYPE_NAME);
        resultJson.put(SensorConstants.NAME, testInfo.getName());
        resultJson.put(SensorConstants.UUID, testInfo.getId());

        JSONArray resultsJsonArray = new JSONArray();
        for (TestInfo.SubTest subTest : testInfo.getSubTests()) {
            JSONObject subTestJson = new JSONObject();
            subTestJson.put(SensorConstants.NAME, subTest.getDesc());
            subTestJson.put(SensorConstants.UNIT, subTest.getUnit());
            subTestJson.put(SensorConstants.ID, subTest.getId());

            // If a result exists for the sub test id then add it
            if (results.size() >= subTest.getId()) {
                subTestJson.put(SensorConstants.VALUE, results.get(subTest.getId()));
            }

            if (color > -1) {
                subTestJson.put("resultColor", Integer.toHexString(color & BIT_MASK));

                // Add calibration details to result
                subTestJson.put("calibratedDate", testInfo.getCalibrationDateString());
                subTestJson.put("reagentExpiry", testInfo.getExpiryDateString());
                subTestJson.put("reagentBatch", testInfo.getBatchNumber());

                JSONArray calibrationSwatches = new JSONArray();
                for (Swatch swatch : testInfo.getSwatches()) {
                    calibrationSwatches.put(Integer.toHexString(swatch.getColor() & BIT_MASK));
                }
                subTestJson.put("calibration", calibrationSwatches);
            }

            resultsJsonArray.put(subTestJson);

            if (groupingType == StripTest.GroupType.GROUP) {
                break;
            }
        }

        resultJson.put(SensorConstants.RESULT, resultsJsonArray);

        if (!resultImageUrl.isEmpty()) {
            resultJson.put(SensorConstants.IMAGE, resultImageUrl);
        }

        // Add current date to result
        resultJson.put("testDate", new SimpleDateFormat(SensorConstants.DATE_TIME_FORMAT, Locale.US)
                .format(Calendar.getInstance().getTime()));

        // Add user preference details to the result
        resultJson.put(SensorConstants.USER, TestConfigHelper.getUserPreferences());

        // Add app details to the result
        resultJson.put(SensorConstants.APP, TestConfigHelper.getAppDetails());

        // Add standard diagnostic details to the result
        resultJson.put(SensorConstants.DEVICE, TestConfigHelper.getDeviceDetails());

    } catch (JSONException e) {
        Timber.e(e);
    }
    return resultJson;
}

From source file:android.databinding.ViewDataBinding.java

/** @hide */
protected static <T> void setTo(SparseArray<T> list, int index, T value) {
    if (list == null || index < 0 || index >= list.size()) {
        return;//from  www. jav  a  2s . c om
    }
    list.put(index, value);
}

From source file:com.abid_mujtaba.fetchheaders.MainActivity.java

private void speak_emails() {
    for (int ii = 0; ii < Account.numberOfAccounts(); ii++) {
        Account account = Account.get(ii);
        SparseArray<Email> emails = account.emails();

        int num_emails = emails.size();

        if (account.num_unseen_emails() > 0) {
            speak(account); // Speak Account name

            for (int jj = 0; jj < num_emails; jj++) {
                Email email = emails.get(jj);

                if (!email.seen()) {
                    speak(email);//w  w  w . j  av  a2 s  .c  o  m
                }
            }
        }
    }
}

From source file:com.app.ecofriend.ecofriend.DisplayMessageActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PHOTO_REQUEST && resultCode == RESULT_OK) {
        launchMediaScanIntent();/* ww w. ja  va2  s  .  c o  m*/
        try {
            Bitmap bitmap = decodeBitmapUri(this, imageUri);
            if (detector.isOperational() && bitmap != null) {
                Frame frame = new Frame.Builder().setBitmap(bitmap).build();
                SparseArray<Barcode> barcodes = detector.detect(frame);
                for (int index = 0; index < barcodes.size(); index++) {
                    Barcode code = barcodes.valueAt(index);
                    scanResults.setText(
                            scanResults.getText() + "20 bottles recycled by " + code.displayValue + "\n");
                    TextView textView = (TextView) findViewById(R.id.textView3);
                    textView.setText("");
                    //Required only if you need to extract the type of barcode
                    int type = barcodes.valueAt(index).valueFormat;
                    switch (type) {
                    case Barcode.CONTACT_INFO:
                        Log.i(LOG_TAG, code.contactInfo.title);
                        break;
                    case Barcode.EMAIL:
                        Log.i(LOG_TAG, code.email.address);
                        break;
                    case Barcode.ISBN:
                        Log.i(LOG_TAG, code.rawValue);
                        break;
                    case Barcode.PHONE:
                        Log.i(LOG_TAG, code.phone.number);
                        break;
                    case Barcode.PRODUCT:
                        Log.i(LOG_TAG, code.rawValue);
                        break;
                    case Barcode.SMS:
                        Log.i(LOG_TAG, code.sms.message);
                        break;
                    case Barcode.TEXT:
                        Log.i(LOG_TAG, code.rawValue);
                        break;
                    case Barcode.URL:
                        Log.i(LOG_TAG, "url: " + code.url.url);
                        break;
                    case Barcode.WIFI:
                        Log.i(LOG_TAG, code.wifi.ssid);
                        break;
                    case Barcode.GEO:
                        Log.i(LOG_TAG, code.geoPoint.lat + ":" + code.geoPoint.lng);
                        break;
                    case Barcode.CALENDAR_EVENT:
                        Log.i(LOG_TAG, code.calendarEvent.description);
                        break;
                    case Barcode.DRIVER_LICENSE:
                        Log.i(LOG_TAG, code.driverLicense.licenseNumber);
                        break;
                    default:
                        Log.i(LOG_TAG, code.rawValue);
                        break;
                    }
                }
                if (barcodes.size() == 0) {
                    //scanResults.setText("Scan Failed: Found nothing to scan");
                }
            } else {
                scanResults.setText("Could not set up the detector!");
            }
        } catch (Exception e) {
            Toast.makeText(this, "Failed to load Image", Toast.LENGTH_SHORT).show();
            Log.e(LOG_TAG, e.toString());
        }
    }
}

From source file:raj.mharo.mharorajasthan.ScanNumberPlate.java

private void init() {
    mCameraView = (SurfaceView) findViewById(R.id.sv_surface);
    mTextView = (TextView) findViewById(R.id.tv_result);

    TextRecognizer textRecognizer = new TextRecognizer.Builder(this).build();

    if (!textRecognizer.isOperational()) {
        Log.d("RAVI", "NOT OPERATIONAL");
        Toast.makeText(ScanNumberPlate.this, "All dependencies not installed ", Toast.LENGTH_SHORT).show();
    } else {//from  w  w w.j a  va2s . c  o  m
        mCameraSource = new CameraSource.Builder(ScanNumberPlate.this, textRecognizer)
                .setFacing(CameraSource.CAMERA_FACING_BACK).setRequestedPreviewSize(1280, 1024)
                .setRequestedFps(2.0f).setAutoFocusEnabled(true).build();

        mCameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                try {

                    if (ActivityCompat.checkSelfPermission(ScanNumberPlate.this,
                            Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(ScanNumberPlate.this,
                                new String[] { Manifest.permission.CAMERA }, REQUESTCAMERA);
                        return;
                    }
                    mCameraSource.start(mCameraView.getHolder());
                } catch (Exception e) {
                    Log.d("RAVI", e.toString());
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                mCameraSource.stop();
            }
        });

        textRecognizer.setProcessor(new Detector.Processor<TextBlock>() {

            @Override
            public void release() {

            }

            @Override
            public void receiveDetections(Detector.Detections<TextBlock> detections) {
                final SparseArray<TextBlock> items = detections.getDetectedItems();
                if (items.size() != 0) {
                    mTextView.post(new Runnable() {
                        @Override
                        public void run() {
                            StringBuilder stringBuilder = new StringBuilder();
                            for (int i = 0; i < items.size(); i++) {
                                TextBlock item = items.valueAt(i);
                                stringBuilder.append(item.getValue());

                            }
                            mTextView.setText(stringBuilder.toString());
                        }
                    });
                }
            }
        });
    }
}

From source file:com.tmall.wireless.tangram.dataparser.concrete.PojoGroupBasicAdapter.java

@Override
protected void diffGroup(SparseArray<Card> added, SparseArray<Card> removed) {
    for (int i = 0, size = removed.size(); i < size; i++) {
        int key = removed.keyAt(i);
        Card card = removed.get(key);//from ww w.j ava2 s .  c o m
        if (card != null) {
            card.removed();
        }
    }

    for (int i = 0, size = added.size(); i < size; i++) {
        int key = added.keyAt(i);
        Card card = added.get(key);
        if (card != null) {
            card.added();
        }
    }
}

From source file:com.irccloud.android.fragment.ServerReorderFragment.java

public void onResume() {
    super.onResume();
    ArrayList<Server> servers = new ArrayList<Server>();
    SparseArray<Server> s = ServersList.getInstance().getServers();
    for (int i = 0; i < s.size(); i++) {
        servers.add(s.valueAt(i));/*ww w.  j av a  2 s. com*/
    }
    Collections.sort(servers);
    refresh(servers);
}

From source file:com.bilibili.lib.pageradapter.IDFragmentStatePagerAdapter.java

@Override
@CallSuper/*from   ww  w.j av  a2s . com*/
public void restoreState(Parcelable state, ClassLoader loader) {
    if (state != null) {
        Bundle bundle = (Bundle) state;
        bundle.setClassLoader(loader);
        mSavedState.clear();
        mFragments.clear();
        SparseArray<Fragment.SavedState> fss = bundle.getSparseParcelableArray(KEY_SAVED_STATES);
        if (fss != null) {
            for (int i = 0; i < fss.size(); i++) {
                mSavedState.put(fss.keyAt(i), fss.valueAt(i));
            }
        }
        Iterable<String> keys = bundle.keySet();
        for (String key : keys) {
            if (key.startsWith(KEY_PREFIX)) {
                Fragment f = mFragmentManager.getFragment(bundle, key);
                int id;
                try {
                    id = Integer.parseInt(key.substring(KEY_PREFIX.length()));
                } catch (NumberFormatException e) {
                    // should not happen!
                    throw new IllegalStateException("Can't find id at key " + key);
                }
                if (f != null) {
                    f.setMenuVisibility(false);
                    mFragments.put(id, f);
                } else {
                    Log.w(TAG, "Bad fragment at key " + key);
                }
            }
        }
    }
}