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;
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
package com.owncloud.android.ui.activity;
import java.io.File;
-
import android.accounts.Account;
import android.app.AlertDialog;
import android.app.Dialog;
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;
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;
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;
*/
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;
+ }
+
}