44
44
import android .os .Handler ;
45
45
import android .text .format .Time ;
46
46
import android .util .*;
47
+ import android .view .LayoutInflater ;
47
48
import android .view .MotionEvent ;
48
49
import android .view .SurfaceHolder ;
49
50
import android .view .SurfaceView ;
51
+ import android .view .View ;
52
+ import android .view .ViewGroup ;
50
53
import android .view .ViewGroup .LayoutParams ;
51
- import android .view .Window ;
52
- import android .view .WindowManager ;
53
54
import android .widget .*;
55
+ import android .app .Fragment ;
54
56
55
57
import org .apache .http .client .HttpClient ;
56
58
import org .apache .http .client .methods .HttpGet ;
63
65
import processing .opengl .*;
64
66
65
67
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
+
67
75
/** The PGraphics renderer associated with this PApplet */
68
76
public PGraphics g ;
69
77
@@ -436,29 +444,24 @@ static public class RendererChangeException extends RuntimeException { }
436
444
//////////////////////////////////////////////////////////////
437
445
//////////////////////////////////////////////////////////////
438
446
447
+ /**
448
+ * Required empty constructor.
449
+ */
450
+ public PApplet () {}
439
451
440
452
/** Called with the activity is first created. */
441
453
@ SuppressWarnings ("unchecked" )
442
454
@ 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 ) {
448
457
449
- Window window = getWindow ( );
458
+ if ( DEBUG ) println ( "onCreateView() happening here: " + Thread . currentThread (). getName () );
450
459
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 ;
459
462
460
463
DisplayMetrics dm = new DisplayMetrics ();
461
- getWindowManager ().getDefaultDisplay ().getMetrics (dm );
464
+ activity . getWindowManager ().getDefaultDisplay ().getMetrics (dm );
462
465
displayWidth = dm .widthPixels ;
463
466
displayHeight = dm .heightPixels ;
464
467
@@ -500,11 +503,11 @@ public void onCreate(Bundle savedInstanceState) {
500
503
501
504
if (rendererName .equals (JAVA2D )) {
502
505
// JAVA2D renderer
503
- surfaceView = new SketchSurfaceView (this , sw , sh ,
506
+ surfaceView = new SketchSurfaceView (activity , sw , sh ,
504
507
(Class <? extends PGraphicsAndroid2D >) rendererClass );
505
508
} else if (PGraphicsOpenGL .class .isAssignableFrom (rendererClass )) {
506
509
// P2D, P3D, and any other PGraphicsOpenGL-based renderer
507
- surfaceView = new SketchSurfaceViewGL (this , sw , sh ,
510
+ surfaceView = new SketchSurfaceViewGL (activity , sw , sh ,
508
511
(Class <? extends PGraphicsOpenGL >) rendererClass );
509
512
} else {
510
513
// Anything else
@@ -541,22 +544,24 @@ public void onCreate(Bundle savedInstanceState) {
541
544
542
545
if (sw == displayWidth && sh == displayHeight ) {
543
546
// If using the full screen, don't embed inside other layouts
544
- window .setContentView (surfaceView );
547
+ // window.setContentView(surfaceView);
548
+ rootView = surfaceView ;
545
549
} else {
546
550
// If not using full screen, setup awkward view-inside-a-view so that
547
551
// the sketch can be centered on screen. (If anyone has a more efficient
548
552
// way to do this, please file an issue on Google Code, otherwise you
549
553
// can keep your "talentless hack" comments to yourself. Ahem.)
550
- RelativeLayout overallLayout = new RelativeLayout (this );
554
+ RelativeLayout overallLayout = new RelativeLayout (activity );
551
555
RelativeLayout .LayoutParams lp =
552
556
new RelativeLayout .LayoutParams (LayoutParams .WRAP_CONTENT ,
553
557
LayoutParams .WRAP_CONTENT );
554
558
lp .addRule (RelativeLayout .CENTER_IN_PARENT );
555
559
556
- LinearLayout layout = new LinearLayout (this );
560
+ LinearLayout layout = new LinearLayout (activity );
557
561
layout .addView (surfaceView , sketchWidth (), sketchHeight ());
558
562
overallLayout .addView (layout , lp );
559
- window .setContentView (overallLayout );
563
+ // window.setContentView(overallLayout);
564
+ rootView = overallLayout ;
560
565
}
561
566
562
567
/*
@@ -615,8 +620,7 @@ public void onCreate(Bundle savedInstanceState) {
615
620
redraw = true ; // draw this guy once
616
621
// firstMotion = true;
617
622
618
- Context context = getApplicationContext ();
619
- sketchPath = context .getFilesDir ().getAbsolutePath ();
623
+ sketchPath = activity .getFilesDir ().getAbsolutePath ();
620
624
621
625
// Looper.prepare();
622
626
handler = new Handler ();
@@ -625,6 +629,7 @@ public void onCreate(Bundle savedInstanceState) {
625
629
// println("done with loop() call, will continue...");
626
630
627
631
start ();
632
+ return rootView ;
628
633
}
629
634
630
635
@@ -636,7 +641,7 @@ public void onConfigurationChanged(Configuration newConfig) {
636
641
637
642
638
643
@ Override
639
- protected void onResume () {
644
+ public void onResume () {
640
645
super .onResume ();
641
646
642
647
// TODO need to bring back app state here!
@@ -651,7 +656,7 @@ protected void onResume() {
651
656
652
657
653
658
@ Override
654
- protected void onPause () {
659
+ public void onPause () {
655
660
super .onPause ();
656
661
657
662
// TODO need to save all application state here!
@@ -776,7 +781,7 @@ public SketchSurfaceView(Context context, int wide, int high,
776
781
// underlying surface is created and destroyed
777
782
surfaceHolder = getHolder ();
778
783
surfaceHolder .addCallback (this );
779
- surfaceHolder .setType (SurfaceHolder .SURFACE_TYPE_GPU );
784
+ // surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_GPU); // no longer needed.
780
785
781
786
// println("creating graphics");
782
787
if (clazz .equals (PGraphicsAndroid2D .class )) {
@@ -843,6 +848,7 @@ public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
843
848
844
849
@ Override
845
850
public void onWindowFocusChanged (boolean hasFocus ) {
851
+ super .onWindowFocusChanged (hasFocus );
846
852
surfaceWindowFocusChanged (hasFocus );
847
853
}
848
854
@@ -855,13 +861,15 @@ public boolean onTouchEvent(MotionEvent event) {
855
861
856
862
@ Override
857
863
public boolean onKeyDown (int code , android .view .KeyEvent event ) {
858
- return surfaceKeyDown (code , event );
864
+ surfaceKeyDown (code , event );
865
+ return super .onKeyDown (code , event );
859
866
}
860
867
861
868
862
869
@ Override
863
870
public boolean onKeyUp (int code , android .view .KeyEvent event ) {
864
- return surfaceKeyUp (code , event );
871
+ surfaceKeyUp (code , event );
872
+ return super .onKeyUp (code , event );
865
873
}
866
874
867
875
@@ -887,7 +895,7 @@ public SketchSurfaceViewGL(Context context, int wide, int high,
887
895
super (context );
888
896
889
897
// 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 );
891
899
final ConfigurationInfo configurationInfo = activityManager .getDeviceConfigurationInfo ();
892
900
final boolean supportsGLES2 = configurationInfo .reqGlEsVersion >= 0x20000 ;
893
901
@@ -1008,6 +1016,7 @@ public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
1008
1016
*/
1009
1017
@ Override
1010
1018
public void onWindowFocusChanged (boolean hasFocus ) {
1019
+ super .onWindowFocusChanged (hasFocus );
1011
1020
surfaceWindowFocusChanged (hasFocus );
1012
1021
// super.onWindowFocusChanged(hasFocus);
1013
1022
// focused = hasFocus;
@@ -1029,13 +1038,15 @@ public boolean onTouchEvent(MotionEvent event) {
1029
1038
1030
1039
@ Override
1031
1040
public boolean onKeyDown (int code , android .view .KeyEvent event ) {
1032
- return surfaceKeyDown (code , event );
1041
+ surfaceKeyDown (code , event );
1042
+ return super .onKeyDown (code , event );
1033
1043
}
1034
1044
1035
1045
1036
1046
@ Override
1037
1047
public boolean onKeyUp (int code , android .view .KeyEvent event ) {
1038
- return surfaceKeyUp (code , event );
1048
+ surfaceKeyUp (code , event );
1049
+ return super .onKeyUp (code , event );
1039
1050
}
1040
1051
1041
1052
@@ -1056,7 +1067,6 @@ public boolean onKeyUp(int code, android.view.KeyEvent event) {
1056
1067
* by Android as well.
1057
1068
*/
1058
1069
public void surfaceWindowFocusChanged (boolean hasFocus ) {
1059
- super .onWindowFocusChanged (hasFocus );
1060
1070
focused = hasFocus ;
1061
1071
if (focused ) {
1062
1072
focusGained ();
@@ -1078,17 +1088,17 @@ public boolean surfaceTouchEvent(MotionEvent event) {
1078
1088
}
1079
1089
1080
1090
1081
- public boolean surfaceKeyDown (int code , android .view .KeyEvent event ) {
1091
+ public void surfaceKeyDown (int code , android .view .KeyEvent event ) {
1082
1092
// System.out.println("got onKeyDown for " + code + " " + event);
1083
1093
nativeKeyEvent (event );
1084
- return super .onKeyDown (code , event );
1094
+ // return super.onKeyDown(code, event);
1085
1095
}
1086
1096
1087
1097
1088
- public boolean surfaceKeyUp (int code , android .view .KeyEvent event ) {
1098
+ public void surfaceKeyUp (int code , android .view .KeyEvent event ) {
1089
1099
// System.out.println("got onKeyUp for " + code + " " + event);
1090
1100
nativeKeyEvent (event );
1091
- return super .onKeyUp (code , event );
1101
+ // return super.onKeyUp(code, event);
1092
1102
}
1093
1103
1094
1104
@@ -1129,9 +1139,9 @@ final public int sketchWindowColor() {
1129
1139
1130
1140
public void orientation (int which ) {
1131
1141
if (which == PORTRAIT ) {
1132
- setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_PORTRAIT );
1142
+ activity . setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_PORTRAIT );
1133
1143
} else if (which == LANDSCAPE ) {
1134
- setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_LANDSCAPE );
1144
+ activity . setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_LANDSCAPE );
1135
1145
}
1136
1146
}
1137
1147
@@ -1804,6 +1814,12 @@ public PGraphics createGraphics(int iwidth, int iheight, String irenderer) {
1804
1814
} catch (InstantiationException e ) {
1805
1815
e .printStackTrace ();
1806
1816
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 ();
1807
1823
}
1808
1824
}
1809
1825
}
@@ -2903,7 +2919,6 @@ protected void handleKeyEvent(KeyEvent event) {
2903
2919
}
2904
2920
2905
2921
2906
- @ Override
2907
2922
public void onBackPressed () {
2908
2923
exit ();
2909
2924
}
@@ -4586,7 +4601,7 @@ public PFont createFont(String name, float size,
4586
4601
Typeface baseFont = null ;
4587
4602
4588
4603
if (lowerName .endsWith (".otf" ) || lowerName .endsWith (".ttf" )) {
4589
- AssetManager assets = getBaseContext () .getAssets ();
4604
+ AssetManager assets = activity .getAssets ();
4590
4605
baseFont = Typeface .createFromAsset (assets , name );
4591
4606
} else {
4592
4607
baseFont = (Typeface ) PFont .findNative (name );
@@ -5023,7 +5038,7 @@ public InputStream createInputRaw(String filename) {
5023
5038
*/
5024
5039
5025
5040
// Try the assets folder
5026
- AssetManager assets = getAssets ();
5041
+ AssetManager assets = activity . getAssets ();
5027
5042
try {
5028
5043
stream = assets .open (filename );
5029
5044
if (stream != null ) {
@@ -5061,10 +5076,9 @@ public InputStream createInputRaw(String filename) {
5061
5076
}
5062
5077
5063
5078
// Attempt to load the file more directly. Doesn't like paths.
5064
- Context context = getApplicationContext ();
5065
5079
try {
5066
5080
// MODE_PRIVATE is default, should we use something else?
5067
- stream = context .openFileInput (filename );
5081
+ stream = activity .openFileInput (filename );
5068
5082
if (stream != null ) {
5069
5083
return stream ;
5070
5084
}
@@ -5475,8 +5489,7 @@ public String sketchPath(String where) {
5475
5489
if (new File (where ).isAbsolute ()) return where ;
5476
5490
} catch (Exception e ) { }
5477
5491
5478
- Context context = getApplicationContext ();
5479
- return context .getFileStreamPath (where ).getAbsolutePath ();
5492
+ return activity .getFileStreamPath (where ).getAbsolutePath ();
5480
5493
}
5481
5494
5482
5495
@@ -7981,19 +7994,19 @@ public void updatePixels(int x1, int y1, int x2, int y2) {
7981
7994
7982
7995
7983
7996
private void tellPDE (final String message ) {
7984
- Log .i (getComponentName ().getPackageName (), "PROCESSING " + message );
7997
+ Log .i (activity . getComponentName ().getPackageName (), "PROCESSING " + message );
7985
7998
}
7986
7999
7987
8000
7988
8001
@ Override
7989
- protected void onStart () {
8002
+ public void onStart () {
7990
8003
tellPDE ("onStart" );
7991
8004
super .onStart ();
7992
8005
}
7993
8006
7994
8007
7995
8008
@ Override
7996
- protected void onStop () {
8009
+ public void onStop () {
7997
8010
tellPDE ("onStop" );
7998
8011
super .onStop ();
7999
8012
}
0 commit comments