JLChen
2021-01-04 ed86fe7ed952bb2151aac8841134246bbde152c9
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
/*
 * Copyright (c) 2010-2019 Belledonne Communications SARL.
 *
 * This file is part of linphone-android
 * (see https://www.linphone.org).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.linphone.compatibility;
 
import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.content.ContentProviderClient;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Vibrator;
import android.provider.Settings;
import android.service.notification.StatusBarNotification;
import org.linphone.core.Address;
import org.linphone.mediastream.Version;
import org.linphone.notifications.Notifiable;
 
public class Compatibility {
    public static final String CHAT_NOTIFICATIONS_GROUP = "CHAT_NOTIF_GROUP";
    public static final String KEY_TEXT_REPLY = "key_text_reply";
    public static final String INTENT_NOTIF_ID = "NOTIFICATION_ID";
    public static final String INTENT_REPLY_NOTIF_ACTION = "org.linphone.REPLY_ACTION";
    public static final String INTENT_HANGUP_CALL_NOTIF_ACTION = "org.linphone.HANGUP_CALL_ACTION";
    public static final String INTENT_ANSWER_CALL_NOTIF_ACTION = "org.linphone.ANSWER_CALL_ACTION";
    public static final String INTENT_LOCAL_IDENTITY = "LOCAL_IDENTITY";
    public static final String INTENT_MARK_AS_READ_ACTION = "org.linphone.MARK_AS_READ_ACTION";
 
    public static String getDeviceName(Context context) {
        if (Version.sdkAboveOrEqual(25)) {
            return ApiTwentySixPlus.getDeviceName(context);
        }
 
        String name = null;
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        if (adapter != null) {
            name = adapter.getName();
        }
 
        if (name == null) {
            name = Settings.Secure.getString(context.getContentResolver(), "bluetooth_name");
        }
        if (name == null) {
            name = Build.MANUFACTURER + " " + Build.MODEL;
        }
        return name;
    }
 
    public static void createNotificationChannels(Context context) {
        if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
            ApiTwentySixPlus.createServiceChannel(context);
            ApiTwentySixPlus.createMessageChannel(context);
        }
    }
 
    public static Notification createSimpleNotification(
            Context context, String title, String text, PendingIntent intent) {
        if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
            return ApiTwentySixPlus.createSimpleNotification(context, title, text, intent);
        }
        return ApiTwentyOnePlus.createSimpleNotification(context, title, text, intent);
    }
 
    public static Notification createMessageNotification(
            Context context,
            Notifiable notif,
            String msgSender,
            String msg,
            Bitmap contactIcon,
            PendingIntent intent) {
        if (Version.sdkAboveOrEqual(Version.API28_PIE_90)) {
            return ApiTwentyEightPlus.createMessageNotification(
                    context, notif, contactIcon, intent);
        } else if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
            return ApiTwentySixPlus.createMessageNotification(context, notif, contactIcon, intent);
        } else if (Version.sdkAboveOrEqual(Version.API24_NOUGAT_70)) {
            return ApiTwentyFourPlus.createMessageNotification(context, notif, contactIcon, intent);
        }
        return ApiTwentyOnePlus.createMessageNotification(
                context, notif.getMessages().size(), msgSender, msg, contactIcon, intent);
    }
 
    public static Notification createRepliedNotification(Context context, String reply) {
        if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
            return ApiTwentySixPlus.createRepliedNotification(context, reply);
        } else if (Version.sdkAboveOrEqual(Version.API24_NOUGAT_70)) {
            return ApiTwentyFourPlus.createRepliedNotification(context, reply);
        }
        return null;
    }
 
    public static Notification createMissedCallNotification(
            Context context, String title, String text, PendingIntent intent, int count) {
        if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
            return ApiTwentySixPlus.createMissedCallNotification(
                    context, title, text, intent, count);
        }
        return ApiTwentyOnePlus.createMissedCallNotification(context, title, text, intent, count);
    }
 
    public static Notification createInCallNotification(
            Context context,
            int callId,
            String msg,
            int iconID,
            Bitmap contactIcon,
            String contactName,
            PendingIntent intent) {
        if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
            return ApiTwentySixPlus.createInCallNotification(
                    context, callId, msg, iconID, contactIcon, contactName, intent);
        } else if (Version.sdkAboveOrEqual(Version.API24_NOUGAT_70)) {
            return ApiTwentyFourPlus.createInCallNotification(
                    context, callId, msg, iconID, contactIcon, contactName, intent);
        }
        return ApiTwentyOnePlus.createInCallNotification(
                context, msg, iconID, contactIcon, contactName, intent);
    }
 
    public static Notification createIncomingCallNotification(
            Context context,
            int callId,
            Bitmap contactIcon,
            String contactName,
            String sipUri,
            PendingIntent intent) {
        if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
            return ApiTwentySixPlus.createIncomingCallNotification(
                    context, callId, contactIcon, contactName, sipUri, intent);
        } else if (Version.sdkAboveOrEqual(Version.API24_NOUGAT_70)) {
            return ApiTwentyFourPlus.createIncomingCallNotification(
                    context, callId, contactIcon, contactName, sipUri, intent);
        }
        return ApiTwentyOnePlus.createIncomingCallNotification(
                context, contactIcon, contactName, sipUri, intent);
    }
 
    public static Notification createNotification(
            Context context,
            String title,
            String message,
            int icon,
            int iconLevel,
            Bitmap largeIcon,
            PendingIntent intent,
            int priority,
            boolean ongoing) {
        if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
            return ApiTwentySixPlus.createNotification(
                    context, title, message, icon, iconLevel, largeIcon, intent, priority, ongoing);
        }
        return ApiTwentyOnePlus.createNotification(
                context, title, message, icon, iconLevel, largeIcon, intent, priority, ongoing);
    }
 
    public static boolean canDrawOverlays(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return Settings.canDrawOverlays(context);
        }
        return true;
    }
 
    public static void startService(Context context, Intent intent) {
        if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
            ApiTwentySixPlus.startService(context, intent);
        } else {
            context.startService(intent);
        }
    }
 
    public static void setFragmentTransactionReorderingAllowed(
            FragmentTransaction transaction, boolean allowed) {
        if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
            ApiTwentySixPlus.setFragmentTransactionReorderingAllowed(transaction, allowed);
        }
    }
 
    public static void closeContentProviderClient(ContentProviderClient client) {
        if (Version.sdkAboveOrEqual(Version.API24_NOUGAT_70)) {
            ApiTwentyFourPlus.closeContentProviderClient(client);
        } else {
            ApiTwentyOnePlus.closeContentProviderClient(client);
        }
    }
 
    public static boolean isAppUserRestricted(Context context) {
        if (Version.sdkAboveOrEqual(Version.API28_PIE_90)) {
            return ApiTwentyEightPlus.isAppUserRestricted(context);
        }
        return false;
    }
 
    public static boolean isAppIdleMode(Context context) {
        if (Version.sdkAboveOrEqual(Version.API23_MARSHMALLOW_60)) {
            return ApiTwentyThreePlus.isAppIdleMode(context);
        }
        return false;
    }
 
    public static int getAppStandbyBucket(Context context) {
        if (Version.sdkAboveOrEqual(Version.API28_PIE_90)) {
            return ApiTwentyEightPlus.getAppStandbyBucket(context);
        }
        return 0;
    }
 
    public static String getAppStandbyBucketNameFromValue(int bucket) {
        if (Version.sdkAboveOrEqual(Version.API28_PIE_90)) {
            return ApiTwentyEightPlus.getAppStandbyBucketNameFromValue(bucket);
        }
        return null;
    }
 
    public static void setShowWhenLocked(Activity activity, boolean enable) {
        if (Version.sdkStrictlyBelow(Version.API27_OREO_81)) {
            ApiTwentyOnePlus.setShowWhenLocked(activity, enable);
        }
    }
 
    public static void setTurnScreenOn(Activity activity, boolean enable) {
        if (Version.sdkStrictlyBelow(Version.API27_OREO_81)) {
            ApiTwentyOnePlus.setTurnScreenOn(activity, enable);
        }
    }
 
    public static boolean isDoNotDisturbSettingsAccessGranted(Context context) {
        if (Version.sdkAboveOrEqual(Version.API23_MARSHMALLOW_60)) {
            return ApiTwentyThreePlus.isDoNotDisturbSettingsAccessGranted(context);
        }
        return true;
    }
 
    public static boolean isDoNotDisturbPolicyAllowingRinging(
            Context context, Address remoteAddress) {
        if (Version.sdkAboveOrEqual(Version.API23_MARSHMALLOW_60)) {
            return ApiTwentyThreePlus.isDoNotDisturbPolicyAllowingRinging(context, remoteAddress);
        }
        return true;
    }
 
    public static void createChatShortcuts(Context context) {
        if (Version.sdkAboveOrEqual(Version.API25_NOUGAT_71)) {
            ApiTwentyFivePlus.createChatShortcuts(context);
        }
    }
 
    public static void removeChatShortcuts(Context context) {
        if (Version.sdkAboveOrEqual(Version.API25_NOUGAT_71)) {
            ApiTwentyFivePlus.removeChatShortcuts(context);
        }
    }
 
    public static void enterPipMode(Activity activity) {
        if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
            ApiTwentySixPlus.enterPipMode(activity);
        }
    }
 
    public static Notification.Action getReplyMessageAction(Context context, Notifiable notif) {
        if (Version.sdkAboveOrEqual(Version.API29_ANDROID_10)) {
            return ApiTwentyNinePlus.getReplyMessageAction(context, notif);
        } else if (Version.sdkAboveOrEqual(Version.API28_PIE_90)) {
            return ApiTwentyEightPlus.getReplyMessageAction(context, notif);
        } else if (Version.sdkAboveOrEqual(Version.API24_NOUGAT_70)) {
            return ApiTwentyFourPlus.getReplyMessageAction(context, notif);
        }
        return null;
    }
 
    public static Notification.Action getMarkMessageAsReadAction(
            Context context, Notifiable notif) {
        if (Version.sdkAboveOrEqual(Version.API29_ANDROID_10)) {
            return ApiTwentyNinePlus.getMarkMessageAsReadAction(context, notif);
        } else if (Version.sdkAboveOrEqual(Version.API28_PIE_90)) {
            return ApiTwentyEightPlus.getMarkMessageAsReadAction(context, notif);
        } else if (Version.sdkAboveOrEqual(Version.API24_NOUGAT_70)) {
            return ApiTwentyFourPlus.getMarkMessageAsReadAction(context, notif);
        }
        return null;
    }
 
    public static Notification.Action getCallAnswerAction(Context context, int callId) {
        if (Version.sdkAboveOrEqual(Version.API29_ANDROID_10)) {
            return ApiTwentyNinePlus.getCallAnswerAction(context, callId);
        } else if (Version.sdkAboveOrEqual(Version.API24_NOUGAT_70)) {
            return ApiTwentyFourPlus.getCallAnswerAction(context, callId);
        }
        return null;
    }
 
    public static Notification.Action getCallDeclineAction(Context context, int callId) {
        if (Version.sdkAboveOrEqual(Version.API29_ANDROID_10)) {
            return ApiTwentyNinePlus.getCallDeclineAction(context, callId);
        } else if (Version.sdkAboveOrEqual(Version.API24_NOUGAT_70)) {
            return ApiTwentyFourPlus.getCallDeclineAction(context, callId);
        }
        return null;
    }
 
    public static StatusBarNotification[] getActiveNotifications(NotificationManager manager) {
        if (Version.sdkAboveOrEqual(Version.API23_MARSHMALLOW_60)) {
            return ApiTwentyThreePlus.getActiveNotifications(manager);
        }
 
        return new StatusBarNotification[0];
    }
 
    public static void vibrate(Vibrator vibrator) {
        if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
            ApiTwentySixPlus.vibrate(vibrator);
        } else {
            ApiTwentyOnePlus.vibrate(vibrator);
        }
    }
}