OC-2459: Ask for pincode, when click on an instant upload notification
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / FileDownloadFragment.java
index da43d11..c18f84b 100644 (file)
@@ -3,9 +3,8 @@
  *   Copyright (C) 2012-2013  ownCloud Inc.
  *
  *   This program is free software: you can redistribute it and/or modify
  *   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 as published by
- *   the Free Software Foundation, either version 3 of the License, or
- *   (at your option) any later version.
+ *   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
  *
  *   This program is distributed in the hope that it will be useful,
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -20,68 +19,64 @@ package com.owncloud.android.ui.preview;
 
 import java.lang.ref.WeakReference;
 
 
 import java.lang.ref.WeakReference;
 
+import com.owncloud.android.R;
+import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
+import com.owncloud.android.ui.fragment.FileFragment;
+import com.owncloud.android.utils.Log_OC;
+
 import android.accounts.Account;
 import android.app.Activity;
 import android.accounts.Account;
 import android.app.Activity;
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.content.IntentFilter;
 import android.os.Bundle;
 import android.os.Bundle;
-import android.util.Log;
+import android.support.v4.app.FragmentStatePagerAdapter;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.ViewGroup;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.ViewGroup;
-import android.widget.Button;
+import android.widget.ImageButton;
 import android.widget.ProgressBar;
 import android.widget.TextView;
 
 import android.widget.ProgressBar;
 import android.widget.TextView;
 
-import com.actionbarsherlock.app.SherlockFragment;
-import com.owncloud.android.datamodel.FileDataStorageManager;
-import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.files.services.FileDownloader;
-import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
-import com.owncloud.android.ui.fragment.FileFragment;
-
-import com.owncloud.android.R;
+import com.owncloud.android.oc_framework.network.webdav.OnDatatransferProgressListener;
 
 
-import eu.alefzero.webdav.OnDatatransferProgressListener;
 
 /**
  * This Fragment is used to monitor the progress of a file downloading.
  * 
  * @author David A. Velasco
  */
 
 /**
  * This Fragment is used to monitor the progress of a file downloading.
  * 
  * @author David A. Velasco
  */
-public class FileDownloadFragment extends SherlockFragment implements OnClickListener, FileFragment {
+public class FileDownloadFragment extends FileFragment implements OnClickListener {
 
     public static final String EXTRA_FILE = "FILE";
     public static final String EXTRA_ACCOUNT = "ACCOUNT";
 
     public static final String EXTRA_FILE = "FILE";
     public static final String EXTRA_ACCOUNT = "ACCOUNT";
+    private static final String EXTRA_ERROR = "ERROR";
 
     private FileFragment.ContainerActivity mContainerActivity;
     
     private View mView;
 
     private FileFragment.ContainerActivity mContainerActivity;
     
     private View mView;
-    private OCFile mFile;
     private Account mAccount;
     private Account mAccount;
-    private FileDataStorageManager mStorageManager;
     
     
-    private DownloadFinishReceiver mDownloadFinishReceiver;
     public ProgressListener mProgressListener;
     private boolean mListening;
     
     private static final String TAG = FileDownloadFragment.class.getSimpleName();
     public ProgressListener mProgressListener;
     private boolean mListening;
     
     private static final String TAG = FileDownloadFragment.class.getSimpleName();
-
     
     
+    private boolean mIgnoreFirstSavedState;
+    private boolean mError;
+    
+
     /**
      * Creates an empty details fragment.
      * 
      * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically. 
      */
     public FileDownloadFragment() {
     /**
      * Creates an empty details fragment.
      * 
      * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically. 
      */
     public FileDownloadFragment() {
-        mFile = null;
+        super();
         mAccount = null;
         mAccount = null;
-        mStorageManager = null;
         mProgressListener = null;
         mListening = false;
         mProgressListener = null;
         mListening = false;
+        mIgnoreFirstSavedState = false;
+        mError = false;
     }
     
     
     }
     
     
@@ -92,20 +87,21 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis
      * 
      * @param fileToDetail      An {@link OCFile} to show in the fragment
      * @param ocAccount         An ownCloud account; needed to start downloads
      * 
      * @param fileToDetail      An {@link OCFile} to show in the fragment
      * @param ocAccount         An ownCloud account; needed to start downloads
+     * @param ignoreFirstSavedState     Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution 
      */
      */
-    public FileDownloadFragment(OCFile fileToDetail, Account ocAccount) {
-        mFile = fileToDetail;
+    public FileDownloadFragment(OCFile fileToDetail, Account ocAccount, boolean ignoreFirstSavedState) {
+        super(fileToDetail);
         mAccount = ocAccount;
         mAccount = ocAccount;
-        mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment 
         mProgressListener = null;
         mListening = false;
         mProgressListener = null;
         mListening = false;
+        mIgnoreFirstSavedState = ignoreFirstSavedState;
+        mError = false;
     }
     
     
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
     }
     
     
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONCREATE " + ((mFile == null)? "(NULL)" : mFile.getFileName()));
     }
     
 
     }
     
 
