FolderSyncActivity.java 16 KB

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