Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-24570] Android: Fix screen density breaking changes + multi-window in Android 7.0 #8970

Merged
merged 3 commits into from
Jul 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions android/cli/commands/_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3633,6 +3633,21 @@ AndroidBuilder.prototype.generateAndroidManifest = function generateAndroidManif
});
}

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 = ['density'];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is density the only possible value in configChanges? Otherwise it would break other configs. And we should warn them to put it there if they don't do so far.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, there other values that go into configChanges but it is a requirement for consistency across different Android 7 devices for screen magnification. For activity.configChanges = ['density']; I used the same logic used elsewhere just in case a situation arises where configChanges is empty but the fact is screenSize will always be added to configChanges so it is kind of redundant in a way.

Also, I should make users aware that density is added by default?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "density" configChange should always be injected into the AndroidManifest.xml for every activity when targeting Android 7.0 or higher.

If we do not add this configChange setting, then a Titanium activity's UI will disappear if the end-user changes the "Display Size" under Android's main "Settings/Display" screen.

The reason this happens is because apps targeting Android 7.0 (or higher) are expected to handle "Display Size" changes dynamically. If we do not override the "density" configChange, then the activity's onCreate() method will be called, which will wipe the UI we were previously displaying (the same behavior you would see if you were missing the "screenOrientation" configChange and you rotated the screen). Apps targeting an older OS version (ie: API Level) are force-quit and restarted instead when the "Display Size" changes.

} else if (activity.configChanges.indexOf('density') == -1) {
activity.configChanges.push('density');
}
});
}

// add permissions
if (!this.tiapp['override-permissions']) {
Array.isArray(finalAndroidManifest['uses-permission']) || (finalAndroidManifest['uses-permission'] = []);
Expand Down
4 changes: 2 additions & 2 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|description|enabled|hasCode|hardwareAccelerated|icon|roundIcon|killAfterRestore|largeHeap|label|logo|manageSpaceActivity|name|permission|persistent|process|resizeableActivity|restoreAnyVersion|requiredAccountType|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|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
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 Expand Up @@ -120,4 +111,4 @@ public String getApiName()
{
return "Ti.Platform.DisplayCaps";
}
}
}
6 changes: 3 additions & 3 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 Down
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 Expand Up @@ -242,4 +237,4 @@ public String getNetmask()
return netmask;
}

}
}