-
Notifications
You must be signed in to change notification settings - Fork 1
/
AdendaSampleUnlockWidget.java
158 lines (133 loc) · 5.24 KB
/
AdendaSampleUnlockWidget.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.adenda.example;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import sdk.adenda.widget.AdendaUnlockInterface;
import sdk.adenda.widget.AdendaUnlockWidget;
import sdk.adenda.widget.DateTimeFragment;
public class AdendaSampleUnlockWidget extends RelativeLayout implements AdendaUnlockWidget
{
private AdendaUnlockInterface mAdendaUnlockInterface;
private Context mContext;
public AdendaSampleUnlockWidget(Context context) {
super(context);
mContext = context;
initialize();
}
public AdendaSampleUnlockWidget(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
mContext = context;
initialize();
}
public AdendaSampleUnlockWidget(Context context, AttributeSet attrs)
{
super(context, attrs);
mContext = context;
initialize();
}
private void initialize()
{
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.adenda_sample_unlock_layout, this);
}
@Override
public void setAdendaUnlockInterface(AdendaUnlockInterface adendaUnlockInterface) {
mAdendaUnlockInterface = adendaUnlockInterface;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
Button btnUnlock = (Button) findViewById(R.id.btn_learn_more);
if (btnUnlock != null)
btnUnlock.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if (mAdendaUnlockInterface != null)
mAdendaUnlockInterface.unlockAndEngage();
}
});
// Get Screen Height
Float screenHeight = getScreenHeight();
if (screenHeight == null)
return;
Float dateTimeSize = null;
View customContentHolder = findViewById(R.id.adenda_content_container);
// And make sure to scale Date/Time accordingly in order to keep at least 75% of the screen real-estate for ads
if (customContentHolder != null)
{
float btnHeight = pixelToDeviceIndependentPixel(getResources().getDimension(R.dimen.button_height));
float requiredSize = (float)(screenHeight * 0.25);
MarginLayoutParams customContentLayoutParams = (MarginLayoutParams)customContentHolder.getLayoutParams();
float margins = pixelToDeviceIndependentPixel(customContentLayoutParams.topMargin + customContentLayoutParams.bottomMargin);
// <-- Factor of DEFAULT_DATE_FACTOR is to account for the additional height of the date, which is typically ~0.27 * height of the clock.
// 40f to account for default padding in Text Views
// 25f to account for system bar
float timeSize = (requiredSize - btnHeight - margins - 40f - 25f) / (1 + DateTimeFragment.DEFAULT_DATE_FACTOR);
// Convert to Scaled Pixels (to take into account user preferences)
dateTimeSize = deviceIndependentPixelToScaledPixel(timeSize);
// Don't scale up, only scale down
if (dateTimeSize > DateTimeFragment.DEFAULT_TXT_SIZE)
dateTimeSize = null;
}
if (FragmentActivity.class.isInstance(mContext))
((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction().replace(R.id.managed_custom_date_time_fragment_holder, DateTimeFragment.newInstance(null, dateTimeSize, null, true, true)).commitAllowingStateLoss();
}
@Override
public void onNewImpression() {
if (mAdendaUnlockInterface == null)
return;
if (mAdendaUnlockInterface.getContentParams() != null)
Log.d(getClass().getSimpleName(), "Impression Recorded: " + mAdendaUnlockInterface.getContentParams().getType());
}
@Override
public void onClick() {
}
@Override
public boolean getMaintainAspectRatio() {
return true;
}
@Override
public boolean getDisableBackgroundAutofill() {
return true;
}
@Override
public void onCtaEnabledChanged(CTA_STATUS status) {
Button btnUnlock = (Button) findViewById(R.id.btn_learn_more);
if (btnUnlock == null)
return;
boolean bEnabled = status == CTA_STATUS.ENABLED;
btnUnlock.setEnabled(bEnabled);
if (status == CTA_STATUS.INTERACTIVE_OFF)
btnUnlock.setText("Interact With Ad Directly");
else if (status == CTA_STATUS.ENABLED)
btnUnlock.setText("Learn More");
}
private Float getScreenHeight()
{
WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
int heightPixels = metrics.heightPixels;
return (float)heightPixels / metrics.density;
}
private float pixelToDeviceIndependentPixel(float px) {
WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
return (float)px / metrics.density;
}
private float deviceIndependentPixelToScaledPixel(float px) {
WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
return (float)px * metrics.density / metrics.scaledDensity;
}
}