Example usage for android.content.res Resources obtainTypedArray

List of usage examples for android.content.res Resources obtainTypedArray

Introduction

In this page you can find the example usage for android.content.res Resources obtainTypedArray.

Prototype

@NonNull
public TypedArray obtainTypedArray(@ArrayRes int id) throws NotFoundException 

Source Link

Document

Return an array of heterogeneous values.

Usage

From source file:com.h6ah4i.android.materialshadowninepatch.MaterialShadowContainerView.java

private int[] getResourceIdArray(Resources resources, int id) {
    if (id == 0) {
        return null;
    }/* w  ww. j a va2s  . c om*/
    if (isInEditMode()) {
        return null;
    }

    TypedArray ta = resources.obtainTypedArray(id);
    int[] array = new int[ta.length()];

    for (int i = 0; i < array.length; i++) {
        array[i] = ta.getResourceId(i, 0);
    }

    ta.recycle();

    return array;
}

From source file:com.vonglasow.michael.satstat.ui.MapSectionFragment.java

/**
 * Updates internal data structures when the user's selection of location providers has changed.
 * @param providers The new set of location providers
 *//*from w w w  .j  a  v a 2s.  co m*/
public void onLocationProvidersChanged(Set<String> providers) {
    Context context = this.getContext();
    List<String> allProviders = mainActivity.locationManager.getAllProviders();
    ArrayList<String> removedProviders = new ArrayList<String>();

    for (String pr : providerLocations.keySet())
        if (!providers.contains(pr))
            removedProviders.add(pr);

    // remove cached locations and invalidators for providers which are no longer selected
    for (String pr : removedProviders) {
        providerLocations.remove(pr);
        providerInvalidators.remove(pr);
    }

    // ensure there is a cached location for each chosen provider (can be null)
    for (String pr : providers) {
        if ((allProviders.indexOf(pr) >= 0) && !providerLocations.containsKey(pr)) {
            Location location = new Location("");
            providerLocations.put(pr, location);
        }
    }

    // add overlays
    updateLocationProviderStyles();

    mapCircles = new HashMap<String, Circle>();
    mapMarkers = new HashMap<String, Marker>();

    Log.d(TAG, "Provider location cache: " + providerLocations.keySet().toString());

    Layers layers = mapMap.getLayerManager().getLayers();

    // remove all layers other than tile render layer from map
    for (Layer layer : layers)
        if (!(layer instanceof TileRendererLayer) && !(layer instanceof TileDownloadLayer)) {
            layer.onDestroy();
            layers.remove(layer);
        }

    for (String pr : providers) {
        // no invalidator for GPS, which is invalidated through GPS status
        if ((!pr.equals(LocationManager.GPS_PROVIDER)) && (providerInvalidators.get(pr)) == null) {
            final String provider = pr;
            final Context ctx = context;
            providerInvalidators.put(pr, new Runnable() {
                private String mProvider = provider;

                @Override
                public void run() {
                    Location location = providerLocations.get(mProvider);
                    if (location != null)
                        markLocationAsStale(location);
                    applyLocationProviderStyle(ctx, mProvider, Const.LOCATION_PROVIDER_GRAY);
                }
            });
        }

        String styleName = assignLocationProviderStyle(pr);
        LatLong latLong;
        float acc;
        boolean visible;
        if ((providerLocations.get(pr) != null) && (providerLocations.get(pr).getProvider() != "")) {
            latLong = new LatLong(providerLocations.get(pr).getLatitude(),
                    providerLocations.get(pr).getLongitude());
            if (providerLocations.get(pr).hasAccuracy())
                acc = providerLocations.get(pr).getAccuracy();
            else
                acc = 0;
            visible = true;
            if (isLocationStale(providerLocations.get(pr)))
                styleName = Const.LOCATION_PROVIDER_GRAY;
            Log.d("MainActivity", pr + " has " + latLong.toString());
        } else {
            latLong = new LatLong(0, 0);
            acc = 0;
            visible = false;
            Log.d("MainActivity", pr + " has no location, hiding");
        }

        // Circle layer
        Resources res = context.getResources();
        TypedArray style = res
                .obtainTypedArray(res.getIdentifier(styleName, "array", context.getPackageName()));
        Paint fill = AndroidGraphicFactory.INSTANCE.createPaint();
        float density = context.getResources().getDisplayMetrics().density;
        fill.setColor(style.getColor(Const.STYLE_FILL, R.color.circle_gray_fill));
        fill.setStyle(Style.FILL);
        Paint stroke = AndroidGraphicFactory.INSTANCE.createPaint();
        stroke.setColor(style.getColor(Const.STYLE_STROKE, R.color.circle_gray_stroke));
        stroke.setStrokeWidth(Math.max(1.5f * density, 1));
        stroke.setStyle(Style.STROKE);
        Circle circle = new Circle(latLong, acc, fill, stroke);
        mapCircles.put(pr, circle);
        layers.add(circle);
        circle.setVisible(visible);

        // Marker layer
        Drawable drawable = style.getDrawable(Const.STYLE_MARKER);
        Bitmap bitmap = AndroidGraphicFactory.convertToBitmap(drawable);
        Marker marker = new Marker(latLong, bitmap, 0, -bitmap.getHeight() * 10 / 24);
        mapMarkers.put(pr, marker);
        layers.add(marker);
        marker.setVisible(visible);
        style.recycle();
    }

    // move layers into view
    updateMap();
}

