Skip to content

Commit a59c4ae

Browse files
committed
properly create Activity class + other fixes
Signed-off-by: Umair Khan <[email protected]>
1 parent fec636a commit a59c4ae

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

core/src/processing/core/PApplet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,15 +1128,15 @@ public String sketchRenderer() {
11281128
return renderer;
11291129
}
11301130

1131-
1131+
/*
11321132
public void orientation(int which) {
11331133
if (which == PORTRAIT) {
11341134
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
11351135
} else if (which == LANDSCAPE) {
11361136
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
11371137
}
11381138
}
1139-
1139+
*/
11401140

11411141
// public int sketchOrientation() {
11421142
// return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;

src/processing/mode/android/AndroidBuild.java

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import org.apache.tools.ant.Project;
2727
import org.apache.tools.ant.ProjectHelper;
2828

29-
import android.view.Window;
30-
import android.view.WindowManager;
3129
import processing.app.Base;
3230
import processing.app.Library;
3331
import processing.app.Preferences;
@@ -170,7 +168,7 @@ public File createProject() throws IOException, SketchException {
170168
final File resFolder = new File(tmpFolder, "res");
171169
writeRes(resFolder, sketchClassName);
172170
writeMainActivity(srcFolder);
173-
171+
174172

175173
// new location for SDK Tools 17: /opt/android/tools/proguard/proguard-android.txt
176174
// File proguardSrc = new File(sdk.getSdkFolder(), "tools/lib/proguard.cfg");
@@ -183,6 +181,8 @@ public File createProject() throws IOException, SketchException {
183181
// InputStream input = PApplet.createInput(getCoreZipLocation());
184182
// PApplet.saveStream(new File(libsFolder, "processing-core.jar"), input);
185183
Base.copyFile(coreZipFile, new File(libsFolder, "processing-core.jar"));
184+
185+
copySupportV4(libsFolder);
186186

187187
// Copy any imported libraries (their libs and assets),
188188
// and anything in the code folder contents to the project.
@@ -772,8 +772,8 @@ private void writeRes(File resFolder,
772772
File mainActivityLayoutFile = new File(layoutFolder, "main.xml");
773773
writeResLayoutMainActivity(mainActivityLayoutFile);
774774

775-
File mainFragmentLayoutFile = new File(layoutFolder, "fragment_main.xml");
776-
writeResLayoutMainFragment(mainFragmentLayoutFile);
775+
// File mainFragmentLayoutFile = new File(layoutFolder, "fragment_main.xml");
776+
// writeResLayoutMainFragment(mainFragmentLayoutFile);
777777

778778
// write the icon files
779779
File sketchFolder = sketch.getFolder();
@@ -866,9 +866,16 @@ private File mkdirs(final File parent, final String name) throws SketchException
866866
private void writeMainActivity(final File file) {
867867
File mainActivityFile = new File(file, "MainActivity.java");
868868
final PrintWriter writer = PApplet.createWriter(mainActivityFile);
869-
writer.println("package " + basePackage + "." + sketch.getName());
870-
writer.println("import android.app.Activity;");
871-
writer.println("public class TestActivity extends Activity {");
869+
writer.println("package " + basePackage + "." + sketch.getName() + ";");
870+
writer.println("import android.support.v4.app.FragmentActivity;");
871+
writer.println("import android.os.Bundle;");
872+
writer.println("import android.view.Window;");
873+
writer.println("import android.view.WindowManager;");
874+
writer.println("import android.widget.FrameLayout;");
875+
writer.println("import android.view.ViewGroup.LayoutParams;");
876+
writer.println("import android.support.v4.app.FragmentTransaction;");
877+
writer.println("import processing.core.PApplet;");
878+
writer.println("public class MainActivity extends FragmentActivity {");
872879
writer.println(" PApplet fragment;");
873880
writer.println(" @Override");
874881
writer.println(" protected void onCreate(Bundle savedInstanceState) {");
@@ -879,19 +886,19 @@ private void writeMainActivity(final File file) {
879886
+ "WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);");
880887
writer.println("window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,"
881888
+ "WindowManager.LayoutParams.FLAG_FULLSCREEN);");
882-
writer.println(" FrameLayout frame = new FrameLayout(this)");
889+
writer.println(" FrameLayout frame = new FrameLayout(this);");
883890
writer.println(" setContentView(frame, new LayoutParams(LayoutParams.MATCH_PARENT, "
884891
+ "LayoutParams.MATCH_PARENT));");
885892
writer.println(" if (savedInstanceState == null) {");
886-
writer.println(" fragment = new " + sketchClassName + "()");
887-
writer.println(" FragmentTransaction ft = getFragmentManager().beginTransaction();");
893+
writer.println(" fragment = new " + sketchClassName + "();");
894+
writer.println(" FragmentTransaction ft = getSupportFragmentManager().beginTransaction();");
888895
writer.println(" ft.add(frame.getId(), fragment).commit();");
889896
writer.println(" }");
890897
writer.println(" }");
891898
writer.println(" @Override");
892899
writer.println(" public void onBackPressed() {");
893900
writer.println(" fragment.onBackPressed();");
894-
writer.println(" super.onBackPressed()");
901+
writer.println(" super.onBackPressed();");
895902
writer.println(" }");
896903
writer.println("}");
897904
writer.flush();
@@ -912,7 +919,7 @@ private void writeResLayoutMainActivity(final File file) {
912919
writer.close();
913920
}
914921

915-
922+
/*
916923
private void writeResLayoutMainFragment(final File file) {
917924
final PrintWriter writer = PApplet.createWriter(file);
918925
writer.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
@@ -922,7 +929,7 @@ private void writeResLayoutMainFragment(final File file) {
922929
writer.println(" android:layout_height=\"fill_parent\">");
923930
writer.println("</LinearLayout>");
924931
}
925-
932+
*/
926933

927934
// This recommended to be a string resource so that it can be localized.
928935
// nah.. we're gonna be messing with it in the GUI anyway...
@@ -938,6 +945,22 @@ private void writeResLayoutMainFragment(final File file) {
938945
// writer.close();
939946
// }
940947

948+
949+
private void copySupportV4(File libsFolder) throws SketchException {
950+
File sdkLocation = sdk.getSdkFolder();
951+
File supportV4Jar = new File(sdkLocation, "extras/android/support/v4/android-support-v4.jar");
952+
if (!supportV4Jar.exists()) {
953+
SketchException sketchException =
954+
new SketchException("Please install support repository from SDK manager");
955+
throw sketchException;
956+
} else {
957+
try {
958+
Base.copyFile(supportV4Jar, new File(libsFolder, "android-support-v4.jar"));
959+
} catch (IOException e) {
960+
e.printStackTrace();
961+
}
962+
}
963+
}
941964

942965
/**
943966
* For each library, copy .jar and .zip files to the 'libs' folder,

0 commit comments

Comments
 (0)