Skip to content

Commit

Permalink
[TIMOB-24570] Android: Fix screen density breaking changes + multi-wi…
Browse files Browse the repository at this point in the history
…ndow in Android 7.0
  • Loading branch information
fmerzadyan authored and fmerzadyan committed Jun 30, 2017
1 parent 8b9d5ad commit 49942ca
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 57 deletions.
15 changes: 8 additions & 7 deletions android/cli/commands/_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3618,16 +3618,17 @@ AndroidBuilder.prototype.generateAndroidManifest = function generateAndroidManif
}
}, this);

// if the target sdk is Android 3.2 or newer, then we need to add 'screenSize' to
// the default AndroidManifest.xml's 'configChanges' attribute for all <activity>
// elements, otherwise changes in orientation will cause the app to restart
if (this.realTargetSDK >= 13) {
if (this.realTargetSDK >= 24 && !finalAndroidManifest.application.hasOwnProperty('resizeableActivity')) {
finalAndroidManifest.application.resizeableActivity = true;
}

if (this.realTargetSDK >= 24) {
Object.keys(finalAndroidManifest.application.activity).forEach(function (name) {
var activity = finalAndroidManifest.application.activity[name];
if (!activity.configChanges) {
activity.configChanges = ['screenSize'];
} else if (activity.configChanges.indexOf('screenSize') == -1) {
activity.configChanges.push('screenSize');
activity.configChanges = ['density'];
} else if (activity.configChanges.indexOf('density') == -1) {
activity.configChanges.push('density');
}
});
}
Expand Down
6 changes: 3 additions & 3 deletions android/cli/lib/AndroidManifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ var appc = require('node-appc'),
},

tagAttrs = {
'application': /^(allowTaskReparenting|allowBackup|backupAgent|debuggable|description|enabled|hasCode|hardwareAccelerated|icon|roundIcon|killAfterRestore|largeHeap|label|logo|manageSpaceActivity|name|permission|persistent|process|restoreAnyVersion|requiredAccountType|restrictedAccountType|supportsRtl|taskAffinity|testOnly|theme|uiOptions|vmSafeMode)$/,
'activity': /^(allowTaskReparenting|alwaysRetainTaskState|clearTaskOnLaunch|configChanges|enabled|excludeFromRecents|exported|finishOnTaskLaunch|hardwareAccelerated|icon|label|launchMode|multiprocess|name|noHistory|parentActivityName|permission|process|screenOrientation|stateNotNeeded|taskAffinity|theme|uiOptions|windowSoftInputMode)$/,
'application': /^(allowTaskReparenting|allowBackup|backupAgent|debuggable|density|description|enabled|hasCode|hardwareAccelerated|icon|killAfterRestore|largeHeap|label|logo|manageSpaceActivity|name|permission|persistent|process|restoreAnyVersion|requiredAccountType|resizeableActivity|restrictedAccountType|supportsRtl|taskAffinity|testOnly|theme|uiOptions|vmSafeMode)$/,
'activity': /^(allowTaskReparenting|alwaysRetainTaskState|clearTaskOnLaunch|configChanges|density|enabled|excludeFromRecents|exported|finishOnTaskLaunch|hardwareAccelerated|icon|label|launchMode|multiprocess|name|noHistory|parentActivityName|permission|process|resizeableActivity|screenOrientation|stateNotNeeded|taskAffinity|theme|uiOptions|windowSoftInputMode)$/,
'activity-alias': /^(enabled|exported|icon|label|name|permission|targetActivity)$/,
'data': /^(host|mimeType|path|pathPattern|pathPrefix|port|scheme)$/,
'intent-filter': /^(icon|label|priority)$/,
Expand Down Expand Up @@ -591,4 +591,4 @@ function AndroidManifest(filename) {
});

