dd194cf77d33ce31e3c4e2f31539701501cae299
[pub/Android/ownCloud.git] / oc_framework-test-project / src / com / owncloud / android / oc_framework_test_project / TestActivity.java
1 package com.owncloud.android.oc_framework_test_project;
2
3 import java.io.IOException;
4
5 import com.owncloud.android.oc_framework.accounts.AccountUtils.AccountNotFoundException;
6 import com.owncloud.android.oc_framework.network.webdav.OwnCloudClientFactory;
7 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
8 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
9 import com.owncloud.android.oc_framework.operations.remote.CreateRemoteFolderOperation;
10 import com.owncloud.android.oc_framework.operations.remote.RenameRemoteFileOperation;
11
12 import android.os.AsyncTask;
13 import android.os.Bundle;
14 import android.accounts.Account;
15 import android.accounts.AccountManager;
16 import android.accounts.AuthenticatorException;
17 import android.accounts.OperationCanceledException;
18 import android.app.Activity;
19 import android.content.Context;
20 import android.util.Log;
21 import android.view.Menu;
22
23 /**
24 * Activity to test OC framework
25 * @author masensio
26 *
27 */
28 public class TestActivity extends Activity {
29
30 private static final String TAG = "TestActivity";
31
32 // This account must exists on the simulator / device
33 private static final String mAccountHost = "beta.owncloud.com";
34 private static final String mAccountUser = "testandroid";
35 private static final String mAccountName = mAccountUser + "@"+ mAccountHost;
36 private static final String mAccountPass = "testandroid";
37 private static final String mAccountType = "owncloud";
38
39 private Account mAccount = null;
40 private WebdavClient mClient;
41
42 @Override
43 protected void onCreate(Bundle savedInstanceState) {
44 super.onCreate(savedInstanceState);
45 setContentView(R.layout.activity_test);
46
47 AccountManager am = AccountManager.get(this);
48
49 Account[] ocAccounts = am.getAccountsByType(mAccountType);
50 for (Account ac : ocAccounts) {
51 if (ac.name.equals(mAccountName)) {
52 mAccount = ac;
53 break;
54 }
55 }
56
57 // Get the WebDavClient
58 AuthTask task = new AuthTask();
59 task.execute(this.getApplicationContext());
60
61 }
62
63 @Override
64 public boolean onCreateOptionsMenu(Menu menu) {
65 // Inflate the menu; this adds items to the action bar if it is present.
66 getMenuInflater().inflate(R.menu.test, menu);
67 return true;
68 }
69
70 /**
71 * Access to the library method to Create a Folder
72 * @param remotePath
73 * @param createFullPath
74 *
75 * @return
76 */
77 public RemoteOperationResult createFolder(String remotePath, boolean createFullPath) {
78
79 CreateRemoteFolderOperation createOperation = new CreateRemoteFolderOperation(remotePath, createFullPath);
80 RemoteOperationResult result = createOperation.execute(mClient);
81
82 return result;
83 }
84
85 /**
86 * Access to the library method to Rename a File or Folder
87 * @param oldName Old name of the file.
88 * @param oldRemotePath Old remote path of the file. For folders it starts and ends by "/"
89 * @param newName New name to set as the name of file.
90 * @param newRemotePath New remote path to move the file, for folders it starts and ends by "/"
91 *
92 * @return
93 */
94
95 public RemoteOperationResult renameFile(String oldName, String oldRemotePath, String newName, String newRemotePath) {
96
97 RenameRemoteFileOperation renameOperation = new RenameRemoteFileOperation(oldName, oldRemotePath, newName, newRemotePath);
98 RemoteOperationResult result = renameOperation.execute(mClient);
99
100 return result;
101 }
102
103 private class AuthTask extends AsyncTask<Context, Void, WebdavClient> {
104
105 @Override
106 protected WebdavClient doInBackground(Context... params) {
107 WebdavClient client = null;
108 try {
109 client = OwnCloudClientFactory.createOwnCloudClient(mAccount, (Context) params[0] );
110 } catch (OperationCanceledException e) {
111 Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
112 e.printStackTrace();
113 } catch (AuthenticatorException e) {
114 Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
115 e.printStackTrace();
116 } catch (AccountNotFoundException e) {
117 Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
118 e.printStackTrace();
119 } catch (IOException e) {
120 Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
121 e.printStackTrace();
122 } catch (IllegalStateException e) {
123 Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
124 e.printStackTrace();
125 }
126 return client;
127 }
128
129 @Override
130 protected void onPostExecute(WebdavClient result) {
131 // TODO Auto-generated method stub
132 super.onPostExecute(result);
133 mClient = result;
134 }
135
136 }
137 }