SyncedFoldersActivity.java 28 KB

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