AccountUtils.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.PackageInfo;
  28. import android.content.pm.PackageManager;
  29. import android.util.Log;
  30. import com.nextcloud.talk.R;
  31. import com.nextcloud.talk.application.NextcloudTalkApplication;
  32. import com.nextcloud.talk.models.ImportAccount;
  33. import com.nextcloud.talk.models.database.UserEntity;
  34. import java.util.ArrayList;
  35. import java.util.List;
  36. public class AccountUtils {
  37. private static final String TAG = "AccountUtils";
  38. public static List<Account> findAccounts(List<UserEntity> userEntitiesList) {
  39. Context context = NextcloudTalkApplication.getSharedApplication().getApplicationContext();
  40. final AccountManager accMgr = AccountManager.get(context);
  41. final Account[] accounts = accMgr.getAccountsByType(context.getString(R.string.nc_import_account_type));
  42. List<Account> accountsAvailable = new ArrayList<>();
  43. ImportAccount importAccount;
  44. UserEntity internalUserEntity;
  45. boolean accountFound;
  46. for (Account account : accounts) {
  47. accountFound = false;
  48. for (int i = 0; i < userEntitiesList.size(); i++) {
  49. internalUserEntity = userEntitiesList.get(i);
  50. importAccount = getInformationFromAccount(account);
  51. if (importAccount.getToken() != null) {
  52. if (importAccount.getBaseUrl().startsWith("http://") ||
  53. importAccount.getBaseUrl().startsWith("https://")) {
  54. if (internalUserEntity.getUsername().equals(importAccount.getUsername()) &&
  55. internalUserEntity.getBaseUrl().equals(importAccount.getBaseUrl())) {
  56. accountFound = true;
  57. break;
  58. }
  59. } else {
  60. if (internalUserEntity.getUsername().equals(importAccount.getUsername()) &&
  61. (internalUserEntity.getBaseUrl().equals("http://" + importAccount.getBaseUrl()) ||
  62. internalUserEntity.getBaseUrl().equals("https://" +
  63. importAccount.getBaseUrl()))) {
  64. accountFound = true;
  65. break;
  66. }
  67. }
  68. } else {
  69. accountFound = true;
  70. break;
  71. }
  72. }
  73. if (!accountFound) {
  74. accountsAvailable.add(account);
  75. }
  76. }
  77. return accountsAvailable;
  78. }
  79. public static String getAppNameBasedOnPackage(String packageName) {
  80. Context context = NextcloudTalkApplication.getSharedApplication().getApplicationContext();
  81. PackageManager packageManager = context.getPackageManager();
  82. String appName = "";
  83. try {
  84. appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(packageName,
  85. PackageManager.GET_META_DATA));
  86. } catch (PackageManager.NameNotFoundException e) {
  87. Log.e(TAG, "Failed to get app name based on package");
  88. }
  89. return appName;
  90. }
  91. public static boolean canWeOpenFilesApp(Context context, String accountName) {
  92. PackageManager pm = context.getPackageManager();
  93. try {
  94. PackageInfo packageInfo =
  95. pm.getPackageInfo(context.getString(R.string.nc_import_accounts_from), 0);
  96. if (packageInfo.versionCode < 1) {
  97. final AccountManager accMgr = AccountManager.get(context);
  98. final Account[] accounts = accMgr.getAccountsByType(context.getString(R.string.nc_import_account_type));
  99. for (Account account : accounts) {
  100. if (account.name.equals(accountName)) {
  101. return true;
  102. }
  103. }
  104. }
  105. } catch (PackageManager.NameNotFoundException appNotFoundException) {
  106. }
  107. return false;
  108. }
  109. public static ImportAccount getInformationFromAccount(Account account) {
  110. int lastAtPos = account.name.lastIndexOf("@");
  111. String urlString = account.name.substring(lastAtPos + 1);
  112. String username = account.name.substring(0, lastAtPos);
  113. Context context = NextcloudTalkApplication.getSharedApplication().getApplicationContext();
  114. final AccountManager accMgr = AccountManager.get(context);
  115. String password = null;
  116. try {
  117. password = accMgr.getPassword(account);
  118. } catch (Exception exception) {
  119. Log.e(TAG, "Failed to import account");
  120. }
  121. if (urlString.endsWith("/")) {
  122. urlString = urlString.substring(0, urlString.length() - 1);
  123. }
  124. return new ImportAccount(username, password, urlString);
  125. }
  126. }