/
WelcomeActivity.java
277 lines (223 loc) · 8.88 KB
/
WelcomeActivity.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package com.stephentuso.welcome;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
public abstract class WelcomeActivity extends AppCompatActivity {
public static final String WELCOME_SCREEN_KEY = "welcome_screen_key";
protected ViewPager viewPager;
private WelcomeFragmentPagerAdapter adapter;
private WelcomeConfiguration configuration;
private WelcomeItemList responsiveItems = new WelcomeItemList();
@Override
protected void onCreate(Bundle savedInstanceState) {
configuration = configuration();
/* Passing null for savedInstanceState fixes issue with fragments in list not matching
the displayed ones after the screen was rotated. (Parallax animations would stop working)
TODO: Look into this more
*/
super.onCreate(null);
setContentView(R.layout.wel_activity_welcome);
adapter = new WelcomeFragmentPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.wel_view_pager);
viewPager.setAdapter(adapter);
responsiveItems = new WelcomeItemList();
// -- Inflate the bottom layout -- //
FrameLayout bottomFrame = (FrameLayout) findViewById(R.id.wel_bottom_frame);
View.inflate(this, configuration.getBottomLayoutResId(), bottomFrame);
if (configuration.getShowActionBarBackButton()) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
actionBar.setDisplayHomeAsUpEnabled(true);
}
// -- Add buttons to responsiveItems -- //
// addViewWrapper() will filter out any that aren't in the current layout
SkipButton skip = new SkipButton(findViewById(R.id.wel_button_skip));
addViewWrapper(skip, new View.OnClickListener() {
@Override
public void onClick(View v) {
completeWelcomeScreen();
}
});
PreviousButton prev = new PreviousButton(findViewById(R.id.wel_button_prev));
addViewWrapper(prev, new View.OnClickListener() {
@Override
public void onClick(View v) {
scrollToPreviousPage();
}
});
NextButton next = new NextButton(findViewById(R.id.wel_button_next));
addViewWrapper(next, new View.OnClickListener() {
@Override
public void onClick(View v) {
scrollToNextPage();
}
});
DoneButton done = new DoneButton(findViewById(R.id.wel_button_done));
addViewWrapper(done, new View.OnClickListener() {
@Override
public void onClick(View v) {
completeWelcomeScreen();
}
});
View firstBarButton = findViewById(R.id.wel_button_bar_first);
if (firstBarButton != null) {
firstBarButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onButtonBarFirstPressed();
}
});
}
View secondBarButton = findViewById(R.id.wel_button_bar_second);
if (secondBarButton != null) {
secondBarButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onButtonBarSecondPressed();
}
});
}
WelcomeViewPagerIndicator indicator = (WelcomeViewPagerIndicator) findViewById(R.id.wel_pager_indicator);
if (indicator != null) {
responsiveItems.add(indicator);
}
WelcomeBackgroundView background = (WelcomeBackgroundView) findViewById(R.id.wel_background_view);
WelcomeViewHider hider = new WelcomeViewHider(findViewById(R.id.wel_root));
hider.setOnViewHiddenListener(new WelcomeViewHider.OnViewHiddenListener() {
@Override
public void onViewHidden() {
completeWelcomeScreen();
}
});
responsiveItems.addAll(background, hider, configuration.getPages());
responsiveItems.setup(configuration);
viewPager.addOnPageChangeListener(responsiveItems);
viewPager.setCurrentItem(configuration.firstPageIndex());
responsiveItems.onPageSelected(viewPager.getCurrentItem());
}
@Override
protected void onDestroy() {
super.onDestroy();
if (viewPager != null) {
viewPager.clearOnPageChangeListeners();
}
}
/**
* Helper method to add button to list,
* checks for null first
*/
private void addViewWrapper(WelcomeViewWrapper wrapper, View.OnClickListener onClickListener) {
if (wrapper.getView() != null) {
wrapper.setOnClickListener(onClickListener);
responsiveItems.add(wrapper);
}
}
/* package */ boolean scrollToNextPage() {
if (!canScrollToNextPage()) {
return false;
}
viewPager.setCurrentItem(getNextPageIndex());
return true;
}
/* package */ boolean scrollToPreviousPage() {
if (!canScrollToPreviousPage()) {
return false;
}
viewPager.setCurrentItem(getPreviousPageIndex());
return true;
}
protected int getNextPageIndex() {
return viewPager.getCurrentItem() + (configuration.isRtl() ? -1 : 1);
}
protected int getPreviousPageIndex() {
return viewPager.getCurrentItem() + (configuration.isRtl() ? 1 : -1);
}
protected boolean canScrollToNextPage() {
return configuration.isRtl() ? getNextPageIndex() >= configuration.lastViewablePageIndex() : getNextPageIndex() <= configuration.lastViewablePageIndex();
}
protected boolean canScrollToPreviousPage() {
return configuration.isRtl() ? getPreviousPageIndex() <= configuration.firstPageIndex() : getPreviousPageIndex() >= configuration.firstPageIndex();
}
/**
* Called when the first (left in LTR layout direction) button
* is pressed when using the button bar bottom layout
*/
protected void onButtonBarFirstPressed() {
}
/**
* Called when the second (right in LTR layout direction) button
* is pressed when using the button bar bottom layout
*/
protected void onButtonBarSecondPressed() {
}
/**
* Closes the activity and saves it as completed.
* A subsequent call to WelcomeScreenHelper.show() would not show this again,
* unless the key is changed.
*/
protected void completeWelcomeScreen() {
WelcomeSharedPreferencesHelper.storeWelcomeCompleted(this, getKey());
setWelcomeScreenResult(RESULT_OK);
finish();
if (configuration.getExitAnimation() != WelcomeConfiguration.NO_ANIMATION_SET)
overridePendingTransition(R.anim.wel_none, configuration.getExitAnimation());
}
/**
* Closes the activity, doesn't save as completed.
* A subsequent call to WelcomeScreenHelper.show() would show this again.
*/
protected void cancelWelcomeScreen() {
setWelcomeScreenResult(RESULT_CANCELED);
finish();
}
@Override
public void onBackPressed() {
// Scroll to previous page and return if back button navigates
if (configuration.getBackButtonNavigatesPages() && scrollToPreviousPage()) {
return;
}
if (configuration.getCanSkip() && configuration.getBackButtonSkips()) {
completeWelcomeScreen();
} else {
cancelWelcomeScreen();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (configuration.getShowActionBarBackButton() && item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
private void setWelcomeScreenResult(int resultCode) {
Intent intent = this.getIntent();
intent.putExtra(WELCOME_SCREEN_KEY, getKey());
this.setResult(resultCode, intent);
}
private String getKey() {
return WelcomeUtils.getKey(this.getClass());
}
protected abstract WelcomeConfiguration configuration();
private class WelcomeFragmentPagerAdapter extends FragmentPagerAdapter {
public WelcomeFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return configuration.createFragment(position);
}
@Override
public int getCount() {
return configuration.pageCount();
}
}
}