SyncedFoldersActivity.java 29 KB

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