Android port:
 * decouple RockboxFramebuffer resume/suspend behaviour from RockboxActivity
 * make RockboxFramebuffer native methods private
 * refactor attaching the RockboxFramebuffer view to RockboxActivity

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@28505 a1c6a512-1295-4272-9138-f99709370657
diff --git a/android/src/org/rockbox/RockboxActivity.java b/android/src/org/rockbox/RockboxActivity.java
index d8c1307..2e13a03 100644
--- a/android/src/org/rockbox/RockboxActivity.java
+++ b/android/src/org/rockbox/RockboxActivity.java
@@ -27,6 +27,7 @@
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
+import android.view.View;
 import android.view.ViewGroup;
 import android.view.Window;
 import android.view.WindowManager;
@@ -98,9 +99,7 @@
                 		loadingdialog.dismiss();
                 		if (rbservice.get_fb() == null)
                 		    throw new IllegalStateException("FB NULL");
-                        rbservice.set_activity(thisActivity);
-                		setContentView(rbservice.get_fb());
-                        rbservice.get_fb().invalidate();
+                        attachFramebuffer();
                     }
                 });
             }
@@ -112,26 +111,29 @@
         	rbservice = RockboxService.get_instance();
         return (rbservice!= null && rbservice.isRockboxRunning() == true);    	
     }
-    
+
+    private void attachFramebuffer()
+    {
+        View rbFramebuffer = rbservice.get_fb();
+        try {
+            setContentView(rbFramebuffer);
+        } catch (IllegalStateException e) {
+            /* we are already using the View,
+             * need to remove it and re-attach it */
+            ViewGroup g = (ViewGroup) rbFramebuffer.getParent();
+            g.removeView(rbFramebuffer);
+            setContentView(rbFramebuffer);
+        } finally {
+            rbFramebuffer.requestFocus();
+            rbservice.set_activity(this);
+        }
+    }
+
     public void onResume()
     {
-    	
         super.onResume();
         if (isRockboxRunning())
-        {
-            try {
-                setContentView(rbservice.get_fb());
-            } catch (IllegalStateException e) {
-                /* we are already using the View,
-                 * need to remove it and re-attach it */
-                ViewGroup g = (ViewGroup)rbservice.get_fb().getParent();
-                g.removeView(rbservice.get_fb());
-                setContentView(rbservice.get_fb());
-            } finally {
-            	rbservice.set_activity(this);
-                rbservice.get_fb().resume();
-            }
-        }
+            attachFramebuffer();
     }
     
     /* this is also called when the backlight goes off,
@@ -142,7 +144,6 @@
     {
         super.onPause();
         rbservice.set_activity(null);
-        rbservice.get_fb().suspend();
     }
     
     @Override
@@ -150,7 +151,6 @@
     {
         super.onStop();
         rbservice.set_activity(null);
-        rbservice.get_fb().suspend();
     }
     
     @Override
@@ -158,7 +158,6 @@
     {
         super.onDestroy();
         rbservice.set_activity(null);
-        rbservice.get_fb().suspend();
     }
     
     private HostCallback hostcallback = null;
diff --git a/android/src/org/rockbox/RockboxFramebuffer.java b/android/src/org/rockbox/RockboxFramebuffer.java
index 84974d6..0a60182 100644
--- a/android/src/org/rockbox/RockboxFramebuffer.java
+++ b/android/src/org/rockbox/RockboxFramebuffer.java
@@ -49,7 +49,6 @@
         setClickable(true);
         btm = Bitmap.createBitmap(lcd_width, lcd_height, Bitmap.Config.RGB_565);
         native_buf = native_fb;
-        requestFocus();
         media_monitor = new MediaButtonReceiver(c);
         media_monitor.register();
         /* the service needs to know the about us */
@@ -60,7 +59,7 @@
     {
         c.drawBitmap(btm, 0.0f, 0.0f, null);
     }
-    
+
     @SuppressWarnings("unused")
     private void java_lcd_update()
     {
@@ -112,29 +111,29 @@
         return buttonHandler(keyCode, false);
     }
 
-    /* the two below should only be called from the activity thread */
-    public void suspend()
-    {    /* suspend, Rockbox will not make any lcd updates */
-        set_lcd_active(0);
-    }
-    public void resume()
-    {    
-        /* Needed so we can catch KeyEvents */
-        setFocusable(true);
-        setFocusableInTouchMode(true);
-        setClickable(true);
-        requestFocus();
-        set_lcd_active(1);
-    }
-
     public void destroy()
     {
-        suspend();
+        set_lcd_active(0);
         media_monitor.unregister();
     }
 
-    public native void set_lcd_active(int active);
-    public native void touchHandler(boolean down, int x, int y);
-    public native boolean buttonHandler(int keycode, boolean state);
-    
+    @Override
+    protected void onWindowVisibilityChanged(int visibility)
+    {
+        super.onWindowVisibilityChanged(visibility);
+
+        switch (visibility) {
+            case VISIBLE:
+                set_lcd_active(1);
+                break;
+            case GONE:
+            case INVISIBLE:
+                set_lcd_active(0);
+                break;
+        }
+    }
+
+    private native void set_lcd_active(int active);
+    private native void touchHandler(boolean down, int x, int y);
+    private native boolean buttonHandler(int keycode, boolean state);
 }