FileSyncAdapter.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author Bartek Przybylski
  5. * @author David A. Velasco
  6. * Copyright (C) 2011 Bartek Przybylski
  7. * Copyright (C) 2015 ownCloud Inc.
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. package com.owncloud.android.syncadapter;
  23. import android.accounts.Account;
  24. import android.accounts.AccountsException;
  25. import android.app.NotificationManager;
  26. import android.app.PendingIntent;
  27. import android.content.AbstractThreadedSyncAdapter;
  28. import android.content.ContentProviderClient;
  29. import android.content.ContentResolver;
  30. import android.content.Context;
  31. import android.content.Intent;
  32. import android.content.SyncResult;
  33. import android.os.Bundle;
  34. import com.nextcloud.client.account.UserAccountManager;
  35. import com.owncloud.android.R;
  36. import com.owncloud.android.authentication.AuthenticatorActivity;
  37. import com.owncloud.android.datamodel.FileDataStorageManager;
  38. import com.owncloud.android.datamodel.OCFile;
  39. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  40. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  41. import com.owncloud.android.lib.common.utils.Log_OC;
  42. import com.owncloud.android.operations.RefreshFolderOperation;
  43. import com.owncloud.android.operations.SynchronizeFolderOperation;
  44. import com.owncloud.android.operations.UpdateOCVersionOperation;
  45. import com.owncloud.android.ui.activity.ErrorsWhileCopyingHandlerActivity;
  46. import com.owncloud.android.ui.notifications.NotificationUtils;
  47. import com.owncloud.android.utils.DataHolderUtil;
  48. import com.owncloud.android.utils.theme.ThemeColorUtils;
  49. import org.apache.jackrabbit.webdav.DavException;
  50. import java.io.IOException;
  51. import java.util.ArrayList;
  52. import java.util.HashMap;
  53. import java.util.List;
  54. import java.util.Map;
  55. import androidx.annotation.PluralsRes;
  56. import androidx.core.app.NotificationCompat;
  57. import androidx.localbroadcastmanager.content.LocalBroadcastManager;
  58. /**
  59. * Implementation of {@link AbstractThreadedSyncAdapter} responsible for synchronizing
  60. * ownCloud files.
  61. *
  62. * Performs a full synchronization of the account received in {@link #onPerformSync(Account, Bundle,
  63. * String, ContentProviderClient, SyncResult)}.
  64. */
  65. public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
  66. private final static String TAG = FileSyncAdapter.class.getSimpleName();
  67. /** Maximum number of failed folder synchronizations that are supported before finishing
  68. * the synchronization operation */
  69. private static final int MAX_FAILED_RESULTS = 3;
  70. public static final String EVENT_FULL_SYNC_START = FileSyncAdapter.class.getName() +
  71. ".EVENT_FULL_SYNC_START";
  72. public static final String EVENT_FULL_SYNC_END = FileSyncAdapter.class.getName() +
  73. ".EVENT_FULL_SYNC_END";
  74. public static final String EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED =
  75. FileSyncAdapter.class.getName() + ".EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED";
  76. public static final String EXTRA_ACCOUNT_NAME = FileSyncAdapter.class.getName() +
  77. ".EXTRA_ACCOUNT_NAME";
  78. public static final String EXTRA_FOLDER_PATH = FileSyncAdapter.class.getName() +
  79. ".EXTRA_FOLDER_PATH";
  80. public static final String EXTRA_RESULT = FileSyncAdapter.class.getName() + ".EXTRA_RESULT";
  81. /** Time stamp for the current synchronization process, used to distinguish fresh data */
  82. private long mCurrentSyncTime;
  83. /** Flag made 'true' when a request to cancel the synchronization is received */
  84. private boolean mCancellation;
  85. /** Counter for failed operations in the synchronization process */
  86. private int mFailedResultsCounter;
  87. /** Result of the last failed operation */
  88. private RemoteOperationResult mLastFailedResult;
  89. /** Counter of conflicts found between local and remote files */
  90. private int mConflictsFound;
  91. /**
  92. * Counter of failed operations in synchronization of kept-in-sync files
  93. */
  94. private int mFailsInFavouritesFound;
  95. /**
  96. * Map of remote and local paths to files that where locally stored in a location out of the ownCloud folder and
  97. * couldn't be copied automatically into it
  98. */
  99. private Map<String, String> mForgottenLocalFiles;
  100. /**
  101. * {@link SyncResult} instance to return to the system when the synchronization finish
  102. */
  103. private SyncResult mSyncResult;
  104. private final ThemeColorUtils themeColorUtils;
  105. /**
  106. * Creates a {@link FileSyncAdapter}
  107. * <p>
  108. * {@inheritDoc}
  109. */
  110. public FileSyncAdapter(Context context,
  111. boolean autoInitialize,
  112. UserAccountManager userAccountManager,
  113. ThemeColorUtils themeColorUtils) {
  114. super(context, autoInitialize, userAccountManager);
  115. this.themeColorUtils = themeColorUtils;
  116. }
  117. /**
  118. * Creates a {@link FileSyncAdapter}
  119. * <p>
  120. * {@inheritDoc}
  121. */
  122. public FileSyncAdapter(Context context,
  123. boolean autoInitialize,
  124. boolean allowParallelSyncs,
  125. UserAccountManager userAccountManager,
  126. ThemeColorUtils themeColorUtils) {
  127. super(context, autoInitialize, allowParallelSyncs, userAccountManager);
  128. this.themeColorUtils = themeColorUtils;
  129. }
  130. /**
  131. * {@inheritDoc}
  132. */
  133. @Override
  134. public synchronized void onPerformSync(Account account, Bundle extras,
  135. String authority, ContentProviderClient providerClient,
  136. SyncResult syncResult) {
  137. mCancellation = false;
  138. mFailedResultsCounter = 0;
  139. mLastFailedResult = null;
  140. mConflictsFound = 0;
  141. mFailsInFavouritesFound = 0;
  142. mForgottenLocalFiles = new HashMap<String, String>();
  143. mSyncResult = syncResult;
  144. mSyncResult.fullSyncRequested = false;
  145. mSyncResult.delayUntil = (System.currentTimeMillis()/1000) + 3*60*60; // avoid too many automatic synchronizations
  146. this.setAccount(account);
  147. this.setContentProviderClient(providerClient);
  148. this.setStorageManager(new FileDataStorageManager(getUser(), providerClient));
  149. try {
  150. this.initClientForCurrentAccount();
  151. } catch (IOException | AccountsException e) {
  152. /// the account is unknown for the Synchronization Manager, unreachable this context,
  153. // or can not be authenticated; don't try this again
  154. mSyncResult.tooManyRetries = true;
  155. notifyFailedSynchronization();
  156. return;
  157. }
  158. Log_OC.d(TAG, "Synchronization of ownCloud account " + account.name + " starting");
  159. sendLocalBroadcast(EVENT_FULL_SYNC_START, null, null); // message to signal the start
  160. // of the synchronization to the UI
  161. /* When 'true' the process was requested by the user through the user interface;
  162. when 'false', it was requested automatically by the system */
  163. boolean mIsManualSync = extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
  164. try {
  165. updateOCVersion();
  166. mCurrentSyncTime = System.currentTimeMillis();
  167. if (!mCancellation) {
  168. synchronizeFolder(getStorageManager().getFileByPath(OCFile.ROOT_PATH));
  169. } else {
  170. Log_OC.d(TAG, "Leaving synchronization before synchronizing the root folder " +
  171. "because cancellation request");
  172. }
  173. } finally {
  174. // it's important making this although very unexpected errors occur;
  175. // that's the reason for the finally
  176. if (mFailedResultsCounter > 0 && mIsManualSync) {
  177. /// don't let the system synchronization manager retries MANUAL synchronizations
  178. // (be careful: "MANUAL" currently includes the synchronization requested when
  179. // a new account is created and when the user changes the current account)
  180. mSyncResult.tooManyRetries = true;
  181. /// notify the user about the failure of MANUAL synchronization
  182. notifyFailedSynchronization();
  183. }
  184. if (mConflictsFound > 0 || mFailsInFavouritesFound > 0) {
  185. notifyFailsInFavourites();
  186. }
  187. if (mForgottenLocalFiles.size() > 0) {
  188. notifyForgottenLocalFiles();
  189. }
  190. sendLocalBroadcast(EVENT_FULL_SYNC_END, null, mLastFailedResult); // message to signal
  191. // the end to the UI
  192. }
  193. }
  194. /**
  195. * Called by system SyncManager when a synchronization is required to be cancelled.
  196. *
  197. * Sets the mCancellation flag to 'true'. THe synchronization will be stopped later,
  198. * before a new folder is fetched. Data of the last folder synchronized will be still
  199. * locally saved.
  200. *
  201. * See {@link #onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)}
  202. * and {@link #synchronizeFolder(OCFile)}.
  203. */
  204. @Override
  205. public void onSyncCanceled() {
  206. Log_OC.d(TAG, "Synchronization of " + getAccount().name + " has been requested to cancel");
  207. mCancellation = true;
  208. super.onSyncCanceled();
  209. }
  210. /**
  211. * Updates the locally stored version value of the ownCloud server
  212. */
  213. private void updateOCVersion() {
  214. UpdateOCVersionOperation update = new UpdateOCVersionOperation(getUser(), getContext());
  215. RemoteOperationResult result = update.execute(getClient());
  216. if (!result.isSuccess()) {
  217. mLastFailedResult = result;
  218. }
  219. }
  220. /**
  221. * Synchronizes the list of files contained in a folder identified with its remote path.
  222. *
  223. * Fetches the list and properties of the files contained in the given folder, including their
  224. * properties, and updates the local database with them.
  225. *
  226. * Enters in the child folders to synchronize their contents also, following a recursive
  227. * depth first strategy.
  228. *
  229. * @param folder Folder to synchronize.
  230. */
  231. private void synchronizeFolder(OCFile folder) {
  232. if (mFailedResultsCounter > MAX_FAILED_RESULTS || isFinisher(mLastFailedResult)) {
  233. return;
  234. }
  235. // folder synchronization
  236. RefreshFolderOperation synchFolderOp = new RefreshFolderOperation(folder,
  237. mCurrentSyncTime,
  238. true,
  239. false,
  240. getStorageManager(),
  241. getUser(),
  242. getContext());
  243. RemoteOperationResult result = synchFolderOp.execute(getClient());
  244. // synchronized folder -> notice to UI - ALWAYS, although !result.isSuccess
  245. sendLocalBroadcast(EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED, folder.getRemotePath(), result);
  246. // check the result of synchronizing the folder
  247. if (result.isSuccess() || result.getCode() == ResultCode.SYNC_CONFLICT) {
  248. if (result.getCode() == ResultCode.SYNC_CONFLICT) {
  249. mConflictsFound += synchFolderOp.getConflictsFound();
  250. mFailsInFavouritesFound += synchFolderOp.getFailsInKeptInSyncFound();
  251. }
  252. if (synchFolderOp.getForgottenLocalFiles().size() > 0) {
  253. mForgottenLocalFiles.putAll(synchFolderOp.getForgottenLocalFiles());
  254. }
  255. if (result.isSuccess()) {
  256. // synchronize children folders
  257. List<OCFile> children = synchFolderOp.getChildren();
  258. // beware of the 'hidden' recursion here!
  259. syncChildren(children);
  260. }
  261. } else if (result.getCode() != ResultCode.FILE_NOT_FOUND) {
  262. // in failures, the statistics for the global result are updated
  263. if (ResultCode.UNAUTHORIZED == result.getCode()) {
  264. mSyncResult.stats.numAuthExceptions++;
  265. } else if (result.getException() instanceof DavException) {
  266. mSyncResult.stats.numParseExceptions++;
  267. } else if (result.getException() instanceof IOException) {
  268. mSyncResult.stats.numIoExceptions++;
  269. }
  270. mFailedResultsCounter++;
  271. mLastFailedResult = result;
  272. } // else, ResultCode.FILE_NOT_FOUND is ignored, remote folder was
  273. // removed from other thread or other client during the synchronization,
  274. // before this thread fetched its contents
  275. }
  276. /**
  277. * Checks if a failed result should terminate the synchronization process immediately,
  278. * according to OUR OWN POLICY
  279. *
  280. * @param failedResult Remote operation result to check.
  281. * @return 'True' if the result should immediately finish the
  282. * synchronization
  283. */
  284. private boolean isFinisher(RemoteOperationResult failedResult) {
  285. if (failedResult != null) {
  286. RemoteOperationResult.ResultCode code = failedResult.getCode();
  287. return code == ResultCode.SSL_ERROR
  288. || code == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED
  289. || code == ResultCode.BAD_OC_VERSION
  290. || code == ResultCode.INSTANCE_NOT_CONFIGURED;
  291. }
  292. return false;
  293. }
  294. /**
  295. * Triggers the synchronization of any folder contained in the list of received files.
  296. *
  297. * No consideration of etag here because it MUST walk down anyway, in case that kept-in-sync files
  298. * have local changes.
  299. *
  300. * @param files Files to recursively synchronize.
  301. */
  302. private void syncChildren(List<OCFile> files) {
  303. int i;
  304. OCFile newFile;
  305. for (i=0; i < files.size() && !mCancellation; i++) {
  306. newFile = files.get(i);
  307. if (newFile.isFolder()) {
  308. synchronizeFolder(newFile);
  309. }
  310. }
  311. if (mCancellation && i <files.size()) {
  312. Log_OC.d(TAG,
  313. "Leaving synchronization before synchronizing " + files.get(i).getRemotePath() +
  314. " due to cancellation request");
  315. }
  316. }
  317. /**
  318. * Sends a message to any application component interested in the progress of the
  319. * synchronization.
  320. *
  321. * @param event Event in the process of synchronization to be notified.
  322. * @param dirRemotePath Remote path of the folder target of the event occurred.
  323. * @param result Result of an individual {@link SynchronizeFolderOperation},
  324. * if completed; may be null.
  325. */
  326. private void sendLocalBroadcast(String event, String dirRemotePath,
  327. RemoteOperationResult result) {
  328. Log_OC.d(TAG, "Send broadcast " + event);
  329. Intent intent = new Intent(event);
  330. intent.putExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME, getAccount().name);
  331. if (dirRemotePath != null) {
  332. intent.putExtra(FileSyncAdapter.EXTRA_FOLDER_PATH, dirRemotePath);
  333. }
  334. if (result != null) {
  335. DataHolderUtil dataHolderUtil = DataHolderUtil.getInstance();
  336. String dataHolderItemId = dataHolderUtil.nextItemId();
  337. dataHolderUtil.save(dataHolderUtil.nextItemId(), result);
  338. intent.putExtra(FileSyncAdapter.EXTRA_RESULT, dataHolderItemId);
  339. }
  340. intent.setPackage(getContext().getPackageName());
  341. LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
  342. }
  343. /**
  344. * Notifies the user about a failed synchronization through the status notification bar
  345. */
  346. private void notifyFailedSynchronization() {
  347. NotificationCompat.Builder notificationBuilder = createNotificationBuilder();
  348. boolean needsToUpdateCredentials = mLastFailedResult != null
  349. && ResultCode.UNAUTHORIZED == mLastFailedResult.getCode();
  350. if (needsToUpdateCredentials) {
  351. // let the user update credentials with one click
  352. Intent updateAccountCredentials = new Intent(getContext(), AuthenticatorActivity.class);
  353. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, getAccount());
  354. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION,
  355. AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN);
  356. updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  357. updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
  358. updateAccountCredentials.addFlags(Intent.FLAG_FROM_BACKGROUND);
  359. notificationBuilder
  360. .setTicker(i18n(R.string.sync_fail_ticker_unauthorized))
  361. .setContentTitle(i18n(R.string.sync_fail_ticker_unauthorized))
  362. .setContentIntent(PendingIntent.getActivity(
  363. getContext(), (int)System.currentTimeMillis(), updateAccountCredentials,
  364. PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE
  365. ))
  366. .setContentText(i18n(R.string.sync_fail_content_unauthorized, getAccount().name));
  367. } else {
  368. notificationBuilder
  369. .setTicker(i18n(R.string.sync_fail_ticker))
  370. .setContentTitle(i18n(R.string.sync_fail_ticker))
  371. .setContentText(i18n(R.string.sync_fail_content, getAccount().name));
  372. }
  373. showNotification(R.string.sync_fail_ticker, notificationBuilder);
  374. }
  375. /**
  376. * Notifies the user about conflicts and strange fails when trying to synchronize the contents
  377. * of kept-in-sync files.
  378. *
  379. * By now, we won't consider a failed synchronization.
  380. */
  381. private void notifyFailsInFavourites() {
  382. if (mFailedResultsCounter > 0) {
  383. NotificationCompat.Builder notificationBuilder = createNotificationBuilder();
  384. notificationBuilder.setTicker(i18n(R.string.sync_fail_in_favourites_ticker));
  385. // TODO put something smart in the contentIntent below
  386. notificationBuilder
  387. .setContentIntent(PendingIntent.getActivity(
  388. getContext(), (int) System.currentTimeMillis(), new Intent(), PendingIntent.FLAG_IMMUTABLE
  389. ))
  390. .setContentTitle(i18n(R.string.sync_fail_in_favourites_ticker))
  391. .setContentText(getQuantityString(
  392. R.plurals.sync_fail_in_favourites_content,
  393. mFailedResultsCounter,
  394. mFailedResultsCounter + mConflictsFound, mConflictsFound
  395. )
  396. );
  397. showNotification(R.string.sync_fail_in_favourites_ticker, notificationBuilder);
  398. } else {
  399. NotificationCompat.Builder notificationBuilder = createNotificationBuilder();
  400. notificationBuilder.setTicker(i18n(R.string.sync_conflicts_in_favourites_ticker));
  401. // TODO put something smart in the contentIntent below
  402. notificationBuilder
  403. .setContentIntent(PendingIntent.getActivity(
  404. getContext(), (int) System.currentTimeMillis(), new Intent(), PendingIntent.FLAG_IMMUTABLE
  405. ))
  406. .setContentTitle(i18n(R.string.sync_conflicts_in_favourites_ticker))
  407. .setContentText(i18n(R.string.sync_conflicts_in_favourites_ticker, mConflictsFound));
  408. showNotification(R.string.sync_conflicts_in_favourites_ticker, notificationBuilder);
  409. }
  410. }
  411. /**
  412. * Notifies the user about local copies of files out of the ownCloud local directory that
  413. * were 'forgotten' because copying them inside the ownCloud local directory was not possible.
  414. *
  415. * We don't want links to files out of the ownCloud local directory (foreign files) anymore.
  416. * It's easy to have synchronization problems if a local file is linked to more than one
  417. * remote file.
  418. *
  419. * We won't consider a synchronization as failed when foreign files can not be copied to
  420. * the ownCloud local directory.
  421. */
  422. private void notifyForgottenLocalFiles() {
  423. NotificationCompat.Builder notificationBuilder = createNotificationBuilder();
  424. notificationBuilder.setTicker(i18n(R.string.sync_foreign_files_forgotten_ticker));
  425. /// includes a pending intent in the notification showing a more detailed explanation
  426. Intent explanationIntent = new Intent(getContext(), ErrorsWhileCopyingHandlerActivity.class);
  427. explanationIntent.putExtra(ErrorsWhileCopyingHandlerActivity.EXTRA_USER, getUser());
  428. ArrayList<String> remotePaths = new ArrayList<String>();
  429. ArrayList<String> localPaths = new ArrayList<String>();
  430. remotePaths.addAll(mForgottenLocalFiles.keySet());
  431. localPaths.addAll(mForgottenLocalFiles.values());
  432. explanationIntent.putExtra(ErrorsWhileCopyingHandlerActivity.EXTRA_LOCAL_PATHS, localPaths);
  433. explanationIntent.putExtra(ErrorsWhileCopyingHandlerActivity.EXTRA_REMOTE_PATHS, remotePaths);
  434. explanationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  435. notificationBuilder
  436. .setContentIntent(PendingIntent.getActivity(
  437. getContext(), (int) System.currentTimeMillis(), explanationIntent, PendingIntent.FLAG_IMMUTABLE
  438. ))
  439. .setContentTitle(i18n(R.string.sync_foreign_files_forgotten_ticker))
  440. .setContentText(getQuantityString(
  441. R.plurals.sync_foreign_files_forgotten_content,
  442. mForgottenLocalFiles.size(),
  443. mForgottenLocalFiles.size(),
  444. i18n(R.string.app_name))
  445. );
  446. showNotification(R.string.sync_foreign_files_forgotten_ticker, notificationBuilder);
  447. }
  448. /**
  449. * Creates a notification builder with some commonly used settings
  450. *
  451. * @return notification builder
  452. */
  453. private NotificationCompat.Builder createNotificationBuilder() {
  454. NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getContext());
  455. notificationBuilder.setSmallIcon(R.drawable.notification_icon).setAutoCancel(true);
  456. notificationBuilder.setColor(themeColorUtils.primaryColor(getContext(), true));
  457. return notificationBuilder;
  458. }
  459. /**
  460. * Builds and shows the notification
  461. *
  462. * @param id
  463. * @param builder
  464. */
  465. private void showNotification(int id, NotificationCompat.Builder builder) {
  466. NotificationManager notificationManager = (NotificationManager) getContext().
  467. getSystemService(Context.NOTIFICATION_SERVICE);
  468. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
  469. builder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_FILE_SYNC);
  470. }
  471. notificationManager.notify(id, builder.build());
  472. }
  473. /**
  474. * Shorthand translation
  475. *
  476. * @param key
  477. * @param args
  478. * @return
  479. */
  480. private String i18n(int key, Object... args) {
  481. return getContext().getString(key, args);
  482. }
  483. /**
  484. * Shorthand plurals translation.
  485. *
  486. * @param id The desired plurals identifier.
  487. * @param quantity The number used to get the correct string for the current language's plural rules.
  488. * @param formatArgs The format arguments that will be used for substitution.
  489. * @return formatted, plurals respecting string
  490. */
  491. private String getQuantityString(@PluralsRes int id, int quantity, Object... formatArgs) {
  492. return getContext().getResources().getQuantityString(id, quantity, formatArgs);
  493. }
  494. }