Skip to content

Commit

Permalink
Merge pull request #105 from SpaceMadness/fix/android-resume-black-sc…
Browse files Browse the repository at this point in the history
…reen

Fix Android black screen while resuming
  • Loading branch information
weeeBox authored Dec 28, 2017
2 parents b298fc9 + f2a97a4 commit 99959b2
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static spacemadness.com.lunarconsole.console.BaseConsoleAdapter.DataSource;
import static spacemadness.com.lunarconsole.console.ConsoleNotifications.*;
import static spacemadness.com.lunarconsole.console.Notifications.*;
import static spacemadness.com.lunarconsole.utils.ObjectUtils.*;

public class ConsoleActionView extends AbstractConsoleView implements
Expand Down Expand Up @@ -93,7 +93,7 @@ public void onItemClick(AdapterView<?> parent, final View view, final int positi
}

// post notification
NotificationCenter.defaultCenter().postNotification(ACTION_SELECT, ACTION_SELECT_KEY_ACTION, action);
NotificationCenter.defaultCenter().postNotification(NOTIFICATION_ACTION_SELECT, NOTIFICATION_KEY_ACTION, action);

// visual feedback
// TODO: user color resource and animation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
import spacemadness.com.lunarconsole.utils.UIUtils;

import static android.view.inputmethod.EditorInfo.IME_ACTION_DONE;
import static spacemadness.com.lunarconsole.console.ConsoleNotifications.VARIABLE_SET;
import static spacemadness.com.lunarconsole.console.ConsoleNotifications.VARIABLE_SET_KEY_VARIABLE;
import static spacemadness.com.lunarconsole.console.Notifications.NOTIFICATION_VARIABLE_SET;
import static spacemadness.com.lunarconsole.console.Notifications.NOTIFICATION_KEY_VARIABLE;

public class VariableViewHolder extends ConsoleActionAdapter.ViewHolder<Variable> implements
CompoundButton.OnCheckedChangeListener, View.OnClickListener
Expand Down Expand Up @@ -304,7 +304,7 @@ public void onClick(View v)
void updateValue(String value)
{
variable.value = value;
NotificationCenter.defaultCenter().postNotification(VARIABLE_SET, VARIABLE_SET_KEY_VARIABLE, variable);
NotificationCenter.defaultCenter().postNotification(NOTIFICATION_VARIABLE_SET, NOTIFICATION_KEY_VARIABLE, variable);
updateUI();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package spacemadness.com.lunarconsole.console;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.os.Build;
import android.os.Bundle;

import java.lang.ref.WeakReference;

import spacemadness.com.lunarconsole.core.Destroyable;
import spacemadness.com.lunarconsole.core.NotificationCenter;

import static spacemadness.com.lunarconsole.console.Notifications.NOTIFICATION_ACTIVITY_STARTED;
import static spacemadness.com.lunarconsole.console.Notifications.NOTIFICATION_ACTIVITY_STOPPED;
import static spacemadness.com.lunarconsole.console.Notifications.NOTIFICATION_KEY_ACTIVITY;

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
class ActivityLifecycleHandler implements Application.ActivityLifecycleCallbacks, Destroyable
{
private final WeakReference<Application> applicationRef;

public ActivityLifecycleHandler(Application application)
{
if (application == null)
{
throw new IllegalArgumentException("Application is null");
}
applicationRef = new WeakReference<>(application);
application.registerActivityLifecycleCallbacks(this);
}

//region Activity Lifecycle Callbacks

@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState)
{
}

@Override
public void onActivityStarted(Activity activity)
{
NotificationCenter.defaultCenter()
.postNotification(NOTIFICATION_ACTIVITY_STARTED, NOTIFICATION_KEY_ACTIVITY, activity);
}

@Override
public void onActivityResumed(Activity activity)
{
}

@Override
public void onActivityPaused(Activity activity)
{
}

@Override
public void onActivityStopped(Activity activity)
{
NotificationCenter.defaultCenter()
.postNotification(NOTIFICATION_ACTIVITY_STOPPED, NOTIFICATION_KEY_ACTIVITY, activity);
}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState)
{
}

@Override
public void onActivityDestroyed(Activity activity)
{
}

//endregion

//region Destroyable

@Override
public void destroy()
{
Application application = getApplication();
if (application != null)
{
application.unregisterActivityLifecycleCallbacks(this);
}
}

//endregion

//region Helpers

private Application getApplication()
{
return applicationRef.get();
}

//endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package spacemadness.com.lunarconsole.console;

import android.app.Activity;
import android.app.Application;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
Expand All @@ -35,6 +36,7 @@