From source file:org.tomasdavid.vehicleroutingproblem.fragments.VrpFragment.java

private void showOnMap(VehicleRoutingSolution vrs) {

    googleMap.clear();//from www.j  a v  a2s  .c  o  m

    LatLngBounds.Builder builder = new LatLngBounds.Builder();

    for (Location location : vrs.getLocationList()) {
        LatLng position = new LatLng(location.getLatitude(), location.getLongitude());
        googleMap.addMarker(new MarkerOptions().position(position).title(location.getName()));
        builder.include(position);
    }

    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(builder.build(), 100);
    googleMap.animateCamera(cameraUpdate);

    for (Depot depot : vrs.getDepotList()) {
        googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(depot.getLocation().getLatitude(), depot.getLocation().getLongitude()))
                .title("Depot"));
    }

    int colorIndex = 0;
    Resources res = getContext().getResources();

    for (Vehicle vehicle : vrs.getVehicleList()) {
        Customer vehicleInfoCustomer = null;
        int longestNonDepotDistance = -1;
        for (Customer customer : vrs.getCustomerList()) {
            if (customer.getPreviousStandstill() != null && customer.getVehicle() == vehicle) {
                Location previousLocation = customer.getPreviousStandstill().getLocation();
                Location location = customer.getLocation();
                PolylineOptions polylineOptions = new PolylineOptions()
                        .add(new LatLng(previousLocation.getLatitude(), previousLocation.getLongitude()),
                                new LatLng(location.getLatitude(), location.getLongitude()))
                        .color(res.obtainTypedArray(R.array.vehicle_colors).getColor(colorIndex, 0));
                googleMap.addPolyline(polylineOptions);
                int distance = customer.getDistanceFromPreviousStandstill();

                if (customer.getPreviousStandstill() instanceof Customer) {
                    if (longestNonDepotDistance < distance) {
                        longestNonDepotDistance = distance;
                        vehicleInfoCustomer = customer;
                    }
                } else if (vehicleInfoCustomer == null) {
                    vehicleInfoCustomer = customer;
                }

                // draw route back to depot
                if (customer.getNextCustomer() == null) {
                    Location vehicleLocation = vehicle.getLocation();
                    googleMap.addPolyline(new PolylineOptions().add(
                            new LatLng(location.getLatitude(), location.getLongitude()),
                            new LatLng(vehicleLocation.getLatitude(), vehicleLocation.getLongitude())));
                }
            }
        }

        colorIndex = (colorIndex + 1) % res.obtainTypedArray(R.array.vehicle_colors).length();
    }
}

From source file:com.android.incallui.widget.multiwaveview.GlowPadView.java

private ArrayList<TargetDrawable> loadDrawableArray(int resourceId) {
    Resources res = getContext().getResources();
    TypedArray array = res.obtainTypedArray(resourceId);
    final int count = array.length();
    ArrayList<TargetDrawable> drawables = new ArrayList<TargetDrawable>(count);
    for (int i = 0; i < count; i++) {
        TypedValue value = array.peekValue(i);
        TargetDrawable target = new TargetDrawable(res, value != null ? value.resourceId : 0, 3);
        drawables.add(target);//from w  ww.ja v  a 2 s  .c om
    }
    array.recycle();
    return drawables;
}

From source file:com.vonglasow.michael.satstat.MainActivity.java

/**
 * Applies a style to the map overlays associated with a given location provider.
 * /* w w  w. j a  v a  2s .  c o m*/
 * This method changes the style (effectively, the color) of the circle and
 * marker overlays. Its main purpose is to switch the color of the overlays
 * between gray and the provider color.
 * 
 * @param context The context of the caller
 * @param provider The name of the location provider, as returned by
 * {@link LocationProvider.getName()}.
 * @param styleName The name of the style to apply. If it is null, the
 * default style for the provider as returned by 
 * assignLocationProviderStyle() is applied. 
 */
