4444import android .os .Handler ;
4545import android .text .format .Time ;
4646import android .util .*;
47+ import android .view .LayoutInflater ;
4748import android .view .MotionEvent ;
4849import android .view .SurfaceHolder ;
4950import android .view .SurfaceView ;
51+ import android .view .View ;
52+ import android .view .ViewGroup ;
5053import android .view .ViewGroup .LayoutParams ;
51- import android .view .Window ;
52- import android .view .WindowManager ;
5354import android .widget .*;
55+ import android .app .Fragment ;
5456
5557import org .apache .http .client .HttpClient ;
5658import org .apache .http .client .methods .HttpGet ;
6365import processing .opengl .*;
6466
6567
66- public class PApplet extends Activity implements PConstants , Runnable {
68+ public class PApplet extends Fragment implements PConstants , Runnable {
69+
70+ /**
71+ * The activity which holds this fragment.
72+ */
73+ private Activity activity ;
74+
6775 /** The PGraphics renderer associated with this PApplet */
6876 public PGraphics g ;
6977
@@ -436,29 +444,24 @@ static public class RendererChangeException extends RuntimeException { }
436444 //////////////////////////////////////////////////////////////
437445 //////////////////////////////////////////////////////////////
438446
447+ /**
448+ * Required empty constructor.
449+ */
450+ public PApplet () {}
439451
440452 /** Called with the activity is first created. */
441453 @ SuppressWarnings ("unchecked" )
442454 @ Override
443- public void onCreate (Bundle savedInstanceState ) {
444- super .onCreate (savedInstanceState );
445- // println("PApplet.onCreate()");
446-
447- if (DEBUG ) println ("onCreate() happening here: " + Thread .currentThread ().getName ());
455+ public View onCreateView (LayoutInflater inflater , ViewGroup container ,
456+ Bundle savedInstanceState ) {
448457
449- Window window = getWindow ( );
458+ if ( DEBUG ) println ( "onCreateView() happening here: " + Thread . currentThread (). getName () );
450459
451- // Take up as much area as possible
452- requestWindowFeature (Window .FEATURE_NO_TITLE );
453- window .setFlags (WindowManager .LayoutParams .FLAG_LAYOUT_IN_SCREEN ,
454- WindowManager .LayoutParams .FLAG_LAYOUT_IN_SCREEN );
455-
456- // This does the actual full screen work
457- window .setFlags (WindowManager .LayoutParams .FLAG_FULLSCREEN ,
458- WindowManager .LayoutParams .FLAG_FULLSCREEN );
460+ activity = getActivity ();
461+ View rootView ;
459462
460463 DisplayMetrics dm = new DisplayMetrics ();
461- getWindowManager ().getDefaultDisplay ().getMetrics (dm );
464+ activity . getWindowManager ().getDefaultDisplay ().getMetrics (dm );
462465 displayWidth = dm .widthPixels ;
463466 displayHeight = dm .heightPixels ;
464467
@@ -500,11 +503,11 @@ public void onCreate(Bundle savedInstanceState) {
500503
501504 if (rendererName .equals (JAVA2D )) {
502505 // JAVA2D renderer
503- surfaceView = new SketchSurfaceView (this , sw , sh ,
506+ surfaceView = new SketchSurfaceView (activity , sw , sh ,
504507 (Class <? extends PGraphicsAndroid2D >) rendererClass );
505508 } else if (PGraphicsOpenGL .class .isAssignableFrom (rendererClass )) {
506509 // P2D, P3D, and any other PGraphicsOpenGL-based renderer
507- surfaceView = new SketchSurfaceViewGL (this , sw , sh ,
510+ surfaceView = new SketchSurfaceViewGL (activity , sw , sh ,
508511 (Class <? extends PGraphicsOpenGL >) rendererClass );
509512 } else {
510513 // Anything else
@@ -541,22 +544,24 @@ public void onCreate(Bundle savedInstanceState) {
541544
542545 if (sw == displayWidth && sh == displayHeight ) {
543546 // If using the full screen, don't embed inside other layouts
544- window .setContentView (surfaceView );
547+ // window.setContentView(surfaceView);
548+ rootView = surfaceView ;
545549 } else {
546550 // If not using full screen, setup awkward view-inside-a-view so that
547551 // the sketch can be centered on screen. (If anyone has a more efficient
548552 // way to do this, please file an issue on Google Code, otherwise you
549553 // can keep your "talentless hack" comments to yourself. Ahem.)
550- RelativeLayout overallLayout = new RelativeLayout (this );
554+ RelativeLayout overallLayout = new RelativeLayout (activity );
551555 RelativeLayout .LayoutParams lp =
552556 new RelativeLayout .LayoutParams (LayoutParams .WRAP_CONTENT ,
553557 LayoutParams .WRAP_CONTENT );
554558 lp .addRule (RelativeLayout .CENTER_IN_PARENT );
555559
556- LinearLayout layout = new LinearLayout (this );
560+ LinearLayout layout = new LinearLayout (activity );
557561 layout .addView (surfaceView , sketchWidth (), sketchHeight ());
558562 overallLayout .addView (layout , lp );
559- window .setContentView (overallLayout );
563+ // window.setContentView(overallLayout);
564+ rootView = overallLayout ;
560565 }
561566
562567 /*
@@ -615,8 +620,7 @@ public void onCreate(Bundle savedInstanceState) {
615620 redraw = true ; // draw this guy once
616621// firstMotion = true;
617622
618- Context context = getApplicationContext ();
619- sketchPath = context .getFilesDir ().getAbsolutePath ();
623+ sketchPath = activity .getFilesDir ().getAbsolutePath ();
620624
621625// Looper.prepare();
622626 handler = new Handler ();
@@ -625,6 +629,7 @@ public void onCreate(Bundle savedInstanceState) {
625629// println("done with loop() call, will continue...");
626630
627631 start ();
632+ return rootView ;
628633 }
629634
630635
@@ -636,7 +641,7 @@ public void onConfigurationChanged(Configuration newConfig) {
636641
637642
638643 @ Override
639- protected void onResume () {
644+ public void onResume () {
640645 super .onResume ();
641646
642647 // TODO need to bring back app state here!
@@ -651,7 +656,7 @@ protected void onResume() {
651656
652657
653658 @ Override
654- protected void onPause () {
659+ public void onPause () {
655660 super .onPause ();
656661
657662 // TODO need to save all application state here!
@@ -776,7 +781,7 @@ public SketchSurfaceView(Context context, int wide, int high,
776781 // underlying surface is created and destroyed
777782 surfaceHolder = getHolder ();
778783 surfaceHolder .addCallback (this );
779- surfaceHolder .setType (SurfaceHolder .SURFACE_TYPE_GPU );
784+ // surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_GPU); // no longer needed.
780785
781786// println("creating graphics");
782787 if (clazz .equals (PGraphicsAndroid2D .class )) {
@@ -843,6 +848,7 @@ public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
843848
844849 @ Override
845850 public void onWindowFocusChanged (boolean hasFocus ) {
851+ super .onWindowFocusChanged (hasFocus );
846852 surfaceWindowFocusChanged (hasFocus );
847853 }
848854
@@ -855,13 +861,15 @@ public boolean onTouchEvent(MotionEvent event) {
855861
856862 @ Override
857863 public boolean onKeyDown (int code , android .view .KeyEvent event ) {
858- return surfaceKeyDown (code , event );
864+ surfaceKeyDown (code , event );
865+ return super .onKeyDown (code , event );
859866 }
860867
861868
862869 @ Override
863870 public boolean onKeyUp (int code , android .view .KeyEvent event ) {
864- return surfaceKeyUp (code , event );
871+ surfaceKeyUp (code , event );
872+ return super .onKeyUp (code , event );
865873 }
866874
867875
@@ -887,7 +895,7 @@ public SketchSurfaceViewGL(Context context, int wide, int high,
887895 super (context );
888896
889897 // Check if the system supports OpenGL ES 2.0.
890- final ActivityManager activityManager = (ActivityManager ) getSystemService (Context .ACTIVITY_SERVICE );
898+ final ActivityManager activityManager = (ActivityManager ) activity . getSystemService (Context .ACTIVITY_SERVICE );
891899 final ConfigurationInfo configurationInfo = activityManager .getDeviceConfigurationInfo ();
892900 final boolean supportsGLES2 = configurationInfo .reqGlEsVersion >= 0x20000 ;
893901
@@ -1008,6 +1016,7 @@ public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
10081016 */
10091017 @ Override
10101018 public void onWindowFocusChanged (boolean hasFocus ) {
1019+ super .onWindowFocusChanged (hasFocus );
10111020 surfaceWindowFocusChanged (hasFocus );
10121021// super.onWindowFocusChanged(hasFocus);
10131022// focused = hasFocus;
@@ -1029,13 +1038,15 @@ public boolean onTouchEvent(MotionEvent event) {
10291038
10301039 @ Override
10311040 public boolean onKeyDown (int code , android .view .KeyEvent event ) {
1032- return surfaceKeyDown (code , event );
1041+ surfaceKeyDown (code , event );
1042+ return super .onKeyDown (code , event );
10331043 }
10341044
10351045
10361046 @ Override
10371047 public boolean onKeyUp (int code , android .view .KeyEvent event ) {
1038- return surfaceKeyUp (code , event );
1048+ surfaceKeyUp (code , event );
1049+ return super .onKeyUp (code , event );
10391050 }
10401051
10411052
@@ -1056,7 +1067,6 @@ public boolean onKeyUp(int code, android.view.KeyEvent event) {
10561067 * by Android as well.
10571068 */
10581069 public void surfaceWindowFocusChanged (boolean hasFocus ) {
1059- super .onWindowFocusChanged (hasFocus );
10601070 focused = hasFocus ;
10611071 if (focused ) {
10621072 focusGained ();
@@ -1078,17 +1088,17 @@ public boolean surfaceTouchEvent(MotionEvent event) {
10781088 }
10791089
10801090
1081- public boolean surfaceKeyDown (int code , android .view .KeyEvent event ) {
1091+ public void surfaceKeyDown (int code , android .view .KeyEvent event ) {
10821092 // System.out.println("got onKeyDown for " + code + " " + event);
10831093 nativeKeyEvent (event );
1084- return super .onKeyDown (code , event );
1094+ // return super.onKeyDown(code, event);
10851095 }
10861096
10871097
1088- public boolean surfaceKeyUp (int code , android .view .KeyEvent event ) {
1098+ public void surfaceKeyUp (int code , android .view .KeyEvent event ) {
10891099 // System.out.println("got onKeyUp for " + code + " " + event);
10901100 nativeKeyEvent (event );
1091- return super .onKeyUp (code , event );
1101+ // return super.onKeyUp(code, event);
10921102 }
10931103
10941104
@@ -1129,9 +1139,9 @@ final public int sketchWindowColor() {
11291139
11301140 public void orientation (int which ) {
11311141 if (which == PORTRAIT ) {
1132- setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_PORTRAIT );
1142+ activity . setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_PORTRAIT );
11331143 } else if (which == LANDSCAPE ) {
1134- setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_LANDSCAPE );
1144+ activity . setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_LANDSCAPE );
11351145 }
11361146 }
11371147
@@ -1804,6 +1814,12 @@ public PGraphics createGraphics(int iwidth, int iheight, String irenderer) {
18041814 } catch (InstantiationException e ) {
18051815 e .printStackTrace ();
18061816 throw new RuntimeException (e .getMessage ());
1817+ } catch (java .lang .InstantiationException e ) {
1818+ // TODO Auto-generated catch block
1819+ e .printStackTrace ();
1820+ } catch (IllegalArgumentException e ) {
1821+ // TODO Auto-generated catch block
1822+ e .printStackTrace ();
18071823 }
18081824 }
18091825 }
@@ -2903,7 +2919,6 @@ protected void handleKeyEvent(KeyEvent event) {
29032919 }
29042920
29052921
2906- @ Override
29072922 public void onBackPressed () {
29082923 exit ();
29092924 }
@@ -4586,7 +4601,7 @@ public PFont createFont(String name, float size,
45864601 Typeface baseFont = null ;
45874602
45884603 if (lowerName .endsWith (".otf" ) || lowerName .endsWith (".ttf" )) {
4589- AssetManager assets = getBaseContext () .getAssets ();
4604+ AssetManager assets = activity .getAssets ();
45904605 baseFont = Typeface .createFromAsset (assets , name );
45914606 } else {
45924607 baseFont = (Typeface ) PFont .findNative (name );
@@ -5023,7 +5038,7 @@ public InputStream createInputRaw(String filename) {
50235038 */
50245039
50255040 // Try the assets folder
5026- AssetManager assets = getAssets ();
5041+ AssetManager assets = activity . getAssets ();
50275042 try {
50285043 stream = assets .open (filename );
50295044 if (stream != null ) {
@@ -5061,10 +5076,9 @@ public InputStream createInputRaw(String filename) {
50615076 }
50625077
50635078 // Attempt to load the file more directly. Doesn't like paths.
5064- Context context = getApplicationContext ();
50655079 try {
50665080 // MODE_PRIVATE is default, should we use something else?
5067- stream = context .openFileInput (filename );
5081+ stream = activity .openFileInput (filename );
50685082 if (stream != null ) {
50695083 return stream ;
50705084 }
@@ -5475,8 +5489,7 @@ public String sketchPath(String where) {
54755489 if (new File (where ).isAbsolute ()) return where ;
54765490 } catch (Exception e ) { }
54775491
5478- Context context = getApplicationContext ();
5479- return context .getFileStreamPath (where ).getAbsolutePath ();
5492+ return activity .getFileStreamPath (where ).getAbsolutePath ();
54805493 }
54815494
54825495
@@ -7981,19 +7994,19 @@ public void updatePixels(int x1, int y1, int x2, int y2) {
79817994
79827995
79837996 private void tellPDE (final String message ) {
7984- Log .i (getComponentName ().getPackageName (), "PROCESSING " + message );
7997+ Log .i (activity . getComponentName ().getPackageName (), "PROCESSING " + message );
79857998 }
79867999
79878000
79888001 @ Override
7989- protected void onStart () {
8002+ public void onStart () {
79908003 tellPDE ("onStart" );
79918004 super .onStart ();
79928005 }
79938006
79948007
79958008 @ Override
7996- protected void onStop () {
8009+ public void onStop () {
79978010 tellPDE ("onStop" );
79988011 super .onStop ();
79998012 }
0 commit comments