@@ -115,8 +111,13 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis
         super.onCreateView(inflater, container, savedInstanceState);
         
         if (savedInstanceState != null) {
         super.onCreateView(inflater, container, savedInstanceState);
         
         if (savedInstanceState != null) {
-            mFile = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_FILE);
-            mAccount = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_ACCOUNT);
+            if (!mIgnoreFirstSavedState) {
+                setFile((OCFile)savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_FILE));
+                mAccount = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_ACCOUNT);
+                mError = savedInstanceState.getBoolean(FileDownloadFragment.EXTRA_ERROR);
+            } else {
+                mIgnoreFirstSavedState = false;
+            }
         }
         
         View view = null;
         }
         
         View view = null;
@@ -126,7 +127,13 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis
         ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.progressBar);
         mProgressListener = new ProgressListener(progressBar);
         
         ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.progressBar);
         mProgressListener = new ProgressListener(progressBar);
         
-        ((Button)mView.findViewById(R.id.cancelBtn)).setOnClickListener(this);
+        ((ImageButton)mView.findViewById(R.id.cancelBtn)).setOnClickListener(this);
+        
+        if (mError) {
+            setButtonsForRemote();
+        } else {
+            setButtonsForTransferring();
+        }
         
         return view;
     }
         
         return view;
     }
@@ -138,7 +145,6 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis
     @Override
     public void onAttach(Activity activity) {
         super.onAttach(activity);
     @Override
     public void onAttach(Activity activity) {
         super.onAttach(activity);
-        Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONATTACH " + ((mFile == null)?" (NULL)":mFile.getFileName()));
         try {
             mContainerActivity = (ContainerActivity) activity;
             
         try {
             mContainerActivity = (ContainerActivity) activity;
             
@@ -154,9 +160,8 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
-        Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONACTIVITYCREATED " + ((mFile == null)?" (NULL)":mFile.getFileName()));
         if (mAccount != null) {
         if (mAccount != null) {
-            mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());;
+            //mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());;
         }
     }
         
         }
     }
         
@@ -164,50 +169,38 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis
     @Override
     public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
     @Override
     public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
-        outState.putParcelable(FileDownloadFragment.EXTRA_FILE, mFile);
+        outState.putParcelable(FileDownloadFragment.EXTRA_FILE, getFile());
         outState.putParcelable(FileDownloadFragment.EXTRA_ACCOUNT, mAccount);
         outState.putParcelable(FileDownloadFragment.EXTRA_ACCOUNT, mAccount);
+        outState.putBoolean(FileDownloadFragment.EXTRA_ERROR, mError);
     }
 
     @Override
     public void onStart() {
         super.onStart();
     }
 
     @Override
     public void onStart() {
         super.onStart();
-        Log.e(TAG, "FILE_DOWNLOAD_FRAGMENT ONSTART " + mFile.getFileName());
         listenForTransferProgress();
     }
     
     @Override
     public void onResume() {
         listenForTransferProgress();
     }
     
     @Override
     public void onResume() {
-        Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONRESUME " + mFile.getFileName());
         super.onResume();
         super.onResume();
-        
-        mDownloadFinishReceiver = new DownloadFinishReceiver();
-        IntentFilter filter = new IntentFilter(
-                FileDownloader.DOWNLOAD_FINISH_MESSAGE);
-        getActivity().registerReceiver(mDownloadFinishReceiver, filter);
     }
 
 
     @Override
     public void onPause() {
         super.onPause();
     }
 
 
     @Override
     public void onPause() {
         super.onPause();
-        Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONPAUSE " + mFile.getFileName());
-        
-        getActivity().unregisterReceiver(mDownloadFinishReceiver);
-        mDownloadFinishReceiver = null;
     }
 
     
     @Override
     public void onStop() {
         super.onStop();
     }
 
     
     @Override
     public void onStop() {
         super.onStop();
-        Log.e(TAG, "FILE_DOWNLOAD_FRAGMENT ONSTOP " + mFile.getFileName());
         leaveTransferProgress();
     }
     
     @Override
     public void onDestroy() {
         super.onDestroy();
         leaveTransferProgress();
     }
     
     @Override
     public void onDestroy() {
         super.onDestroy();
-        Log.e(TAG, "FILE_DOWNLOAD_FRAGMENT ONDESTROY " + mFile.getFileName());
     }
     
     
     }
     
     