import spacemadness.com.lunarconsole.R;
import spacemadness.com.lunarconsole.core.Destroyable;
import spacemadness.com.lunarconsole.core.DispatchQueue;
import spacemadness.com.lunarconsole.core.Notification;
import spacemadness.com.lunarconsole.core.NotificationCenter;
import spacemadness.com.lunarconsole.debug.Assert;
Expand All @@ -46,14 +48,24 @@

import static android.widget.FrameLayout.LayoutParams;
import static spacemadness.com.lunarconsole.console.Console.Options;
import static spacemadness.com.lunarconsole.console.ConsoleLogType.*;
import static spacemadness.com.lunarconsole.utils.ThreadUtils.*;
import static spacemadness.com.lunarconsole.console.ConsoleLogType.isErrorType;
import static spacemadness.com.lunarconsole.console.Notifications.NOTIFICATION_ACTION_SELECT;
import static spacemadness.com.lunarconsole.console.Notifications.NOTIFICATION_ACTIVITY_STARTED;
import static spacemadness.com.lunarconsole.console.Notifications.NOTIFICATION_ACTIVITY_STOPPED;
import static spacemadness.com.lunarconsole.console.Notifications.NOTIFICATION_KEY_ACTION;
import static spacemadness.com.lunarconsole.console.Notifications.NOTIFICATION_KEY_ACTIVITY;
import static spacemadness.com.lunarconsole.console.Notifications.NOTIFICATION_KEY_VARIABLE;
import static spacemadness.com.lunarconsole.console.Notifications.NOTIFICATION_VARIABLE_SET;
import static spacemadness.com.lunarconsole.debug.Tags.CONSOLE;
import static spacemadness.com.lunarconsole.debug.Tags.GESTURES;
import static spacemadness.com.lunarconsole.debug.Tags.OVERLAY_VIEW;
import static spacemadness.com.lunarconsole.debug.Tags.PLUGIN;
import static spacemadness.com.lunarconsole.debug.Tags.WARNING_VIEW;
import static spacemadness.com.lunarconsole.ui.gestures.GestureRecognizer.OnGestureListener;
import static spacemadness.com.lunarconsole.debug.Tags.*;
import static spacemadness.com.lunarconsole.console.ConsoleNotifications.*;
import static spacemadness.com.lunarconsole.utils.ObjectUtils.*;
import static spacemadness.com.lunarconsole.utils.ThreadUtils.isRunningOnMainThread;
import static spacemadness.com.lunarconsole.utils.ThreadUtils.runOnUIThread;

public class ConsolePlugin implements Destroyable
public class ConsolePlugin implements NotificationCenter.OnNotificationListener, Destroyable
{
private static final String SCRIPT_MESSAGE_CONSOLE_OPEN = "console_open";
private static final String SCRIPT_MESSAGE_CONSOLE_CLOSE = "console_close";
Expand All @@ -63,6 +75,7 @@ public class ConsolePlugin implements Destroyable
private static ConsolePlugin instance;

private Console console;
private ActivityLifecycleHandler activityLifecycleHandler;
private final ActionRegistry actionRegistry;
private final ConsolePluginImp pluginImp;
private final String version;
Expand Down Expand Up @@ -645,6 +658,15 @@ private ConsolePlugin(Activity activity, UnitySettings unitySettings)
actionRegistry.setVariableSortingEnabled(unitySettings.editorSettings.sortVariables);

activityRef = new WeakReference<>(activity);
Application application = activity.getApplication();
if (application != null)
{
activityLifecycleHandler = new ActivityLifecycleHandler(application);
}
else
{
Log.e("Unable to resolve application object: plugin might not function properly");
}

gestureDetector = GestureRecognizerFactory.create(activity, unitySettings.gesture);
gestureDetector.setListener(new OnGestureListener()
Expand Down Expand Up @@ -674,6 +696,14 @@ public void destroy()
unregisterNotifications();

console.destroy();
if (activityLifecycleHandler != null)
{
activityLifecycleHandler.destroy();
}
if (console != null)
{
console.destroy();
}
entryDispatcher.cancelAll();

Log.d(PLUGIN, "Plugin destroyed");
Expand Down Expand Up @@ -1076,32 +1106,25 @@ private void sendNativeCallback(String name, Map<String, Object> data)

//region Notifications

private NotificationCenter.OnNotificationListener actionSelectListener;
private NotificationCenter.OnNotificationListener variableSetListener;

private void registerNotifications()
@Override
public void onNotification(Notification notification)
{
actionSelectListener = new NotificationCenter.OnNotificationListener()
switch (notification.getName())
{
@Override
public void onNotification(Notification notification)
case NOTIFICATION_ACTION_SELECT:
{
Action action = as(notification.getUserData(ACTION_SELECT_KEY_ACTION), Action.class);
Action action = notification.getUserData(NOTIFICATION_KEY_ACTION, Action.class);
Assert.IsNotNull(action);

if (action != null)
{
sendNativeCallback(SCRIPT_MESSAGE_ACTION, DictionaryUtils.createMap("id", action.actionId()));
}
break;
}
};

variableSetListener = new NotificationCenter.OnNotificationListener()
{
@Override
public void onNotification(Notification notification)
case NOTIFICATION_VARIABLE_SET:
{
Variable variable = as(notification.getUserData(VARIABLE_SET_KEY_VARIABLE), Variable.class);
Variable variable = notification.getUserData(NOTIFICATION_KEY_VARIABLE, Variable.class);
Assert.IsNotNull(variable);

if (variable != null)
Expand All @@ -1113,26 +1136,55 @@ public void onNotification(Notification notification)

sendNativeCallback(SCRIPT_MESSAGE_VARIABLE_SET, params);
}
break;
}
case NOTIFICATION_ACTIVITY_STOPPED:
{
// we need this as a workaround for the blank screen issue:
// https://github.com/SpaceMadness/lunar-unity-console/issues/104
Activity activity = notification.getUserData(NOTIFICATION_KEY_ACTIVITY, Activity.class);
if (getActivity() == activity && overlayDialog != null)
{
Log.d("Hiding overlay dialog");
overlayDialog.hide();
}
break;
}
case NOTIFICATION_ACTIVITY_STARTED:
{
// we need this as a workaround for the blank screen issue:
// https://github.com/SpaceMadness/lunar-unity-console/issues/104
Activity activity = notification.getUserData(NOTIFICATION_KEY_ACTIVITY, Activity.class);
if (getActivity() == activity && overlayDialog != null)
{
Log.d("Showing overlay dialog");
// we need to give activity a chance to initialize
DispatchQueue.mainQueue().dispatchAsync(new Runnable()
{
@Override
public void run()
{
overlayDialog.show();
}
});
}
break;
}
};
}
}

