Merge branch 'develop' into check_server_certificates_in_SSO_webview
authormasensio <masensio@solidgear.es>
Mon, 10 Mar 2014 10:08:42 +0000 (11:08 +0100)
committermasensio <masensio@solidgear.es>
Mon, 10 Mar 2014 10:08:42 +0000 (11:08 +0100)
1  2 
res/values/setup.xml
res/values/strings.xml
src/com/owncloud/android/authentication/SsoWebViewClient.java
src/com/owncloud/android/syncadapter/FileSyncAdapter.java
src/com/owncloud/android/ui/activity/CopyToClipboardActivity.java
src/com/owncloud/android/ui/activity/FileDisplayActivity.java
src/com/owncloud/android/ui/preview/PreviewImageActivity.java
src/com/owncloud/android/ui/preview/PreviewVideoActivity.java

diff --combined res/values/setup.xml
@@@ -11,7 -11,7 +11,7 @@@
      <string name ="default_display_name_for_root_folder">ownCloud</string>
      
      <!-- URLs and flags related -->
 -    <string name="server_url"></string>
 +    <string name="server_url">https://test1.owncloud.com/oc6-shib</string>
      <bool name="show_server_url_input">true</bool>
      <bool name="show_welcome_link">true</bool>
        <string name="welcome_link_url">"https://owncloud.com/mobile/new"</string>
      
      <!-- Flags to setup the authentication methods available in the app -->
      <string name="auth_method_oauth2">off</string>
 -    <string name="auth_method_saml_web_sso">off</string>
 +    <string name="auth_method_saml_web_sso">on</string>
      
+     <!-- Flags to enable/disable some features -->
+     <string name = "send_files_to_other_apps">on</string>
+     
+     
      <!-- Colors -->
      <color name="login_background_color">#FFFFFF</color>
      <color name="login_logo_background_color">#FFFFFF</color>
diff --combined res/values/strings.xml
        <string name="ssl_validator_label_validity_to">To:</string>
        <string name="ssl_validator_label_signature">Signature:</string>
        <string name="ssl_validator_label_signature_algorithm">Algorithm:</string>
 +      <string name="ssl_validator_null_cert">The certificate could not be shown.</string>
 +      <string name="ssl_validator_no_info_about_error">- No information about the error</string>
                        
      <string name="placeholder_sentence">This is a placeholder</string>
      <string name="placeholder_filename">placeholder.txt</string>
        <string name="unshare_link_file_no_exist">Unable to unshare this file or folder. It does not exist.</string>
        <string name="unshare_link_file_error">An error occurred while trying to unshare this file or folder</string>
  
+       <string name="activity_chooser_send_file_title">Send</string>
        <string name="copy_link">Copy link</string>     
        <string name="clipboard_text_copied">Copied to clipboard</string>
  </resources>
  
  package com.owncloud.android.authentication;
  
 +import java.io.ByteArrayInputStream;
  import java.lang.ref.WeakReference;
 +import java.security.cert.Certificate;
 +import java.security.cert.CertificateException;
 +import java.security.cert.CertificateFactory;
 +import java.security.cert.X509Certificate;
  
- import com.owncloud.android.R;
- import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;
 +import com.owncloud.android.lib.common.network.NetworkUtils;
 +import com.actionbarsherlock.app.SherlockFragmentActivity;
- import com.owncloud.android.ui.dialog.SslUntrustedCertDialog.OnSslUntrustedCertListener;
 +import com.owncloud.android.ui.dialog.SslUntrustedCertDialogABSTRACT;
  import com.owncloud.android.utils.Log_OC;
  
 +import android.content.Context;
  import android.graphics.Bitmap;
 +import android.net.http.SslCertificate;
  import android.net.http.SslError;
 +import android.os.Bundle;
  import android.os.Handler;
  import android.os.Message;
 +import android.support.v4.app.FragmentManager;
 +import android.support.v4.app.FragmentTransaction;
  import android.view.KeyEvent;
  import android.view.View;
  import android.webkit.CookieManager;