@@ -225,32 +218,27 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis
         switch (v.getId()) {
             case R.id.cancelBtn: {
                 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
         switch (v.getId()) {
             case R.id.cancelBtn: {
                 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
-                if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) {
-                    downloaderBinder.cancel(mAccount, mFile);
+                if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) {
+                    downloaderBinder.cancel(mAccount, getFile());
+                    getActivity().finish(); // :)
+                    /*
                     leaveTransferProgress();
                     if (mFile.isDown()) {
                         setButtonsForDown();
                     } else {
                         setButtonsForRemote();
                     }
                     leaveTransferProgress();
                     if (mFile.isDown()) {
                         setButtonsForDown();
                     } else {
                         setButtonsForRemote();
                     }
+                    */
                 }
                 break;
             }
             default:
                 }
                 break;
             }
             default:
-                Log.e(TAG, "Incorrect view clicked!");
+                Log_OC.e(TAG, "Incorrect view clicked!");
         }
     }
 
     
     /**
         }
     }
 
     
     /**
-     * {@inheritDoc}
-     */
-    public OCFile getFile(){
-        return mFile;
-    }
-    
-    
-    /**
      * Updates the view depending upon the state of the downloading file.
      * 
      * @param   transferring    When true, the view must be updated assuming that the holded file is 
      * Updates the view depending upon the state of the downloading file.
      * 
      * @param   transferring    When true, the view must be updated assuming that the holded file is 
@@ -258,11 +246,11 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis
      */
     public void updateView(boolean transferring) {
         // configure UI for depending upon local state of the file
      */
     public void updateView(boolean transferring) {
         // configure UI for depending upon local state of the file
-        FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
-        if (transferring || (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile))) {
+        FileDownloaderBinder downloaderBinder = (mContainerActivity == null) ? null : mContainerActivity.getFileDownloaderBinder();
+        if (transferring || (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile()))) {
             setButtonsForTransferring();
             
             setButtonsForTransferring();
             
-        } else if (mFile.isDown()) {
+        } else if (getFile().isDown()) {
             
             setButtonsForDown();
             
             
             setButtonsForDown();
             
@@ -270,6 +258,7 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis
             setButtonsForRemote();
         }
         getView().invalidate();
             setButtonsForRemote();
         }
         getView().invalidate();
+        
     }
 
 
     }
 
 
@@ -277,14 +266,17 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis
      * Enables or disables buttons for a file being downloaded
      */
     private void setButtonsForTransferring() {
      * Enables or disables buttons for a file being downloaded
      */
     private void setButtonsForTransferring() {
-        Button downloadButton = (Button) getView().findViewById(R.id.cancelBtn);
-        downloadButton.setText(R.string.common_cancel);
+        getView().findViewById(R.id.cancelBtn).setVisibility(View.VISIBLE);
     
         // show the progress bar for the transfer
     
         // show the progress bar for the transfer
-        ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progressBar);
-        progressBar.setVisibility(View.VISIBLE);
+        getView().findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
         TextView progressText = (TextView)getView().findViewById(R.id.progressText);
         progressText.setText(R.string.downloader_download_in_progress_ticker);
         TextView progressText = (TextView)getView().findViewById(R.id.progressText);
         progressText.setText(R.string.downloader_download_in_progress_ticker);
+        progressText.setVisibility(View.VISIBLE);
+                
+        // hides the error icon
+        getView().findViewById(R.id.errorText).setVisibility(View.GONE);
+        getView().findViewById(R.id.error_image).setVisibility(View.GONE);
     }
     
 
     }
     
 
