Commit 5dcc24e0 authored by Grigory Fedorov's avatar Grigory Fedorov

Application: code formatting.

parent dadb28e3
...@@ -104,27 +104,26 @@ public class Application extends android.app.Application { ...@@ -104,27 +104,26 @@ public class Application extends android.app.Application {
notified = false; notified = false;
closing = false; closing = false;
closed = false; closed = false;
uiListeners = new HashMap<Class<? extends BaseUIListener>, Collection<? extends BaseUIListener>>(); uiListeners = new HashMap<>();
managerInterfaces = new HashMap<Class<? extends BaseManagerInterface>, Collection<? extends BaseManagerInterface>>(); managerInterfaces = new HashMap<>();
registeredManagers = new ArrayList<Object>(); registeredManagers = new ArrayList<>();
handler = new Handler(); handler = new Handler();
backgroundExecutor = Executors backgroundExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {
.newSingleThreadExecutor(new ThreadFactory() { @Override
@Override public Thread newThread(Runnable runnable) {
public Thread newThread(Runnable runnable) { Thread thread = new Thread(runnable, "Background executor service");
Thread thread = new Thread(runnable, thread.setPriority(Thread.MIN_PRIORITY);
"Background executor service"); thread.setDaemon(true);
thread.setPriority(Thread.MIN_PRIORITY); return thread;
thread.setDaemon(true); }
return thread; });
}
});
} }
public static Application getInstance() { public static Application getInstance() {
if (instance == null) if (instance == null) {
throw new IllegalStateException(); throw new IllegalStateException();
}
return instance; return instance;
} }
...@@ -156,17 +155,21 @@ public class Application extends android.app.Application { ...@@ -156,17 +155,21 @@ public class Application extends android.app.Application {
private void onClose() { private void onClose() {
LogManager.i(this, "onClose"); LogManager.i(this, "onClose");
for (Object manager : registeredManagers) for (Object manager : registeredManagers) {
if (manager instanceof OnCloseListener) if (manager instanceof OnCloseListener) {
((OnCloseListener) manager).onClose(); ((OnCloseListener) manager).onClose();
}
}
closed = true; closed = true;
} }
private void onUnload() { private void onUnload() {
LogManager.i(this, "onUnload"); LogManager.i(this, "onUnload");
for (Object manager : registeredManagers) for (Object manager : registeredManagers) {
if (manager instanceof OnUnloadListener) if (manager instanceof OnUnloadListener) {
((OnUnloadListener) manager).onUnload(); ((OnUnloadListener) manager).onUnload();
}
}
android.os.Process.killProcess(android.os.Process.myPid()); android.os.Process.killProcess(android.os.Process.myPid());
} }
...@@ -175,8 +178,9 @@ public class Application extends android.app.Application { ...@@ -175,8 +178,9 @@ public class Application extends android.app.Application {
* calls will always returns <code>false</code>. * calls will always returns <code>false</code>.
*/ */
public boolean doNotify() { public boolean doNotify() {
if (notified) if (notified) {
return false; return false;
}
notified = true; notified = true;
return true; return true;
} }
...@@ -187,8 +191,9 @@ public class Application extends android.app.Application { ...@@ -187,8 +191,9 @@ public class Application extends android.app.Application {
* @return * @return
*/ */
public void onServiceStarted() { public void onServiceStarted() {
if (serviceStarted) if (serviceStarted) {
return; return;
}
serviceStarted = true; serviceStarted = true;
LogManager.i(this, "onStart"); LogManager.i(this, "onStart");
loadFuture = backgroundExecutor.submit(new Callable<Void>() { loadFuture = backgroundExecutor.submit(new Callable<Void>() {
...@@ -203,9 +208,7 @@ public class Application extends android.app.Application { ...@@ -203,9 +208,7 @@ public class Application extends android.app.Application {
// Throw exceptions in UI thread if any. // Throw exceptions in UI thread if any.
try { try {
loadFuture.get(); loadFuture.get();
} catch (InterruptedException e) { } catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
onInitialized(); onInitialized();
...@@ -252,41 +255,41 @@ public class Application extends android.app.Application { ...@@ -252,41 +255,41 @@ public class Application extends android.app.Application {
super.onCreate(); super.onCreate();
Thread.currentThread().setPriority(Thread.MAX_PRIORITY); Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
ArrayList<String> contactManager = new ArrayList<String>(); ArrayList<String> contactManager = new ArrayList<>();
TypedArray contactManagerClasses = getResources().obtainTypedArray( TypedArray contactManagerClasses = getResources().obtainTypedArray(R.array.contact_managers);
R.array.contact_managers); for (int index = 0; index < contactManagerClasses.length(); index++) {
for (int index = 0; index < contactManagerClasses.length(); index++)
contactManager.add(contactManagerClasses.getString(index)); contactManager.add(contactManagerClasses.getString(index));
}
contactManagerClasses.recycle(); contactManagerClasses.recycle();
TypedArray managerClasses = getResources().obtainTypedArray( TypedArray managerClasses = getResources().obtainTypedArray(R.array.managers);
R.array.managers); for (int index = 0; index < managerClasses.length(); index++) {
for (int index = 0; index < managerClasses.length(); index++) if (isContactsSupported() || !contactManager.contains(managerClasses.getString(index))) {
if (isContactsSupported()
|| !contactManager
.contains(managerClasses.getString(index)))
try { try {
Class.forName(managerClasses.getString(index)); Class.forName(managerClasses.getString(index));
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
}
}
managerClasses.recycle(); managerClasses.recycle();
TypedArray tableClasses = getResources().obtainTypedArray( TypedArray tableClasses = getResources().obtainTypedArray(R.array.tables);
R.array.tables); for (int index = 0; index < tableClasses.length(); index++) {
for (int index = 0; index < tableClasses.length(); index++)
try { try {
Class.forName(tableClasses.getString(index)); Class.forName(tableClasses.getString(index));
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
}
tableClasses.recycle(); tableClasses.recycle();
} }
@Override @Override
public void onLowMemory() { public void onLowMemory() {
for (OnLowMemoryListener listener : getManagers(OnLowMemoryListener.class)) for (OnLowMemoryListener listener : getManagers(OnLowMemoryListener.class)) {
listener.onLowMemory(); listener.onLowMemory();
}
super.onLowMemory(); super.onLowMemory();
} }
...@@ -294,8 +297,9 @@ public class Application extends android.app.Application { ...@@ -294,8 +297,9 @@ public class Application extends android.app.Application {
* Service have been destroyed. * Service have been destroyed.
*/ */
public void onServiceDestroy() { public void onServiceDestroy() {
if (closed) if (closed) {
return; return;
}
onClose(); onClose();
runInBackground(new Runnable() { runInBackground(new Runnable() {
@Override @Override
...@@ -320,8 +324,6 @@ public class Application extends android.app.Application { ...@@ -320,8 +324,6 @@ public class Application extends android.app.Application {
/** /**
* Register new manager. * Register new manager.
*
* @param manager
*/ */
public void addManager(Object manager) { public void addManager(Object manager) {
registeredManagers.add(manager); registeredManagers.add(manager);
...@@ -332,16 +334,18 @@ public class Application extends android.app.Application { ...@@ -332,16 +334,18 @@ public class Application extends android.app.Application {
* @return List of registered manager. * @return List of registered manager.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends BaseManagerInterface> Collection<T> getManagers( public <T extends BaseManagerInterface> Collection<T> getManagers(Class<T> cls) {
Class<T> cls) { if (closed) {
if (closed)
return Collections.emptyList(); return Collections.emptyList();
}
Collection<T> collection = (Collection<T>) managerInterfaces.get(cls); Collection<T> collection = (Collection<T>) managerInterfaces.get(cls);
if (collection == null) { if (collection == null) {
collection = new ArrayList<T>(); collection = new ArrayList<>();
for (Object manager : registeredManagers) for (Object manager : registeredManagers) {
if (cls.isInstance(manager)) if (cls.isInstance(manager)) {
collection.add((T) manager); collection.add((T) manager);
}
}
collection = Collections.unmodifiableCollection(collection); collection = Collections.unmodifiableCollection(collection);
managerInterfaces.put(cls, collection); managerInterfaces.put(cls, collection);
} }
...@@ -361,9 +365,11 @@ public class Application extends android.app.Application { ...@@ -361,9 +365,11 @@ public class Application extends android.app.Application {
} }
private void clear() { private void clear() {
for (Object manager : registeredManagers) for (Object manager : registeredManagers) {
if (manager instanceof OnClearListener) if (manager instanceof OnClearListener) {
((OnClearListener) manager).onClear(); ((OnClearListener) manager).onClear();
}
}
} }
/** /**
...@@ -382,8 +388,7 @@ public class Application extends android.app.Application { ...@@ -382,8 +388,7 @@ public class Application extends android.app.Application {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private <T extends BaseUIListener> Collection<T> getOrCreateUIListeners( private <T extends BaseUIListener> Collection<T> getOrCreateUIListeners(Class<T> cls) {
Class<T> cls) {
Collection<T> collection = (Collection<T>) uiListeners.get(cls); Collection<T> collection = (Collection<T>) uiListeners.get(cls);
if (collection == null) { if (collection == null) {
collection = new ArrayList<T>(); collection = new ArrayList<T>();
...@@ -397,8 +402,9 @@ public class Application extends android.app.Application { ...@@ -397,8 +402,9 @@ public class Application extends android.app.Application {
* @return List of registered UI listeners. * @return List of registered UI listeners.
*/ */
public <T extends BaseUIListener> Collection<T> getUIListeners(Class<T> cls) { public <T extends BaseUIListener> Collection<T> getUIListeners(Class<T> cls) {
if (closed) if (closed) {
return Collections.emptyList(); return Collections.emptyList();
}
return Collections.unmodifiableCollection(getOrCreateUIListeners(cls)); return Collections.unmodifiableCollection(getOrCreateUIListeners(cls));
} }
...@@ -406,12 +412,8 @@ public class Application extends android.app.Application { ...@@ -406,12 +412,8 @@ public class Application extends android.app.Application {
* Register new listener. * Register new listener.
* <p/> * <p/>
* Should be called from {@link Activity#onResume()}. * Should be called from {@link Activity#onResume()}.
*
* @param cls
* @param listener
*/ */
public <T extends BaseUIListener> void addUIListener(Class<T> cls, public <T extends BaseUIListener> void addUIListener(Class<T> cls, T listener) {
T listener) {
getOrCreateUIListeners(cls).add(listener); getOrCreateUIListeners(cls).add(listener);
} }
...@@ -419,34 +421,27 @@ public class Application extends android.app.Application { ...@@ -419,34 +421,27 @@ public class Application extends android.app.Application {
* Unregister listener. * Unregister listener.
* <p/> * <p/>
* Should be called from {@link Activity#onPause()}. * Should be called from {@link Activity#onPause()}.
*
* @param cls
* @param listener
*/ */
public <T extends BaseUIListener> void removeUIListener(Class<T> cls, public <T extends BaseUIListener> void removeUIListener(Class<T> cls, T listener) {
T listener) {
getOrCreateUIListeners(cls).remove(listener); getOrCreateUIListeners(cls).remove(listener);
} }
/** /**
* Notify about error. * Notify about error.
*
* @param resourceId
*/ */
public void onError(final int resourceId) { public void onError(final int resourceId) {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
for (OnErrorListener onErrorListener : getUIListeners(OnErrorListener.class)) for (OnErrorListener onErrorListener : getUIListeners(OnErrorListener.class)) {
onErrorListener.onError(resourceId); onErrorListener.onError(resourceId);
}
} }
}); });
} }
/** /**
* Notify about error. * Notify about error.
*
* @param networkException
*/ */
public void onError(NetworkException networkException) { public void onError(NetworkException networkException) {
LogManager.exception(this, networkException); LogManager.exception(this, networkException);
...@@ -455,8 +450,6 @@ public class Application extends android.app.Application { ...@@ -455,8 +450,6 @@ public class Application extends android.app.Application {
/** /**
* Submits request to be executed in background. * Submits request to be executed in background.
*
* @param runnable
*/ */
public void runInBackground(final Runnable runnable) { public void runInBackground(final Runnable runnable) {
backgroundExecutor.submit(new Runnable() { backgroundExecutor.submit(new Runnable() {
...@@ -473,8 +466,6 @@ public class Application extends android.app.Application { ...@@ -473,8 +466,6 @@ public class Application extends android.app.Application {
/** /**
* Submits request to be executed in UI thread. * Submits request to be executed in UI thread.
*
* @param runnable
*/ */
public void runOnUiThread(final Runnable runnable) { public void runOnUiThread(final Runnable runnable) {
handler.post(runnable); handler.post(runnable);
...@@ -482,9 +473,6 @@ public class Application extends android.app.Application { ...@@ -482,9 +473,6 @@ public class Application extends android.app.Application {
/** /**
* Submits request to be executed in UI thread. * Submits request to be executed in UI thread.
*
* @param runnable
* @param delayMillis
*/ */
public void runOnUiThreadDelay(final Runnable runnable, long delayMillis) { public void runOnUiThreadDelay(final Runnable runnable, long delayMillis) {
handler.postDelayed(runnable, delayMillis); handler.postDelayed(runnable, delayMillis);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment