AppModule.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Chris Narkiewicz
  5. * Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero 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 Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.client.di;
  21. import android.accounts.AccountManager;
  22. import android.app.Application;
  23. import android.content.ContentResolver;
  24. import android.content.Context;
  25. import android.content.res.Resources;
  26. import android.os.Handler;
  27. import com.nextcloud.client.account.CurrentAccountProvider;
  28. import com.nextcloud.client.account.UserAccountManager;
  29. import com.nextcloud.client.account.UserAccountManagerImpl;
  30. import com.nextcloud.client.core.AsyncRunner;
  31. import com.nextcloud.client.core.ThreadPoolAsyncRunner;
  32. import com.nextcloud.client.core.Clock;
  33. import com.nextcloud.client.core.ClockImpl;
  34. import com.nextcloud.client.device.DeviceInfo;
  35. import com.nextcloud.client.logger.FileLogHandler;
  36. import com.nextcloud.client.logger.Logger;
  37. import com.nextcloud.client.logger.LoggerImpl;
  38. import com.nextcloud.client.logger.LogsRepository;
  39. import com.owncloud.android.datamodel.ArbitraryDataProvider;
  40. import com.owncloud.android.datamodel.UploadsStorageManager;
  41. import com.owncloud.android.ui.activities.data.activities.ActivitiesRepository;
  42. import com.owncloud.android.ui.activities.data.activities.ActivitiesServiceApi;
  43. import com.owncloud.android.ui.activities.data.activities.ActivitiesServiceApiImpl;
  44. import com.owncloud.android.ui.activities.data.activities.RemoteActivitiesRepository;
  45. import com.owncloud.android.ui.activities.data.files.FilesRepository;
  46. import com.owncloud.android.ui.activities.data.files.FilesServiceApiImpl;
  47. import com.owncloud.android.ui.activities.data.files.RemoteFilesRepository;
  48. import java.io.File;
  49. import javax.inject.Singleton;
  50. import dagger.Module;
  51. import dagger.Provides;
  52. @Module(includes = {ComponentsModule.class, VariantComponentsModule.class})
  53. class AppModule {
  54. @Provides
  55. AccountManager accountManager(Application application) {
  56. return (AccountManager)application.getSystemService(Context.ACCOUNT_SERVICE);
  57. }
  58. @Provides
  59. Context context(Application application) {
  60. return application;
  61. }
  62. @Provides
  63. Resources resources(Application application) {
  64. return application.getResources();
  65. }
  66. @Provides
  67. UserAccountManager userAccountManager(
  68. Context context,
  69. AccountManager accountManager
  70. ) {
  71. return new UserAccountManagerImpl(context, accountManager);
  72. }
  73. @Provides
  74. ArbitraryDataProvider arbitraryDataProvider(Context context) {
  75. final ContentResolver resolver = context.getContentResolver();
  76. return new ArbitraryDataProvider(resolver);
  77. }
  78. @Provides
  79. ActivitiesServiceApi activitiesServiceApi(UserAccountManager accountManager) {
  80. return new ActivitiesServiceApiImpl(accountManager);
  81. }
  82. @Provides
  83. ActivitiesRepository activitiesRepository(ActivitiesServiceApi api) {
  84. return new RemoteActivitiesRepository(api);
  85. }
  86. @Provides
  87. FilesRepository filesRepository(UserAccountManager accountManager) {
  88. return new RemoteFilesRepository(new FilesServiceApiImpl(accountManager));
  89. }
  90. @Provides
  91. UploadsStorageManager uploadsStorageManager(Context context,
  92. CurrentAccountProvider currentAccountProvider) {
  93. return new UploadsStorageManager(currentAccountProvider, context.getContentResolver());
  94. }
  95. @Provides CurrentAccountProvider currentAccountProvider(UserAccountManager accountManager) {
  96. return accountManager;
  97. }
  98. @Provides
  99. DeviceInfo deviceInfo() {
  100. return new DeviceInfo();
  101. }
  102. @Provides
  103. @Singleton
  104. Clock clock() {
  105. return new ClockImpl();
  106. }
  107. @Provides
  108. @Singleton
  109. Logger logger(Context context, Clock clock) {
  110. File logDir = new File(context.getFilesDir(), "logs");
  111. FileLogHandler handler = new FileLogHandler(logDir, "log.txt", 1024*1024);
  112. LoggerImpl logger = new LoggerImpl(clock, handler, new Handler(), 1000);
  113. logger.start();
  114. return logger;
  115. }
  116. @Provides
  117. @Singleton
  118. LogsRepository logsRepository(Logger logger) {
  119. return (LogsRepository)logger;
  120. }
  121. @Provides
  122. @Singleton
  123. AsyncRunner asyncRunner() {
  124. Handler uiHandler = new Handler();
  125. return new ThreadPoolAsyncRunner(uiHandler, 4);
  126. }
  127. }