Skip to content

Commit

Permalink
Android: removed StaticContext
Browse files Browse the repository at this point in the history
  • Loading branch information
nyxiscoo1 committed Jan 26, 2017
1 parent 2472a10 commit ef7934a
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
package com.shtrih.util;

import android.content.Context;

import com.shtrih.fiscalprinter.BuildConfig;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

import static junit.framework.Assert.assertEquals;

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
public class LocalizerTests {
@RunWith(AndroidJUnit4.class)
public class LocalizerAndroidUnitTest {

@Before
public void SetupContext(){
Context context = RuntimeEnvironment.application.getApplicationContext();

StaticContext.setContext(context);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
package com.shtrih.util;

import android.content.Context;

import com.shtrih.fiscalprinter.BuildConfig;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

import java.io.InputStream;

import static org.junit.Assert.assertTrue;

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
public class ResourceLoaderTests {
@RunWith(AndroidJUnit4.class)
public class ResourceLoaderAndroidUnitTest {

@Before
public void SetupContext(){
Context context = RuntimeEnvironment.application.getApplicationContext();

StaticContext.setContext(context);
public void SetupContext() {
}

@Test
Expand All @@ -34,13 +25,12 @@ public void testResourcesLoading() throws Exception {
canLoad("models.xml");
}

private void canLoad(final String resName) throws Exception
{
private void canLoad(final String resName) throws Exception {
InputStream commands = ResourceLoader.load(resName);

boolean isLoaded = commands != null;

if(commands != null)
if (commands != null)
commands.close();

assertTrue(resName, isLoaded);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,42 @@

import java.io.InputStream;

import android.content.Context;

public class ResourceLoader {

public static InputStream load(String fileName) throws Exception {
Context context = StaticContext.getContext();
return context.getResources().getAssets().open(fileName);
}
private static ResourceLoader instance;

private static ResourceLoader getInstance() {
if (instance == null) {
instance = new ResourceLoader();
}
return instance;
}

public static InputStream load(String fileName) throws Exception {
return getInstance().loadResource(fileName);
}

private final ClassLoader classLoader;

private ResourceLoader(){
this.classLoader = getClassLoaderOfClass(this.getClass());
}

private InputStream loadResource(String fileName) {
return this.classLoader.getResourceAsStream("assets/" + fileName);
}

private static ClassLoader getClassLoaderOfClass(final Class<?> clazz) {
ClassLoader cl = clazz.getClassLoader();
if (cl == null) {
return ClassLoader.getSystemClassLoader();
} else {
return cl;
}
}

// public static InputStream load(String fileName) throws Exception {
// Context context = StaticContext.getContext();
// return context.getResources().getAssets().open(fileName);
// }
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
package com.shtrih.util;

import android.content.Context;
import android.os.Environment;

import java.io.File;

public class StaticContext {
private static Context context = null;

public static Context getContext() {
if (context == null) {
throw new RuntimeException("Context is not set");
}
return context;
public static void setContext(Context context) {
SysUtils.setFilesPath(getFilesPath(context));
}

// NOTE: Application must call setContext to provide application instance to
// library
public static void setContext(Context value) {
context = value;
private static String getFilesPath(Context context)
{
File downloads = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);

if (downloads != null) {

if (downloads.exists())
return downloads.getAbsolutePath() + File.separator;

if (downloads.mkdirs())
return downloads.getAbsolutePath() + File.separator;
}

return context.getFilesDir().getAbsolutePath() + File.separator;
}
}
24 changes: 11 additions & 13 deletions Source/android/FptrServiceAndroid/src/com/shtrih/util/SysUtils.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
package com.shtrih.util;

import java.io.File;

import android.content.Context;
import android.os.Environment;

import java.io.File;

public class SysUtils {
public static String getFilesPath() {
Context context = StaticContext.getContext();

File downloads = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
private static String filesPath;

if (downloads != null) {
static void setFilesPath(String value) {
filesPath = value;
}

if (downloads.exists())
return downloads.getAbsolutePath() + File.separator;
public static String getFilesPath() {

if (downloads.mkdirs())
return downloads.getAbsolutePath() + File.separator;
}
if (filesPath != null)
return filesPath;

return context.getFilesDir().getAbsolutePath() + File.separator;
filesPath = Environment.getExternalStorageDirectory().getAbsoluteFile() + File.separator;
return filesPath;
}

public static void sleep(long millis) throws InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.shtrih.tinyjavapostester;

import android.content.Context;

import com.shtrih.util.SysUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
Expand All @@ -10,14 +14,11 @@
import jpos.loader.JposServiceLoader;
import jpos.util.JposPropertiesConst;

import com.shtrih.util.StaticContext;
import com.shtrih.util.SysUtils;

public class JposConfig {

public static void configure(String deviceName, String portName)
public static void configure(String deviceName, String portName, Context context)
throws Exception {
copyAsset("jpos.xml", SysUtils.getFilesPath() + "jpos.xml");
copyAsset("jpos.xml", SysUtils.getFilesPath() + "jpos.xml", context);
String fileURL = "file://" + SysUtils.getFilesPath() + "jpos.xml";
System.setProperty(
JposPropertiesConst.JPOS_POPULATOR_FILE_URL_PROP_NAME, fileURL);
Expand All @@ -37,12 +38,12 @@ public static void configure(String deviceName, String portName)
}
}

public static void copyAsset(String assetFileName, String destFile)
public static void copyAsset(String assetFileName, String destFile, Context context)
throws Exception {
InputStream is = null;
OutputStream os = null;
try {
is = StaticContext.getContext().getAssets().open(assetFileName);
is = context.getAssets().open(assetFileName);
os = new FileOutputStream(new File(destFile));
byte[] buffer = new byte[1024];
int length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

ConfigureLog4J.configure(getApplicationContext());
StaticContext.setContext(getApplicationContext());

printer = new ShtrihFiscalPrinter(new FiscalPrinter());
}
Expand All @@ -66,7 +65,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}

public void connectToDevice(final String address) throws Exception {
JposConfig.configure("ShtrihFptr", address);
JposConfig.configure("ShtrihFptr", address, getApplicationContext());

if (printer.getState() != JposConst.JPOS_S_CLOSED) {
printer.close();
Expand Down

0 comments on commit ef7934a

Please sign in to comment.