Sat, 14 Feb 2015 21:29:32 +0100
Merge manually from fa9cb0e70440, conclusively use Lolipop API level 21.
michael@18 | 1 | /** |
michael@18 | 2 | * Copyright (c) 2012, Ben Fortuna |
michael@18 | 3 | * All rights reserved. |
michael@18 | 4 | * |
michael@18 | 5 | * Redistribution and use in source and binary forms, with or without |
michael@18 | 6 | * modification, are permitted provided that the following conditions |
michael@18 | 7 | * are met: |
michael@18 | 8 | * |
michael@18 | 9 | * o Redistributions of source code must retain the above copyright |
michael@18 | 10 | * notice, this list of conditions and the following disclaimer. |
michael@18 | 11 | * |
michael@18 | 12 | * o Redistributions in binary form must reproduce the above copyright |
michael@18 | 13 | * notice, this list of conditions and the following disclaimer in the |
michael@18 | 14 | * documentation and/or other materials provided with the distribution. |
michael@18 | 15 | * |
michael@18 | 16 | * o Neither the name of Ben Fortuna nor the names of any other contributors |
michael@18 | 17 | * may be used to endorse or promote products derived from this software |
michael@18 | 18 | * without specific prior written permission. |
michael@18 | 19 | * |
michael@18 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@18 | 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@18 | 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@18 | 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
michael@18 | 24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@18 | 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@18 | 26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@18 | 27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
michael@18 | 28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
michael@18 | 29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
michael@18 | 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@18 | 31 | */ |
michael@18 | 32 | package net.fortuna.ical4j.util; |
michael@18 | 33 | |
michael@18 | 34 | import java.io.InputStream; |
michael@18 | 35 | import java.net.URL; |
michael@18 | 36 | |
michael@18 | 37 | import android.util.Log; |
michael@18 | 38 | |
michael@18 | 39 | /** |
michael@18 | 40 | * @author fortuna |
michael@18 | 41 | * |
michael@18 | 42 | */ |
michael@18 | 43 | public class ResourceLoader { |
michael@18 | 44 | |
michael@18 | 45 | /** |
michael@18 | 46 | * Load a resource via the thread context classloader. If security permissions don't allow |
michael@18 | 47 | * this fallback to loading via current classloader. |
michael@18 | 48 | * @param name a resource name |
michael@18 | 49 | * @return a {@link URL} or null if resource is not found |
michael@18 | 50 | */ |
michael@18 | 51 | public static URL getResource(String name) { |
michael@18 | 52 | URL resource = null; |
michael@18 | 53 | try { |
michael@18 | 54 | // Hack to bootstrap a multithreaded class loader context |
michael@18 | 55 | if (Thread.currentThread().getContextClassLoader() == null) |
michael@18 | 56 | Thread.currentThread().setContextClassLoader(ResourceLoader.class.getClassLoader()); |
michael@18 | 57 | resource = Thread.currentThread().getContextClassLoader().getResource(name); |
michael@18 | 58 | |
michael@18 | 59 | if (resource == null) // Flawed build path for assets, try again |
michael@18 | 60 | resource = Thread.currentThread().getContextClassLoader().getResource("/" + name); |
michael@18 | 61 | } |
michael@18 | 62 | catch (SecurityException e) { |
michael@18 | 63 | Log.i("MSvB-Hack", "Unable to access context classloader, using default. " + e.getMessage()); |
michael@18 | 64 | } |
michael@18 | 65 | catch (Exception e) { |
michael@18 | 66 | Log.i("MSvB-Hack", "General context classloader error, using default. " + e.getMessage()); |
michael@18 | 67 | } |
michael@18 | 68 | if (resource == null) { |
michael@18 | 69 | resource = ResourceLoader.class.getResource("/" + name); |
michael@18 | 70 | } |
michael@18 | 71 | return resource; |
michael@18 | 72 | } |
michael@18 | 73 | |
michael@18 | 74 | /** |
michael@18 | 75 | * Load a resource via the thread context classloader. If security permissions don't allow |
michael@18 | 76 | * this fallback to loading via current classloader. |
michael@18 | 77 | * @param name a resource name |
michael@18 | 78 | * @return an {@link InputStream} or null if resource is not found |
michael@18 | 79 | */ |
michael@18 | 80 | public static InputStream getResourceAsStream(String name) { |
michael@18 | 81 | InputStream stream = null; |
michael@18 | 82 | try { |
michael@18 | 83 | // Hack to bootstrap a multithreaded class loader context |
michael@18 | 84 | if (Thread.currentThread().getContextClassLoader() == null) |
michael@18 | 85 | Thread.currentThread().setContextClassLoader(ResourceLoader.class.getClassLoader()); |
michael@18 | 86 | stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(name); |
michael@18 | 87 | if (stream == null) |
michael@18 | 88 | stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("/" + name); |
michael@18 | 89 | } |
michael@18 | 90 | catch (SecurityException e) { |
michael@18 | 91 | Log.i("MSvB-Hack", "Unable to access context classloader, using default. " + e.getMessage()); |
michael@18 | 92 | } |
michael@18 | 93 | catch (Exception e) { |
michael@18 | 94 | Log.i("MSvB-Hack", "General context classloader error, using default. " + e.getMessage()); |
michael@18 | 95 | } |
michael@18 | 96 | if (stream == null) { |
michael@18 | 97 | stream = ResourceLoader.class.getResourceAsStream("/" + name); |
michael@18 | 98 | } |
michael@18 | 99 | return stream; |
michael@18 | 100 | } |
michael@18 | 101 | } |