@@@ -68,14 -52,12 +65,14 @@@ public class SsoWebViewClient extends W
          public void onSsoFinished(String sessionCookie);
      }
      
 +    private Context mContext;
      private Handler mListenerHandler;
      private WeakReference<SsoWebViewClientListener> mListenerRef;
      private String mTargetUrl;
      private String mLastReloadedUrlAtError;
      
 -    public SsoWebViewClient (Handler listenerHandler, SsoWebViewClientListener listener) {
 +    public SsoWebViewClient (Context context, Handler listenerHandler, SsoWebViewClientListener listener) {
 +        mContext = context;
          mListenerHandler = listenerHandler;
          mListenerRef = new WeakReference<SsoWebViewClient.SsoWebViewClientListener>(listener);
          mTargetUrl = "fake://url.to.be.set";
      }
      
      @Override
 -    public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
 +    public void onReceivedSslError (final WebView view, final SslErrorHandler handler, SslError error) {
          Log_OC.d(TAG, "onReceivedSslError : " + error);
 -        handler.proceed();
 +        // Test 1
 +        X509Certificate x509Certificate = getX509CertificateFromError(error);
 +        boolean isKnownServer = false;
 +        
 +        if (x509Certificate != null) {
 +            Log_OC.d(TAG, "------>>>>> x509Certificate " + x509Certificate.toString());
 +            
 +            try {
 +                isKnownServer = NetworkUtils.isCertInKnownServersStore((Certificate) x509Certificate, mContext);
 +            } catch (Exception e) {
 +                Log_OC.e(TAG, "Exception: " + e.getMessage());
 +            }
 +        }
 +        
 +         if (isKnownServer) {
 +             handler.proceed();
 +         } else if (x509Certificate != null) {
 +             // Show a dialog with the certificate info
 +             ((AuthenticatorActivity)mContext).showUntrustedCertDialog(x509Certificate, error);
 +             handler.cancel();
 +         } else {
 +             // Show a dialog with the certificate information available in SslError (not full)
 +             SslUntrustedCertDialogABSTRACT dialog = SslUntrustedCertDialogABSTRACT.newInstanceForEmptySslError(error, handler);
 +             FragmentManager fm = ((SherlockFragmentActivity)mContext).getSupportFragmentManager();
 +             FragmentTransaction ft = fm.beginTransaction();
 +             dialog.show(ft, AuthenticatorActivity.DIALOG_UNTRUSTED_CERT);
 +             // let's forward the handler, and see what happens...
 +         }
 +    }
 +    
 +    /**
 +     * Obtain the X509Certificate from SslError
 +     * @param   error     SslError
 +     * @return  X509Certificate from error
 +     */
 +    public X509Certificate getX509CertificateFromError (SslError error) {
 +        Bundle bundle = SslCertificate.saveState(error.getCertificate());
 +        X509Certificate x509Certificate;
 +        byte[] bytes = bundle.getByteArray("x509-certificate");
 +        if (bytes == null) {
 +            x509Certificate = null;
 +        } else {
 +            try {
 +                CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
 +                Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(bytes));
 +                x509Certificate = (X509Certificate) cert;
 +            } catch (CertificateException e) {
 +                x509Certificate = null;
 +            }
 +        }        
 +        return x509Certificate;
      }
      
      @Override
@@@ -30,7 -30,7 +30,6 @@@ import com.owncloud.android.R
  import com.owncloud.android.authentication.AuthenticatorActivity;
  import com.owncloud.android.datamodel.FileDataStorageManager;
  import com.owncloud.android.datamodel.OCFile;
--import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
  import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  import com.owncloud.android.operations.SynchronizeFolderOperation;
  import com.owncloud.android.operations.UpdateOCVersionOperation;
@@@ -41,7 -41,7 +40,6 @@@ import com.owncloud.android.utils.Log_O
  
  
  import android.accounts.Account;
--import android.accounts.AccountManager;
  import android.accounts.AccountsException;
  import android.app.Notification;
  import android.app.NotificationManager;
@@@ -22,7 -22,7 +22,6 @@@ import com.owncloud.android.R
  import android.app.Activity;
  import android.content.ClipData;
  import android.content.Intent;
--import android.os.Build;
  import android.os.Bundle;
  import android.text.ClipboardManager;
  import android.widget.Toast;
@@@ -19,6 -19,7 +19,6 @@@
  package com.owncloud.android.ui.activity;
  
  import java.io.File;
 -
  import android.accounts.Account;
  import android.app.AlertDialog;
  import android.app.Dialog;
@@@ -33,6 -34,7 +33,6 @@@ import android.content.IntentFilter
  import android.content.ServiceConnection;
  import android.content.SharedPreferences;
  import android.content.SyncRequest;
 -import android.content.res.Configuration;
  import android.content.res.Resources.NotFoundException;
  import android.database.Cursor;
  import android.net.Uri;
@@@ -41,7 -43,6 +41,7 @@@ import android.os.IBinder
  import android.preference.PreferenceManager;
  import android.provider.MediaStore;
  import android.support.v4.app.Fragment;
 +import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentTransaction;
  //import android.support.v4.content.LocalBroadcastManager;
  import android.util.Log;
