react-native-notify-kit 9.1.22 → 9.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/README.md +5 -2
  2. package/android/build.gradle +67 -35
  3. package/android/proguard-rules.pro +14 -1
  4. package/android/schemas/app.notifee.core.database.NotifeeCoreDatabase/1.json +44 -0
  5. package/android/schemas/app.notifee.core.database.NotifeeCoreDatabase/2.json +51 -0
  6. package/android/src/androidTest/java/app/notifee/core/ExampleInstrumentedTest.java +25 -0
  7. package/android/src/androidTest/java/app/notifee/core/database/NotifeeCoreDatabaseTest.java +55 -0
  8. package/android/src/main/AndroidManifest.xml +71 -4
  9. package/android/src/main/java/app/notifee/core/AlarmPermissionBroadcastReceiver.java +25 -0
  10. package/android/src/main/java/app/notifee/core/BlockStateBroadcastReceiver.java +164 -0
  11. package/android/src/main/java/app/notifee/core/ChannelManager.java +350 -0
  12. package/android/src/main/java/app/notifee/core/ContextHolder.java +33 -0
  13. package/android/src/main/java/app/notifee/core/EventBus.java +63 -0
  14. package/android/src/main/java/app/notifee/core/EventSubscriber.java +82 -0
  15. package/android/src/main/java/app/notifee/core/ForegroundService.java +347 -0
  16. package/android/src/main/java/app/notifee/core/InitProvider.java +93 -0
  17. package/android/src/main/java/app/notifee/core/KeepForSdk.java +26 -0
  18. package/android/src/main/java/app/notifee/core/Logger.java +68 -0
  19. package/android/src/main/java/app/notifee/core/Notifee.java +570 -0
  20. package/android/src/main/java/app/notifee/core/NotifeeAlarmManager.java +352 -0
  21. package/android/src/main/java/app/notifee/core/NotificationAlarmReceiver.java +42 -0
  22. package/android/src/main/java/app/notifee/core/NotificationManager.java +939 -0
  23. package/android/src/main/java/app/notifee/core/NotificationPendingIntent.java +191 -0
  24. package/android/src/main/java/app/notifee/core/NotificationReceiverActivity.java +39 -0
  25. package/android/src/main/java/app/notifee/core/NotificationReceiverHandler.java +144 -0
  26. package/android/src/main/java/app/notifee/core/Preferences.java +79 -0
  27. package/android/src/main/java/app/notifee/core/RebootBroadcastReceiver.java +39 -0
  28. package/android/src/main/java/app/notifee/core/ReceiverService.java +281 -0
  29. package/android/src/main/java/app/notifee/core/Worker.java +87 -0
  30. package/android/src/main/java/app/notifee/core/database/NotifeeCoreDatabase.java +77 -0
  31. package/android/src/main/java/app/notifee/core/database/WorkDataDao.java +52 -0
  32. package/android/src/main/java/app/notifee/core/database/WorkDataEntity.java +68 -0
  33. package/android/src/main/java/app/notifee/core/database/WorkDataRepository.java +107 -0
  34. package/android/src/main/java/app/notifee/core/event/BlockStateEvent.java +102 -0
  35. package/android/src/main/java/app/notifee/core/event/ForegroundServiceEvent.java +48 -0
  36. package/android/src/main/java/app/notifee/core/event/InitialNotificationEvent.java +50 -0
  37. package/android/src/main/java/app/notifee/core/event/LogEvent.java +74 -0
  38. package/android/src/main/java/app/notifee/core/event/MainComponentEvent.java +33 -0
  39. package/android/src/main/java/app/notifee/core/event/NotificationEvent.java +107 -0
  40. package/android/src/main/java/app/notifee/core/interfaces/EventListener.java +39 -0
  41. package/android/src/main/java/app/notifee/core/interfaces/MethodCallResult.java +27 -0
  42. package/android/src/main/java/app/notifee/core/model/ChannelGroupModel.java +48 -0
  43. package/android/src/main/java/app/notifee/core/model/ChannelModel.java +126 -0
  44. package/android/src/main/java/app/notifee/core/model/IntervalTriggerModel.java +63 -0
  45. package/android/src/main/java/app/notifee/core/model/NotificationAndroidActionModel.java +125 -0
  46. package/android/src/main/java/app/notifee/core/model/NotificationAndroidModel.java +688 -0
  47. package/android/src/main/java/app/notifee/core/model/NotificationAndroidPressActionModel.java +150 -0
  48. package/android/src/main/java/app/notifee/core/model/NotificationAndroidStyleModel.java +336 -0
  49. package/android/src/main/java/app/notifee/core/model/NotificationModel.java +72 -0
  50. package/android/src/main/java/app/notifee/core/model/TimestampTriggerModel.java +206 -0
  51. package/android/src/main/java/app/notifee/core/model/package-info.java +4 -0
  52. package/android/src/main/java/app/notifee/core/utility/AlarmUtils.java +52 -0
  53. package/android/src/main/java/app/notifee/core/utility/Callbackable.java +12 -0
  54. package/android/src/main/java/app/notifee/core/utility/ColorUtils.java +62 -0
  55. package/android/src/main/java/app/notifee/core/utility/ExtendedListenableFuture.java +84 -0
  56. package/android/src/main/java/app/notifee/core/utility/IntentUtils.java +146 -0
  57. package/android/src/main/java/app/notifee/core/utility/ObjectUtils.java +191 -0
  58. package/android/src/main/java/app/notifee/core/utility/PowerManagerUtils.java +338 -0
  59. package/android/src/main/java/app/notifee/core/utility/ResourceUtils.java +308 -0
  60. package/android/src/main/java/app/notifee/core/utility/TextUtils.java +28 -0
  61. package/android/src/main/java/app/notifee/core/utility/package-info.java +4 -0
  62. package/android/src/test/java/app/notifee/core/model/NotificationAndroidPressActionModelTest.java +56 -0
  63. package/android/src/test/java/app/notifee/core/model/TimestampTriggerModelTest.java +44 -0
  64. package/dist/version.d.ts +1 -1
  65. package/dist/version.js +1 -1
  66. package/dist/version.js.map +1 -1
  67. package/package.json +2 -2
  68. package/src/version.ts +1 -1
  69. package/android/libs/app/notifee/core/202108261754/core-202108261754.aar +0 -0
  70. package/android/libs/app/notifee/core/202108261754/core-202108261754.aar.md5 +0 -1
  71. package/android/libs/app/notifee/core/202108261754/core-202108261754.aar.sha1 +0 -1
  72. package/android/libs/app/notifee/core/202108261754/core-202108261754.aar.sha256 +0 -1
  73. package/android/libs/app/notifee/core/202108261754/core-202108261754.aar.sha512 +0 -1
  74. package/android/libs/app/notifee/core/202108261754/core-202108261754.module +0 -146
  75. package/android/libs/app/notifee/core/202108261754/core-202108261754.module.md5 +0 -1
  76. package/android/libs/app/notifee/core/202108261754/core-202108261754.module.sha1 +0 -1
  77. package/android/libs/app/notifee/core/202108261754/core-202108261754.module.sha256 +0 -1
  78. package/android/libs/app/notifee/core/202108261754/core-202108261754.module.sha512 +0 -1
  79. package/android/libs/app/notifee/core/202108261754/core-202108261754.pom +0 -64
  80. package/android/libs/app/notifee/core/202108261754/core-202108261754.pom.md5 +0 -1
  81. package/android/libs/app/notifee/core/202108261754/core-202108261754.pom.sha1 +0 -1
  82. package/android/libs/app/notifee/core/202108261754/core-202108261754.pom.sha256 +0 -1
  83. package/android/libs/app/notifee/core/202108261754/core-202108261754.pom.sha512 +0 -1
  84. package/android/libs/app/notifee/core/maven-metadata.xml +0 -13
  85. package/android/libs/app/notifee/core/maven-metadata.xml.md5 +0 -1
  86. package/android/libs/app/notifee/core/maven-metadata.xml.sha1 +0 -1
  87. package/android/libs/app/notifee/core/maven-metadata.xml.sha256 +0 -1
  88. package/android/libs/app/notifee/core/maven-metadata.xml.sha512 +0 -1