@@ -292,64 +284,44 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis
      * Enables or disables buttons for a file locally available 
      */
     private void setButtonsForDown() {
      * Enables or disables buttons for a file locally available 
      */
     private void setButtonsForDown() {
-        Button downloadButton = (Button) getView().findViewById(R.id.cancelBtn);
-        downloadButton.setVisibility(View.GONE);
+        getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE);
     
         // hides the progress bar
     
         // hides the progress bar
-        ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progressBar);
-        progressBar.setVisibility(View.GONE);
+        getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
         
         // updates the text message
         TextView progressText = (TextView)getView().findViewById(R.id.progressText);
         progressText.setText(R.string.common_loading);
         
         // updates the text message
         TextView progressText = (TextView)getView().findViewById(R.id.progressText);
         progressText.setText(R.string.common_loading);
+        progressText.setVisibility(View.VISIBLE);
+        
+        // hides the error icon
+        getView().findViewById(R.id.errorText).setVisibility(View.GONE);
+        getView().findViewById(R.id.error_image).setVisibility(View.GONE);
     }
 
     
     /**
      * Enables or disables buttons for a file not locally available 
     }
 
     
     /**
      * Enables or disables buttons for a file not locally available 
+     * 
+     * Currently, this is only used when a download was failed
      */
     private void setButtonsForRemote() {
      */
     private void setButtonsForRemote() {
-        Button downloadButton = (Button) getView().findViewById(R.id.cancelBtn);
-        downloadButton.setVisibility(View.GONE);
-        //downloadButton.setText(R.string.filedetails_download);
+        getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE);
         
         
-        // hides the progress bar
-        ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progressBar);
-        progressBar.setVisibility(View.GONE);
-        
-        // updates the text message
-        TextView progressText = (TextView)getView().findViewById(R.id.progressText);
-        progressText.setText(R.string.downloader_not_downloaded_yet);
-    }
-    
+        // hides the progress bar and message
+        getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
+        getView().findViewById(R.id.progressText).setVisibility(View.GONE);
 
 
-    /**
-     * Once the file download has finished -> update view
-     */
-    private class DownloadFinishReceiver extends BroadcastReceiver {
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
-            if (accountName.equals(mAccount.name)) {
-                boolean downloadWasFine = intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false);
-                String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
-                if (mFile.getRemotePath().equals(downloadedRemotePath)) {
-                    if (downloadWasFine) {
-                        mFile = mStorageManager.getFileByPath(downloadedRemotePath);
-                    }
-                    updateView(false);
-                    getActivity().removeStickyBroadcast(intent);
-                    mContainerActivity.notifySuccessfulDownload(mFile, intent, downloadWasFine);
-                }
-            }
-        }
+        // shows the error icon and message
+        getView().findViewById(R.id.errorText).setVisibility(View.VISIBLE);
+        getView().findViewById(R.id.error_image).setVisibility(View.VISIBLE);
     }
     
     }
     
-    
+
     public void listenForTransferProgress() {
         if (mProgressListener != null && !mListening) {
             if (mContainerActivity.getFileDownloaderBinder() != null) {
     public void listenForTransferProgress() {
         if (mProgressListener != null && !mListening) {
             if (mContainerActivity.getFileDownloaderBinder() != null) {
-                mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, mFile);
+                mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, getFile());
                 mListening = true;
                 setButtonsForTransferring();
             }
                 mListening = true;
                 setButtonsForTransferring();
             }
@@ -360,7 +332,8 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis
     public void leaveTransferProgress() {
         if (mProgressListener != null) {
             if (mContainerActivity.getFileDownloaderBinder() != null) {
     public void leaveTransferProgress() {
         if (mProgressListener != null) {
             if (mContainerActivity.getFileDownloaderBinder() != null) {
-                mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, mFile);
+                mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
+                mListening = false;
             }
         }
     }
             }
         }
     }
@@ -397,6 +370,11 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis
             mLastPercent = percent;
         }
 
             mLastPercent = percent;
         }
 
+    }
+
+
+    public void setError(boolean error) {
+        mError = error;
     };
     
 
     };