<commit_before>/**
 *  Copyright 2014-2016 CyberVision, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.kaaproject.kaa.demo.notification;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;

public class ImageCache {

    private final Map<String, Bitmap> imageMap;

    private static final String DEFAULT_IMAGE_KEY = "default";
    private static final String TAG = KaaNotificationApp.class.getSimpleName();
    public static final ImageCache cache = new ImageCache();

    private ImageCache() {
        imageMap = Collections.synchronizedMap(new WeakHashMap<String, Bitmap>());
        Bitmap bmp = BitmapFactory.decodeResource(KaaNotificationApp.getContext().getResources(), R.drawable.default_image);
        imageMap.put(DEFAULT_IMAGE_KEY, bmp);
    }

    public Bitmap getImage(String imageUrl) {
        Bitmap bmp;
        if (!imageMap.containsKey(imageUrl)) {
            try {
                URL url = new URL(imageUrl);
                bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                imageMap.put(imageUrl, bmp);
            } catch (MalformedURLException e) {
                bmp = imageMap.get(DEFAULT_IMAGE_KEY);
                Log.e(TAG, e.getMessage());
            } catch (IOException e) {
                bmp = imageMap.get(DEFAULT_IMAGE_KEY);
                Log.e(TAG, e.getMessage());
            }
        } else {
            return imageMap.get(imageUrl);
        }
        return bmp;
    }
}<commit_msg>APP-35: fix Android notification demo<commit_after>/**
 *  Copyright 2014-2016 CyberVision, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.kaaproject.kaa.demo.notification;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;

public class ImageCache {

    private final Map<String, Bitmap> imageMap;

    private static final String DEFAULT_IMAGE_KEY = "default";
    private static final String TAG = KaaNotificationApp.class.getSimpleName();
    public static final ImageCache cache = new ImageCache();

    private ImageCache() {
        imageMap = Collections.synchronizedMap(new WeakHashMap<String, Bitmap>());
        Bitmap bmp = BitmapFactory.decodeResource(KaaNotificationApp.getContext().getResources(), R.drawable.default_image);
        imageMap.put(DEFAULT_IMAGE_KEY, bmp);
    }

    public Bitmap getImage(String imageUrl) {
        Bitmap bmp;
        if (!imageMap.containsKey(imageUrl)) {
            try {
                URL url = new URL(imageUrl);
                bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                imageMap.put(imageUrl, bmp);
            } catch (IOException e) {
                bmp = imageMap.get(DEFAULT_IMAGE_KEY);
                Log.e(TAG, "Unable to get image by URL because of " + e.getClass().getSimpleName() + ":" + e.getMessage());
            }
        } else {
            return imageMap.get(imageUrl);
        }
        return bmp;
    }
}
{'fraction_non_alphanumeric': 0.09434822242479489, 'fraction_numerical': 0.005925250683682771, 'mean_word_length': 2.9152542372881354, 'pattern_counts': {'":': 1, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1923329', 'n_tokens_mistral': 1225, 'n_tokens_neox': 1178, 'n_words': 360}