@@@ -79,9 -80,9 +79,9 @@@ import com.owncloud.android.operations.
  import com.owncloud.android.services.OperationsService;
  import com.owncloud.android.syncadapter.FileSyncAdapter;
  import com.owncloud.android.ui.dialog.EditNameDialog;
 +import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;
  import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;
 -import com.owncloud.android.ui.dialog.SslValidatorDialog;
 -import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener;
 +import com.owncloud.android.ui.dialog.SslUntrustedCertDialog.OnSslUntrustedCertListener;
  import com.owncloud.android.ui.fragment.FileDetailFragment;
  import com.owncloud.android.ui.fragment.FileFragment;
  import com.owncloud.android.ui.fragment.OCFileListFragment;
@@@ -100,7 -101,7 +100,7 @@@ import com.owncloud.android.utils.Log_O
   */
  
  public class FileDisplayActivity extends HookActivity implements
 -OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener, OnSslValidatorListener, EditNameDialogListener {
 +OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener, OnSslUntrustedCertListener, EditNameDialogListener {
  
      private ArrayAdapter<String> mDirectories;
  
      private static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
      private static final String KEY_SYNC_IN_PROGRESS = "SYNC_IN_PROGRESS";
      //private static final String KEY_REFRESH_SHARES_IN_PROGRESS = "SHARES_IN_PROGRESS";
+     private static final String KEY_WAITING_TO_SEND = "WAITING_TO_SEND";
  
      public static final int DIALOG_SHORT_WAIT = 0;
      private static final int DIALOG_CHOOSE_UPLOAD_SOURCE = 1;
 -    private static final int DIALOG_SSL_VALIDATOR = 2;
 -    private static final int DIALOG_CERT_NOT_SAVED = 3;
 +    //private static final int DIALOG_SSL_VALIDATOR = 2;
 +    private static final int DIALOG_CERT_NOT_SAVED = 2;
      
      public static final String ACTION_DETAILS = "com.owncloud.android.ui.activity.action.DETAILS";
  
      private boolean mSyncInProgress = false;
      //private boolean mRefreshSharesInProgress = false;
  
 +    private String DIALOG_UNTRUSTED_CERT;
++    
+     private OCFile mWaitingToSend;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
              mWaitingToPreview = (OCFile) savedInstanceState.getParcelable(FileDisplayActivity.KEY_WAITING_TO_PREVIEW);
              mSyncInProgress = savedInstanceState.getBoolean(KEY_SYNC_IN_PROGRESS);
              //mRefreshSharesInProgress = savedInstanceState.getBoolean(KEY_REFRESH_SHARES_IN_PROGRESS);
+             mWaitingToSend = (OCFile) savedInstanceState.getParcelable(FileDisplayActivity.KEY_WAITING_TO_SEND);
             
          } else {
              mWaitingToPreview = null;
              mSyncInProgress = false;
              //mRefreshSharesInProgress = false;
+             mWaitingToSend = null;
          }        
  
          /// USER INTERFACE
              unbindService(mUploadConnection);
      }
  
      /**
       *  Called when the ownCloud {@link Account} associated to the Activity was just updated.
       */ 
          outState.putParcelable(FileDisplayActivity.KEY_WAITING_TO_PREVIEW, mWaitingToPreview);
          outState.putBoolean(FileDisplayActivity.KEY_SYNC_IN_PROGRESS, mSyncInProgress);
          //outState.putBoolean(FileDisplayActivity.KEY_REFRESH_SHARES_IN_PROGRESS, mRefreshSharesInProgress);
+         outState.putParcelable(FileDisplayActivity.KEY_WAITING_TO_SEND, mWaitingToSend);
  
          Log_OC.d(TAG, "onSaveInstanceState() end");
      }
  
  
      @Override
 -    protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
 -        if (id == DIALOG_SSL_VALIDATOR && mLastSslUntrustedServerResult != null) {
 -            ((SslValidatorDialog)dialog).updateResult(mLastSslUntrustedServerResult);
 -        }
 -    }
 -
 -
 -    @Override
      protected Dialog onCreateDialog(int id) {
          Dialog dialog = null;
          AlertDialog.Builder builder;
              dialog = builder.create();
              break;
          }
 -        case DIALOG_SSL_VALIDATOR: {
 -            dialog = SslValidatorDialog.newInstance(this, mLastSslUntrustedServerResult, this);
 -            break;
 -        }
          case DIALOG_CERT_NOT_SAVED: {
              builder = new AlertDialog.Builder(this);
              builder.setMessage(getResources().getString(R.string.ssl_validator_not_saved));
              if (synchResult != null) {
                  if (synchResult.getCode().equals(RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED)) {
                      mLastSslUntrustedServerResult = synchResult;
 -                    showDialog(DIALOG_SSL_VALIDATOR); 
                  }
              }
          }
                  refreshSecondFragment(intent.getAction(), downloadedRemotePath, intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false));
              }
  
