ShareUtils.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017 Mario Danic <mario@lovelyhq.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * Part of the code in ShareUtils was inspired by BottomSheet under the Apache licence
  21. * located here: https://github.com/Kennyc1012/BottomSheet/blob/master/library/src/main/java/com/kennyc/bottomsheet/BottomSheet.java#L425
  22. */
  23. package com.nextcloud.talk.utils;
  24. import android.content.Context;
  25. import android.content.Intent;
  26. import android.content.pm.PackageManager;
  27. import android.content.pm.ResolveInfo;
  28. import android.graphics.drawable.Drawable;
  29. import android.support.annotation.Nullable;
  30. import android.text.TextUtils;
  31. import com.kennyc.bottomsheet.adapters.AppAdapter;
  32. import com.nextcloud.talk.R;
  33. import com.nextcloud.talk.models.database.UserEntity;
  34. import com.nextcloud.talk.models.json.rooms.Room;
  35. import com.nextcloud.talk.utils.database.user.UserUtils;
  36. import java.util.ArrayList;
  37. import java.util.List;
  38. import java.util.Set;
  39. public class ShareUtils {
  40. public static String getStringForIntent(Context context, @Nullable String password, UserUtils userUtils, Room
  41. room) {
  42. UserEntity userEntity = userUtils.getCurrentUser();
  43. String shareString = "";
  44. if (userEntity != null && context != null) {
  45. shareString = String.format(context.getResources().getString(R.string.nc_share_text),
  46. userEntity.getBaseUrl(), room.getToken());
  47. if (!TextUtils.isEmpty(password)) {
  48. shareString += String.format(context.getResources().getString(R.string.nc_share_text_pass), password);
  49. }
  50. shareString += String.format(context.getResources().getString(R.string.nc_talk_soon), userEntity
  51. .getDisplayName()
  52. );
  53. }
  54. return shareString;
  55. }
  56. public static List<AppAdapter.AppInfo> getShareApps(Context context, Intent intent,
  57. @Nullable Set<String> appsFilter, @Nullable Set<String> toExclude) {
  58. if (context == null || intent == null) return null;
  59. PackageManager manager = context.getPackageManager();
  60. List<ResolveInfo> apps = manager.queryIntentActivities(intent, 0);
  61. if (apps != null && !apps.isEmpty()) {
  62. List<AppAdapter.AppInfo> appResources = new ArrayList<>(apps.size());
  63. boolean shouldCheckPackages = appsFilter != null && !appsFilter.isEmpty();
  64. for (ResolveInfo resolveInfo : apps) {
  65. String packageName = resolveInfo.activityInfo.packageName;
  66. if (shouldCheckPackages && !appsFilter.contains(packageName)) {
  67. continue;
  68. }
  69. String title = resolveInfo.loadLabel(manager).toString();
  70. String name = resolveInfo.activityInfo.name;
  71. Drawable drawable = resolveInfo.loadIcon(manager);
  72. appResources.add(new AppAdapter.AppInfo(title, packageName, name, drawable));
  73. }
  74. if (toExclude != null && !toExclude.isEmpty()) {
  75. List<AppAdapter.AppInfo> toRemove = new ArrayList<>();
  76. for (AppAdapter.AppInfo appInfo : appResources) {
  77. if (toExclude.contains(appInfo.packageName)) {
  78. toRemove.add(appInfo);
  79. }
  80. }
  81. if (!toRemove.isEmpty()) appResources.removeAll(toRemove);
  82. }
  83. return appResources;
  84. }
  85. return null;
  86. }
  87. }