SyncedFoldersActivity.java 28 KB

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