From: David A. Velasco Date: Mon, 27 May 2013 13:01:54 +0000 (+0200) Subject: Code refactored to 'open file' in a single point X-Git-Tag: oc-android-1.4.3~21^2~10 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/edb380e0cd04b32dccd18a5470075749f2d5189f Code refactored to 'open file' in a single point --- diff --git a/src/com/owncloud/android/files/FileHandler.java b/src/com/owncloud/android/files/FileHandler.java new file mode 100644 index 00000000..2eb754dd --- /dev/null +++ b/src/com/owncloud/android/files/FileHandler.java @@ -0,0 +1,30 @@ +/* ownCloud Android client application + * Copyright (C) 2012-2013 ownCloud Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package com.owncloud.android.files; + +import com.owncloud.android.datamodel.OCFile; + +public interface FileHandler { + + /** + * TODO + */ + public void openFile(OCFile file); + + +} diff --git a/src/com/owncloud/android/ui/activity/FileActivity.java b/src/com/owncloud/android/ui/activity/FileActivity.java index 6168879d..87be8496 100644 --- a/src/com/owncloud/android/ui/activity/FileActivity.java +++ b/src/com/owncloud/android/ui/activity/FileActivity.java @@ -23,20 +23,28 @@ import android.accounts.AccountManager; import android.accounts.AccountManagerCallback; import android.accounts.AccountManagerFuture; import android.accounts.OperationCanceledException; +import android.content.ActivityNotFoundException; +import android.content.Intent; +import android.net.Uri; import android.os.Bundle; +import android.webkit.MimeTypeMap; +import android.widget.Toast; import com.actionbarsherlock.app.SherlockFragmentActivity; import com.owncloud.android.AccountUtils; import com.owncloud.android.Log_OC; import com.owncloud.android.authentication.AccountAuthenticator; import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.files.FileHandler; + +import eu.alefzero.webdav.WebdavUtils; /** * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s . * * @author David A. Velasco */ -public abstract class FileActivity extends SherlockFragmentActivity { +public abstract class FileActivity extends SherlockFragmentActivity implements FileHandler { public static final String EXTRA_FILE = "com.owncloud.android.ui.activity.FILE"; public static final String EXTRA_ACCOUNT = "com.owncloud.android.ui.activity.ACCOUNT"; @@ -52,6 +60,8 @@ public abstract class FileActivity extends SherlockFragmentActivity { /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */ private boolean mRedirectingToSetupAccount = false; + + private FileHandlerImpl mFileHandler; @Override @@ -70,6 +80,8 @@ public abstract class FileActivity extends SherlockFragmentActivity { if (mAccount != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), mAccount.name)) { onAccountChanged(); } + + mFileHandler = new FileHandlerImpl(); } @@ -201,6 +213,11 @@ public abstract class FileActivity extends SherlockFragmentActivity { } } + + + public void openFile(OCFile file) { + mFileHandler.openFile(file); + } /** @@ -209,4 +226,57 @@ public abstract class FileActivity extends SherlockFragmentActivity { * Child classes must grant that state depending on the {@link Account} is updated. */ protected abstract void onAccountChanged(); + + + public class FileHandlerImpl implements FileHandler { + + public void openFile(OCFile file) { + if (file != null) { + String storagePath = file.getStoragePath(); + String encodedStoragePath = WebdavUtils.encodePath(storagePath); + try { + Intent i = new Intent(Intent.ACTION_VIEW); + i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype()); + i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + startActivity(i); + + } catch (Throwable t) { + Log_OC.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + file.getMimetype()); + boolean toastIt = true; + String mimeType = ""; + try { + Intent i = new Intent(Intent.ACTION_VIEW); + mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1)); + if (mimeType == null || !mimeType.equals(file.getMimetype())) { + if (mimeType != null) { + i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType); + } else { + // desperate try + i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), "*/*"); + } + i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + startActivity(i); + toastIt = false; + } + + } catch (IndexOutOfBoundsException e) { + Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath); + + } catch (ActivityNotFoundException e) { + Log_OC.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension"); + + } catch (Throwable th) { + Log_OC.e(TAG, "Unexpected problem when opening: " + storagePath, th); + + } finally { + if (toastIt) { + Toast.makeText(FileActivity.this, "There is no application to handle file " + file.getFileName(), Toast.LENGTH_SHORT).show(); + } + } + } + } else { + Log_OC.wtf(TAG, "Trying to open a NULL OCFile"); + } + } + } } diff --git a/src/com/owncloud/android/ui/fragment/FileDetailFragment.java b/src/com/owncloud/android/ui/fragment/FileDetailFragment.java index 6d8039a2..96dc7098 100644 --- a/src/com/owncloud/android/ui/fragment/FileDetailFragment.java +++ b/src/com/owncloud/android/ui/fragment/FileDetailFragment.java @@ -24,12 +24,10 @@ import java.util.List; import android.accounts.Account; import android.app.Activity; -import android.content.ActivityNotFoundException; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.support.v4.app.FragmentTransaction; @@ -37,7 +35,6 @@ import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; -import android.webkit.MimeTypeMap; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.ProgressBar; @@ -73,7 +70,6 @@ import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener; import com.owncloud.android.R; import eu.alefzero.webdav.OnDatatransferProgressListener; -import eu.alefzero.webdav.WebdavUtils; /** * This Fragment is used to display the details about a file. @@ -344,7 +340,7 @@ public class FileDetailFragment extends SherlockFragment implements public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_open_file_with: { - openFile(); + mContainerActivity.openFile(mFile); return true; } case R.id.action_remove_file: { @@ -469,56 +465,6 @@ public class FileDetailFragment extends SherlockFragment implements } } - /** - * Opens mFile. - */ - private void openFile() { - - String storagePath = mFile.getStoragePath(); - String encodedStoragePath = WebdavUtils.encodePath(storagePath); - try { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mFile.getMimetype()); - i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); - startActivity(i); - - } catch (Throwable t) { - Log_OC.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype()); - boolean toastIt = true; - String mimeType = ""; - try { - Intent i = new Intent(Intent.ACTION_VIEW); - mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1)); - if (mimeType == null || !mimeType.equals(mFile.getMimetype())) { - if (mimeType != null) { - i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType); - } else { - // desperate try - i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), "*/*"); - } - i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); - startActivity(i); - toastIt = false; - } - - } catch (IndexOutOfBoundsException e) { - Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath); - - } catch (ActivityNotFoundException e) { - Log_OC.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension"); - - } catch (Throwable th) { - Log_OC.e(TAG, "Unexpected problem when opening: " + storagePath, th); - - } finally { - if (toastIt) { - Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show(); - } - } - - } - } - @Override public void onConfirmation(String callerTag) { if (callerTag.equals(FTAG_CONFIRMATION)) { diff --git a/src/com/owncloud/android/ui/fragment/FileFragment.java b/src/com/owncloud/android/ui/fragment/FileFragment.java index 30549513..4e3fc8a3 100644 --- a/src/com/owncloud/android/ui/fragment/FileFragment.java +++ b/src/com/owncloud/android/ui/fragment/FileFragment.java @@ -21,6 +21,7 @@ import android.content.Intent; import android.support.v4.app.Fragment; import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.files.FileHandler; import com.owncloud.android.ui.activity.TransferServiceGetter; /** @@ -44,7 +45,7 @@ public interface FileFragment { * * @author David A. Velasco */ - public interface ContainerActivity extends TransferServiceGetter { + public interface ContainerActivity extends TransferServiceGetter, FileHandler { /** * Callback method invoked when the detail fragment wants to notice its container diff --git a/src/com/owncloud/android/ui/fragment/OCFileListFragment.java b/src/com/owncloud/android/ui/fragment/OCFileListFragment.java index f0d074fd..309f9da8 100644 --- a/src/com/owncloud/android/ui/fragment/OCFileListFragment.java +++ b/src/com/owncloud/android/ui/fragment/OCFileListFragment.java @@ -26,6 +26,7 @@ import com.owncloud.android.Log_OC; import com.owncloud.android.R; import com.owncloud.android.datamodel.DataStorageManager; import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.files.FileHandler; import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder; import com.owncloud.android.files.services.FileUploader.FileUploaderBinder; import com.owncloud.android.operations.OnRemoteOperationListener; @@ -266,49 +267,7 @@ public class OCFileListFragment extends FragmentListView implements EditNameDial return true; } case R.id.action_open_file_with: { - String storagePath = mTargetFile.getStoragePath(); - String encodedStoragePath = WebdavUtils.encodePath(storagePath); - try { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mTargetFile.getMimetype()); - i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); - startActivity(i); - - } catch (Throwable t) { - Log_OC.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mTargetFile.getMimetype()); - boolean toastIt = true; - String mimeType = ""; - try { - Intent i = new Intent(Intent.ACTION_VIEW); - mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1)); - if (mimeType == null || !mimeType.equals(mTargetFile.getMimetype())) { - if (mimeType != null) { - i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType); - } else { - // desperate try - i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), "*/*"); - } - i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); - startActivity(i); - toastIt = false; - } - - } catch (IndexOutOfBoundsException e) { - Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath); - - } catch (ActivityNotFoundException e) { - Log_OC.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension"); - - } catch (Throwable th) { - Log_OC.e(TAG, "Unexpected problem when opening: " + storagePath, th); - - } finally { - if (toastIt) { - Toast.makeText(getActivity(), "There is no application to handle file " + mTargetFile.getFileName(), Toast.LENGTH_SHORT).show(); - } - } - - } + mContainerActivity.openFile(mTargetFile); return true; } case R.id.action_download_file: @@ -422,7 +381,7 @@ public class OCFileListFragment extends FragmentListView implements EditNameDial * * @author David A. Velasco */ - public interface ContainerActivity extends TransferServiceGetter, OnRemoteOperationListener { + public interface ContainerActivity extends TransferServiceGetter, OnRemoteOperationListener, FileHandler { /** * Callback method invoked when a directory is clicked by the user on the files list diff --git a/src/com/owncloud/android/ui/preview/PreviewImageActivity.java b/src/com/owncloud/android/ui/preview/PreviewImageActivity.java index 7a0fc750..164f9293 100644 --- a/src/com/owncloud/android/ui/preview/PreviewImageActivity.java +++ b/src/com/owncloud/android/ui/preview/PreviewImageActivity.java @@ -56,7 +56,7 @@ import com.owncloud.android.R; * * @author David A. Velasco */ -public class PreviewImageActivity extends SherlockFragmentActivity implements FileFragment.ContainerActivity, ViewPager.OnPageChangeListener, OnTouchListener { +public class PreviewImageActivity extends FileActivity implements FileFragment.ContainerActivity, ViewPager.OnPageChangeListener, OnTouchListener { public static final int DIALOG_SHORT_WAIT = 0; @@ -440,6 +440,11 @@ public class PreviewImageActivity extends SherlockFragmentActivity implements Fi } mFullScreen = !mFullScreen; } + + @Override + protected void onAccountChanged() { + // TODO + } }