Merge branch 'master' into oauth_login
authorDavid A. Velasco <dvelasco@solidgear.es>
Thu, 17 Jan 2013 13:25:49 +0000 (14:25 +0100)
committerDavid A. Velasco <dvelasco@solidgear.es>
Thu, 17 Jan 2013 13:25:49 +0000 (14:25 +0100)
Conflicts:
AndroidManifest.xml
res/values-de-rDE/strings.xml
res/values-de/strings.xml
res/values/strings.xml
src/com/owncloud/android/Uploader.java
src/com/owncloud/android/datamodel/FileDataStorageManager.java
src/com/owncloud/android/files/OwnCloudFileObserver.java
src/com/owncloud/android/files/services/FileDownloader.java
src/com/owncloud/android/files/services/FileUploader.java
src/com/owncloud/android/operations/RemoteOperationResult.java
src/com/owncloud/android/syncadapter/FileSyncAdapter.java
src/com/owncloud/android/ui/activity/AuthenticatorActivity.java
src/com/owncloud/android/ui/activity/FileDisplayActivity.java
src/com/owncloud/android/ui/activity/UploadFilesActivity.java
src/com/owncloud/android/ui/fragment/FileDetailFragment.java
src/com/owncloud/android/ui/fragment/OCFileListFragment.java

1  2 
AndroidManifest.xml
res/values/strings.xml
src/com/owncloud/android/Uploader.java
src/com/owncloud/android/files/OwnCloudFileObserver.java
src/com/owncloud/android/files/services/FileDownloader.java
src/com/owncloud/android/files/services/FileUploader.java
src/com/owncloud/android/operations/RemoteOperationResult.java
src/com/owncloud/android/syncadapter/FileSyncAdapter.java
src/com/owncloud/android/ui/activity/AuthenticatorActivity.java
src/com/owncloud/android/ui/fragment/FileDetailFragment.java

Simple merge
Simple merge
@@@ -30,7 -30,7 +30,6 @@@ import com.owncloud.android.datamodel.D
  import com.owncloud.android.datamodel.FileDataStorageManager;\r
  import com.owncloud.android.datamodel.OCFile;\r
  import com.owncloud.android.files.services.FileUploader;\r
--import com.owncloud.android.network.OwnCloudClientUtils;\r
  \r
  import android.accounts.Account;\r
  import android.accounts.AccountManager;\r
@@@ -60,7 -60,7 +59,6 @@@ import android.widget.SimpleAdapter
  import android.widget.Toast;\r
  \r
  import com.owncloud.android.R;\r
--import eu.alefzero.webdav.WebdavClient;\r
  \r
  /**\r
   * This can be used to upload things to an ownCloud instance.\r
@@@ -22,13 -22,13 +22,11 @@@ import java.io.File
  
  import com.owncloud.android.datamodel.FileDataStorageManager;
  import com.owncloud.android.datamodel.OCFile;
--import com.owncloud.android.network.OwnCloudClientUtils;
  import com.owncloud.android.operations.RemoteOperationResult;
  import com.owncloud.android.operations.SynchronizeFileOperation;
  import com.owncloud.android.operations.RemoteOperationResult.ResultCode;
  import com.owncloud.android.ui.activity.ConflictsResolveActivity;
  
--import eu.alefzero.webdav.WebdavClient;
  
  import android.accounts.Account;
  import android.content.Context;
@@@ -264,19 -263,17 +265,19 @@@ public class FileDownloader extends Ser
              \r
              notifyDownloadStart(mCurrentDownload);\r
  \r
 -            /// prepare client object to send the request to the ownCloud server\r
 -            if (mDownloadClient == null || !mLastAccount.equals(mCurrentDownload.getAccount())) {\r
 -                mLastAccount = mCurrentDownload.getAccount();\r
 -                mStorageManager = new FileDataStorageManager(mLastAccount, getContentResolver());\r
 -                mDownloadClient = OwnCloudClientUtils.createOwnCloudClient(mLastAccount, getApplicationContext());\r
 -            }\r
 -\r
 -            /// perform the download\r
              RemoteOperationResult downloadResult = null;\r
              try {\r
 -                downloadResult = mCurrentDownload.execute(mDownloadClient);\r
 +                /// prepare client object to send the request to the ownCloud server\r
 +                if (mDownloadClient == null || !mLastAccount.equals(mCurrentDownload.getAccount())) {\r
 +                    mLastAccount = mCurrentDownload.getAccount();\r
 +                    mStorageManager = new FileDataStorageManager(mLastAccount, getContentResolver());\r
-                     mDownloadClient = OwnCloudClientUtils.createOwnCloudClient(mLastAccount, this);\r
++                    mDownloadClient = OwnCloudClientUtils.createOwnCloudClient(mLastAccount, getApplicationContext());\r
 +                }\r
 +\r
 +                /// perform the download\r
 +                if (downloadResult == null) {\r
 +                    downloadResult = mCurrentDownload.execute(mDownloadClient);\r
 +                }\r
                  if (downloadResult.isSuccess()) {\r
                      saveDownloadedFile();\r
                  }\r
@@@ -45,7 -45,8 +45,7 @@@ import com.owncloud.android.network.Cer
  public class RemoteOperationResult implements Serializable {
      
      /** Generated - should be refreshed every time the class changes!! */
