2021-01-10 23:37:09 +01:00
|
|
|
/* Copyright (C) 2017-2021 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
|
|
|
Gobbetti, Lukas Veneziano, Maxim Baz
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge 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 Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2017-03-02 00:27:54 +01:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2022-12-08 19:14:07 +01:00
|
|
|
import android.content.pm.LauncherActivityInfo;
|
|
|
|
import android.content.pm.LauncherApps;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.UserHandle;
|
|
|
|
import android.os.UserManager;
|
2017-03-02 00:27:54 +01:00
|
|
|
|
2019-01-26 15:52:40 +01:00
|
|
|
import androidx.annotation.NonNull;
|
2022-12-08 19:14:07 +01:00
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-03-11 22:15:44 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2017-03-02 00:27:54 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
|
|
|
|
|
|
|
|
public class NotificationUtils {
|
2022-12-08 19:14:07 +01:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(NotificationUtils.class);
|
|
|
|
|
2017-03-02 00:27:54 +01:00
|
|
|
@NonNull
|
|
|
|
public static String getPreferredTextFor(NotificationSpec notificationSpec, int lengthBody, int lengthSubject, Context context) {
|
|
|
|
switch (notificationSpec.type) {
|
|
|
|
case GENERIC_ALARM_CLOCK:
|
|
|
|
return StringUtils.getFirstOf(notificationSpec.title, notificationSpec.subject);
|
|
|
|
case GENERIC_SMS:
|
|
|
|
case GENERIC_EMAIL:
|
|
|
|
return formatText(notificationSpec.sender, notificationSpec.subject, notificationSpec.body, lengthBody, lengthSubject, context);
|
|
|
|
case GENERIC_NAVIGATION:
|
|
|
|
return StringUtils.getFirstOf(notificationSpec.title, notificationSpec.body);
|
2017-10-22 00:02:36 +02:00
|
|
|
case CONVERSATIONS:
|
|
|
|
case FACEBOOK_MESSENGER:
|
|
|
|
case GOOGLE_MESSENGER:
|
|
|
|
case GOOGLE_HANGOUTS:
|
|
|
|
case HIPCHAT:
|
|
|
|
case KAKAO_TALK:
|
|
|
|
case LINE:
|
2017-03-02 00:27:54 +01:00
|
|
|
case RIOT:
|
|
|
|
case SIGNAL:
|
2020-10-24 22:27:01 +02:00
|
|
|
case WIRE:
|
2017-10-22 00:02:36 +02:00
|
|
|
case SKYPE:
|
|
|
|
case SNAPCHAT:
|
2017-03-02 00:27:54 +01:00
|
|
|
case TELEGRAM:
|
2017-12-27 08:59:09 +01:00
|
|
|
case THREEMA:
|
2018-02-17 17:33:34 +01:00
|
|
|
case KONTALK:
|
2018-02-17 17:39:15 +01:00
|
|
|
case ANTOX:
|
2017-03-02 00:27:54 +01:00
|
|
|
case TWITTER:
|
|
|
|
case WHATSAPP:
|
2017-10-22 00:02:36 +02:00
|
|
|
case VIBER:
|
|
|
|
case WECHAT:
|
2017-03-18 11:41:47 +01:00
|
|
|
return StringUtils.ensureNotNull(notificationSpec.body);
|
2017-03-02 00:27:54 +01:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-03-11 22:15:44 +01:00
|
|
|
@NonNull
|
|
|
|
public static String formatSender(String sender, Context context) {
|
|
|
|
if (sender == null || sender.length() == 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return context.getString(R.string.StringUtils_sender, sender);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-02 00:27:54 +01:00
|
|
|
@NonNull
|
|
|
|
public static String formatText(String sender, String subject, String body, int lengthBody, int lengthSubject, Context context) {
|
2017-03-11 22:15:44 +01:00
|
|
|
String fBody = StringUtils.truncate(body, lengthBody);
|
|
|
|
String fSubject = StringUtils.truncate(subject, lengthSubject);
|
|
|
|
String fSender = formatSender(sender, context);
|
2017-03-02 00:27:54 +01:00
|
|
|
|
2017-03-11 22:15:44 +01:00
|
|
|
StringBuilder builder = StringUtils.join(" ", fBody, fSubject, fSender);
|
|
|
|
return builder.toString().trim();
|
2017-03-02 00:27:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String getPreferredTextFor(CallSpec callSpec) {
|
|
|
|
return StringUtils.getFirstOf(callSpec.name, callSpec.number);
|
|
|
|
}
|
2022-12-08 19:14:07 +01:00
|
|
|
|
|
|
|
@Nullable
|
|
|
|
public static Drawable getAppIcon(final Context context, final String packageName) {
|
|
|
|
try {
|
|
|
|
return context.getPackageManager().getApplicationIcon(packageName);
|
|
|
|
} catch (final PackageManager.NameNotFoundException ignored) {
|
|
|
|
LOG.warn("Failed to find icon for {}, attempting fallback", packageName);
|
|
|
|
return getAppIconFallback(context, packageName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fallback method to get an app icon - iterate through all the users and attempt to find the
|
|
|
|
* app. This includes work profiles.
|
|
|
|
*/
|
|
|
|
@Nullable
|
|
|
|
private static Drawable getAppIconFallback(final Context context, final String packageName) {
|
|
|
|
try {
|
|
|
|
final LauncherApps launcherAppsService = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
|
|
|
|
final UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
|
|
|
final List<UserHandle> userProfiles = userManager.getUserProfiles();
|
|
|
|
// Skip user 0 (current, main user)
|
|
|
|
for (int i = 1; i < userProfiles.size(); i++) {
|
|
|
|
final UserHandle userHandle = userProfiles.get(i);
|
|
|
|
final List<LauncherActivityInfo> activityList = launcherAppsService.getActivityList(packageName, userHandle);
|
|
|
|
|
|
|
|
if (!activityList.isEmpty()) {
|
|
|
|
LOG.info("Found {} icons for {} in user {}", activityList.size(), packageName, userHandle);
|
|
|
|
return activityList.get(0).getIcon(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG.warn("Failed to find fallback icon for {}", packageName);
|
|
|
|
} catch (final Exception e) {
|
|
|
|
LOG.error("Error during fallback icon search", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2017-03-02 00:27:54 +01:00
|
|
|
}
|