Example usage for android.util Pair Pair

List of usage examples for android.util Pair Pair

Introduction

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

Prototype

public Pair(F first, S second) 

Source Link

Document

Constructor for a Pair.

Usage

From source file:Main.java

private static Pair<Integer, Integer> clampDimensions(int inWidth, int inHeight, int maxWidth, int maxHeight) {
    if (inWidth > maxWidth || inHeight > maxHeight) {
        final float aspectWidth, aspectHeight;

        if (inWidth == 0 || inHeight == 0) {
            aspectWidth = maxWidth;//from  w w  w. ja v a2s .co  m
            aspectHeight = maxHeight;
        } else if (inWidth >= inHeight) {
            aspectWidth = maxWidth;
            aspectHeight = (aspectWidth / inWidth) * inHeight;
        } else {
            aspectHeight = maxHeight;
            aspectWidth = (aspectHeight / inHeight) * inWidth;
        }

        return new Pair<>(Math.round(aspectWidth), Math.round(aspectHeight));
    } else {
        return new Pair<>(inWidth, inHeight);
    }
}

From source file:Main.java

public static LinkedList<Pair<Integer, String>> retrieveIntegerStringPairFromCursor(Cursor cursor,
        String integerColumnName, String stringColumnName) {
    LinkedList<Pair<Integer, String>> result = new LinkedList<Pair<Integer, String>>();

    if (null == cursor || 0 == cursor.getCount()) {
        return result;
    }/*from   w  w  w.ja v a 2s . c om*/

    try {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            Integer integerVal = cursor.getInt(cursor.getColumnIndex(integerColumnName));
            String stringVal = cursor.getString(cursor.getColumnIndexOrThrow(stringColumnName));
            result.add(new Pair(integerVal, stringVal));
        }
    } catch (Exception e) {
        //do nothing.
    } finally {
        cursor.close();
    }

    return result;
}

From source file:Main.java

public static Pair<Integer, String> parseInternalProviderData(String internalData) {
    String[] values = internalData.split(",", 2);
    if (values.length != 2) {
        throw new IllegalArgumentException(internalData);
    }/*from   w w  w  . j  a v  a2s .c om*/
    return new Pair<>(Integer.parseInt(values[0]), values[1]);
}

From source file:Main.java

public static Pair<Integer, String> parseProgramInternalProviderData(String internalData) {
    String[] values = internalData.split(",", 2);
    if (values.length != 2) {
        throw new IllegalArgumentException(internalData);
    }// w ww.j a  v  a2 s.co  m
    return new Pair<>(Integer.parseInt(values[0]), values[1]);
}

From source file:Main.java

public static Pair<Integer, Integer> getWindowDimensionsWithoutMargin(Context context,
        WindowManager windowManager, int dpMarginX, int dpMarginY) {

    dpMarginX *= 2;//from  www  .j  a v  a  2s  .com
    dpMarginY *= 2;

    DisplayMetrics displayMetrics = new DisplayMetrics();
    windowManager.getDefaultDisplay().getMetrics(displayMetrics);
    int screenWidth = displayMetrics.widthPixels;
    int screenHeight = displayMetrics.heightPixels;

    int marginX = dpToPx(context, dpMarginX);
    if (hasSoftNavigation(context)) {
        dpMarginY += 30;
    }
    int marginY = dpToPx(context, dpMarginY);

    Pair<Integer, Integer> dimensions = new Pair<Integer, Integer>(screenWidth - marginX,
            screenHeight - marginY);

    return dimensions;
}

From source file:Main.java

private static Pair<String, String> getAddressVersion0(Uri uri) {
    final String path = uri.getPath();
    if ((path == null) || (path.length() <= 1)) {
        // Missing address.
        return null;
    }//from  w  w w.  j  av  a2 s  .co m

    final String address = path.substring(1);
    if (!BluetoothAdapter.checkBluetoothAddress(address)) {
        // Invalid address.
        return null;
    }

    return new Pair<String, String>(address, null);
}