-     private static final long serialVersionUID = 5336333154035462033L;
+     private static final long serialVersionUID = -7805531062432602444L;
 -
      
      public enum ResultCode { 
          OK,
@@@ -697,210 -498,9 +699,211 @@@ public class AuthenticatorActivity exte
                          | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;\r
              }\r
              view.setInputType(input_type);\r
+             view.setSelection(selectionStart, selectionEnd);\r
          }\r
      }\r
 +    \r
 +    @Override protected void onDestroy() {       \r
 +        // We must stop the service thats it's pooling to oAuth2 server for a token.\r
 +        Intent tokenService = new Intent(this, OAuth2GetTokenService.class);\r
 +        stopService(tokenService);\r
 +        \r
 +        // We stop listening the result of the pooling service.\r
 +        if (tokenReceiver != null) {\r
 +            unregisterReceiver(tokenReceiver);\r
 +            tokenReceiver = null;\r
 +            finish();\r
 +        }\r
 +\r
 +        super.onDestroy();\r
 +    }    \r
 +    \r
 +    // Controlling the oAuth2 checkbox on the activity: hide and show widgets.\r
 +    public void onOff_check_Click(View view) {\r
 +        CheckBox oAuth2Check = (CheckBox)view;      \r
 +        changeViewByOAuth2Check(oAuth2Check.isChecked());\r
 +\r
 +    }\r
 +    \r
 +    public void changeViewByOAuth2Check(Boolean checked) {\r
 +        \r
 +        EditText oAuth2Url = (EditText) findViewById(R.id.oAuth_URL);\r
 +        EditText accountUsername = (EditText) findViewById(R.id.account_username);\r
 +        EditText accountPassword = (EditText) findViewById(R.id.account_password);\r
 +        ImageView viewPassword = (ImageView) findViewById(R.id.viewPassword); \r
 +        ImageView auth2ActionIndicator = (ImageView) findViewById(R.id.auth2_action_indicator); \r
 +        TextView oauth2StatusText = (TextView) findViewById(R.id.oauth2_status_text);         \r
 +\r
 +        if (checked) {\r
 +            oAuth2Url.setVisibility(View.VISIBLE);\r
 +            accountUsername.setVisibility(View.GONE);\r
 +            accountPassword.setVisibility(View.GONE);\r
 +            viewPassword.setVisibility(View.GONE);\r
 +            auth2ActionIndicator.setVisibility(View.INVISIBLE);\r
 +            oauth2StatusText.setVisibility(View.INVISIBLE);\r
 +        } else {\r
 +            oAuth2Url.setVisibility(View.GONE);\r
 +            accountUsername.setVisibility(View.VISIBLE);\r
 +            accountPassword.setVisibility(View.VISIBLE);\r
 +            viewPassword.setVisibility(View.INVISIBLE);\r
 +            auth2ActionIndicator.setVisibility(View.GONE);\r
 +            oauth2StatusText.setVisibility(View.GONE);\r
 +        }     \r
 +\r
 +    }    \r
 +    \r
 +    // Controlling the oAuth2 result of server connection.\r
 +    private void setOAuth2ResultIconAndText(int drawable_id, int text_id) {\r
 +        ImageView iv = (ImageView) findViewById(R.id.auth2_action_indicator);\r
 +        TextView tv = (TextView) findViewById(R.id.oauth2_status_text);\r
 +\r
 +        if (drawable_id == 0 && text_id == 0) {\r
 +            iv.setVisibility(View.INVISIBLE);\r
 +            tv.setVisibility(View.INVISIBLE);\r
 +        } else {\r
 +            iv.setImageResource(drawable_id);\r
 +            tv.setText(text_id);\r
 +            iv.setVisibility(View.VISIBLE);\r
 +            tv.setVisibility(View.VISIBLE);\r
 +        }\r
 +    }     \r
 +    \r
 +    // Results from the first call to oAuth2 server : getting the user_code and verification_url.\r
 +    @Override\r
 +    public void onOAuth2GetCodeResult(ResultOAuthType type, JSONObject responseJson) {\r
 +        if ((type == ResultOAuthType.OK_SSL)||(type == ResultOAuthType.OK_NO_SSL)) {\r
 +            codeResponseJson = responseJson;\r
 +            if (codeResponseJson != null) {\r
 +                getOAuth2AccessTokenFromJsonResponse();\r
 +            }  // else - nothing to do here - wait for callback !!!\r
 +        \r
 +        } else if (type == ResultOAuthType.HOST_NOT_AVAILABLE) {\r
 +            setOAuth2ResultIconAndText(R.drawable.common_error, R.string.oauth_connection_url_unavailable);\r
 +        }\r
 +    }\r
 +\r
 +    // If the results of getting the user_code and verification_url are OK, we get the received data and we start\r
 +    // the polling service to oAuth2 server to get a valid token.\r
 +    private void getOAuth2AccessTokenFromJsonResponse() {\r
 +        String deviceCode = null;\r
 +        String verificationUrl = null;\r
 +        String userCode = null;\r
 +        int expiresIn = -1;\r
 +        int interval = -1;\r
 +\r
 +        Log.d(TAG, "ResponseOAuth2->" + codeResponseJson.toString());\r
 +\r
 +        try {\r
 +            // We get data that we must show to the user or we will use internally.\r
 +            verificationUrl = codeResponseJson.getString(OAuth2GetCodeRunnable.CODE_VERIFICATION_URL);\r
 +            userCode = codeResponseJson.getString(OAuth2GetCodeRunnable.CODE_USER_CODE);\r
 +            expiresIn = codeResponseJson.getInt(OAuth2GetCodeRunnable.CODE_EXPIRES_IN);                \r
 +\r
 +            // And we get data that we must use to get a token.\r
 +            deviceCode = codeResponseJson.getString(OAuth2GetCodeRunnable.CODE_DEVICE_CODE);\r
 +            interval = codeResponseJson.getInt(OAuth2GetCodeRunnable.CODE_INTERVAL);\r
 +\r
 +        } catch (JSONException e) {\r
 +            Log.e(TAG, "Exception accesing data in Json object" + e.toString());\r
 +        }\r
 +\r
 +        // Updating status widget to OK.\r
 +        setOAuth2ResultIconAndText(R.drawable.ic_ok, R.string.auth_connection_established);\r
 +        \r
 +        // Showing the dialog with instructions for the user.\r
 +        showDialog(OAUTH2_LOGIN_PROGRESS);\r
 +\r
 +        // Loggin all the data.\r
 +        Log.d(TAG, "verificationUrl->" + verificationUrl);\r
 +        Log.d(TAG, "userCode->" + userCode);\r
 +        Log.d(TAG, "deviceCode->" + deviceCode);\r
 +        Log.d(TAG, "expiresIn->" + expiresIn);\r
 +        Log.d(TAG, "interval->" + interval);\r
 +\r
 +        // Starting the pooling service.\r
 +        try {\r
 +            Intent tokenService = new Intent(this, OAuth2GetTokenService.class);\r
 +            tokenService.putExtra(OAuth2GetTokenService.TOKEN_URI, OAuth2Context.OAUTH2_G_DEVICE_GETTOKEN_URL);\r
 +            tokenService.putExtra(OAuth2GetTokenService.TOKEN_DEVICE_CODE, deviceCode);\r
 +            tokenService.putExtra(OAuth2GetTokenService.TOKEN_INTERVAL, interval);\r
 +\r
 +            startService(tokenService);\r
 +        }\r
 +        catch (Exception e) {\r
 +            Log.e(TAG, "tokenService creation problem :", e);\r
 +        }\r
 +        \r
 +    }   \r
 +    \r
 +    private void getOAuth2AccessTokenFromCapturedRedirection() {\r
 +        Map<String, String> responseValues = new HashMap<String, String>();\r
 +        //String queryParameters = getIntent().getData().getQuery();\r
 +        String queryParameters = mNewCapturedUriFromOAuth2Redirection.getQuery();\r
 +        mNewCapturedUriFromOAuth2Redirection = null;\r
 +        \r
 +        Log.v(TAG, "Queryparameters (Code) = " + queryParameters);\r
 +\r
 +        String[] pairs = queryParameters.split("&");\r
 +        Log.v(TAG, "Pairs (Code) = " + pairs.toString());\r
 +\r
 +        int i = 0;\r
 +        String key = "";\r
 +        String value = "";\r
 +\r
 +        StringBuilder sb = new StringBuilder();\r
 +\r
 +        while (pairs.length > i) {\r
 +            int j = 0;\r
 +            String[] part = pairs[i].split("=");\r
 +\r
 +            while (part.length > j) {\r
 +                String p = part[j];\r
 +                if (j == 0) {\r
 +                    key = p;\r
 +                    sb.append(key + " = ");\r
 +                } else if (j == 1) {\r
 +                    value = p;\r
 +                    responseValues.put(key, value);\r
 +                    sb.append(value + "\n");\r
 +                }\r
 +\r
 +                Log.v(TAG, "[" + i + "," + j + "] = " + p);\r
 +                j++;\r
 +            }\r
 +            i++;\r
 +        }\r
 +        \r
 +        \r
 +        // Updating status widget to OK.\r
 +        setOAuth2ResultIconAndText(R.drawable.ic_ok, R.string.auth_connection_established);\r
 +        \r
 +        // Showing the dialog with instructions for the user.\r
 +        showDialog(OAUTH2_LOGIN_PROGRESS);\r
 +\r
 +        // \r
 +        RemoteOperation operation = new GetOAuth2AccessToken(responseValues);\r
 +        WebdavClient client = OwnCloudClientUtils.createOwnCloudClient(Uri.parse(OAuth2Context.OAUTH2_F_TOKEN_ENDPOINT_URL), getApplicationContext());\r
 +        operation.execute(client, this, mHandler);\r
 +    }\r
 +\r
 +    \r
 +\r
 +    // We get data from the oAuth2 token service with this broadcast receiver.\r
 +    private class TokenReceiver extends BroadcastReceiver {\r
 +        /**\r
 +         * The token is received.\r
 +         *  @author\r
 +         * {@link BroadcastReceiver} to enable oAuth2 token receiving.\r
 +         */\r
 +        @Override\r
 +        public void onReceive(Context context, Intent intent) {\r
 +            @SuppressWarnings("unchecked")\r
 +            HashMap<String, String> tokenResponse = (HashMap<String, String>)intent.getExtras().get(OAuth2GetTokenService.TOKEN_RECEIVED_DATA);\r
 +            Log.d(TAG, "TokenReceiver->" + tokenResponse.get(OAuth2GetTokenService.TOKEN_ACCESS_TOKEN));\r
 +            dismissDialog(OAUTH2_LOGIN_PROGRESS);\r
 +\r
 +        }\r
 +    }\r
  \r
        @Override\r
        public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {\r
  package com.owncloud.android.ui.fragment;\r
  \r
  import java.io.File;\r
 -import java.util.ArrayList;\r
 -import java.util.List;\r
 -\r
 -import org.apache.commons.httpclient.methods.GetMethod;\r
 -import org.apache.commons.httpclient.methods.PostMethod;\r
 -import org.apache.commons.httpclient.methods.StringRequestEntity;\r
 -import org.apache.commons.httpclient.params.HttpConnectionManagerParams;\r
 -import org.apache.http.HttpStatus;\r
 -import org.apache.http.NameValuePair;\r
 -import org.apache.http.client.utils.URLEncodedUtils;\r
 -import org.apache.http.message.BasicNameValuePair;\r
 -import org.apache.http.protocol.HTTP;\r
 -import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;\r
 -import org.json.JSONObject;\r
 +\r
 +import org.apache.commons.httpclient.Credentials;\r
- import org.apache.commons.httpclient.UsernamePasswordCredentials;\r
  \r
  import android.accounts.Account;\r
  import android.accounts.AccountManager;\r
@@@ -64,8 -76,7 +63,7 @@@ import com.owncloud.android.files.servi
  import com.owncloud.android.files.services.FileUploader;\r
  import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;\r
  import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;\r
 -import com.owncloud.android.network.OwnCloudClientUtils;\r
 +import com.owncloud.android.network.BearerCredentials;\r
- import com.owncloud.android.network.OwnCloudClientUtils;\r
  import com.owncloud.android.operations.OnRemoteOperationListener;\r
  import com.owncloud.android.operations.RemoteOperation;\r
  import com.owncloud.android.operations.RemoteOperationResult;\r
@@@ -409,8 -423,9 +407,7 @@@ public class FileDetailFragment extend
                  mLastRemoteOperation = new RemoveFileOperation( mFile, \r
                                                                  true, \r
                                                                  mStorageManager);\r
 -                WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());\r
 -                mLastRemoteOperation.execute(wc, this, mHandler);\r
 -                \r
 +                mLastRemoteOperation.execute(mAccount, getSherlockActivity(), this, mHandler, getSherlockActivity());\r
-                 \r
                  boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;\r
                  getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);\r
              }\r
                  if (mFile.getRemotePath().equals(uploadRemotePath) ||\r
                      renamedInUpload) {\r
                      if (uploadWasFine) {\r
-                         mFile = mStorageManager.getFileByPath(mFile.getRemotePath());\r
 -                        mFile = mStorageManager.getFileByPath(uploadRemotePath);\r
++                       mFile = mStorageManager.getFileByPath(uploadRemotePath);\r
                      }\r
                      if (renamedInUpload) {\r
                          String newName = (new File(uploadRemotePath)).getName();\r