NotificationCenter.defaultCenter().addListener(ACTION_SELECT, actionSelectListener);
NotificationCenter.defaultCenter().addListener(VARIABLE_SET, variableSetListener);
private void registerNotifications()
{
NotificationCenter.defaultCenter()
.addListener(NOTIFICATION_ACTION_SELECT, this)
.addListener(NOTIFICATION_VARIABLE_SET, this)
.addListener(NOTIFICATION_ACTIVITY_STOPPED, this)
.addListener(NOTIFICATION_ACTIVITY_STARTED, this);
}

private void unregisterNotifications()
{
if (actionSelectListener != null)
{
NotificationCenter.defaultCenter().removeListener(actionSelectListener);
actionSelectListener = null;
}

if (variableSetListener != null)
{
NotificationCenter.defaultCenter().removeListener(variableSetListener);
variableSetListener = null;
}
NotificationCenter.defaultCenter().removeListener(this);
}

//endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// ConsoleNotifications.java
// Notifications.java
//
// Lunar Unity Mobile Console
// https://github.com/SpaceMadness/lunar-unity-console
Expand All @@ -25,11 +25,15 @@
* Created by alementuev on 12/20/16.
*/

public class ConsoleNotifications
public class Notifications
{
public static final String ACTION_SELECT = "ACTION_SELECT";
public static final String ACTION_SELECT_KEY_ACTION = "action";
public static final String NOTIFICATION_ACTIVITY_STARTED = "ACTIVITY_STARTED"; // { activity: Activity }
public static final String NOTIFICATION_ACTIVITY_STOPPED = "ACTIVITY_STOPPED"; // { activity: Activity }

public static final String VARIABLE_SET = "VARIABLE_SET";
public static final String VARIABLE_SET_KEY_VARIABLE = "variable";
public static final String NOTIFICATION_ACTION_SELECT = "ACTION_SELECT";
public static final String NOTIFICATION_VARIABLE_SET = "VARIABLE_SET";

public static final String NOTIFICATION_KEY_ACTIVITY = "activity";
public static final String NOTIFICATION_KEY_ACTION = "action";
public static final String NOTIFICATION_KEY_VARIABLE = "variable";
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import java.util.Map;

import spacemadness.com.lunarconsole.utils.ObjectUtils;

public class Notification
{
private final String name;
Expand Down Expand Up @@ -53,4 +55,9 @@ public Object getUserData(String key)
{
return userData != null ? userData.get(key) : null;
}

public <T> T getUserData(String key, Class<T> cls)
{
return ObjectUtils.as(getUserData(key), cls);
}
}
Loading

0 comments on commit 99959b2

Please sign in to comment.