From source file:Main.java

public static Pair<BluetoothManager, BluetoothAdapter> checkBT(final Activity activity) {
    // Use this check to determine whether BLE is supported on the device. Then
    // you can selectively disable BLE-related features.
    if (!btleSupport(activity)) {
        return null;
    }//from  w  ww.  ja v  a 2  s . c o m
    final BluetoothManager bluetoothManager = (BluetoothManager) activity
            .getSystemService(Context.BLUETOOTH_SERVICE);
    BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();

    //On some devices this does not work
    try {
        bluetoothAdapter.getBluetoothLeScanner();
    } catch (NoSuchMethodError e) {
        return null;
    }

    // Ensures Bluetooth is available on the device and it is enabled. If not,
    // displays a dialog requesting user permission to enable Bluetooth.
    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        activity.startActivityForResult(enableBtIntent, 1);
        bluetoothAdapter = bluetoothManager.getAdapter();
    }
    return new Pair<BluetoothManager, BluetoothAdapter>(bluetoothManager, bluetoothAdapter);
}

From source file:Main.java

@SuppressLint("UseSparseArrays")
@SuppressWarnings("ResourceType")
public static Map<Integer, Pair<String, String>> obtainBadgeMap(Context context, @ArrayRes int id) {

    TypedArray badgeArray = context.getResources().obtainTypedArray(id);

    Map<Integer, Pair<String, String>> badgeMap = new HashMap<>();
    for (int i = 0; i < badgeArray.length(); i++) {
        int resId = badgeArray.getResourceId(i, -1);
        if (resId != -1) {
            TypedArray array = context.getResources().obtainTypedArray(resId);
            badgeMap.put(resId, new Pair<>(array.getString(0), array.getString(1)));
            array.recycle();//from w  ww  .j av a 2  s .c o m
        }
    }
    badgeArray.recycle();
    return badgeMap;
}

From source file:Main.java

public static Pair<String, String> getFieldNameAndNamespaceFromFullName(String fullName) {
    String namespace = null;/*from ww w .j a  v  a 2 s  .  c  o  m*/
    String fieldName;
    int index = fullName.indexOf(':');
    if (index != -1 && fullName.length() > index + 1) {
        namespace = fullName.substring(0, index);
        fieldName = fullName.substring(index + 1);
    } else {
        fieldName = fullName;
    }
    return new Pair<>(namespace, fieldName);
}

From source file:Main.java

/**
 * Get a list of initial and end calendar of months in the range received
 * @param cStart Calendar date to start/*from w w w.j  a va 2s  . c  o m*/
 * @param cEnd Calendar date to end
 * @return List<Pair<Calendar, Calendar>> list of calendars (initial and end)
 */
public static List<Pair<Calendar, Calendar>> getRangeInMonths(Calendar cStart, Calendar cEnd) {

    //generate the list
    List<Pair<Calendar, Calendar>> calendars = new ArrayList<>();

    //from the first calendar start adding a month until the actual calendar is after the end
    Calendar cActual = generateCalendar(cStart);
    cActual.set(Calendar.DAY_OF_MONTH, 1);
    Calendar c0;
    Calendar cF;

    while (cActual.compareTo(cEnd) < 0) {

        //calendar start
        if (calendars.size() == 0) {
            c0 = generateCalendar(cStart);
        } else {
            c0 = generateCalendar(cActual);
        }

        //increment a month
        cActual.add(Calendar.MONTH, 1);

        //calendar end
        if (cActual.after(cEnd)) {
            cF = generateCalendar(cEnd);
        } else {
            cF = generateCalendar(cActual);

            //remove 1 day to set the last day of the month
            cF.add(Calendar.DAY_OF_YEAR, -1);
        }

        //add the pair to the list
        calendars.add(new Pair<Calendar, Calendar>(c0, cF));
    }

    //return the list
    return calendars;
}