+             if (mWaitingToSend != null) {
+                 mWaitingToSend = getStorageManager().getFileByPath(mWaitingToSend.getRemotePath()); // Update the file to send
+                 if (mWaitingToSend.isDown()) { 
+                     sendDownloadedFile();
+                 }
+             }
+             
              removeStickyBroadcast(intent);
          }
  
                  if ((getSharesResult != null) &&
                          RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED.equals(getSharesResult.getCode())) {
                      mLastSslUntrustedServerResult = getSharesResult;
 -                    showDialog(DIALOG_SSL_VALIDATOR); 
 +                    showUntrustedCertDialog(mLastSslUntrustedServerResult);
                  }
  
                  //setSupportProgressBarIndeterminateVisibility(mRefreshSharesInProgress || mSyncInProgress);
              msg.show();
              if (result.isSslRecoverableException()) {
                  mLastSslUntrustedServerResult = result;
 -                showDialog(DIALOG_SSL_VALIDATOR); 
 +                showUntrustedCertDialog(mLastSslUntrustedServerResult);
              }
          }
      }
                  msg.show();
                  if (result.isSslRecoverableException()) {
                      mLastSslUntrustedServerResult = result;
 -                    showDialog(DIALOG_SSL_VALIDATOR); 
 +                    showUntrustedCertDialog(mLastSslUntrustedServerResult);
                  }
              }
          }
          mRefreshSharesInProgress = true;
      }
      */
 +    
 +    /**
 +     * Show untrusted cert dialog 
 +     */
 +    public void showUntrustedCertDialog(RemoteOperationResult result) {
 +        // Show a dialog with the certificate info
 +        SslUntrustedCertDialog dialog = SslUntrustedCertDialog.newInstance(result, this);
 +        FragmentManager fm = getSupportFragmentManager();
 +        FragmentTransaction ft = fm.beginTransaction();
 +        dialog.show(ft, DIALOG_UNTRUSTED_CERT);
 +        
 +    }
 +    
 +    /**
 +     * Dismiss untrusted cert dialog
 +     */
 +    public void dismissUntrustedCertDialog(){
 +        Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_UNTRUSTED_CERT);
 +        if (frag != null) {
 +            SslUntrustedCertDialog dialog = (SslUntrustedCertDialog) frag;
 +            dialog.dismiss();
 +        }
 +    }
 +
 +    @Override
 +    public void onCancelCertificate() {
 +        // TODO Auto-generated method stub
 +        
 +    }
  
+     /**
+      * Requests the download of the received {@link OCFile} , updates the UI
+      * to monitor the download progress and prepares the activity to send the file
+      * when the download finishes.
+      * 
+      * @param file          {@link OCFile} to download and preview.
+      */
+     @Override
+     public void startDownloadForSending(OCFile file) {
+         mWaitingToSend = file;
+         requestForDownload(mWaitingToSend);
+         boolean hasSecondFragment = (getSecondFragment()!= null);
+         updateFragmentsVisibility(hasSecondFragment);
+     }
+     
+     private void requestForDownload(OCFile file) {
+         Account account = getAccount();
+         if (!mDownloaderBinder.isDownloading(account, file)) {
+             Intent i = new Intent(this, FileDownloader.class);
+             i.putExtra(FileDownloader.EXTRA_ACCOUNT, account);
+             i.putExtra(FileDownloader.EXTRA_FILE, file);
+             startService(i);
+         }
+     }
+     
+     private void sendDownloadedFile(){
+         getFileOperationsHelper().sendDownloadedFile(mWaitingToSend, this);
+         mWaitingToSend = null;
+     }
+     
  }
@@@ -23,6 -23,7 +23,6 @@@ import android.content.Intent
  import android.content.IntentFilter;
  import android.content.ServiceConnection;
  import android.content.SharedPreferences;
 -import android.content.res.Configuration;
  import android.os.Bundle;
  import android.os.IBinder;
  import android.preference.PreferenceManager;
@@@ -245,7 -246,6 +245,6 @@@ public class PreviewImageActivity exten
          super.onDestroy();
      }
      
-     
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          boolean returnValue = false;
@@@ -27,6 -27,7 +27,6 @@@ import android.accounts.Account
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.content.Intent;
 -import android.content.res.Configuration;
  import android.media.MediaPlayer;
  import android.media.MediaPlayer.OnCompletionListener;
  import android.media.MediaPlayer.OnErrorListener;
@@@ -188,7 -189,6 +188,6 @@@ public class PreviewVideoActivity exten
          return true;
      }
      
-     
      @Override
      protected void onAccountSet(boolean stateWasRecovered) {
          super.onAccountSet(stateWasRecovered);