FolderSyncActivity.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * @author Andy Scherzinger
  5. * Copyright (C) 2016 Andy Scherzinger
  6. * Copyright (C) 2016 Nextcloud
  7. * <p>
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. * <p>
  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 AFFERO GENERAL PUBLIC LICENSE for more details.
  17. * <p>
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui.activity;
  22. import android.content.Intent;
  23. import android.os.Bundle;
  24. import android.os.Handler;
  25. import android.support.annotation.NonNull;
  26. import android.support.v4.app.FragmentManager;
  27. import android.support.v4.app.FragmentTransaction;
  28. import android.support.v7.widget.GridLayoutManager;
  29. import android.support.v7.widget.RecyclerView;
  30. import android.view.MenuItem;
  31. import android.view.View;
  32. import android.widget.LinearLayout;
  33. import android.widget.TextView;
  34. import com.owncloud.android.MainApp;
  35. import com.owncloud.android.R;
  36. import com.owncloud.android.authentication.AccountUtils;
  37. import com.owncloud.android.datamodel.MediaFolder;
  38. import com.owncloud.android.datamodel.MediaProvider;
  39. import com.owncloud.android.datamodel.OCFile;
  40. import com.owncloud.android.datamodel.SyncedFolder;
  41. import com.owncloud.android.datamodel.SyncedFolderDisplayItem;
  42. import com.owncloud.android.datamodel.SyncedFolderProvider;
  43. import com.owncloud.android.ui.adapter.FolderSyncAdapter;
  44. import com.owncloud.android.ui.decoration.MediaGridItemDecoration;
  45. import com.owncloud.android.ui.dialog.SyncedFolderPreferencesDialogFragment;
  46. import com.owncloud.android.ui.dialog.parcel.SyncedFolderParcelable;
  47. import java.util.ArrayList;
  48. import java.util.Collections;
  49. import java.util.Comparator;
  50. import java.util.HashMap;
  51. import java.util.List;
  52. import java.util.Map;
  53. import java.util.TimerTask;
  54. import static com.owncloud.android.datamodel.SyncedFolderDisplayItem.UNPERSISTED_ID;
  55. /**
  56. * Activity displaying all auto-synced folders and/or instant upload media folders.
  57. */
  58. public class FolderSyncActivity extends FileActivity implements FolderSyncAdapter.ClickListener,
  59. SyncedFolderPreferencesDialogFragment.OnSyncedFolderPreferenceListener {
  60. private static final String SYNCED_FOLDER_PREFERENCES_DIALOG_TAG = "SYNCED_FOLDER_PREFERENCES_DIALOG";
  61. public static final String PRIORITIZED_FOLDER = "Camera";
  62. private RecyclerView mRecyclerView;
  63. private FolderSyncAdapter mAdapter;
  64. private LinearLayout mProgress;
  65. private TextView mEmpty;
  66. private SyncedFolderProvider mSyncedFolderProvider;
  67. private List<SyncedFolderDisplayItem> syncFolderItems;
  68. private SyncedFolderPreferencesDialogFragment mSyncedFolderPreferencesDialogFragment;
  69. @Override
  70. protected void onCreate(Bundle savedInstanceState) {
  71. super.onCreate(savedInstanceState);
  72. setContentView(R.layout.folder_sync_layout);
  73. // setup toolbar
  74. setupToolbar();
  75. // setup drawer
  76. setupDrawer(R.id.nav_folder_sync);
  77. getSupportActionBar().setTitle(getString(R.string.drawer_folder_sync));
  78. setupContent();
  79. }
  80. /**
  81. * sets up the UI elements and loads all media/synced folders.
  82. */
  83. private void setupContent() {
  84. mRecyclerView = (RecyclerView) findViewById(android.R.id.list);
  85. mProgress = (LinearLayout) findViewById(android.R.id.progress);
  86. mEmpty = (TextView) findViewById(android.R.id.empty);
  87. final int gridWidth = getResources().getInteger(R.integer.media_grid_width);
  88. mAdapter = new FolderSyncAdapter(this, gridWidth, this);
  89. mSyncedFolderProvider = new SyncedFolderProvider(getContentResolver());
  90. final GridLayoutManager lm = new GridLayoutManager(this, gridWidth);
  91. mAdapter.setLayoutManager(lm);
  92. int spacing = getResources().getDimensionPixelSize(R.dimen.media_grid_spacing);
  93. mRecyclerView.addItemDecoration(new MediaGridItemDecoration(spacing));
  94. mRecyclerView.setLayoutManager(lm);
  95. mRecyclerView.setAdapter(mAdapter);
  96. load(gridWidth * 2);
  97. }
  98. /**
  99. * loads all media/synced folders, adds them to the recycler view adapter and shows the list.
  100. *
  101. * @param perFolderMediaItemLimit the amount of media items to be loaded/shown per media folder
  102. */
  103. private void load(final int perFolderMediaItemLimit) {
  104. if (mAdapter.getItemCount() > 0) {
  105. return;
  106. }
  107. setListShown(false);
  108. final Handler mHandler = new Handler();
  109. new Thread(new Runnable() {
  110. @Override
  111. public void run() {
  112. final List<MediaFolder> mediaFolders = MediaProvider.getMediaFolders(getContentResolver(),
  113. perFolderMediaItemLimit);
  114. syncFolderItems = sortSyncedFolderItems(mergeFolderData(mSyncedFolderProvider.getSyncedFolders(),
  115. mediaFolders));
  116. mHandler.post(new TimerTask() {
  117. @Override
  118. public void run() {
  119. mAdapter.setSyncFolderItems(syncFolderItems);
  120. setListShown(true);
  121. }
  122. });
  123. }
  124. }).start();
  125. }
  126. /**
  127. * merges two lists of {@link SyncedFolder} and {@link MediaFolder} items into one of SyncedFolderItems.
  128. *
  129. * @param syncedFolders the synced folders
  130. * @param mediaFolders the media folders
  131. * @return the merged list of SyncedFolderItems
  132. */
  133. @NonNull
  134. private List<SyncedFolderDisplayItem> mergeFolderData(List<SyncedFolder> syncedFolders,
  135. @NonNull List<MediaFolder> mediaFolders) {
  136. Map<String, SyncedFolder> syncedFoldersMap = createSyncedFoldersMap(syncedFolders);
  137. List<SyncedFolderDisplayItem> result = new ArrayList<>();
  138. for (MediaFolder mediaFolder : mediaFolders) {
  139. if (syncedFoldersMap.containsKey(mediaFolder.absolutePath)) {
  140. SyncedFolder syncedFolder = syncedFoldersMap.get(mediaFolder.absolutePath);
  141. result.add(createSyncedFolder(syncedFolder, mediaFolder));
  142. } else {
  143. result.add(createSyncedFolderFromMediaFolder(mediaFolder));
  144. }
  145. }
  146. return result;
  147. }
  148. /**
  149. * Sorts list of {@link SyncedFolderDisplayItem}s.
  150. *
  151. * @param syncFolderItemList list of items to be sorted
  152. * @return sorted list of items
  153. */
  154. public static List<SyncedFolderDisplayItem> sortSyncedFolderItems(List<SyncedFolderDisplayItem>
  155. syncFolderItemList) {
  156. Collections.sort(syncFolderItemList, new Comparator<SyncedFolderDisplayItem>() {
  157. public int compare(SyncedFolderDisplayItem f1, SyncedFolderDisplayItem f2) {
  158. if (f1 == null && f2 == null) {
  159. return 0;
  160. } else if (f1 == null) {
  161. return -1;
  162. } else if (f2 == null) {
  163. return 1;
  164. } else if (f1.isEnabled() && f2.isEnabled()) {
  165. return f1.getFolderName().toLowerCase().compareTo(f2.getFolderName().toLowerCase());
  166. } else if (f1.isEnabled()) {
  167. return -1;
  168. } else if (f2.isEnabled()) {
  169. return 1;
  170. } else if (f1.getFolderName() == null && f2.getFolderName() == null) {
  171. return 0;
  172. } else if (f1.getFolderName() == null) {
  173. return -1;
  174. } else if (f2.getFolderName() == null) {
  175. return 1;
  176. } else if (PRIORITIZED_FOLDER.equals(f1.getFolderName())) {
  177. return -1;
  178. } else if (PRIORITIZED_FOLDER.equals(f2.getFolderName())) {
  179. return 1;
  180. } else {
  181. return f1.getFolderName().toLowerCase().compareTo(f2.getFolderName().toLowerCase());
  182. }
  183. }
  184. });
  185. return syncFolderItemList;
  186. }
  187. /**
  188. * creates a SyncedFolderDisplayItem merging a {@link SyncedFolder} and a {@link MediaFolder} object instance.
  189. *
  190. * @param syncedFolder the synced folder object
  191. * @param mediaFolder the media folder object
  192. * @return the created SyncedFolderDisplayItem
  193. */
  194. @NonNull
  195. private SyncedFolderDisplayItem createSyncedFolder(@NonNull SyncedFolder syncedFolder, @NonNull MediaFolder mediaFolder) {
  196. return new SyncedFolderDisplayItem(
  197. syncedFolder.getId(),
  198. syncedFolder.getLocalPath(),
  199. syncedFolder.getRemotePath(),
  200. syncedFolder.getWifiOnly(),
  201. syncedFolder.getChargingOnly(),
  202. syncedFolder.getSubfolderByDate(),
  203. syncedFolder.getAccount(),
  204. syncedFolder.getUploadAction(),
  205. syncedFolder.isEnabled(),
  206. mediaFolder.filePaths,
  207. mediaFolder.folderName,
  208. mediaFolder.numberOfFiles);
  209. }
  210. /**
  211. * creates a {@link SyncedFolderDisplayItem} based on a {@link MediaFolder} object instance.
  212. *
  213. * @param mediaFolder the media folder object
  214. * @return the created SyncedFolderDisplayItem
  215. */
  216. @NonNull
  217. private SyncedFolderDisplayItem createSyncedFolderFromMediaFolder(@NonNull MediaFolder mediaFolder) {
  218. return new SyncedFolderDisplayItem(
  219. UNPERSISTED_ID,
  220. mediaFolder.absolutePath,
  221. getString(R.string.instant_upload_path) + "/" + mediaFolder.folderName,
  222. true,
  223. false,
  224. false,
  225. AccountUtils.getCurrentOwnCloudAccount(this).name,
  226. 0,
  227. false,
  228. mediaFolder.filePaths,
  229. mediaFolder.folderName,
  230. mediaFolder.numberOfFiles);
  231. }
  232. /**
  233. * creates a lookup map for a list of given {@link SyncedFolder}s with their local path as the key.
  234. *
  235. * @param syncFolders list of {@link SyncedFolder}s
  236. * @return the lookup map for {@link SyncedFolder}s
  237. */
  238. @NonNull
  239. private Map<String, SyncedFolder> createSyncedFoldersMap(List<SyncedFolder> syncFolders) {
  240. Map<String, SyncedFolder> result = new HashMap<>();
  241. if (syncFolders != null) {
  242. for (SyncedFolder syncFolder : syncFolders) {
  243. result.put(syncFolder.getLocalPath(), syncFolder);
  244. }
  245. }
  246. return result;
  247. }
  248. /**
  249. * show/hide recycler view list or the empty message / progress info.
  250. *
  251. * @param shown flag if list should be shown
  252. */
  253. private void setListShown(boolean shown) {
  254. if (mRecyclerView != null) {
  255. mRecyclerView.setVisibility(shown ? View.VISIBLE : View.GONE);
  256. mProgress.setVisibility(shown ? View.GONE : View.VISIBLE);
  257. mEmpty.setVisibility(shown && mAdapter.getItemCount() == 0 ? View.VISIBLE : View.GONE);
  258. }
  259. }
  260. @Override
  261. public boolean onOptionsItemSelected(MenuItem item) {
  262. boolean result;
  263. switch (item.getItemId()) {
  264. case android.R.id.home: {
  265. if (isDrawerOpen()) {
  266. closeDrawer();
  267. } else {
  268. openDrawer();
  269. }
  270. }
  271. default:
  272. result = super.onOptionsItemSelected(item);
  273. }
  274. return result;
  275. }
  276. @Override
  277. public void restart() {
  278. Intent i = new Intent(this, FileDisplayActivity.class);
  279. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  280. startActivity(i);
  281. }
  282. @Override
  283. public void showFiles(boolean onDeviceOnly) {
  284. MainApp.showOnlyFilesOnDevice(onDeviceOnly);
  285. Intent fileDisplayActivity = new Intent(getApplicationContext(), FileDisplayActivity.class);
  286. fileDisplayActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  287. startActivity(fileDisplayActivity);
  288. }
  289. @Override
  290. public void onSyncStatusToggleClick(int section, SyncedFolderDisplayItem syncedFolderDisplayItem) {
  291. if (syncedFolderDisplayItem.getId() > UNPERSISTED_ID) {
  292. mSyncedFolderProvider.updateFolderSyncEnabled(syncedFolderDisplayItem.getId(), syncedFolderDisplayItem.isEnabled());
  293. } else {
  294. mSyncedFolderProvider.storeFolderSync(syncedFolderDisplayItem);
  295. }
  296. }
  297. @Override
  298. public void onSyncFolderSettingsClick(int section, SyncedFolderDisplayItem syncedFolderDisplayItem) {
  299. FragmentManager fm = getSupportFragmentManager();
  300. FragmentTransaction ft = fm.beginTransaction();
  301. ft.addToBackStack(null);
  302. mSyncedFolderPreferencesDialogFragment = SyncedFolderPreferencesDialogFragment.newInstance(
  303. syncedFolderDisplayItem, section);
  304. mSyncedFolderPreferencesDialogFragment.show(ft, SYNCED_FOLDER_PREFERENCES_DIALOG_TAG);
  305. }
  306. @Override
  307. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  308. if (requestCode == SyncedFolderPreferencesDialogFragment.REQUEST_CODE__SELECT_REMOTE_FOLDER
  309. && resultCode == RESULT_OK && mSyncedFolderPreferencesDialogFragment != null) {
  310. OCFile chosenFolder = data.getParcelableExtra(FolderPickerActivity.EXTRA_FOLDER);
  311. mSyncedFolderPreferencesDialogFragment.setRemoteFolderSummary(chosenFolder.getRemotePath());
  312. } else {
  313. super.onActivityResult(requestCode, resultCode, data);
  314. }
  315. }
  316. @Override
  317. public void onSaveSyncedFolderPreference(SyncedFolderParcelable syncedFolder) {
  318. SyncedFolderDisplayItem item = syncFolderItems.get(syncedFolder.getSection());
  319. boolean dirty = item.isEnabled() != syncedFolder.getEnabled();
  320. item = updateSyncedFolderItem(item, syncedFolder.getLocalPath(), syncedFolder.getRemotePath(), syncedFolder
  321. .getWifiOnly(), syncedFolder.getChargingOnly(), syncedFolder.getSubfolderByDate(), syncedFolder
  322. .getUploadAction(), syncedFolder.getEnabled());
  323. if (syncedFolder.getId() == UNPERSISTED_ID) {
  324. // newly set up folder sync config
  325. mSyncedFolderProvider.storeFolderSync(item);
  326. } else {
  327. // existing synced folder setup to be updated
  328. mSyncedFolderProvider.updateSyncFolder(item);
  329. }
  330. mSyncedFolderPreferencesDialogFragment = null;
  331. if(dirty) {
  332. mAdapter.setSyncFolderItem(syncedFolder.getSection(), item);
  333. }
  334. }
  335. @Override
  336. public void onCancelSyncedFolderPreference() {
  337. mSyncedFolderPreferencesDialogFragment = null;
  338. }
  339. /**
  340. * update given synced folder with the given values.
  341. *
  342. * @param item the synced folder to be updated
  343. * @param localPath the local path
  344. * @param remotePath the remote path
  345. * @param wifiOnly upload on wifi only
  346. * @param chargingOnly upload on charging only
  347. * @param subfolderByDate created sub folders
  348. * @param uploadAction upload action
  349. * @param enabled is sync enabled
  350. * @return the updated item
  351. */
  352. private SyncedFolderDisplayItem updateSyncedFolderItem(SyncedFolderDisplayItem item,
  353. String localPath,
  354. String remotePath,
  355. Boolean wifiOnly,
  356. Boolean chargingOnly,
  357. Boolean subfolderByDate,
  358. Integer uploadAction,
  359. Boolean enabled) {
  360. item.setLocalPath(localPath);
  361. item.setRemotePath(remotePath);
  362. item.setWifiOnly(wifiOnly);
  363. item.setChargingOnly(chargingOnly);
  364. item.setSubfolderByDate(subfolderByDate);
  365. item.setUploadAction(uploadAction);
  366. item.setEnabled(enabled);
  367. return item;
  368. }
  369. }