@@ -0,0 +1,206 @@
1
+ package app.notifee.core.model;
2
+
3
+ /*
4
+ * Copyright (c) 2016-present Invertase Limited & Contributors
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this library except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ *
18
+ */
19
+
20
+ import android.os.Bundle;
21
+ import androidx.annotation.NonNull;
22
+ import app.notifee.core.utility.ObjectUtils;
23
+ import java.util.Calendar;
24
+ import java.util.concurrent.TimeUnit;
25
+
26
+ public class TimestampTriggerModel {
27
+ private Bundle mTimeTriggerBundle;
28
+ private int mInterval = -1;
29
+ private TimeUnit mTimeUnit = null;
30
+ private Boolean mWithAlarmManager = false;
31
+ private AlarmType mAlarmType = AlarmType.SET_EXACT;
32
+ private String mRepeatFrequency = null;
33
+ private Long mTimestamp = null;
34
+
35
+ public static final String HOURLY = "HOURLY";
36
+ public static final String DAILY = "DAILY";
37
+ public static final String WEEKLY = "WEEKLY";
38
+
39
+ private static final long HOUR_IN_MS = 60L * 60 * 1000;
40
+
41
+ private static final String TAG = "TimeTriggerModel";
42
+
43
+ private TimestampTriggerModel(Bundle bundle) {
44
+ mTimeTriggerBundle = bundle;
45
+
46
+ // set initial values
47
+ TimeUnit timeUnit = null;
48
+ if (mTimeTriggerBundle.containsKey("repeatFrequency")) {
49
+ int repeatFrequency = ObjectUtils.getInt(mTimeTriggerBundle.get("repeatFrequency"));
50
+ mTimestamp = ObjectUtils.getLong(mTimeTriggerBundle.get("timestamp"));
51
+
52
+ switch (repeatFrequency) {
53
+ case -1:
54
+ // default value for one-time trigger
55
+ break;
56
+ case 0:
57
+ mInterval = 1;
58
+ mTimeUnit = TimeUnit.HOURS;
59
+ mRepeatFrequency = HOURLY;
60
+ break;
61
+ case 1:
62
+ mInterval = 1;
63
+ mTimeUnit = TimeUnit.DAYS;
64
+ mRepeatFrequency = DAILY;
65
+ break;
66
+ case 2:
67
+ // weekly, 7 days
68
+ mInterval = 7;
69
+ mTimeUnit = TimeUnit.DAYS;
70
+ mRepeatFrequency = WEEKLY;
71
+ break;
72
+ }
73
+ }
74
+
75
+ if (mTimeTriggerBundle.containsKey("alarmManager")) {
76
+ mWithAlarmManager = true;
77
+
78
+ Bundle alarmManagerBundle = mTimeTriggerBundle.getBundle("alarmManager");
79
+
80
+ Object typeObj = alarmManagerBundle.get("type");
81
+
82
+ int type;
83
+ if (typeObj != null) {
84
+ type = ObjectUtils.getInt(typeObj);
85
+ } else {
86
+ type = 3;
87
+ }
88
+
89
+ // this is for the deprecated `alarmManager.allowWhileIdle` option
90
+ if (alarmManagerBundle.containsKey("allowWhileIdle")
91
+ && alarmManagerBundle.getBoolean("allowWhileIdle")) {
92
+ type = 3;
93
+ }
94
+
95
+ switch (type) {
96
+ case 0:
97
+ mAlarmType = AlarmType.SET;
98
+ break;
99
+ case 1:
100
+ mAlarmType = AlarmType.SET_AND_ALLOW_WHILE_IDLE;
101
+ break;
102
+ // default behavior when alarmManager is true:
103
+ default:
104
+ case 2:
105
+ mAlarmType = AlarmType.SET_EXACT;
106
+ break;
107
+ case 3:
108
+ mAlarmType = AlarmType.SET_EXACT_AND_ALLOW_WHILE_IDLE;
109
+ break;
110
+ case 4:
111
+ mAlarmType = AlarmType.SET_ALARM_CLOCK;
112
+ break;
113
+ }
114
+ } else if (mTimeTriggerBundle.containsKey("allowWhileIdle")) {
115
+ // for dart
116
+ mWithAlarmManager = true;
117
+ mAlarmType = AlarmType.SET_EXACT_AND_ALLOW_WHILE_IDLE;
118
+ }
119
+ }
120
+
121
+ public static TimestampTriggerModel fromBundle(@NonNull Bundle bundle) {
122
+ return new TimestampTriggerModel(bundle);
123
+ }
124
+
125
+ public long getTimestamp() {
126
+ return mTimestamp;
127
+ }
128
+
129
+ public long getDelay() {
130
+ if (mTimestamp != null && mTimestamp > 0) {
131
+ return Math.round((mTimestamp - System.currentTimeMillis()) / 1000.0);
132
+ }
133
+ return 0;
134
+ }
135
+
136
+ public void setNextTimestamp() {
137
+ // Skip for non-repeating triggers
138
+ if (mRepeatFrequency == null) {
139
+ return;
140
+ }
141
+
142
+ long timestamp = getTimestamp();
143
+
144
+ if (HOURLY.equals(mRepeatFrequency)) {
145
+ // Hourly: fixed ms is correct — no wall-clock preservation needed
146
+ while (timestamp < System.currentTimeMillis()) {
147
+ timestamp += HOUR_IN_MS;
148
+ }
149
+ this.mTimestamp = timestamp;
150
+ } else {
151
+ // Daily/Weekly: use Calendar.add() to preserve local wall-clock time across DST boundaries.
152
+ // Adding fixed ms (86,400,000) breaks when a calendar day is 23 or 25 hours during DST.
153
+ Calendar cal = Calendar.getInstance();
154
+ cal.setTimeInMillis(timestamp);
155
+
156
+ int field;
157
+ if (WEEKLY.equals(mRepeatFrequency)) {
158
+ field = Calendar.WEEK_OF_YEAR;
159
+ } else {
160
+ field = Calendar.DAY_OF_MONTH;
161
+ }
162
+
163
+ while (cal.getTimeInMillis() < System.currentTimeMillis()) {
164
+ cal.add(field, 1);
165
+ }
166
+
167
+ this.mTimestamp = cal.getTimeInMillis();
168
+ }
169
+ }
170
+
171
+ public enum AlarmType {
172
+ SET,
173
+ SET_AND_ALLOW_WHILE_IDLE,
174
+ SET_EXACT,
175
+ SET_EXACT_AND_ALLOW_WHILE_IDLE,
176
+ SET_ALARM_CLOCK,
177
+ }
178
+
179
+ public int getInterval() {
180
+ return mInterval;
181
+ }
182
+
183
+ public TimeUnit getTimeUnit() {
184
+ return mTimeUnit;
185
+ }
186
+
187
+ public Boolean getWithAlarmManager() {
188
+ return mWithAlarmManager;
189
+ }
190
+
191
+ public AlarmType getAlarmType() {
192
+ return mAlarmType;
193
+ }
194
+
195
+ public String getRepeatFrequency() {
196
+ return mRepeatFrequency;
197
+ }
198
+
199
+ public Bundle toBundle() {
200
+ Bundle bundle = (Bundle) mTimeTriggerBundle.clone();
201
+ if (mTimestamp != null) {
202
+ bundle.putLong("timestamp", mTimestamp);
203
+ }
204
+ return bundle;
205
+ }
206
+ }
@@ -0,0 +1,4 @@
1
+ @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
2
+ package app.notifee.core.model;
3
+
4
+ import androidx.annotation.RestrictTo;
@@ -0,0 +1,52 @@
1
+ package app.notifee.core.utility;
2
+
3
+ import static app.notifee.core.ContextHolder.getApplicationContext;
4
+
5
+ import android.app.Activity;
6
+ import android.app.AlarmManager;
7
+ import android.content.Context;
8
+ import android.content.Intent;
9
+ import android.net.Uri;
10
+ import android.os.Build;
11
+ import android.provider.Settings;
12
+ import app.notifee.core.ContextHolder;
13
+ import app.notifee.core.Logger;
14
+
15
+ public class AlarmUtils {
16
+ private static final String TAG = "AlarmUtils";
17
+
18
+ public static AlarmManager getAlarmManager() {
19
+ return (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
20
+ }
21
+
22
+ /**
23
+ * Attempts to open the device's alarm special access settings
24
+ *
25
+ * @param activity
26
+ */
27
+ public static void openAlarmPermissionSettings(Activity activity) {
28
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
29
+ return;
30
+ }
31
+
32
+ try {
33
+ Intent intent = new Intent();
34
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
35
+ intent.setAction(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM);
36
+ Context context = ContextHolder.getApplicationContext();
37
+ String packageName = context.getPackageName();
38
+ intent.setData(Uri.parse("package:" + packageName));
39
+
40
+ IntentUtils.startActivityOnUiThread(activity, intent);
41
+ } catch (Exception e) {
42
+ Logger.e(TAG, "An error occurred whilst trying to open alarm permission settings", e);
43
+ }
44
+ }
45
+
46
+ public static boolean canScheduleExactAlarms() {
47
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
48
+ return getAlarmManager().canScheduleExactAlarms();
49
+ }
50
+ return true;
51
+ }
52
+ }
@@ -0,0 +1,12 @@
1
+ package app.notifee.core.utility;
2
+
3
+ import androidx.annotation.Nullable;
4
+
5
+ /**
6
+ * Since lambda functions are not supported prior to SDK 24, and this project's SDK min. is 20, This
7
+ * interface was created to pass a function to another object.
8
+ */
9
+ public interface Callbackable<T> {
10
+
11
+ void call(@Nullable Exception e, @Nullable T result);
12
+ }
@@ -0,0 +1,62 @@
1
+ package app.notifee.core.utility;
2
+
3
+ /*
4
+ * Copyright (c) 2016-present Invertase Limited & Contributors
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this library except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ *
18
+ */
19
+
20
+ import android.graphics.Color;
21
+ import androidx.annotation.Nullable;
22
+ import java.util.HashMap;
23
+
24
+ public class ColorUtils {
25
+ public static @Nullable String getColorString(int color) {
26
+ String colorString = iColorNameMap.get(color);
27
+ if (colorString != null) {
28
+ return colorString;
29
+ } else {
30
+ return "#" + Integer.toHexString(color).substring(2);
31
+ }
32
+ }
33
+
34
+ private static final HashMap<Integer, String> iColorNameMap;
35
+
36
+ static {
37
+ iColorNameMap = new HashMap<>();
38
+ iColorNameMap.put(Color.BLACK, "black");
39
+ iColorNameMap.put(Color.DKGRAY, "darkgray");
40
+ iColorNameMap.put(Color.GRAY, "gray");
41
+ iColorNameMap.put(Color.LTGRAY, "lightgray");
42
+ iColorNameMap.put(Color.WHITE, "white");
43
+ iColorNameMap.put(Color.RED, "red");
44
+ iColorNameMap.put(Color.GREEN, "green");
45
+ iColorNameMap.put(Color.BLUE, "blue");
46
+ iColorNameMap.put(Color.YELLOW, "yellow");
47
+ iColorNameMap.put(Color.CYAN, "cyan");
48
+ iColorNameMap.put(Color.MAGENTA, "magenta");
49
+ iColorNameMap.put(0xFF00FFFF, "aqua");
50
+ iColorNameMap.put(0xFFFF00FF, "fuchsia");
51
+ iColorNameMap.put(Color.DKGRAY, "darkgrey");
52
+ iColorNameMap.put(Color.GRAY, "grey");
53
+ iColorNameMap.put(Color.LTGRAY, "lightgrey");
54
+ iColorNameMap.put(0xFF00FF00, "lime");
55
+ iColorNameMap.put(0xFF800000, "maroon");
56
+ iColorNameMap.put(0xFF000080, "navy");
57
+ iColorNameMap.put(0xFF808000, "olive");
58
+ iColorNameMap.put(0xFF800080, "purple");
59
+ iColorNameMap.put(0xFFC0C0C0, "silver");
60
+ iColorNameMap.put(0xFF008080, "teal");
61
+ }
62
+ }
@@ -0,0 +1,84 @@
1
+ package app.notifee.core.utility;
2
+
3
+ import com.google.common.util.concurrent.AsyncFunction;
4
+ import com.google.common.util.concurrent.FutureCallback;
5
+ import com.google.common.util.concurrent.Futures;
6
+ import com.google.common.util.concurrent.ListenableFuture;
7
+ import java.util.concurrent.ExecutionException;
8
+ import java.util.concurrent.Executor;
9
+ import java.util.concurrent.TimeUnit;
10
+ import java.util.concurrent.TimeoutException;
11
+ import org.checkerframework.checker.nullness.qual.Nullable;
12
+
13
+ /**
14
+ * A simple wrapper around {@link ListenableFuture} that provides additional methods that makes
15
+ * chaining {@link ListenableFuture}s easier and more readable.
16
+ */
17
+ public class ExtendedListenableFuture<T> implements ListenableFuture<T> {
18
+
19
+ private final ListenableFuture<T> future;
20
+
21
+ public ExtendedListenableFuture(ListenableFuture<T> future) {
22
+ this.future = future;
23
+ }
24
+
25
+ /**
26
+ * A wrapper around {@link Futures#transformAsync} that uses the Future It's called on as first
27
+ * parameter by default. This allows easier chaining.
28
+ */
29
+ public <O extends @Nullable Object> ExtendedListenableFuture<O> continueWith(
30
+ AsyncFunction<T, O> asyncFunction, Executor lExecutor) {
31
+ ListenableFuture<O> future = Futures.transformAsync(this, asyncFunction, lExecutor);
32
+ return new ExtendedListenableFuture<>(future);
33
+ }
34
+
35
+ public ExtendedListenableFuture<T> addOnCompleteListener(
36
+ Callbackable<T> callbackable, Executor executor) {
37
+ Futures.addCallback(
38
+ this,
39
+ new FutureCallback<T>() {
40
+ @Override
41
+ public void onSuccess(T result) {
42
+ callbackable.call(null, result);
43
+ }
44
+
45
+ @Override
46
+ public void onFailure(Throwable t) {
47
+ callbackable.call(new Exception(t), null);
48
+ }
49
+ },
50
+ executor);
51
+ return this;
52
+ }
53
+
54
+ @Override
55
+ public void addListener(Runnable listener, Executor executor) {
56
+ future.addListener(listener, executor);
57
+ }
58
+
59
+ @Override
60
+ public boolean cancel(boolean mayInterruptIfRunning) {
61
+ return future.cancel(mayInterruptIfRunning);
62
+ }
63
+
64
+ @Override
65
+ public boolean isCancelled() {
66
+ return future.isCancelled();
67
+ }
68
+
69
+ @Override
70
+ public boolean isDone() {
71
+ return future.isDone();
72
+ }
73
+
74
+ @Override
75
+ public T get() throws ExecutionException, InterruptedException {
76
+ return future.get();
77
+ }
78
+
79
+ @Override
80
+ public T get(long timeout, TimeUnit unit)
81
+ throws ExecutionException, InterruptedException, TimeoutException {
82
+ return future.get(timeout, unit);
83
+ }
84
+ }
@@ -0,0 +1,146 @@
1
+ package app.notifee.core.utility;
2
+
3
+ /*
4
+ * Copyright (c) 2016-present Invertase Limited & Contributors
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this library except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ *
18
+ */
19
+
20
+ import static app.notifee.core.ContextHolder.getApplicationContext;
21
+
22
+ import android.app.Activity;
23
+ import android.content.Context;
24
+ import android.content.Intent;
25
+ import android.content.pm.PackageManager;
26
+ import android.content.pm.ResolveInfo;
27
+ import android.os.Build;
28
+ import androidx.annotation.Nullable;
29
+ import app.notifee.core.Logger;
30
+ import java.util.List;
31
+
32
+ public class IntentUtils {
33
+ private static final String TAG = "IntentUtils";
34
+
35
+ @SuppressWarnings("deprecation")
36
+ public static boolean isAvailableOnDevice(Context ctx, Intent intent) {
37
+ try {
38
+ if (ctx == null || intent == null) {
39
+ return false;
40
+ }
41
+
42
+ final PackageManager mgr = ctx.getPackageManager();
43
+ List<ResolveInfo> list;
44
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
45
+ list =
46
+ mgr.queryIntentActivities(
47
+ intent, PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY));
48
+ } else {
49
+ list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
50
+ }
51
+ return !list.isEmpty();
52
+ } catch (Exception e) {
53
+ Logger.e(TAG, "An error occurred whilst trying to check if intent is available on device", e);
54
+
55
+ return false;
56
+ }
57
+ }
58
+
59
+ public static String getActivityName(Intent intent) {
60
+ if (intent == null) {
61
+ return null;
62
+ }
63
+
64
+ try {
65
+ String className = intent.getComponent().getClassName();
66
+ int index = className.lastIndexOf(".");
67
+
68
+ if (index != -1) {
69
+ return className.substring(index + 1);
70
+ }
71
+
72
+ } catch (Exception e) {
73
+ // noop
74
+ }
75
+
76
+ return null;
77
+ }
78
+
79
+ public static void startActivityOnUiThread(Activity activity, Intent intent) {
80
+ if (activity == null || intent == null) {
81
+ Logger.w(TAG, "Activity or intent is null when calling startActivityOnUiThread()");
82
+ return;
83
+ }
84
+
85
+ Context ctx = getApplicationContext();
86
+ if (ctx == null) {
87
+ Logger.w(TAG, "Unable to get application context when calling startActivityOnUiThread()");
88
+ return;
89
+ }
90
+
91
+ activity.runOnUiThread(
92
+ () -> {
93
+ try {
94
+ ctx.startActivity(intent);
95
+ } catch (Exception e) {
96
+ Logger.e(TAG, "An error occurred whilst trying to start activity on Ui Thread", e);
97
+ }
98
+ });
99
+ }
100
+
101
+ public static Class<?> getLaunchActivity(@Nullable String launchActivity) {
102
+ String activity;
103
+
104
+ if (launchActivity != null && !launchActivity.equals("default")) {
105
+ activity = launchActivity;
106
+ } else {
107
+ activity = getMainActivityClassName();
108
+ }
109
+
110
+ if (activity == null) {
111
+ Logger.e("ReceiverService", "Launch Activity for notification could not be found.");
112
+ return null;
113
+ }
114
+
115
+ Class<?> launchActivityClass = getClassForName(activity);
116
+
117
+ if (launchActivityClass == null) {
118
+ Logger.e(
119
+ "ReceiverService",
120
+ String.format("Launch Activity for notification does not exist ('%s').", launchActivity));
121
+ return null;
122
+ }
123
+
124
+ return launchActivityClass;
125
+ }
126
+
127
+ private @Nullable static Class<?> getClassForName(String className) {
128
+ try {
129
+ return Class.forName(className);
130
+ } catch (ClassNotFoundException e) {
131
+ return null;
132
+ }
133
+ }
134
+
135
+ private @Nullable static String getMainActivityClassName() {
136
+ String packageName = getApplicationContext().getPackageName();
137
+ Intent launchIntent =
138
+ getApplicationContext().getPackageManager().getLaunchIntentForPackage(packageName);
139
+
140
+ if (launchIntent == null || launchIntent.getComponent() == null) {
141
+ return null;
142
+ }
143
+
144
+ return launchIntent.getComponent().getClassName();
145
+ }
146
+ }