filename && this.load(filename);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@Kroll.proxy(parentModule=PlatformModule.class)
public class DisplayCapsProxy extends KrollProxy
{
private final DisplayMetrics dm;
private DisplayMetrics dm;
private SoftReference<Display> softDisplay;

public DisplayCapsProxy()
Expand Down Expand Up @@ -56,30 +56,21 @@ public int getPlatformHeight() {
public String getDensity() {
synchronized(dm) {
getDisplay().getMetrics(dm);
switch(dm.densityDpi) {
case DisplayMetrics.DENSITY_HIGH :
int dpi = dm.densityDpi;
if (dpi >= 560) { // DisplayMetrics.DENSITY_560
return "xxxhigh";
} else if (dpi >= 400) { // DisplayMetrics.DENSITY_400
return "xxhigh";
} else if (dpi >= 280) { // DisplayMetrics.DENSITY_280
return "xhigh";
} else if (dpi >= DisplayMetrics.DENSITY_HIGH) {
return "high";
case DisplayMetrics.DENSITY_MEDIUM :
return "medium";
case 213: //TV
} else if (dpi >= DisplayMetrics.DENSITY_TV) {
return "tvdpi";
case 280: //Introduce in API 22.
return "xhigh";
case 320:
return "xhigh";
case 400:
return "xxhigh";
case 480 :
return "xxhigh";
case 560:
return "xxxhigh";
case 640:
return "xxxhigh";
case DisplayMetrics.DENSITY_LOW :
return "low";
default :
} else if (dpi >= DisplayMetrics.DENSITY_MEDIUM) {
return "medium";
}
return "low";
}
}

Expand Down
8 changes: 4 additions & 4 deletions android/templates/build/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@

<activity android:name=".<%- classname %>Activity"
android:label="@string/app_name" android:theme="@style/Theme.Titanium"
android:configChanges="keyboardHidden|orientation">
android:configChanges="keyboardHidden|orientation|fontScale|screenSize|smallestScreenSize|screenLayout"/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name="org.appcelerator.titanium.TiActivity"
android:configChanges="keyboardHidden|orientation" />
android:configChanges="keyboardHidden|orientation|fontScale|screenSize|smallestScreenSize|screenLayout"/>
<activity android:name="org.appcelerator.titanium.TiTranslucentActivity"
android:configChanges="keyboardHidden|orientation"
android:configChanges="keyboardHidden|orientation|fontScale|screenSize|smallestScreenSize|screenLayout"/>
android:theme="@style/Theme.AppCompat.Translucent" />
<activity android:name="ti.modules.titanium.ui.android.TiPreferencesActivity" />

Expand All @@ -40,4 +40,4 @@

</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public class TiPlatformHelper extends APSAnalyticsHelper

public static float applicationScaleFactor = 1.0F;
public static int applicationLogicalDensity = DisplayMetrics.DENSITY_MEDIUM;
private static boolean applicationDisplayInfoInitialized = false;

private static class InstanceHolder
{
Expand All @@ -68,28 +67,24 @@ public void initialize()

public synchronized void intializeDisplayMetrics(Activity activity)
{
if (!applicationDisplayInfoInitialized) {
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);

// Note: this isn't public API, so there should be lots of error checking here
try {
Method gciMethod = Resources.class.getMethod("getCompatibilityInfo");
Object compatInfo = gciMethod.invoke(activity.getResources());
applicationScaleFactor = (Float) compatInfo.getClass().getField("applicationScale").get(compatInfo);
} catch (Exception e) {
Log.w(TAG, "Unable to get application scale factor, using reported density and its factor", Log.DEBUG_MODE);
}

if (applicationScaleFactor == 1.0f) {
applicationLogicalDensity = dm.densityDpi;
} else if (applicationScaleFactor > 1.0f) {
applicationLogicalDensity = DisplayMetrics.DENSITY_MEDIUM;
} else {
applicationLogicalDensity = DisplayMetrics.DENSITY_LOW;
}
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);

// Note: this isn't public API, so there should be lots of error checking here
try {
Method gciMethod = Resources.class.getMethod("getCompatibilityInfo");
Object compatInfo = gciMethod.invoke(activity.getResources());
applicationScaleFactor = (Float) compatInfo.getClass().getField("applicationScale").get(compatInfo);
} catch (Exception e) {
Log.w(TAG, "Unable to get application scale factor, using reported density and its factor", Log.DEBUG_MODE);
}

applicationDisplayInfoInitialized = true;
if (applicationScaleFactor == 1.0f) {
applicationLogicalDensity = dm.densityDpi;
} else if (applicationScaleFactor > 1.0f) {
applicationLogicalDensity = DisplayMetrics.DENSITY_MEDIUM;
} else {
applicationLogicalDensity = DisplayMetrics.DENSITY_LOW;
}
}

Expand Down

0 comments on commit 49942ca

Please sign in to comment.