-
-
Notifications
You must be signed in to change notification settings - Fork 303
/
Copy pathSimpleSlide.java
610 lines (532 loc) · 22.8 KB
/
SimpleSlide.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
* MIT License
*
* Copyright (c) 2017 Jan Heinrich Reimer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.heinrichreimersoftware.materialintro.slide;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.ColorUtils;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.heinrichreimersoftware.materialintro.R;
import com.heinrichreimersoftware.materialintro.view.parallax.ParallaxSlideFragment;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SimpleSlide implements Slide, RestorableSlide, ButtonCtaSlide {
private static final int DEFAULT_PERMISSIONS_REQUEST_CODE = 34; //Random number
private SimpleSlideFragment fragment;
private final long id;
private final CharSequence title;
@StringRes
private final int titleRes;
private final CharSequence description;
@StringRes
private final int descriptionRes;
@DrawableRes
private final int imageRes;
@LayoutRes
private final int layoutRes;
@ColorRes
private final int backgroundRes;
@ColorRes
private final int backgroundDarkRes;
private final boolean canGoForward;
private final boolean canGoBackward;
private String[] permissions;
private int permissionsRequestCode;
private CharSequence buttonCtaLabel = null;
@StringRes
private int buttonCtaLabelRes = 0;
private View.OnClickListener buttonCtaClickListener = null;
protected SimpleSlide(Builder builder) {
fragment = SimpleSlideFragment.newInstance(builder.id, builder.title, builder.titleRes,
builder.description, builder.descriptionRes, builder.imageRes,
builder.backgroundRes, builder.layoutRes, builder.permissionsRequestCode);
id = builder.id;
title = builder.title;
titleRes = builder.titleRes;
description = builder.description;
descriptionRes = builder.descriptionRes;
imageRes = builder.imageRes;
layoutRes = builder.layoutRes;
backgroundRes = builder.backgroundRes;
backgroundDarkRes = builder.backgroundDarkRes;
canGoForward = builder.canGoForward;
canGoBackward = builder.canGoBackward;
permissions = builder.permissions;
permissionsRequestCode = builder.permissionsRequestCode;
buttonCtaLabel = builder.buttonCtaLabel;
buttonCtaLabelRes = builder.buttonCtaLabelRes;
buttonCtaClickListener = builder.buttonCtaClickListener;
updatePermissions();
}
@Override
public Fragment getFragment() {
return fragment;
}
@Override
public void setFragment(Fragment fragment) {
if (fragment instanceof SimpleSlideFragment)
this.fragment = (SimpleSlideFragment) fragment;
}
@Override
public int getBackground() {
return backgroundRes;
}
@Override
public int getBackgroundDark() {
return backgroundDarkRes;
}
@Override
public boolean canGoForward() {
updatePermissions();
return canGoForward && permissions == null;
}
@Override
public boolean canGoBackward() {
return canGoBackward;
}
@Override
public View.OnClickListener getButtonCtaClickListener() {
updatePermissions();
if (permissions == null) {
return buttonCtaClickListener;
}
return new View.OnClickListener() {
@Override
public void onClick(View v) {
if (fragment.getActivity() != null)
ActivityCompat.requestPermissions(fragment.getActivity(), permissions,
permissionsRequestCode);
}
};
}
@Override
public CharSequence getButtonCtaLabel() {
updatePermissions();
if (permissions == null) {
return buttonCtaLabel;
}
Context context = fragment.getContext();
if (context != null)
return context.getResources().getQuantityText(
R.plurals.mi_label_grant_permission, permissions.length);
return null;
}
@Override
public int getButtonCtaLabelRes() {
updatePermissions();
if (permissions == null) {
return buttonCtaLabelRes;
}
return 0;
}
private synchronized void updatePermissions() {
if (permissions != null) {
final List<String> permissionsNotGranted = new ArrayList<>();
for (String permission : permissions) {
if (fragment.getContext() == null ||
ContextCompat.checkSelfPermission(fragment.getContext(), permission) !=
PackageManager.PERMISSION_GRANTED) {
permissionsNotGranted.add(permission);
}
}
if (permissionsNotGranted.size() > 0) {
permissions = permissionsNotGranted.toArray(
new String[permissionsNotGranted.size()]);
} else {
permissions = null;
}
} else {
permissions = null;
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SimpleSlide that = (SimpleSlide) o;
if (id != that.id) return false;
if (titleRes != that.titleRes) return false;
if (descriptionRes != that.descriptionRes) return false;
if (imageRes != that.imageRes) return false;
if (layoutRes != that.layoutRes) return false;
if (backgroundRes != that.backgroundRes) return false;
if (backgroundDarkRes != that.backgroundDarkRes) return false;
if (canGoForward != that.canGoForward) return false;
if (canGoBackward != that.canGoBackward) return false;
if (permissionsRequestCode != that.permissionsRequestCode) return false;
if (buttonCtaLabelRes != that.buttonCtaLabelRes) return false;
if (fragment != null ? !fragment.equals(that.fragment) : that.fragment != null)
return false;
if (title != null ? !title.equals(that.title) : that.title != null) return false;
if (description != null ? !description.equals(that.description) : that.description != null)
return false;
if (!Arrays.equals(permissions, that.permissions)) return false;
if (buttonCtaLabel != null ? !buttonCtaLabel.equals(that.buttonCtaLabel) : that.buttonCtaLabel != null)
return false;
return buttonCtaClickListener != null ? buttonCtaClickListener.equals(that.buttonCtaClickListener) : that.buttonCtaClickListener == null;
}
@Override
public int hashCode() {
int result = fragment != null ? fragment.hashCode() : 0;
result = 31 * result + ((Long) id).hashCode();
result = 31 * result + (title != null ? title.hashCode() : 0);
result = 31 * result + titleRes;
result = 31 * result + (description != null ? description.hashCode() : 0);
result = 31 * result + descriptionRes;
result = 31 * result + imageRes;
result = 31 * result + layoutRes;
result = 31 * result + backgroundRes;
result = 31 * result + backgroundDarkRes;
result = 31 * result + (canGoForward ? 1 : 0);
result = 31 * result + (canGoBackward ? 1 : 0);
result = 31 * result + Arrays.hashCode(permissions);
result = 31 * result + permissionsRequestCode;
result = 31 * result + (buttonCtaLabel != null ? buttonCtaLabel.hashCode() : 0);
result = 31 * result + buttonCtaLabelRes;
result = 31 * result + (buttonCtaClickListener != null ? buttonCtaClickListener.hashCode() : 0);
return result;
}
public static class Builder {
@ColorRes
private int backgroundRes = 0;
private long id = 0;
@ColorRes
private int backgroundDarkRes = 0;
private CharSequence title = null;
@StringRes
private int titleRes = 0;
private CharSequence description = null;
@StringRes
private int descriptionRes = 0;
@DrawableRes
private int imageRes = 0;
@LayoutRes
private int layoutRes = R.layout.mi_fragment_simple_slide;
private boolean canGoForward = true;
private boolean canGoBackward = true;
private String[] permissions = null;
private CharSequence buttonCtaLabel = null;
@StringRes
private int buttonCtaLabelRes = 0;
private View.OnClickListener buttonCtaClickListener = null;
private int permissionsRequestCode = DEFAULT_PERMISSIONS_REQUEST_CODE;
public Builder background(@ColorRes int backgroundRes) {
this.backgroundRes = backgroundRes;
return this;
}
public Builder backgroundDark(@ColorRes int backgroundDarkRes) {
this.backgroundDarkRes = backgroundDarkRes;
return this;
}
public Builder title(CharSequence title) {
this.title = title;
this.titleRes = 0;
return this;
}
public Builder titleHtml(String titleHtml) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
this.title = Html.fromHtml(titleHtml, Html.FROM_HTML_MODE_LEGACY);
} else {
//noinspection deprecation
this.title = Html.fromHtml(titleHtml);
}
this.titleRes = 0;
return this;
}
public Builder id(long id) {
this.id = id;
return this;
}
public Builder title(@StringRes int titleRes) {
this.titleRes = titleRes;
this.title = null;
return this;
}
public Builder description(CharSequence description) {
this.description = description;
this.descriptionRes = 0;
return this;
}
public Builder descriptionHtml(String descriptionHtml) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
this.description = Html.fromHtml(descriptionHtml, Html.FROM_HTML_MODE_LEGACY);
} else {
//noinspection deprecation
this.description = Html.fromHtml(descriptionHtml);
}
this.descriptionRes = 0;
return this;
}
public Builder description(@StringRes int descriptionRes) {
this.descriptionRes = descriptionRes;
this.description = null;
return this;
}
public Builder image(@DrawableRes int imageRes) {
this.imageRes = imageRes;
return this;
}
public Builder layout(@LayoutRes int layoutRes) {
this.layoutRes = layoutRes;
return this;
}
public Builder scrollable(boolean scrollable) {
this.layoutRes = scrollable ? R.layout.mi_fragment_simple_slide_scrollable :
R.layout.mi_fragment_simple_slide;
return this;
}
public Builder canGoForward(boolean canGoForward) {
this.canGoForward = canGoForward;
return this;
}
public Builder canGoBackward(boolean canGoBackward) {
this.canGoBackward = canGoBackward;
return this;
}
public Builder permissions(String[] permissions) {
this.permissions = permissions;
return this;
}
public Builder permission(String permission) {
this.permissions = new String[]{permission};
return this;
}
public Builder permissionsRequestCode(int permissionsRequestCode) {
this.permissionsRequestCode = permissionsRequestCode;
return this;
}
public Builder buttonCtaLabel(CharSequence buttonCtaLabel) {
this.buttonCtaLabel = buttonCtaLabel;
this.buttonCtaLabelRes = 0;
return this;
}
public Builder buttonCtaLabelHtml(String buttonCtaLabelHtml) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
this.buttonCtaLabel = Html.fromHtml(buttonCtaLabelHtml, Html.FROM_HTML_MODE_LEGACY);
} else {
//noinspection deprecation
this.buttonCtaLabel = Html.fromHtml(buttonCtaLabelHtml);
}
this.buttonCtaLabelRes = 0;
return this;
}
public Builder buttonCtaLabel(@StringRes int buttonCtaLabelRes) {
this.buttonCtaLabelRes = buttonCtaLabelRes;
this.buttonCtaLabel = null;
return this;
}
public Builder buttonCtaClickListener(View.OnClickListener buttonCtaClickListener) {
this.buttonCtaClickListener = buttonCtaClickListener;
return this;
}
public SimpleSlide build() {
if (backgroundRes == 0)
throw new IllegalArgumentException("You must set a background.");
return new SimpleSlide(this);
}
}
public static class SimpleSlideFragment extends ParallaxSlideFragment {
private static final String ARGUMENT_ID =
"com.heinrichreimersoftware.materialintro.SimpleFragment.ARGUMENT_ID";
private static final String ARGUMENT_TITLE =
"com.heinrichreimersoftware.materialintro.SimpleFragment.ARGUMENT_TITLE";
private static final String ARGUMENT_TITLE_RES =
"com.heinrichreimersoftware.materialintro.SimpleFragment.ARGUMENT_TITLE_RES";
private static final String ARGUMENT_DESCRIPTION =
"com.heinrichreimersoftware.materialintro.SimpleFragment.ARGUMENT_DESCRIPTION";
private static final String ARGUMENT_DESCRIPTION_RES =
"com.heinrichreimersoftware.materialintro.SimpleFragment.ARGUMENT_DESCRIPTION_RES";
private static final String ARGUMENT_IMAGE_RES =
"com.heinrichreimersoftware.materialintro.SimpleFragment.ARGUMENT_IMAGE_RES";
private static final String ARGUMENT_BACKGROUND_RES =
"com.heinrichreimersoftware.materialintro.SimpleFragment.ARGUMENT_BACKGROUND_RES";
private static final String ARGUMENT_LAYOUT_RES =
"com.heinrichreimersoftware.materialintro.SimpleFragment.ARGUMENT_LAYOUT_RES";
private static final String ARGUMENT_PERMISSIONS_REQUEST_CODE =
"com.heinrichreimersoftware.materialintro.SimpleFragment.ARGUMENT_PERMISSIONS_REQUEST_CODE";
private TextView titleView = null;
private TextView descriptionView = null;
private ImageView imageView = null;
public SimpleSlideFragment() {
}
public static SimpleSlideFragment newInstance(long id, CharSequence title, @StringRes int titleRes,
CharSequence description, @StringRes int descriptionRes,
@DrawableRes int imageRes, @ColorRes int backgroundRes,
@LayoutRes int layout, int permissionsRequestCode) {
Bundle arguments = new Bundle();
arguments.putLong(ARGUMENT_ID, id);
arguments.putCharSequence(ARGUMENT_TITLE, title);
arguments.putInt(ARGUMENT_TITLE_RES, titleRes);
arguments.putCharSequence(ARGUMENT_DESCRIPTION, description);
arguments.putInt(ARGUMENT_DESCRIPTION_RES, descriptionRes);
arguments.putInt(ARGUMENT_IMAGE_RES, imageRes);
arguments.putInt(ARGUMENT_BACKGROUND_RES, backgroundRes);
arguments.putInt(ARGUMENT_LAYOUT_RES, layout);
arguments.putInt(ARGUMENT_PERMISSIONS_REQUEST_CODE, permissionsRequestCode);
SimpleSlideFragment fragment = new SimpleSlideFragment();
fragment.setArguments(arguments);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
updateNavigation();
}
@Override
public void onResume() {
super.onResume();
//Lock scroll for the case that users revoke accepted permission settings while in the intro
updateNavigation();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle arguments = getArguments();
View fragment = inflater.inflate(arguments.getInt(ARGUMENT_LAYOUT_RES,
R.layout.mi_fragment_simple_slide), container, false);
titleView = fragment.findViewById(R.id.mi_title);
descriptionView = fragment.findViewById(R.id.mi_description);
imageView = fragment.findViewById(R.id.mi_image);
long id = arguments.getLong(ARGUMENT_ID);
CharSequence title = arguments.getCharSequence(ARGUMENT_TITLE);
int titleRes = arguments.getInt(ARGUMENT_TITLE_RES);
CharSequence description = arguments.getCharSequence(ARGUMENT_DESCRIPTION);
int descriptionRes = arguments.getInt(ARGUMENT_DESCRIPTION_RES);
int imageRes = arguments.getInt(ARGUMENT_IMAGE_RES);
int backgroundRes = arguments.getInt(ARGUMENT_BACKGROUND_RES);
//Title
if (titleView != null) {
if (title != null) {
titleView.setText(title);
titleView.setVisibility(View.VISIBLE);
} else if (titleRes != 0) {
titleView.setText(titleRes);
titleView.setVisibility(View.VISIBLE);
} else {
titleView.setVisibility(View.GONE);
}
}
//Description
if (descriptionView != null) {
if (description != null) {
descriptionView.setText(description);
descriptionView.setVisibility(View.VISIBLE);
} else if (descriptionRes != 0) {
descriptionView.setText(descriptionRes);
descriptionView.setVisibility(View.VISIBLE);
} else {
descriptionView.setVisibility(View.GONE);
}
}
//Image
if (imageView != null) {
if (imageRes != 0) {
try {
imageView.setImageResource(imageRes);
} catch (OutOfMemoryError oome) {
imageView.setVisibility(View.GONE);
}
imageView.setVisibility(View.VISIBLE);
} else {
imageView.setVisibility(View.GONE);
}
}
@ColorInt
int textColorPrimary;
@ColorInt
int textColorSecondary;
if (backgroundRes != 0 &&
ColorUtils.calculateLuminance(ContextCompat.getColor(getContext(), backgroundRes)) < 0.6) {
//Use light text color
textColorPrimary = ContextCompat.getColor(getContext(), R.color.mi_text_color_primary_dark);
textColorSecondary = ContextCompat.getColor(getContext(), R.color.mi_text_color_secondary_dark);
} else {
//Use dark text color
textColorPrimary = ContextCompat.getColor(getContext(), R.color.mi_text_color_primary_light);
textColorSecondary = ContextCompat.getColor(getContext(), R.color.mi_text_color_secondary_light);
}
if (titleView != null) {
titleView.setTextColor(textColorPrimary);
}
if (descriptionView != null) {
descriptionView.setTextColor(textColorSecondary);
}
if (getActivity() instanceof SimpleSlideActivity) {
((SimpleSlideActivity) getActivity()).onSlideViewCreated(this, fragment, id);
}
return fragment;
}
@Override
public void onDestroyView() {
if (getActivity() instanceof SimpleSlideActivity) {
long id = getArguments().getLong(ARGUMENT_ID);
((SimpleSlideActivity) getActivity()).onSlideDestroyView(this, getView(), id);
}
titleView = null;
descriptionView = null;
imageView = null;
super.onDestroyView();
}
public TextView getTitleView() {
return titleView;
}
public TextView getDescriptionView() {
return descriptionView;
}
public ImageView getImageView() {
return imageView;
}
public long getSlideId() {
return getArguments().getLong(ARGUMENT_ID);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
int permissionsRequestCode = getArguments() == null ? DEFAULT_PERMISSIONS_REQUEST_CODE :
getArguments().getInt(ARGUMENT_PERMISSIONS_REQUEST_CODE,
DEFAULT_PERMISSIONS_REQUEST_CODE);
if (requestCode == permissionsRequestCode) {
updateNavigation();
}
}
}
}