diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index bf027041de..4706e3d6f0 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -27,6 +27,7 @@ import android.content.ContentProviderResult; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; +import android.content.Context; import android.content.Intent; import android.content.OperationApplicationException; import android.database.Cursor; @@ -36,7 +37,10 @@ import android.provider.MediaStore; import com.owncloud.android.MainApp; import com.owncloud.android.db.ProviderMeta.ProviderTableMeta; +import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.utils.Log_OC; +import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation; +import com.owncloud.android.lib.resources.files.RemoteFile; import com.owncloud.android.lib.resources.shares.OCShare; import com.owncloud.android.lib.resources.shares.ShareType; import com.owncloud.android.lib.resources.status.CapabilityBooleanType; @@ -247,6 +251,39 @@ public class FileDataStorageManager { return overriden; } + public long saveFileWithParent(OCFile file, Context context) { + if (file.getParentId() != 0 || file.getRemotePath().equals("/")) { + saveFile(file); + + } else { + String remotePath = file.getRemotePath(); + String parentPath = remotePath.substring(0, remotePath.lastIndexOf(file.getFileName())); + + OCFile parentFile = getFileByPath(parentPath); + + long fileId; + if (parentFile == null) { + // remote request + ReadRemoteFileOperation operation = new ReadRemoteFileOperation(parentPath); + RemoteOperationResult result = operation.execute(getAccount(), context); + if (result.isSuccess()) { + OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0)); + + fileId = saveFileWithParent(remoteFolder, context); + } else { + fileId = -1; + Log_OC.e(TAG, "Error during saving file with parents: " + file.getRemotePath()); + } + } else { + fileId = saveFileWithParent(parentFile, context); + } + + file.setParentId(fileId); + saveFile(file); + } + + return getFileByPath(file.getRemotePath()).getFileId(); + } public void saveNewFile(OCFile newFile) { String remoteParentPath = new File(newFile.getRemotePath()).getParent(); @@ -868,6 +905,15 @@ public class FileDataStorageManager { return c; } + private OCFile createFileInstanceFromVirtual(Cursor c) { + OCFile file = null; + if (c != null) { + long fileId = c.getLong(c.getColumnIndex(ProviderTableMeta.VIRTUAL_OCFILE_ID)); + file = getFileById(fileId); + } + + return file; + } private OCFile createFileInstance(Cursor c) { OCFile file = null; @@ -2005,4 +2051,89 @@ public class FileDataStorageManager { return capability; } + public void deleteVirtuals(VirtualFolderType type) { + if (getContentResolver() != null) { + getContentResolver().delete(ProviderTableMeta.CONTENT_URI_VIRTUAL, + ProviderTableMeta.VIRTUAL_TYPE + "=?", new String[]{String.valueOf(type)}); + } else { + try { + getContentProviderClient().delete(ProviderTableMeta.CONTENT_URI_VIRTUAL, + ProviderTableMeta.VIRTUAL_TYPE + "=?", new String[]{String.valueOf(type)}); + } catch (RemoteException e) { + Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); + } + } + } + + public void saveVirtual(VirtualFolderType type, OCFile file) { + ContentValues cv = new ContentValues(); + cv.put(ProviderTableMeta.VIRTUAL_TYPE, type.toString()); + cv.put(ProviderTableMeta.VIRTUAL_OCFILE_ID, file.getFileId()); + + if (getContentResolver() != null) { + getContentResolver().insert(ProviderTableMeta.CONTENT_URI_VIRTUAL, cv); + } else { + try { + getContentProviderClient().insert(ProviderTableMeta.CONTENT_URI_VIRTUAL, cv); + } catch (RemoteException e) { + Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); + } + } + } + + public Vector getVirtualFolderContent(VirtualFolderType type, boolean onlyImages) { + Vector ocFiles = new Vector<>(); + Uri req_uri = ProviderTableMeta.CONTENT_URI_VIRTUAL; + Cursor c; + + if (getContentProviderClient() != null) { + try { + c = getContentProviderClient().query( + req_uri, + null, + ProviderTableMeta.VIRTUAL_TYPE + "=?", + new String[]{String.valueOf(type)}, + null + ); + } catch (RemoteException e) { + Log_OC.e(TAG, e.getMessage(), e); + return ocFiles; + } + } else { + c = getContentResolver().query( + req_uri, + null, + ProviderTableMeta.VIRTUAL_TYPE + "=?", + new String[]{String.valueOf(type)}, + null + ); + } + + if (c != null && c.moveToFirst()) { + do { + OCFile child = createFileInstanceFromVirtual(c); + ocFiles.add(child); + } while (c.moveToNext()); + c.close(); + } + + if (onlyImages) { + OCFile current = null; + Vector temp = new Vector<>(); + for (int i=0; i < ocFiles.size(); i++) { + current = ocFiles.get(i); + if (MimeTypeUtil.isImage(current)) { + temp.add(current); + } + } + ocFiles = temp; + } + + if (ocFiles.size() > 0) { + Collections.sort(ocFiles); + } + + return ocFiles; + } + } diff --git a/src/main/java/com/owncloud/android/datamodel/VirtualFolderType.java b/src/main/java/com/owncloud/android/datamodel/VirtualFolderType.java new file mode 100644 index 0000000000..8fdbd67743 --- /dev/null +++ b/src/main/java/com/owncloud/android/datamodel/VirtualFolderType.java @@ -0,0 +1,30 @@ +/** + * Nextcloud Android client application + * + * @author Tobias Kaminsky + * Copyright (C) 2017 Tobias Kaminsky + * Copyright (C) 2017 Nextcloud GmbH. + *

+ * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + *

+ * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + *

+ * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.owncloud.android.datamodel; + +/** + * Type for virtual folders + */ + +public enum VirtualFolderType { + FAVORITE, PHOTOS, NONE +} diff --git a/src/main/java/com/owncloud/android/db/ProviderMeta.java b/src/main/java/com/owncloud/android/db/ProviderMeta.java index 548322d67c..c9c5b44810 100644 --- a/src/main/java/com/owncloud/android/db/ProviderMeta.java +++ b/src/main/java/com/owncloud/android/db/ProviderMeta.java @@ -45,6 +45,7 @@ public class ProviderMeta { public static final String SYNCED_FOLDERS_TABLE_NAME = "synced_folders"; public static final String EXTERNAL_LINKS_TABLE_NAME = "external_links"; public static final String ARBITRARY_DATA_TABLE_NAME = "arbitrary_data"; + public static final String VIRTUAL_TABLE_NAME = "virtual"; private static final String CONTENT_PREFIX = "content://"; @@ -66,6 +67,7 @@ public class ProviderMeta { + MainApp.getAuthority() + "/external_links"); public static final Uri CONTENT_URI_ARBITRARY_DATA = Uri.parse(CONTENT_PREFIX + MainApp.getAuthority() + "/arbitrary_data"); + public static final Uri CONTENT_URI_VIRTUAL = Uri.parse(CONTENT_PREFIX + MainApp.getAuthority() + "/virtual"); public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.owncloud.file"; public static final String CONTENT_TYPE_ITEM = "vnd.android.cursor.item/vnd.owncloud.file"; @@ -185,5 +187,9 @@ public class ProviderMeta { public static final String ARBITRARY_DATA_CLOUD_ID = "cloud_id"; public static final String ARBITRARY_DATA_KEY = "key"; public static final String ARBITRARY_DATA_VALUE = "value"; + + // Columns of virtual + public static final String VIRTUAL_TYPE = "type"; + public static final String VIRTUAL_OCFILE_ID = "ocfile_id"; } } \ No newline at end of file diff --git a/src/main/java/com/owncloud/android/providers/FileContentProvider.java b/src/main/java/com/owncloud/android/providers/FileContentProvider.java index 9078d480f3..ac5795e290 100644 --- a/src/main/java/com/owncloud/android/providers/FileContentProvider.java +++ b/src/main/java/com/owncloud/android/providers/FileContentProvider.java @@ -73,6 +73,7 @@ public class FileContentProvider extends ContentProvider { private static final int SYNCED_FOLDERS = 7; private static final int EXTERNAL_LINKS = 8; private static final int ARBITRARY_DATA = 9; + private static final int VIRTUAL = 10; private static final String TAG = FileContentProvider.class.getSimpleName(); @@ -205,6 +206,9 @@ public class FileContentProvider extends ContentProvider { case ARBITRARY_DATA: count = db.delete(ProviderTableMeta.ARBITRARY_DATA_TABLE_NAME, where, whereArgs); break; + case VIRTUAL: + count = db.delete(ProviderTableMeta.VIRTUAL_TABLE_NAME, where, whereArgs); + break; default: //Log_OC.e(TAG, "Unknown uri " + uri); throw new IllegalArgumentException("Unknown uri: " + uri.toString()); @@ -350,7 +354,17 @@ public class FileContentProvider extends ContentProvider { } return insertedArbitraryDataUri; + case VIRTUAL: + Uri insertedVirtualUri; + long virtualId = db.insert(ProviderTableMeta.VIRTUAL_TABLE_NAME, null, values); + if (virtualId > 0) { + insertedVirtualUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_VIRTUAL, virtualId); + } else { + throw new SQLException("ERROR " + uri); + } + + return insertedVirtualUri; default: throw new IllegalArgumentException("Unknown uri id: " + uri); } @@ -402,6 +416,7 @@ public class FileContentProvider extends ContentProvider { mUriMatcher.addURI(authority, "synced_folders", SYNCED_FOLDERS); mUriMatcher.addURI(authority, "external_links", EXTERNAL_LINKS); mUriMatcher.addURI(authority, "arbitrary_data", ARBITRARY_DATA); + mUriMatcher.addURI(authority, "virtual", VIRTUAL); return true; } @@ -497,6 +512,12 @@ public class FileContentProvider extends ContentProvider { + uri.getPathSegments().get(1)); } break; + case VIRTUAL: + sqlQuery.setTables(ProviderTableMeta.VIRTUAL_TABLE_NAME); + if (uri.getPathSegments().size() > 1) { + sqlQuery.appendWhere(ProviderTableMeta._ID + "=" + uri.getPathSegments().get(1)); + } + break; default: throw new IllegalArgumentException("Unknown uri id: " + uri); } @@ -522,6 +543,9 @@ public class FileContentProvider extends ContentProvider { case ARBITRARY_DATA: order = ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID; break; + case VIRTUAL: + order = ProviderTableMeta.VIRTUAL_TYPE; + break; default: // Files order = ProviderTableMeta.FILE_DEFAULT_SORT_ORDER; break; @@ -635,6 +659,9 @@ public class FileContentProvider extends ContentProvider { // Create arbitrary data table createArbitraryData(db); + + // Create virtual table + createVirtualTable(db); } @Override @@ -957,11 +984,13 @@ public class FileContentProvider extends ContentProvider { db.beginTransaction(); try { createArbitraryData(db); + createVirtualTable(db); upgraded = true; db.setTransactionSuccessful(); } finally { db.endTransaction(); } + } if (!upgraded) { @@ -1047,7 +1076,7 @@ public class FileContentProvider extends ContentProvider { + ProviderTableMeta.CAPABILITIES_FILES_UNDELETE + INTEGER // boolean + ProviderTableMeta.CAPABILITIES_FILES_VERSIONING + INTEGER // boolean + ProviderTableMeta.CAPABILITIES_FILES_DROP + INTEGER // boolean - + ProviderTableMeta.CAPABILITIES_EXTERNAL_LINKS + " INTEGER );" ); // boolean + + ProviderTableMeta.CAPABILITIES_EXTERNAL_LINKS + " INTEGER );"); // boolean } private void createUploadsTable(SQLiteDatabase db) { @@ -1113,6 +1142,14 @@ public class FileContentProvider extends ContentProvider { ); } + private void createVirtualTable(SQLiteDatabase db) { + db.execSQL("CREATE TABLE " + ProviderTableMeta.VIRTUAL_TABLE_NAME + "(" + + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, " // id + + ProviderTableMeta.VIRTUAL_TYPE + " TEXT, " // type + + ProviderTableMeta.VIRTUAL_OCFILE_ID + " INTEGER )" // file id + ); + } + /** * Version 10 of database does not modify its scheme. It coincides with the upgrade of the ownCloud account names * structure to include in it the path to the server instance. Updating the account names and path to local files @@ -1120,7 +1157,7 @@ public class FileContentProvider extends ContentProvider { * * See {@link com.owncloud.android.authentication.AccountUtils#updateAccountVersion(android.content.Context)} * - * @param db Database where table of files is included. + * @param db Database where table of files is included. */ private void updateAccountName(SQLiteDatabase db) { Log_OC.d(SQL, "THREAD: " + Thread.currentThread().getName()); @@ -1177,9 +1214,9 @@ public class FileContentProvider extends ContentProvider { * Rename the local ownCloud folder of one account to match the a rename of the account itself. Updates the * table of files in database so that the paths to the local files keep being the same. * - * @param db Database where table of files is included. - * @param newAccountName New name for the target OC account. - * @param oldAccountName Old name of the target OC account. + * @param db Database where table of files is included. + * @param newAccountName New name for the target OC account. + * @param oldAccountName Old name of the target OC account. */ private void updateDownloadedFiles(SQLiteDatabase db, String newAccountName, String oldAccountName) { diff --git a/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java index ab56e8fa2b..785ff90bea 100644 --- a/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -62,6 +62,7 @@ import com.owncloud.android.MainApp; import com.owncloud.android.R; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.datamodel.VirtualFolderType; import com.owncloud.android.db.PreferenceManager; import com.owncloud.android.files.services.FileDownloader; import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder; @@ -1914,6 +1915,19 @@ public class FileDisplayActivity extends HookActivity showDetailsIntent.putExtra(EXTRA_FILE, file); showDetailsIntent.putExtra(EXTRA_ACCOUNT, getAccount()); startActivity(showDetailsIntent); + } + + /** + * Opens the image gallery showing the image {@link OCFile} received as parameter. + * + * @param file Image {@link OCFile} to show. + */ + public void startImagePreview(OCFile file, VirtualFolderType type) { + Intent showDetailsIntent = new Intent(this, PreviewImageActivity.class); + showDetailsIntent.putExtra(PreviewImageActivity.EXTRA_FILE, file); + showDetailsIntent.putExtra(EXTRA_ACCOUNT, getAccount()); + showDetailsIntent.putExtra(PreviewImageActivity.EXTRA_VIRTUAL_TYPE, type); + startActivity(showDetailsIntent); } diff --git a/src/main/java/com/owncloud/android/ui/adapter/FileListListAdapter.java b/src/main/java/com/owncloud/android/ui/adapter/FileListListAdapter.java index 7b6b0b9a69..ca9473f71f 100644 --- a/src/main/java/com/owncloud/android/ui/adapter/FileListListAdapter.java +++ b/src/main/java/com/owncloud/android/ui/adapter/FileListListAdapter.java @@ -46,6 +46,7 @@ import com.owncloud.android.authentication.AccountUtils; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.datamodel.ThumbnailsCacheManager; +import com.owncloud.android.datamodel.VirtualFolderType; import com.owncloud.android.db.PreferenceManager; import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder; import com.owncloud.android.files.services.FileUploader.FileUploaderBinder; @@ -485,37 +486,9 @@ public class FileListListAdapter extends BaseAdapter { public void setData(ArrayList objects, ExtendedListFragment.SearchType searchType) { mFiles = new Vector<>(); if (searchType.equals(ExtendedListFragment.SearchType.SHARED_FILTER)) { - ArrayList shares = new ArrayList<>(); - for (int i = 0; i < objects.size(); i++) { - // check type before cast as of long running data fetch it is possible that old result is filled - if (objects.get(i) instanceof OCShare) { - OCShare ocShare = (OCShare) objects.get(i); - - shares.add(ocShare); - - // get ocFile from Server to have an up-to-date copy - ReadRemoteFileOperation operation = new ReadRemoteFileOperation(ocShare.getPath()); - RemoteOperationResult result = operation.execute(mAccount, mContext); - if (result.isSuccess()) { - OCFile file = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0)); - - mStorageManager.saveFile(file); - - if (!mFiles.contains(file)) { - mFiles.add(file); - } - } else { - Log_OC.e(TAG, "Error in getting prop for file: " + ocShare.getPath()); - } - } - } - mStorageManager.saveShares(shares); + parseShares(objects); } else { - for (int i = 0; i < objects.size(); i++) { - OCFile ocFile = FileStorageUtils.fillOCFile((RemoteFile) objects.get(i)); - searchForLocalFileInDefaultPath(ocFile); - mFiles.add(ocFile); - } + parseVirtuals(objects, searchType); } if (!searchType.equals(ExtendedListFragment.SearchType.PHOTO_SEARCH) && @@ -539,6 +512,62 @@ public class FileListListAdapter extends BaseAdapter { }); } + private void parseShares(ArrayList objects) { + ArrayList shares = new ArrayList<>(); + for (int i = 0; i < objects.size(); i++) { + // check type before cast as of long running data fetch it is possible that old result is filled + if (objects.get(i) instanceof OCShare) { + OCShare ocShare = (OCShare) objects.get(i); + + shares.add(ocShare); + + // get ocFile from Server to have an up-to-date copy + ReadRemoteFileOperation operation = new ReadRemoteFileOperation(ocShare.getPath()); + RemoteOperationResult result = operation.execute(mAccount, mContext); + if (result.isSuccess()) { + OCFile file = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0)); + + mStorageManager.saveFile(file); + + if (!mFiles.contains(file)) { + mFiles.add(file); + } + } else { + Log_OC.e(TAG, "Error in getting prop for file: " + ocShare.getPath()); + } + } + } + mStorageManager.saveShares(shares); + } + + private void parseVirtuals(ArrayList objects, ExtendedListFragment.SearchType searchType) { + VirtualFolderType type; + boolean onlyImages = false; + switch (searchType) { + case FAVORITE_SEARCH: + type = VirtualFolderType.FAVORITE; + break; + case PHOTO_SEARCH: + type = VirtualFolderType.PHOTOS; + onlyImages = true; + break; + default: + type = VirtualFolderType.NONE; + break; + } + + mStorageManager.deleteVirtuals(type); + + for (int i = 0; i < objects.size(); i++) { + OCFile ocFile = FileStorageUtils.fillOCFile((RemoteFile) objects.get(i)); + searchForLocalFileInDefaultPath(ocFile); + mStorageManager.saveFileWithParent(ocFile, mContext); + mStorageManager.saveVirtual(type, mStorageManager.getFileByPath(ocFile.getRemotePath())); + } + + mFiles.addAll(mStorageManager.getVirtualFolderContent(type, onlyImages)); + } + /** * Filter for getting only the folders * diff --git a/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java b/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java index 7c163687a3..f60160f157 100644 --- a/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java +++ b/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java @@ -59,6 +59,7 @@ import com.owncloud.android.R; import com.owncloud.android.authentication.AccountUtils; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.datamodel.VirtualFolderType; import com.owncloud.android.files.FileMenuFilter; import com.owncloud.android.lib.common.OwnCloudAccount; import com.owncloud.android.lib.common.OwnCloudClient; @@ -801,7 +802,23 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi } else { /// Click on a file if (PreviewImageFragment.canBePreviewed(file)) { // preview image - it handles the download, if needed - ((FileDisplayActivity) mContainerActivity).startImagePreview(file); + if (searchFragment) { + VirtualFolderType type; + switch (currentSearchType) { + case FAVORITE_SEARCH: + type = VirtualFolderType.FAVORITE; + break; + case PHOTO_SEARCH: + type = VirtualFolderType.PHOTOS; + break; + default: + type = VirtualFolderType.NONE; + break; + } + ((FileDisplayActivity) mContainerActivity).startImagePreview(file, type); + } else { + ((FileDisplayActivity) mContainerActivity).startImagePreview(file); + } } else if (file.isDown() && MimeTypeUtil.isVCard(file)){ ((FileDisplayActivity) mContainerActivity).startContactListFragment(file); } else if (PreviewTextFragment.canBePreviewed(file)) { diff --git a/src/main/java/com/owncloud/android/ui/preview/PreviewImageActivity.java b/src/main/java/com/owncloud/android/ui/preview/PreviewImageActivity.java index b1bf94f7b6..c3cb92309e 100644 --- a/src/main/java/com/owncloud/android/ui/preview/PreviewImageActivity.java +++ b/src/main/java/com/owncloud/android/ui/preview/PreviewImageActivity.java @@ -40,6 +40,7 @@ import com.owncloud.android.R; import com.owncloud.android.authentication.AccountUtils; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.datamodel.VirtualFolderType; import com.owncloud.android.files.services.FileDownloader; import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder; import com.owncloud.android.files.services.FileUploader; @@ -72,6 +73,8 @@ public class PreviewImageActivity extends FileActivity implements private static final String KEY_WAITING_FOR_BINDER = "WAITING_FOR_BINDER"; private static final String KEY_SYSTEM_VISIBLE = "TRUE"; + public static final String EXTRA_VIRTUAL_TYPE = "EXTRA_VIRTUAL_TYPE"; + private ExtendedViewPager mViewPager; private PreviewImagePagerAdapter mPreviewImagePagerAdapter; private int mSavedPosition = 0; @@ -128,25 +131,36 @@ public class PreviewImageActivity extends FileActivity implements } private void initViewPager() { - // get parent from path - String parentPath = getFile().getRemotePath().substring(0, - getFile().getRemotePath().lastIndexOf(getFile().getFileName())); - OCFile parentFolder = getStorageManager().getFileByPath(parentPath); - if (parentFolder == null) { - // should not be necessary - parentFolder = getStorageManager().getFileByPath(OCFile.ROOT_PATH); + // virtual folder + if (getIntent().getSerializableExtra(EXTRA_VIRTUAL_TYPE) != null) { + VirtualFolderType type = (VirtualFolderType) getIntent().getSerializableExtra(EXTRA_VIRTUAL_TYPE); + + mPreviewImagePagerAdapter = new PreviewImagePagerAdapter(getSupportFragmentManager(), + type, getAccount(), getStorageManager()); + } else { + // get parent from path + String parentPath = getFile().getRemotePath().substring(0, + getFile().getRemotePath().lastIndexOf(getFile().getFileName())); + OCFile parentFolder = getStorageManager().getFileByPath(parentPath); + + if (parentFolder == null) { + // should not be necessary + parentFolder = getStorageManager().getFileByPath(OCFile.ROOT_PATH); + } + + mPreviewImagePagerAdapter = new PreviewImagePagerAdapter(getSupportFragmentManager(), + parentFolder, getAccount(), getStorageManager(), MainApp.isOnlyOnDevice()); } - mPreviewImagePagerAdapter = new PreviewImagePagerAdapter(getSupportFragmentManager(), - parentFolder, getAccount(), getStorageManager(), MainApp.isOnlyOnDevice()); - mViewPager = (ExtendedViewPager) findViewById(R.id.fragmentPager); - int position = mHasSavedPosition ? mSavedPosition : - mPreviewImagePagerAdapter.getFilePosition(getFile()); + + int position = mHasSavedPosition ? mSavedPosition : mPreviewImagePagerAdapter.getFilePosition(getFile()); position = (position >= 0) ? position : 0; - mViewPager.setAdapter(mPreviewImagePagerAdapter); + + mViewPager.setAdapter(mPreviewImagePagerAdapter); mViewPager.setOnPageChangeListener(this); mViewPager.setCurrentItem(position); + if (position == 0 && !getFile().isDown()) { // this is necessary because mViewPager.setCurrentItem(0) just after setting the // adapter does not result in a call to #onPageSelected(0) @@ -446,7 +460,7 @@ public class PreviewImageActivity extends FileActivity implements super.onAccountSet(stateWasRecovered); if (getAccount() != null) { OCFile file = getFile(); - /// Validate handled file (first image to preview) + /// Validate handled file (first image to preview) if (file == null) { throw new IllegalStateException("Instanced with a NULL OCFile"); } diff --git a/src/main/java/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java b/src/main/java/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java index d58dfb5bf3..4329d63ae7 100644 --- a/src/main/java/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java +++ b/src/main/java/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java @@ -27,6 +27,7 @@ import android.view.ViewGroup; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.datamodel.VirtualFolderType; import com.owncloud.android.ui.fragment.FileFragment; import com.owncloud.android.utils.FileStorageUtils; @@ -87,7 +88,44 @@ public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter { //mFragmentManager = fragmentManager; mCachedFragments = new HashMap(); } - + + /** + * Constructor. + * + * @param fragmentManager {@link FragmentManager} instance that will handle + * the {@link Fragment}s provided by the adapter. + * @param type Type of virtual folder, e.g. favorite or photos + * @param storageManager Bridge to database. + */ + public PreviewImagePagerAdapter(FragmentManager fragmentManager, VirtualFolderType type, + Account account, FileDataStorageManager storageManager) { + super(fragmentManager); + + if (fragmentManager == null) { + throw new IllegalArgumentException("NULL FragmentManager instance"); + } + if (type == null) { + throw new IllegalArgumentException("NULL parent folder"); + } + if (storageManager == null) { + throw new IllegalArgumentException("NULL storage manager"); + } + + mAccount = account; + mStorageManager = storageManager; + mImageFiles = mStorageManager.getVirtualFolderContent(type, true); + + if (type == VirtualFolderType.PHOTOS) { + mImageFiles = FileStorageUtils.sortOCFilesByDate(mImageFiles); + } + + mObsoleteFragments = new HashSet(); + mObsoletePositions = new HashSet(); + mDownloadErrors = new HashSet(); + //mFragmentManager = fragmentManager; + mCachedFragments = new HashMap(); + } + /** * Returns the image files handled by the adapter. * @@ -195,7 +233,7 @@ public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter { public void resetZoom() { Iterator entries = mCachedFragments.values().iterator(); while (entries.hasNext()) { - FileFragment fileFragment = (FileFragment) entries.next(); + FileFragment fileFragment = entries.next(); if (fileFragment instanceof PreviewImageFragment) { ((PreviewImageFragment) fileFragment).getImageView().resetZoom(); }