Skip to content

Commit 78c34df

Browse files
feat: percents to activity
1 parent ceba130 commit 78c34df

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.app.Activity;
55
import android.content.Context;
66
import android.content.Intent;
7+
import android.content.res.Resources;
78
import android.graphics.Color;
89
import android.os.Bundle;
910
import android.view.Gravity;
@@ -79,6 +80,9 @@ private WebView createWebView(String uri, int width, int height) {
7980
*/
8081
protected static void showActivity(@NonNull Context context, @NonNull TransparentActivityConfig config) {
8182
assert context != null;
83+
int screenHeight = Resources.getSystem().getDisplayMetrics().heightPixels;
84+
int screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
85+
calculateSize(screenWidth, screenHeight, config);
8286

8387
Intent intent = new Intent(context, TransparentActivity.class);
8488
intent.putExtra(X_KEY, config.x);
@@ -89,4 +93,52 @@ protected static void showActivity(@NonNull Context context, @NonNull Transparen
8993

9094
context.startActivity(intent);
9195
}
96+
97+
private static void calculateSize(int screenWidth, int screenHeight, TransparentActivityConfig config) {
98+
if (config.xPercent != null) {
99+
config.x = (int) (screenWidth * adjustPercent(config.xPercent));
100+
}
101+
if (config.yPercent != null) {
102+
config.y = (int) (screenHeight * adjustPercent(config.yPercent));
103+
}
104+
105+
int remainingWidth = screenWidth - (config.x != null ? config.x : 0);
106+
int remainingHeight = screenHeight - (config.y != null ? config.y : 0);
107+
108+
if (config.widthPercent != null) {
109+
config.width = (int) (remainingWidth * adjustPercent(config.widthPercent));
110+
config.width = Math.min(config.width, remainingWidth);
111+
}
112+
113+
if (config.heightPercent != null) {
114+
config.height = (int) (remainingHeight * adjustPercent(config.heightPercent));
115+
config.height = Math.min(config.height, remainingHeight);
116+
}
117+
118+
//fallback to remaining screen
119+
if (config.width == null) {
120+
config.width = remainingWidth;
121+
}
122+
if (config.height == null) {
123+
config.height = remainingHeight;
124+
}
125+
126+
//fallback to top left corner
127+
if (config.x == null) {
128+
config.x = 0;
129+
}
130+
if (config.y == null) {
131+
config.y = 0;
132+
}
133+
}
134+
135+
private static Double adjustPercent(Double percent) {
136+
if (percent > 1 || percent < -1) {
137+
percent = percent % 1;
138+
}
139+
if (percent < 0) {
140+
percent = 1 + percent;
141+
}
142+
return percent;
143+
}
92144
}
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package ly.count.android.sdk;
22

33
class TransparentActivityConfig {
4-
final int x;
5-
final int y;
6-
final int width;
7-
final int height;
4+
Integer x;
5+
Integer y;
6+
Integer width;
7+
Integer height;
8+
Double widthPercent;
9+
Double heightPercent;
10+
Double xPercent;
11+
Double yPercent;
812
String url;
913

10-
TransparentActivityConfig(int x, int y, int width, int height) {
14+
TransparentActivityConfig(Integer x, Integer y, Integer width, Integer height) {
1115
this.x = x;
1216
this.y = y;
1317
this.width = width;
@@ -17,4 +21,11 @@ class TransparentActivityConfig {
1721
void setUrl(String url) {
1822
this.url = url;
1923
}
24+
25+
void setPercents(Double xPercent, Double yPercent, Double widthPercent, Double heightPercent) {
26+
this.xPercent = xPercent;
27+
this.yPercent = yPercent;
28+
this.widthPercent = widthPercent;
29+
this.heightPercent = heightPercent;
30+
}
2031
}

0 commit comments

Comments
 (0)