Fixed possible leak and its Android Lint warning in Java file.
authorPhilipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 05 May 2013 12:50:34 +0200
changeset 71454552268486f2
parent 7144 c700de9c8ed4
child 7146 1968d3a85e35
Fixed possible leak and its Android Lint warning in Java file.
android-project/src/org/libsdl/app/SDLActivity.java
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Sun May 05 12:47:44 2013 +0200
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Sun May 05 12:50:34 2013 +0200
     1.3 @@ -24,6 +24,7 @@
     1.4      SDL Activity
     1.5  */
     1.6  public class SDLActivity extends Activity {
     1.7 +    private static final String TAG = "SDL";
     1.8  
     1.9      // Keep track of the paused state
    1.10      public static boolean mIsPaused = false;
    1.11 @@ -113,25 +114,41 @@
    1.12      static final int COMMAND_UNUSED = 2;
    1.13      static final int COMMAND_TEXTEDIT_HIDE = 3;
    1.14  
    1.15 -    // Handler for the messages
    1.16 -    Handler commandHandler = new Handler() {
    1.17 +    /**
    1.18 +     * A Handler class for Messages from native SDL applications.
    1.19 +     * It uses current Activities as target (e.g. for the title).
    1.20 +     * static to prevent implicit references to enclosing object.
    1.21 +     */
    1.22 +    protected static class SDLCommandHandler extends Handler {
    1.23          @Override
    1.24          public void handleMessage(Message msg) {
    1.25 +            Context context = getContext();
    1.26 +            if (context == null) {
    1.27 +                Log.e(TAG, "error handling message, getContext() returned null");
    1.28 +                return;
    1.29 +            }
    1.30              switch (msg.arg1) {
    1.31              case COMMAND_CHANGE_TITLE:
    1.32 -                setTitle((String)msg.obj);
    1.33 +                if (context instanceof Activity) {
    1.34 +                    ((Activity) context).setTitle((String)msg.obj);
    1.35 +                } else {
    1.36 +                    Log.e(TAG, "error handling message, getContext() returned no Activity");
    1.37 +                }
    1.38                  break;
    1.39              case COMMAND_TEXTEDIT_HIDE:
    1.40                  if (mTextEdit != null) {
    1.41                      mTextEdit.setVisibility(View.GONE);
    1.42  
    1.43 -                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    1.44 +                    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    1.45                      imm.hideSoftInputFromWindow(mTextEdit.getWindowToken(), 0);
    1.46                  }
    1.47                  break;
    1.48              }
    1.49          }
    1.50 -    };
    1.51 +    }
    1.52 +
    1.53 +    // Handler for the messages
    1.54 +    Handler commandHandler = new SDLCommandHandler();
    1.55  
    1.56      // Send a message from the SDLMain thread
    1.57      void sendCommand(int command, Object data) {