Migrate to new Share APIs.

This commit is contained in:
Alex Hart
2021-07-09 12:59:26 -03:00
committed by Greyson Parrelli
parent 68a2d5ed20
commit 3f53abedab
11 changed files with 70 additions and 275 deletions

View File

@@ -3,13 +3,11 @@ package org.thoughtcrime.securesms.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.WorkerThread;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat;
@@ -100,20 +98,6 @@ public final class AvatarUtil {
}
}
@RequiresApi(ConversationUtil.CONVERSATION_SUPPORT_VERSION)
@WorkerThread
public static @NonNull Icon getIconForShortcut(@NonNull Context context, @NonNull Recipient recipient) {
try {
GlideRequest<Bitmap> glideRequest = GlideApp.with(context).asBitmap().load(new ConversationShortcutPhoto(recipient));
if (recipient.shouldBlurAvatar()) {
glideRequest = glideRequest.transform(new BlurTransformation(context, 0.25f, BlurTransformation.MAX_RADIUS));
}
return Icon.createWithAdaptiveBitmap(glideRequest.submit().get());
} catch (ExecutionException | InterruptedException e) {
throw new AssertionError("This call should not fail.", e);
}
}
@WorkerThread
public static @NonNull IconCompat getIconCompatForShortcut(@NonNull Context context, @NonNull Recipient recipient) {
try {

View File

@@ -1,16 +1,15 @@
package org.thoughtcrime.securesms.util;
import android.app.Person;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.WorkerThread;
import androidx.core.app.Person;
import androidx.core.content.pm.ShortcutInfoCompat;
import androidx.core.content.pm.ShortcutManagerCompat;
import com.annimon.stream.Stream;
@@ -37,7 +36,7 @@ import java.util.Set;
*/
public final class ConversationUtil {
public static final int CONVERSATION_SUPPORT_VERSION = 30;
public static final int CONVERSATION_SUPPORT_VERSION = 30;
private static final String TAG = Log.tag(ConversationUtil.class);
@@ -49,7 +48,7 @@ public final class ConversationUtil {
*/
@WorkerThread
public static @NonNull String getChannelId(@NonNull Context context, @NonNull Recipient recipient) {
Recipient resolved = recipient.resolve();
Recipient resolved = recipient.resolve();
return resolved.getNotificationChannel() != null ? resolved.getNotificationChannel() : NotificationChannels.getMessagesChannel(context);
}
@@ -63,24 +62,21 @@ public final class ConversationUtil {
/**
* Synchronously pushes a new dynamic shortcut for the given recipient if one does not already exist.
*
* <p>
* If added, this recipient is given a high ranking with the intention of not appearing immediately in results.
*/
@WorkerThread
public static void pushShortcutForRecipientIfNeededSync(@NonNull Context context, @NonNull Recipient recipient) {
if (Build.VERSION.SDK_INT >= CONVERSATION_SUPPORT_VERSION) {
ShortcutManager shortcutManager = ServiceUtil.getShortcutManager(context);
String shortcutId = getShortcutId(recipient);
List<ShortcutInfo> shortcuts = shortcutManager.getDynamicShortcuts();
String shortcutId = getShortcutId(recipient);
List<ShortcutInfoCompat> shortcuts = ShortcutManagerCompat.getDynamicShortcuts(context);
boolean hasPushedRecipientShortcut = Stream.of(shortcuts)
.filter(info -> Objects.equals(shortcutId, info.getId()))
.findFirst()
.isPresent();
boolean hasPushedRecipientShortcut = Stream.of(shortcuts)
.filter(info -> Objects.equals(shortcutId, info.getId()))
.findFirst()
.isPresent();
if (!hasPushedRecipientShortcut) {
pushShortcutForRecipientInternal(context, recipient, shortcuts.size());
}
if (!hasPushedRecipientShortcut) {
pushShortcutForRecipientInternal(context, recipient, shortcuts.size());
}
}
@@ -88,33 +84,26 @@ public final class ConversationUtil {
* Clears all currently set dynamic shortcuts
*/
public static void clearAllShortcuts(@NonNull Context context) {
if (Build.VERSION.SDK_INT >= CONVERSATION_SUPPORT_VERSION) {
ShortcutManager shortcutManager = ServiceUtil.getShortcutManager(context);
List<ShortcutInfo> shortcutInfos = shortcutManager.getDynamicShortcuts();
List<ShortcutInfoCompat> shortcutInfos = ShortcutManagerCompat.getDynamicShortcuts(context);
shortcutManager.removeLongLivedShortcuts(Stream.of(shortcutInfos).map(ShortcutInfo::getId).toList());
}
ShortcutManagerCompat.removeLongLivedShortcuts(context, Stream.of(shortcutInfos).map(ShortcutInfoCompat::getId).toList());
}
/**
* Clears the shortcuts tied to a given thread.
*/
public static void clearShortcuts(@NonNull Context context, @NonNull Set<Long> threadIds) {
if (Build.VERSION.SDK_INT >= CONVERSATION_SUPPORT_VERSION) {
SignalExecutors.BOUNDED.execute(() -> {
List<RecipientId> recipientIds = DatabaseFactory.getThreadDatabase(context).getRecipientIdsForThreadIds(threadIds);
ShortcutManager shortcutManager = ServiceUtil.getShortcutManager(context);
SignalExecutors.BOUNDED.execute(() -> {
List<RecipientId> recipientIds = DatabaseFactory.getThreadDatabase(context).getRecipientIdsForThreadIds(threadIds);
shortcutManager.removeLongLivedShortcuts(Stream.of(recipientIds).map(ConversationUtil::getShortcutId).toList());
});
}
ShortcutManagerCompat.removeLongLivedShortcuts(context, Stream.of(recipientIds).map(ConversationUtil::getShortcutId).toList());
});
}
/**
* Returns an ID that is unique between all recipients.
*
* @param recipientId The recipient ID to get a shortcut ID for
*
* @return A unique identifier that is stable for a given recipient id
*/
public static @NonNull String getShortcutId(@NonNull RecipientId recipientId) {
@@ -125,7 +114,6 @@ public final class ConversationUtil {
* Returns an ID that is unique between all recipients.
*
* @param recipient The recipient to get a shortcut for.
*
* @return A unique identifier that is stable for a given recipient id
*/
public static @NonNull String getShortcutId(@NonNull Recipient recipient) {
@@ -148,10 +136,8 @@ public final class ConversationUtil {
}
}
@RequiresApi(CONVERSATION_SUPPORT_VERSION)
public static int getMaxShortcuts(@NonNull Context context) {
ShortcutManager shortcutManager = ServiceUtil.getShortcutManager(context);
return Math.min(shortcutManager.getMaxShortcutCountPerActivity(), 150);
return Math.min(ShortcutManagerCompat.getMaxShortcutCountPerActivity(context), 150);
}
/**
@@ -162,42 +148,37 @@ public final class ConversationUtil {
* recipient should be first in the list.
* @return True if the update was successful, false if we were rate-limited.
*/
@RequiresApi(CONVERSATION_SUPPORT_VERSION)
@WorkerThread
public static boolean setActiveShortcuts(@NonNull Context context, @NonNull List<Recipient> rankedRecipients) {
ShortcutManager shortcutManager = ServiceUtil.getShortcutManager(context);
if (shortcutManager.isRateLimitingActive()) {
if (ShortcutManagerCompat.isRateLimitingActive(context)) {
return false;
}
int maxShortcuts = shortcutManager.getMaxShortcutCountPerActivity();
int maxShortcuts = ShortcutManagerCompat.getMaxShortcutCountPerActivity(context);
if (rankedRecipients.size() > maxShortcuts) {
Log.w(TAG, "Too many recipients provided! Provided: " + rankedRecipients.size() + ", Max: " + maxShortcuts);
rankedRecipients = rankedRecipients.subList(0, maxShortcuts);
}
List<ShortcutInfo> shortcuts = new ArrayList<>(rankedRecipients.size());
List<ShortcutInfoCompat> shortcuts = new ArrayList<>(rankedRecipients.size());
for (int i = 0; i < rankedRecipients.size(); i++) {
ShortcutInfo info = buildShortcutInfo(context, rankedRecipients.get(i), i);
ShortcutInfoCompat info = buildShortcutInfo(context, rankedRecipients.get(i), i);
shortcuts.add(info);
}
return shortcutManager.setDynamicShortcuts(shortcuts);
return ShortcutManagerCompat.setDynamicShortcuts(context, shortcuts);
}
/**
* Pushes a dynamic shortcut for a given recipient to the shortcut manager
*/
@RequiresApi(CONVERSATION_SUPPORT_VERSION)
@WorkerThread
private static void pushShortcutForRecipientInternal(@NonNull Context context, @NonNull Recipient recipient, int rank) {
ShortcutInfo shortcutInfo = buildShortcutInfo(context, recipient, rank);
ShortcutManager shortcutManager = ServiceUtil.getShortcutManager(context);
ShortcutInfoCompat shortcutInfo = buildShortcutInfo(context, recipient, rank);
shortcutManager.pushDynamicShortcut(shortcutInfo);
ShortcutManagerCompat.pushDynamicShortcut(context, shortcutInfo);
}
/**
@@ -206,13 +187,12 @@ public final class ConversationUtil {
* @param context The Context under which we are operating
* @param recipient The Recipient to generate a ShortcutInfo for
* @param rank The rank that should be assigned to this recipient
* @return The new ShortcutInfo
* @return The new ShortcutInfo
*/
@RequiresApi(CONVERSATION_SUPPORT_VERSION)
@WorkerThread
private static @NonNull ShortcutInfo buildShortcutInfo(@NonNull Context context,
@NonNull Recipient recipient,
int rank)
private static @NonNull ShortcutInfoCompat buildShortcutInfo(@NonNull Context context,
@NonNull Recipient recipient,
int rank)
{
Recipient resolved = recipient.resolve();
Person[] persons = buildPersons(context, resolved);
@@ -220,49 +200,46 @@ public final class ConversationUtil {
String shortName = resolved.isSelf() ? context.getString(R.string.note_to_self) : resolved.getShortDisplayName(context);
String longName = resolved.isSelf() ? context.getString(R.string.note_to_self) : resolved.getDisplayName(context);
return new ShortcutInfo.Builder(context, getShortcutId(resolved))
.setLongLived(true)
.setIntent(ConversationIntents.createBuilder(context, resolved.getId(), threadId).build())
.setShortLabel(shortName)
.setLongLabel(longName)
.setIcon(AvatarUtil.getIconForShortcut(context, resolved))
.setPersons(persons)
.setCategories(Collections.singleton(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION))
.setActivity(new ComponentName(context, "org.thoughtcrime.securesms.RoutingActivity"))
.setRank(rank)
.build();
return new ShortcutInfoCompat.Builder(context, getShortcutId(resolved))
.setLongLived(true)
.setIntent(ConversationIntents.createBuilder(context, resolved.getId(), threadId).build())
.setShortLabel(shortName)
.setLongLabel(longName)
.setIcon(AvatarUtil.getIconCompatForShortcut(context, resolved))
.setPersons(persons)
.setCategories(Collections.singleton("android.shortcut.conversation"))
.setActivity(new ComponentName(context, "org.thoughtcrime.securesms.RoutingActivity"))
.setRank(rank)
.build();
}
/**
* @return an array of Person objects correlating to members of a conversation (other than self)
*/
@RequiresApi(CONVERSATION_SUPPORT_VERSION)
@WorkerThread
private static @NonNull Person[] buildPersons(@NonNull Context context, @NonNull Recipient recipient) {
if (recipient.isGroup()) {
return buildPersonsForGroup(context, recipient.getGroupId().get());
} else {
return new Person[]{buildPerson(context, recipient)};
return new Person[] { buildPerson(context, recipient) };
}
}
/**
* @return an array of Person objects correlating to members of a group (other than self)
*/
@RequiresApi(CONVERSATION_SUPPORT_VERSION)
@WorkerThread
private static @NonNull Person[] buildPersonsForGroup(@NonNull Context context, @NonNull GroupId groupId) {
List<Recipient> members = DatabaseFactory.getGroupDatabase(context).getGroupMembers(groupId, GroupDatabase.MemberSet.FULL_MEMBERS_EXCLUDING_SELF);
return Stream.of(members).map(member -> buildPerson(context, member.resolve())).toArray(Person[]::new);
return Stream.of(members).map(member -> buildPersonWithoutIcon(context, member.resolve())).toArray(Person[]::new);
}
/**
* @return A Person object representing the given Recipient
*/
@RequiresApi(28)
@WorkerThread
public static @NonNull Person buildPerson(@NonNull Context context, @NonNull Recipient recipient) {
public static @NonNull Person buildPersonWithoutIcon(@NonNull Context context, @NonNull Recipient recipient) {
return new Person.Builder()
.setKey(getShortcutId(recipient.getId()))
.setName(recipient.getDisplayName(context))
@@ -274,12 +251,12 @@ public final class ConversationUtil {
* @return A Compat Library Person object representing the given Recipient
*/
@WorkerThread
public static @NonNull androidx.core.app.Person buildPersonCompat(@NonNull Context context, @NonNull Recipient recipient) {
return new androidx.core.app.Person.Builder()
.setKey(getShortcutId(recipient.getId()))
.setName(recipient.getDisplayName(context))
.setIcon(AvatarUtil.getIconForNotification(context, recipient))
.setUri(recipient.isSystemContact() ? recipient.getContactUri().toString() : null)
.build();
public static @NonNull Person buildPerson(@NonNull Context context, @NonNull Recipient recipient) {
return new Person.Builder()
.setKey(getShortcutId(recipient.getId()))
.setName(recipient.getDisplayName(context))
.setIcon(AvatarUtil.getIconForNotification(context, recipient))
.setUri(recipient.isSystemContact() ? recipient.getContactUri().toString() : null)
.build();
}
}

View File

@@ -8,7 +8,6 @@ import android.app.NotificationManager;
import android.app.job.JobScheduler;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.pm.ShortcutManager;
import android.hardware.SensorManager;
import android.hardware.display.DisplayManager;
import android.location.LocationManager;
@@ -35,11 +34,6 @@ public class ServiceUtil {
return (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
}
@RequiresApi(25)
public static @Nullable ShortcutManager getShortcutManager(@NonNull Context context) {
return ContextCompat.getSystemService(context, ShortcutManager.class);
}
public static WindowManager getWindowManager(Context context) {
return (WindowManager) context.getSystemService(Activity.WINDOW_SERVICE);
}