AccountUtils.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. * Parts related to account import were either copied from or inspired by the great work done by David Luhmer at:
  21. * https://github.com/nextcloud/ownCloud-Account-Importer
  22. */
  23. package com.nextcloud.talk.utils;
  24. import android.accounts.Account;
  25. import android.accounts.AccountManager;
  26. import android.content.Context;
  27. import android.content.pm.PackageManager;
  28. import android.util.Log;
  29. import com.nextcloud.talk.R;
  30. import com.nextcloud.talk.application.NextcloudTalkApplication;
  31. import com.nextcloud.talk.models.ImportAccount;
  32. import com.nextcloud.talk.persistence.entities.UserEntity;
  33. import java.util.ArrayList;
  34. import java.util.List;
  35. public class AccountUtils {
  36. private static final String TAG = "AccountUtils";
  37. public static List<Account> findAccounts(List<UserEntity> userEntitiesList) {
  38. Context context = NextcloudTalkApplication.getSharedApplication().getApplicationContext();
  39. final AccountManager accMgr = AccountManager.get(context);
  40. final Account[] accounts = accMgr.getAccountsByType(context.getString(R.string.nc_import_account_type));
  41. List<Account> accountsAvailable = new ArrayList<>();
  42. ImportAccount importAccount;
  43. UserEntity internalUserEntity;
  44. boolean accountFound;
  45. for (Account account : accounts) {
  46. accountFound = false;
  47. for (int i = 0; i < userEntitiesList.size(); i++) {
  48. internalUserEntity = userEntitiesList.get(i);
  49. importAccount = getInformationFromAccount(account);
  50. if (importAccount.getBaseUrl().startsWith("http://") ||
  51. importAccount.getBaseUrl().startsWith("https://")) {
  52. if (internalUserEntity.getUsername().equals(importAccount.getUsername()) &&
  53. internalUserEntity.getBaseUrl().equals(importAccount.getBaseUrl())) {
  54. accountFound = true;
  55. break;
  56. }
  57. } else {
  58. if (internalUserEntity.getUsername().equals(importAccount.getUsername()) &&
  59. (internalUserEntity.getBaseUrl().equals("http://" + importAccount.getBaseUrl()) ||
  60. internalUserEntity.getBaseUrl().equals("https://" +
  61. importAccount.getBaseUrl()))) {
  62. accountFound = true;
  63. break;
  64. }
  65. }
  66. }
  67. if (!accountFound) {
  68. accountsAvailable.add(account);
  69. }
  70. }
  71. return accountsAvailable;
  72. }
  73. public static String getAppNameBasedOnPackage(String packageName) {
  74. Context context = NextcloudTalkApplication.getSharedApplication().getApplicationContext();
  75. PackageManager packageManager = context.getPackageManager();
  76. String appName = "";
  77. try {
  78. appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA));
  79. } catch (PackageManager.NameNotFoundException e) {
  80. Log.e(TAG, "Failed to get app name based on package");
  81. }
  82. return appName;
  83. }
  84. public static ImportAccount getInformationFromAccount(Account account) {
  85. int lastAtPos = account.name.lastIndexOf("@");
  86. String urlString = account.name.substring(lastAtPos + 1);
  87. String username = account.name.substring(0, lastAtPos);
  88. Context context = NextcloudTalkApplication.getSharedApplication().getApplicationContext();
  89. final AccountManager accMgr = AccountManager.get(context);
  90. String password = accMgr.getPassword(account);
  91. if (urlString.endsWith("/")) {
  92. urlString = urlString.substring(0, urlString.length() - 1);
  93. }
  94. return new ImportAccount(username, password, urlString);
  95. }
  96. }