Skip to content

Commit

Permalink
new way of access to assets (2nd round)
Browse files Browse the repository at this point in the history
  • Loading branch information
MenoData committed Feb 7, 2017
1 parent e2fe8f7 commit 85b2c1a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public class ApplicationStarter {

//~ Statische Felder/Initialisierungen --------------------------------

private static final String VERSION = "v3.27-2016j";
private static final String VERSION = "v3.28-2016j";
private static final int RELEASE_YEAR = 2017;
private static final int RELEASE_MONTH = 1;
private static final int RELEASE_DAY = 8;
private static final int RELEASE_MONTH = 2;
private static final int RELEASE_DAY = 7;
private static final String TIME4A = "time4a";

private static final AtomicBoolean PREPARED = new AtomicBoolean(false);
Expand Down Expand Up @@ -126,7 +126,7 @@ public static void initialize(

long start = System.nanoTime();

prepareAssets(context); // initialize the class AndroidResourceLoader (must be called first)
prepareAssets(context, null); // initialize AndroidResourceLoader (must be called first)
registerReceiver(context.getApplicationContext()); // includes NPE-check

// ensures that class PlainDate is loaded before any further initialization
Expand Down Expand Up @@ -170,6 +170,7 @@ public void run() {
*
* @param application Android app
* @since 3.5
* @deprecated Use {@link #prepareAssets(Context, AssetLocation)}
*/
/*[deutsch]
* <p>Bereitet den allgemeinen Ressourcenzugriff vor und optimiert ihn. </p>
Expand All @@ -180,10 +181,46 @@ public void run() {
*
* @param application Android app
* @since 3.5
* @deprecated Use {@link #prepareAssets(Context, AssetLocation)}
*/
@Deprecated
public static void prepareResources(Application application) {

prepareAssets(application);
prepareAssets(application, null);

}

/**
* <p>Prepares and optimizes the access to all internal asset files. </p>
*
* <p>The context parameter must be specified while the second parameter
* is optional. </p>
*
* @param context Android context
* @param assetLocation environment of assets (optional, maybe {@code null})
* @since 3.28
*/
/*[deutsch]
* <p>Bereitet den allgemeinen <i>asset</i>-Zugriff vor und optimiert ihn. </p>
*
* <p>Der Android-Kontext mu&szlig; angegeben werden, w&auml;hrend der zweite
* Parameter optional ist. </p>
*
* @param context Android context
* @param assetLocation environment of assets (optional, maybe {@code null})
* @since 3.28
*/
public static void prepareAssets(
Context context,
AssetLocation assetLocation
) {

if (!PREPARED.getAndSet(true)) {
System.setProperty(
"net.time4j.base.ResourceLoader",
"net.time4j.android.spi.AndroidResourceLoader");
((AndroidResourceLoader) ResourceLoader.getInstance()).init(context, assetLocation);
}

}

Expand Down Expand Up @@ -218,17 +255,6 @@ public static void registerReceiver(Context context) {

}

private static void prepareAssets(Context context) {

if (!PREPARED.getAndSet(true)) {
System.setProperty(
"net.time4j.base.ResourceLoader",
"net.time4j.android.spi.AndroidResourceLoader");
((AndroidResourceLoader) ResourceLoader.getInstance()).init(context);
}

}

//~ Innere Klassen ----------------------------------------------------

private static class TimezoneChangedReceiver extends BroadcastReceiver {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* -----------------------------------------------------------------------
* Copyright © 2013-2016 Meno Hochschild, <http://www.menodata.de/>
* Copyright © 2013-2017 Meno Hochschild, <http://www.menodata.de/>
* -----------------------------------------------------------------------
* This file (AndroidResourceLoader.java) is part of project Time4J.
*
Expand All @@ -24,6 +24,7 @@
import android.content.Context;
import android.text.format.DateFormat;

import net.time4j.android.AssetLocation;
import net.time4j.base.ResourceLoader;
import net.time4j.calendar.service.GenericTextProviderSPI;
import net.time4j.engine.ChronoExtension;
Expand Down Expand Up @@ -107,18 +108,28 @@ public class AndroidResourceLoader
//~ Instanzvariablen --------------------------------------------------

private Context context = null;
private AssetLocation assetLocation = null;
private List<FormatPatternProvider> patterns = Collections.emptyList();

//~ Methoden ----------------------------------------------------------

/**
* Sets the application context.
* Sets the application context and the asset environment.
*
* @param context Android app or context
* @param context Android app or context
* @param assetLocation asset environment (optional)
*/
public void init(Context context) {
public void init(
Context context,
AssetLocation assetLocation
) {

if (context == null) {
throw new NullPointerException("Missing Android-context.");
}

this.context = context;
this.assetLocation = assetLocation;

FormatPatternProvider p = new AndroidFormatPatterns();
this.patterns = Collections.singletonList(p);
Expand Down Expand Up @@ -164,10 +175,15 @@ public InputStream load(
try {
if (uri == null) {
return null;
} else if (uri.isAbsolute() || (this.context == null)) {
} else if (uri.isAbsolute()) {
URLConnection conn = uri.toURL().openConnection();
conn.setUseCaches(false);
return conn.getInputStream();
} else if (this.assetLocation != null) {
return this.assetLocation.open(uri.toString());
} else if (context == null) {
throw new IllegalStateException(
"'ApplicationStarter.initialize(context)' must be called first at app start.");
} else {
return this.context.getAssets().open(uri.toString());
}
Expand Down

0 comments on commit 85b2c1a

Please sign in to comment.