Skip to content

Commit

Permalink
feat: new android.extendAppCompatActivity build hint
Browse files Browse the repository at this point in the history
This build hint, when set true, will cause the CodenameOneActivity class to extend AppCompatActivity instead of Activity.

Why?

Because some newer APIs require AppCompatActivity.  Eg. The new photopicker #3727
  • Loading branch information
shannah committed Jan 6, 2024
1 parent 7ac8171 commit 0b94d45
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2723,6 +2723,9 @@ public NotifyActionBar(Activity activity, boolean show) {
@Override
public void run() {
activity.invalidateOptionsMenu();
if (activity.getActionBar() == null) {
return;
}
if (show) {
activity.getActionBar().show();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ public void onCreate(Bundle savedInstanceState) {

if (android.os.Build.VERSION.SDK_INT >= 11) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();
if (getActionBar() != null) {
getActionBar().hide();
}
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ public class AndroidGradleBuilder extends Executor {
public boolean PREFER_MANAGED_GRADLE=true;

private boolean useGradle8 = false;

private boolean extendAppCompatActivity = false;

private boolean useJava8SourceLevel = true;

private File gradleProjectDirectory;
Expand Down Expand Up @@ -473,6 +476,7 @@ public boolean build(File sourceZip, final BuildRequest request) throws BuildExc
boolean facebookSupported = request.getArg("facebook.appId", null) != null;
newFirebaseMessaging = request.getArg("android.newFirebaseMessaging", "false").equals("true");
useGradle8 = request.getArg("android.useGradle8", ""+(useGradle8 || newFirebaseMessaging || facebookSupported)).equals("true");
extendAppCompatActivity = request.getArg("android.extendAppCompatActivity", "false").equals("true");
useJava8SourceLevel = request.getArg("android.java8", ""+useJava8SourceLevel).equals("true");
if (useGradle8) {
getGradleJavaHome(); // will throw build exception if JAVA17_HOME is not set
Expand Down Expand Up @@ -1590,6 +1594,24 @@ public void usesClassMethod(String cls, String method) {
delTree(json);
}
}

if (extendAppCompatActivity) {
try {
replaceInFile(
new File(srcDir, "com/codename1/impl/android/CodenameOneActivity.java"),
"extends Activity",
"extends AppCompatActivity"
);
replaceInFile(
new File(srcDir, "com/codename1/impl/android/CodenameOneActivity.java"),
"import android.app.Activity;",
"import android.support.v7.app.AppCompatActivity;"
);
} catch (IOException ex) {
throw new BuildException("Failed to extend AppCompatActivity", ex);
}

}
if (!playServicesLocation) {
File fb = new File(srcDir, "com/codename1/location/AndroidLocationPlayServiceManager.java");
fb.delete();
Expand Down Expand Up @@ -2241,6 +2263,10 @@ public void usesClassMethod(String cls, String method) {
if (!xActivity.contains("android:exported")) {
xActivity += " android:exported=\"true\"";
}
String activityTheme = "@style/CustomTheme";
if (extendAppCompatActivity) {
activityTheme = "@@style/Theme.AppCompat.NoActionBar";
}
String manifestSource
= "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
+ "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
Expand Down Expand Up @@ -2268,7 +2294,7 @@ public void usesClassMethod(String cls, String method) {
+ googlePlayAdsMetaData
+ " <activity android:name=\"" + request.getMainClass() + "Stub\"\n"
+ xActivity
+ " android:theme=\"@style/CustomTheme\"\n"
+ " android:theme=\""+activityTheme+"\"\n"
+ " android:configChanges=\"orientation|keyboardHidden|screenSize|smallestScreenSize|screenLayout\"\n"
+ " android:launchMode=\""+launchMode+"\"\n"
+ " android:label=\"" + xmlizedDisplayName + "\" >\n"
Expand Down

0 comments on commit 0b94d45

Please sign in to comment.