protected static void applyLocationProviderStyle(Context context, String provider, String styleName) {
    String sn = (styleName != null) ? styleName : assignLocationProviderStyle(provider);

    Boolean isStyleChanged = !sn.equals(providerAppliedStyles.get(provider));
    Boolean needsRedraw = false;

    Resources res = context.getResources();
    TypedArray style = res.obtainTypedArray(res.getIdentifier(sn, "array", context.getPackageName()));

    // Circle layer
    Circle circle = mapCircles.get(provider);
    if (circle != null) {
        circle.getPaintFill().setColor(style.getColor(STYLE_FILL, R.color.circle_gray_fill));
        circle.getPaintStroke().setColor(style.getColor(STYLE_STROKE, R.color.circle_gray_stroke));
        needsRedraw = isStyleChanged && circle.isVisible();
    }

    //Marker layer
    Marker marker = mapMarkers.get(provider);
    if (marker != null) {
        Drawable drawable = style.getDrawable(STYLE_MARKER);
        Bitmap bitmap = AndroidGraphicFactory.convertToBitmap(drawable);
        marker.setBitmap(bitmap);
        needsRedraw = needsRedraw || (isStyleChanged && marker.isVisible());
    }

    if (needsRedraw)
        mapMap.getLayerManager().redrawLayers();
    providerAppliedStyles.put(provider, sn);
    style.recycle();
}

From source file:com.vonglasow.michael.satstat.MainActivity.java

/**
 * Updates internal data structures when the user's selection of location providers has changed.
 * @param context// w  ww.  ja  v  a  2  s . co m
 */
protected static void updateLocationProviders(Context context) {
    // add overlays
    if (isMapViewReady) {
        Set<String> providers = mSharedPreferences.getStringSet(SettingsActivity.KEY_PREF_LOC_PROV,
                new HashSet<String>(Arrays.asList(
                        new String[] { LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER })));

        updateLocationProviderStyles();

        mapCircles = new HashMap<String, Circle>();
        mapMarkers = new HashMap<String, Marker>();

        ArrayList<String> removedProviders = new ArrayList<String>();
        for (String pr : providerInvalidators.keySet())
            if (!providers.contains(pr))
                removedProviders.add(pr);
        for (String pr : removedProviders)
            providerInvalidators.remove(pr);

        Log.d("MainActivity", "Provider location cache: " + providerLocations.keySet().toString());

        Layers layers = mapMap.getLayerManager().getLayers();

        // remove all layers other than tile render layer from map
        for (int i = 0; i < layers.size();)
            if ((layers.get(i) instanceof TileRendererLayer) || (layers.get(i) instanceof TileDownloadLayer)) {
                i++;
            } else {
                layers.remove(i);
            }

        for (String pr : providers) {
            // no invalidator for GPS, which is invalidated through GPS status
            if ((!pr.equals(LocationManager.GPS_PROVIDER)) && (providerInvalidators.get(pr)) == null) {
                final String provider = pr;
                final Context ctx = context;
                providerInvalidators.put(pr, new Runnable() {
                    private String mProvider = provider;

                    @Override
                    public void run() {
                        if (isMapViewReady) {
                            Location location = providerLocations.get(mProvider);
                            if (location != null)
                                markLocationAsStale(location);
                            applyLocationProviderStyle(ctx, mProvider, LOCATION_PROVIDER_GRAY);
                        }
                    }
                });
            }

            String styleName = assignLocationProviderStyle(pr);
            LatLong latLong;
            float acc;
            boolean visible;
            if ((providerLocations.get(pr) != null) && (providerLocations.get(pr).getProvider() != "")) {
                latLong = new LatLong(providerLocations.get(pr).getLatitude(),
                        providerLocations.get(pr).getLongitude());
                if (providerLocations.get(pr).hasAccuracy())
                    acc = providerLocations.get(pr).getAccuracy();
                else
                    acc = 0;
                visible = true;
                if (isLocationStale(providerLocations.get(pr)))
                    styleName = LOCATION_PROVIDER_GRAY;
                Log.d("MainActivity", pr + " has " + latLong.toString());
            } else {
                latLong = new LatLong(0, 0);
                acc = 0;
                visible = false;
                Log.d("MainActivity", pr + " has no location, hiding");
            }

            // Circle layer
            Resources res = context.getResources();
            TypedArray style = res
                    .obtainTypedArray(res.getIdentifier(styleName, "array", context.getPackageName()));
            Paint fill = AndroidGraphicFactory.INSTANCE.createPaint();
            fill.setColor(style.getColor(STYLE_FILL, R.color.circle_gray_fill));
            fill.setStyle(Style.FILL);
            Paint stroke = AndroidGraphicFactory.INSTANCE.createPaint();
            stroke.setColor(style.getColor(STYLE_STROKE, R.color.circle_gray_stroke));
            stroke.setStrokeWidth(4); // FIXME: make this DPI-dependent
            stroke.setStyle(Style.STROKE);
            Circle circle = new Circle(latLong, acc, fill, stroke);
            mapCircles.put(pr, circle);
            layers.add(circle);
            circle.setVisible(visible);

            // Marker layer
            Drawable drawable = style.getDrawable(STYLE_MARKER);
            Bitmap bitmap = AndroidGraphicFactory.convertToBitmap(drawable);
            Marker marker = new Marker(latLong, bitmap, 0, -bitmap.getHeight() * 9 / 20);
            mapMarkers.put(pr, marker);
            layers.add(marker);
            marker.setVisible(visible);
            style.recycle();
        }

        // move layers into view
        updateMap();
    }
}