SyncedFoldersActivity.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Andy Scherzinger
  5. * Copyright (C) 2016 Andy Scherzinger
  6. * Copyright (C) 2016 Nextcloud
  7. *
  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. *
  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. *
  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.accounts.Account;
  23. import android.content.Intent;
  24. import android.content.SharedPreferences;
  25. import android.content.pm.PackageManager;
  26. import android.os.Bundle;
  27. import android.preference.PreferenceManager;
  28. import android.support.annotation.NonNull;
  29. import android.support.design.widget.AppBarLayout;
  30. import android.support.design.widget.BottomNavigationView;
  31. import android.support.design.widget.CollapsingToolbarLayout;
  32. import android.support.v4.app.FragmentManager;
  33. import android.support.v4.app.FragmentTransaction;
  34. import android.support.v4.widget.DrawerLayout;
  35. import android.support.v7.app.ActionBar;
  36. import android.support.v7.widget.GridLayoutManager;
  37. import android.support.v7.widget.RecyclerView;
  38. import android.text.TextUtils;
  39. import android.util.Log;
  40. import android.view.MenuItem;
  41. import android.view.View;
  42. import android.widget.LinearLayout;
  43. import android.widget.RelativeLayout;
  44. import android.widget.TextView;
  45. import com.owncloud.android.MainApp;
  46. import com.owncloud.android.R;
  47. import com.owncloud.android.authentication.AccountUtils;
  48. import com.owncloud.android.datamodel.ArbitraryDataProvider;
  49. import com.owncloud.android.datamodel.MediaFolder;
  50. import com.owncloud.android.datamodel.MediaFolderType;
  51. import com.owncloud.android.datamodel.MediaProvider;
  52. import com.owncloud.android.datamodel.OCFile;
  53. import com.owncloud.android.datamodel.SyncedFolder;
  54. import com.owncloud.android.datamodel.SyncedFolderDisplayItem;
  55. import com.owncloud.android.datamodel.SyncedFolderProvider;
  56. import com.owncloud.android.files.services.FileUploader;
  57. import com.owncloud.android.jobs.MediaFoldersDetectionJob;
  58. import com.owncloud.android.jobs.NotificationJob;
  59. import com.owncloud.android.ui.adapter.SyncedFolderAdapter;
  60. import com.owncloud.android.ui.decoration.MediaGridItemDecoration;
  61. import com.owncloud.android.ui.dialog.SyncedFolderPreferencesDialogFragment;
  62. import com.owncloud.android.ui.dialog.parcel.SyncedFolderParcelable;
  63. import com.owncloud.android.utils.DisplayUtils;
  64. import com.owncloud.android.utils.FilesSyncHelper;
  65. import com.owncloud.android.utils.PermissionUtil;
  66. import com.owncloud.android.utils.ThemeUtils;
  67. import java.io.File;
  68. import java.io.FileFilter;
  69. import java.util.ArrayList;
  70. import java.util.Arrays;
  71. import java.util.Collections;
  72. import java.util.Comparator;
  73. import java.util.HashMap;
  74. import java.util.List;
  75. import java.util.Locale;
  76. import java.util.Map;
  77. import static android.support.design.widget.AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS;
  78. import static com.owncloud.android.datamodel.SyncedFolderDisplayItem.UNPERSISTED_ID;
  79. /**
  80. * Activity displaying all auto-synced folders and/or instant upload media folders.
  81. */
  82. public class SyncedFoldersActivity extends FileActivity implements SyncedFolderAdapter.ClickListener,
  83. SyncedFolderPreferencesDialogFragment.OnSyncedFolderPreferenceListener {
  84. public static final String[] PRIORITIZED_FOLDERS = new String[]{"Camera", "Screenshots"};
  85. public static final String EXTRA_SHOW_SIDEBAR = "SHOW_SIDEBAR";
  86. private static final String SYNCED_FOLDER_PREFERENCES_DIALOG_TAG = "SYNCED_FOLDER_PREFERENCES_DIALOG";
  87. private static final String TAG = SyncedFoldersActivity.class.getSimpleName();
  88. private RecyclerView mRecyclerView;
  89. private SyncedFolderAdapter mAdapter;
  90. private LinearLayout mProgress;
  91. private TextView mEmpty;
  92. private SyncedFolderProvider mSyncedFolderProvider;
  93. private SyncedFolderPreferencesDialogFragment mSyncedFolderPreferencesDialogFragment;
  94. private boolean showSidebar = true;
  95. private RelativeLayout mCustomFolderRelativeLayout;
  96. private String path;
  97. private int type;
  98. /**
  99. * Sorts list of {@link SyncedFolderDisplayItem}s.
  100. *
  101. * @param syncFolderItemList list of items to be sorted
  102. * @return sorted list of items
  103. */
  104. public static List<SyncedFolderDisplayItem> sortSyncedFolderItems(List<SyncedFolderDisplayItem>
  105. syncFolderItemList) {
  106. Collections.sort(syncFolderItemList, new Comparator<SyncedFolderDisplayItem>() {
  107. public int compare(SyncedFolderDisplayItem f1, SyncedFolderDisplayItem f2) {
  108. if (f1 == null && f2 == null) {
  109. return 0;
  110. } else if (f1 == null) {
  111. return -1;
  112. } else if (f2 == null) {
  113. return 1;
  114. } else if (f1.isEnabled() && f2.isEnabled()) {
  115. return f1.getFolderName().toLowerCase(Locale.getDefault()).compareTo(
  116. f2.getFolderName().toLowerCase(Locale.getDefault()));
  117. } else if (f1.isEnabled()) {
  118. return -1;
  119. } else if (f2.isEnabled()) {
  120. return 1;
  121. } else if (f1.getFolderName() == null && f2.getFolderName() == null) {
  122. return 0;
  123. } else if (f1.getFolderName() == null) {
  124. return -1;
  125. } else if (f2.getFolderName() == null) {
  126. return 1;
  127. }
  128. for (String folder : PRIORITIZED_FOLDERS) {
  129. if (folder.equals(f1.getFolderName()) &&
  130. folder.equals(f2.getFolderName())) {
  131. return 0;
  132. } else if (folder.equals(f1.getFolderName())) {
  133. return -1;
  134. } else if (folder.equals(f2.getFolderName())) {
  135. return 1;
  136. }
  137. }
  138. return f1.getFolderName().toLowerCase(Locale.getDefault()).compareTo(
  139. f2.getFolderName().toLowerCase(Locale.getDefault()));
  140. }
  141. });
  142. return syncFolderItemList;
  143. }
  144. @Override
  145. protected void onCreate(Bundle savedInstanceState) {
  146. super.onCreate(savedInstanceState);
  147. if (getIntent().getExtras() != null) {
  148. showSidebar = getIntent().getExtras().getBoolean(EXTRA_SHOW_SIDEBAR);
  149. }
  150. setContentView(R.layout.synced_folders_layout);
  151. String account;
  152. Account currentAccount;
  153. if (getIntent() != null && getIntent().getExtras() != null) {
  154. account = getIntent().getExtras().getString(NotificationJob.KEY_NOTIFICATION_ACCOUNT);
  155. currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  156. if (account != null && currentAccount != null && !account.equalsIgnoreCase(currentAccount.name)) {
  157. AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), account);
  158. setAccount(AccountUtils.getCurrentOwnCloudAccount(this));
  159. }
  160. path = getIntent().getStringExtra(MediaFoldersDetectionJob.KEY_MEDIA_FOLDER_PATH);
  161. type = getIntent().getIntExtra(MediaFoldersDetectionJob.KEY_MEDIA_FOLDER_TYPE, -1);
  162. }
  163. // setup toolbar
  164. setupToolbar();
  165. CollapsingToolbarLayout mCollapsingToolbarLayout = findViewById(R.id.collapsing_toolbar);
  166. mCollapsingToolbarLayout.setTitle(this.getString(R.string.drawer_synced_folders));
  167. mCustomFolderRelativeLayout = findViewById(R.id.custom_folder_toolbar);
  168. SharedPreferences appPrefs =
  169. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  170. findViewById(R.id.toolbar).post(() -> {
  171. if (!appPrefs.getBoolean(Preferences.PREFERENCE_EXPERT_MODE, false)) {
  172. findViewById(R.id.app_bar).getLayoutParams().height = findViewById(R.id.toolbar).getHeight();
  173. AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams) mCollapsingToolbarLayout.getLayoutParams();
  174. p.setScrollFlags(SCROLL_FLAG_ENTER_ALWAYS);
  175. mCollapsingToolbarLayout.setLayoutParams(p);
  176. mCustomFolderRelativeLayout.setVisibility(View.GONE);
  177. } else {
  178. mCustomFolderRelativeLayout.setVisibility(View.VISIBLE);
  179. findViewById(R.id.app_bar).setBackgroundColor(getResources().getColor(R.color.filelist_icon_backgorund));
  180. }
  181. });
  182. // setup drawer
  183. setupDrawer(R.id.nav_synced_folders);
  184. if (!showSidebar) {
  185. setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
  186. mDrawerToggle.setDrawerIndicatorEnabled(false);
  187. }
  188. setupContent();
  189. ActionBar actionBar = getSupportActionBar();
  190. if (actionBar != null) {
  191. ThemeUtils.setColoredTitle(getSupportActionBar(), getString(R.string.drawer_synced_folders), this);
  192. actionBar.setDisplayHomeAsUpEnabled(true);
  193. }
  194. if (ThemeUtils.themingEnabled(this)) {
  195. setTheme(R.style.FallbackThemingTheme);
  196. }
  197. }
  198. /**
  199. * sets up the UI elements and loads all media/synced folders.
  200. */
  201. private void setupContent() {
  202. mRecyclerView = findViewById(android.R.id.list);
  203. mProgress = findViewById(android.R.id.progress);
  204. mEmpty = findViewById(android.R.id.empty);
  205. final int gridWidth = getResources().getInteger(R.integer.media_grid_width);
  206. boolean lightVersion = getResources().getBoolean(R.bool.syncedFolder_light);
  207. mAdapter = new SyncedFolderAdapter(this, gridWidth, this, lightVersion);
  208. mSyncedFolderProvider = new SyncedFolderProvider(getContentResolver());
  209. final GridLayoutManager lm = new GridLayoutManager(this, gridWidth);
  210. mAdapter.setLayoutManager(lm);
  211. int spacing = getResources().getDimensionPixelSize(R.dimen.media_grid_spacing);
  212. mRecyclerView.addItemDecoration(new MediaGridItemDecoration(spacing));
  213. mRecyclerView.setLayoutManager(lm);
  214. mRecyclerView.setAdapter(mAdapter);
  215. BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation_view);
  216. if (getResources().getBoolean(R.bool.bottom_toolbar_enabled)) {
  217. bottomNavigationView.setVisibility(View.VISIBLE);
  218. DisplayUtils.setupBottomBar(bottomNavigationView, getResources(), this, -1);
  219. }
  220. load(gridWidth * 2, false);
  221. }
  222. /**
  223. * loads all media/synced folders, adds them to the recycler view adapter and shows the list.
  224. *
  225. * @param perFolderMediaItemLimit the amount of media items to be loaded/shown per media folder
  226. */
  227. private void load(final int perFolderMediaItemLimit, boolean force) {
  228. if (mAdapter.getItemCount() > 0 && !force) {
  229. return;
  230. }
  231. setListShown(false);
  232. final List<MediaFolder> mediaFolders = MediaProvider.getImageFolders(getContentResolver(),
  233. perFolderMediaItemLimit, this, false);
  234. mediaFolders.addAll(MediaProvider.getVideoFolders(getContentResolver(), perFolderMediaItemLimit,
  235. this, false));
  236. List<SyncedFolder> syncedFolderArrayList = mSyncedFolderProvider.getSyncedFolders();
  237. List<SyncedFolder> currentAccountSyncedFoldersList = new ArrayList<>();
  238. Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(SyncedFoldersActivity.this);
  239. for (SyncedFolder syncedFolder : syncedFolderArrayList) {
  240. if (currentAccount != null && syncedFolder.getAccount().equals(currentAccount.name)) {
  241. currentAccountSyncedFoldersList.add(syncedFolder);
  242. }
  243. }
  244. List<SyncedFolderDisplayItem> syncFolderItems = sortSyncedFolderItems(
  245. mergeFolderData(currentAccountSyncedFoldersList, mediaFolders));
  246. mAdapter.setSyncFolderItems(syncFolderItems);
  247. mAdapter.notifyDataSetChanged();
  248. setListShown(true);
  249. if (!TextUtils.isEmpty(path)) {
  250. for(SyncedFolderDisplayItem syncFolderItem : syncFolderItems) {
  251. if (syncFolderItem.getLocalPath().equalsIgnoreCase(path) &&
  252. syncFolderItem.getType().getId().equals(type)) {
  253. onSyncFolderSettingsClick(1, syncFolderItem);
  254. }
  255. }
  256. }
  257. }
  258. /**
  259. * merges two lists of {@link SyncedFolder} and {@link MediaFolder} items into one of SyncedFolderItems.
  260. *
  261. * @param syncedFolders the synced folders
  262. * @param mediaFolders the media folders
  263. * @return the merged list of SyncedFolderItems
  264. */
  265. @NonNull
  266. private List<SyncedFolderDisplayItem> mergeFolderData(List<SyncedFolder> syncedFolders,
  267. @NonNull List<MediaFolder> mediaFolders) {
  268. Map<String, SyncedFolder> syncedFoldersMap = createSyncedFoldersMap(syncedFolders);
  269. List<SyncedFolderDisplayItem> result = new ArrayList<>();
  270. for (MediaFolder mediaFolder : mediaFolders) {
  271. if (syncedFoldersMap.containsKey(mediaFolder.absolutePath + "-" + mediaFolder.type)) {
  272. SyncedFolder syncedFolder = syncedFoldersMap.get(mediaFolder.absolutePath + "-" + mediaFolder.type);
  273. syncedFoldersMap.remove(mediaFolder.absolutePath + "-" + mediaFolder.type);
  274. if (MediaFolderType.CUSTOM == syncedFolder.getType()) {
  275. result.add(createSyncedFolderWithoutMediaFolder(syncedFolder));
  276. } else {
  277. result.add(createSyncedFolder(syncedFolder, mediaFolder));
  278. }
  279. } else {
  280. result.add(createSyncedFolderFromMediaFolder(mediaFolder));
  281. }
  282. }
  283. for (SyncedFolder syncedFolder : syncedFoldersMap.values()) {
  284. result.add(createSyncedFolderWithoutMediaFolder(syncedFolder));
  285. }
  286. return result;
  287. }
  288. @NonNull
  289. private SyncedFolderDisplayItem createSyncedFolderWithoutMediaFolder(@NonNull SyncedFolder syncedFolder) {
  290. File localFolder = new File(syncedFolder.getLocalPath());
  291. File[] files = getFileList(localFolder);
  292. List<String> filePaths = getDisplayFilePathList(files);
  293. return new SyncedFolderDisplayItem(
  294. syncedFolder.getId(),
  295. syncedFolder.getLocalPath(),
  296. syncedFolder.getRemotePath(),
  297. syncedFolder.getWifiOnly(),
  298. syncedFolder.getChargingOnly(),
  299. syncedFolder.getSubfolderByDate(),
  300. syncedFolder.getAccount(),
  301. syncedFolder.getUploadAction(),
  302. syncedFolder.isEnabled(),
  303. filePaths,
  304. localFolder.getName(),
  305. files.length,
  306. syncedFolder.getType());
  307. }
  308. /**
  309. * creates a SyncedFolderDisplayItem merging a {@link SyncedFolder} and a {@link MediaFolder} object instance.
  310. *
  311. * @param syncedFolder the synced folder object
  312. * @param mediaFolder the media folder object
  313. * @return the created SyncedFolderDisplayItem
  314. */
  315. @NonNull
  316. private SyncedFolderDisplayItem createSyncedFolder(@NonNull SyncedFolder syncedFolder, @NonNull MediaFolder mediaFolder) {
  317. return new SyncedFolderDisplayItem(
  318. syncedFolder.getId(),
  319. syncedFolder.getLocalPath(),
  320. syncedFolder.getRemotePath(),
  321. syncedFolder.getWifiOnly(),
  322. syncedFolder.getChargingOnly(),
  323. syncedFolder.getSubfolderByDate(),
  324. syncedFolder.getAccount(),
  325. syncedFolder.getUploadAction(),
  326. syncedFolder.isEnabled(),
  327. mediaFolder.filePaths,
  328. mediaFolder.folderName,
  329. mediaFolder.numberOfFiles,
  330. mediaFolder.type);
  331. }
  332. /**
  333. * creates a {@link SyncedFolderDisplayItem} based on a {@link MediaFolder} object instance.
  334. *
  335. * @param mediaFolder the media folder object
  336. * @return the created SyncedFolderDisplayItem
  337. */
  338. @NonNull
  339. private SyncedFolderDisplayItem createSyncedFolderFromMediaFolder(@NonNull MediaFolder mediaFolder) {
  340. return new SyncedFolderDisplayItem(
  341. UNPERSISTED_ID,
  342. mediaFolder.absolutePath,
  343. getString(R.string.instant_upload_path) + "/" + mediaFolder.folderName,
  344. true,
  345. false,
  346. false,
  347. AccountUtils.getCurrentOwnCloudAccount(this).name,
  348. FileUploader.LOCAL_BEHAVIOUR_FORGET,
  349. false,
  350. mediaFolder.filePaths,
  351. mediaFolder.folderName,
  352. mediaFolder.numberOfFiles,
  353. mediaFolder.type);
  354. }
  355. private File[] getFileList(File localFolder) {
  356. File[] files = localFolder.listFiles(new FileFilter() {
  357. @Override
  358. public boolean accept(File pathname) {
  359. return !pathname.isDirectory();
  360. }
  361. });
  362. if (files != null) {
  363. Arrays.sort(files, new Comparator<File>() {
  364. public int compare(File f1, File f2) {
  365. return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
  366. }
  367. });
  368. } else {
  369. files = new File[]{};
  370. }
  371. return files;
  372. }
  373. private List<String> getDisplayFilePathList(File[] files) {
  374. List<String> filePaths = null;
  375. if (files != null && files.length > 0) {
  376. filePaths = new ArrayList<>();
  377. for (int i = 0; i < 7 && i < files.length; i++) {
  378. filePaths.add(files[i].getAbsolutePath());
  379. }
  380. }
  381. return filePaths;
  382. }
  383. /**
  384. * creates a lookup map for a list of given {@link SyncedFolder}s with their local path as the key.
  385. *
  386. * @param syncFolders list of {@link SyncedFolder}s
  387. * @return the lookup map for {@link SyncedFolder}s
  388. */
  389. @NonNull
  390. private Map<String, SyncedFolder> createSyncedFoldersMap(List<SyncedFolder> syncFolders) {
  391. Map<String, SyncedFolder> result = new HashMap<>();
  392. if (syncFolders != null) {
  393. for (SyncedFolder syncFolder : syncFolders) {
  394. result.put(syncFolder.getLocalPath() + "-" + syncFolder.getType(), syncFolder);
  395. }
  396. }
  397. return result;
  398. }
  399. /**
  400. * show/hide recycler view list or the empty message / progress info.
  401. *
  402. * @param shown flag if list should be shown
  403. */
  404. private void setListShown(boolean shown) {
  405. if (mRecyclerView != null) {
  406. mRecyclerView.setVisibility(shown ? View.VISIBLE : View.GONE);
  407. mProgress.setVisibility(shown ? View.GONE : View.VISIBLE);
  408. mEmpty.setVisibility(shown && mAdapter.getItemCount() == 0 ? View.VISIBLE : View.GONE);
  409. }
  410. }
  411. @Override
  412. public boolean onOptionsItemSelected(MenuItem item) {
  413. boolean result = true;
  414. switch (item.getItemId()) {
  415. case android.R.id.home: {
  416. if (showSidebar) {
  417. if (isDrawerOpen()) {
  418. closeDrawer();
  419. } else {
  420. openDrawer();
  421. }
  422. } else {
  423. Intent settingsIntent = new Intent(getApplicationContext(), Preferences.class);
  424. startActivity(settingsIntent);
  425. }
  426. break;
  427. }
  428. default:
  429. result = super.onOptionsItemSelected(item);
  430. break;
  431. }
  432. return result;
  433. }
  434. @Override
  435. public void restart() {
  436. Intent i = new Intent(this, FileDisplayActivity.class);
  437. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  438. startActivity(i);
  439. }
  440. @Override
  441. public void showFiles(boolean onDeviceOnly) {
  442. MainApp.showOnlyFilesOnDevice(onDeviceOnly);
  443. Intent fileDisplayActivity = new Intent(getApplicationContext(), FileDisplayActivity.class);
  444. fileDisplayActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  445. startActivity(fileDisplayActivity);
  446. }
  447. @Override
  448. public void onSyncStatusToggleClick(int section, SyncedFolderDisplayItem syncedFolderDisplayItem) {
  449. ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(MainApp.getAppContext().
  450. getContentResolver());
  451. if (syncedFolderDisplayItem.getId() > UNPERSISTED_ID) {
  452. mSyncedFolderProvider.updateSyncedFolderEnabled(syncedFolderDisplayItem.getId(),
  453. syncedFolderDisplayItem.isEnabled());
  454. } else {
  455. long storedId = mSyncedFolderProvider.storeSyncedFolder(syncedFolderDisplayItem);
  456. if (storedId != -1) {
  457. syncedFolderDisplayItem.setId(storedId);
  458. }
  459. }
  460. if (syncedFolderDisplayItem.isEnabled()) {
  461. FilesSyncHelper.insertAllDBEntriesForSyncedFolder(syncedFolderDisplayItem);
  462. } else {
  463. String syncedFolderInitiatedKey = "syncedFolderIntitiated_" + syncedFolderDisplayItem.getId();
  464. arbitraryDataProvider.deleteKeyForAccount("global", syncedFolderInitiatedKey);
  465. }
  466. }
  467. @Override
  468. public void onSyncFolderSettingsClick(int section, SyncedFolderDisplayItem syncedFolderDisplayItem) {
  469. FragmentManager fm = getSupportFragmentManager();
  470. FragmentTransaction ft = fm.beginTransaction();
  471. ft.addToBackStack(null);
  472. mSyncedFolderPreferencesDialogFragment = SyncedFolderPreferencesDialogFragment.newInstance(
  473. syncedFolderDisplayItem, section);
  474. mSyncedFolderPreferencesDialogFragment.show(ft, SYNCED_FOLDER_PREFERENCES_DIALOG_TAG);
  475. }
  476. @Override
  477. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  478. if (requestCode == SyncedFolderPreferencesDialogFragment.REQUEST_CODE__SELECT_REMOTE_FOLDER
  479. && resultCode == RESULT_OK && mSyncedFolderPreferencesDialogFragment != null) {
  480. OCFile chosenFolder = data.getParcelableExtra(FolderPickerActivity.EXTRA_FOLDER);
  481. mSyncedFolderPreferencesDialogFragment.setRemoteFolderSummary(chosenFolder.getRemotePath());
  482. }
  483. if (requestCode == SyncedFolderPreferencesDialogFragment.REQUEST_CODE__SELECT_LOCAL_FOLDER
  484. && resultCode == RESULT_OK && mSyncedFolderPreferencesDialogFragment != null) {
  485. String localPath = data.getStringExtra(UploadFilesActivity.EXTRA_CHOSEN_FILES);
  486. mSyncedFolderPreferencesDialogFragment.setLocalFolderSummary(localPath);
  487. } else {
  488. super.onActivityResult(requestCode, resultCode, data);
  489. }
  490. }
  491. @Override
  492. public void onSaveSyncedFolderPreference(SyncedFolderParcelable syncedFolder) {
  493. ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(MainApp.getAppContext().
  494. getContentResolver());
  495. // custom folders newly created aren't in the list already,
  496. // so triggering a refresh
  497. if (MediaFolderType.CUSTOM.equals(syncedFolder.getType()) && syncedFolder.getId() == UNPERSISTED_ID) {
  498. SyncedFolderDisplayItem newCustomFolder = new SyncedFolderDisplayItem(
  499. SyncedFolder.UNPERSISTED_ID, syncedFolder.getLocalPath(), syncedFolder.getRemotePath(),
  500. syncedFolder.getWifiOnly(), syncedFolder.getChargingOnly(), syncedFolder.getSubfolderByDate(),
  501. syncedFolder.getAccount(), syncedFolder.getUploadAction(), syncedFolder.getEnabled(),
  502. new File(syncedFolder.getLocalPath()).getName(), syncedFolder.getType());
  503. long storedId = mSyncedFolderProvider.storeSyncedFolder(newCustomFolder);
  504. if (storedId != -1) {
  505. newCustomFolder.setId(storedId);
  506. if (newCustomFolder.isEnabled()) {
  507. FilesSyncHelper.insertAllDBEntriesForSyncedFolder(newCustomFolder);
  508. } else {
  509. String syncedFolderInitiatedKey = "syncedFolderIntitiated_" + newCustomFolder.getId();
  510. arbitraryDataProvider.deleteKeyForAccount("global", syncedFolderInitiatedKey);
  511. }
  512. }
  513. mAdapter.addSyncFolderItem(newCustomFolder);
  514. } else {
  515. SyncedFolderDisplayItem item = mAdapter.get(syncedFolder.getSection());
  516. item = updateSyncedFolderItem(item, syncedFolder.getLocalPath(), syncedFolder.getRemotePath(), syncedFolder
  517. .getWifiOnly(), syncedFolder.getChargingOnly(), syncedFolder.getSubfolderByDate(), syncedFolder
  518. .getUploadAction(), syncedFolder.getEnabled());
  519. if (syncedFolder.getId() == UNPERSISTED_ID) {
  520. // newly set up folder sync config
  521. long storedId = mSyncedFolderProvider.storeSyncedFolder(item);
  522. if (storedId != -1) {
  523. item.setId(storedId);
  524. if (item.isEnabled()) {
  525. FilesSyncHelper.insertAllDBEntriesForSyncedFolder(item);
  526. } else {
  527. String syncedFolderInitiatedKey = "syncedFolderIntitiated_" + item.getId();
  528. arbitraryDataProvider.deleteKeyForAccount("global", syncedFolderInitiatedKey);
  529. }
  530. }
  531. } else {
  532. // existing synced folder setup to be updated
  533. mSyncedFolderProvider.updateSyncFolder(item);
  534. if (item.isEnabled()) {
  535. FilesSyncHelper.insertAllDBEntriesForSyncedFolder(item);
  536. } else {
  537. String syncedFolderInitiatedKey = "syncedFolderIntitiated_" + item.getId();
  538. arbitraryDataProvider.deleteKeyForAccount("global", syncedFolderInitiatedKey);
  539. }
  540. }
  541. mAdapter.setSyncFolderItem(syncedFolder.getSection(), item);
  542. }
  543. mSyncedFolderPreferencesDialogFragment = null;
  544. }
  545. @Override
  546. public void onCancelSyncedFolderPreference() {
  547. mSyncedFolderPreferencesDialogFragment = null;
  548. }
  549. @Override
  550. public void onDeleteSyncedFolderPreference(SyncedFolderParcelable syncedFolder) {
  551. mSyncedFolderProvider.deleteSyncedFolder(syncedFolder.getId());
  552. mAdapter.removeItem(syncedFolder.getSection());
  553. }
  554. /**
  555. * update given synced folder with the given values.
  556. *
  557. * @param item the synced folder to be updated
  558. * @param localPath the local path
  559. * @param remotePath the remote path
  560. * @param wifiOnly upload on wifi only
  561. * @param chargingOnly upload on charging only
  562. * @param subfolderByDate created sub folders
  563. * @param uploadAction upload action
  564. * @param enabled is sync enabled
  565. * @return the updated item
  566. */
  567. private SyncedFolderDisplayItem updateSyncedFolderItem(SyncedFolderDisplayItem item,
  568. String localPath,
  569. String remotePath,
  570. Boolean wifiOnly,
  571. Boolean chargingOnly,
  572. Boolean subfolderByDate,
  573. Integer uploadAction,
  574. Boolean enabled) {
  575. item.setLocalPath(localPath);
  576. item.setRemotePath(remotePath);
  577. item.setWifiOnly(wifiOnly);
  578. item.setChargingOnly(chargingOnly);
  579. item.setSubfolderByDate(subfolderByDate);
  580. item.setUploadAction(uploadAction);
  581. item.setEnabled(enabled);
  582. return item;
  583. }
  584. @Override
  585. public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
  586. @NonNull int[] grantResults) {
  587. switch (requestCode) {
  588. case PermissionUtil.PERMISSIONS_WRITE_EXTERNAL_STORAGE: {
  589. // If request is cancelled, result arrays are empty.
  590. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  591. // permission was granted
  592. int gridWidth = getResources().getInteger(R.integer.media_grid_width);
  593. load(gridWidth * 2, true);
  594. } else {
  595. // permission denied --> do nothing
  596. return;
  597. }
  598. return;
  599. }
  600. default:
  601. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  602. }
  603. }
  604. public void onAddCustomFolderClick(View view) {
  605. Log.d(TAG, "Show custom folder dialog");
  606. SyncedFolderDisplayItem emptyCustomFolder = new SyncedFolderDisplayItem(
  607. SyncedFolder.UNPERSISTED_ID, null, null, true, false,
  608. false, AccountUtils.getCurrentOwnCloudAccount(this).name,
  609. FileUploader.LOCAL_BEHAVIOUR_FORGET, false, null, MediaFolderType.CUSTOM);
  610. onSyncFolderSettingsClick(0, emptyCustomFolder);
  611. }
  612. @Override
  613. protected void onResume() {
  614. super.onResume();
  615. setDrawerMenuItemChecked(R.id.nav_synced_folders);
  616. }
  617. }