7df78bd4c8edd322e6f4887360cca60a13110259
[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.Bundle;
13 import android.accounts.Account;
14 import android.accounts.AccountManager;
15 import android.accounts.AuthenticatorException;
16 import android.accounts.OperationCanceledException;
17 import android.app.Activity;
18 import android.util.Log;
19 import android.view.Menu;
20
21 /**
22 * Activity to test OC framework
23 * @author masensio
24 *
25 */
26 public class TestActivity extends Activity {
27
28 private static final String TAG = "TestActivity";
29
30 // This account must exists on the simulator / device
31 private static final String mAccountHost = "beta.owncloud.com";
32 private static final String mAccountUser = "testandroid";
33 private static final String mAccountName = mAccountUser + "@"+ mAccountHost;
34 private static final String mAccountPass = "testandroid";
35 private static final String mAccountType = "owncloud";
36
37 private Account mAccount = null;
38 private WebdavClient mClient;
39
40 @Override
41 protected void onCreate(Bundle savedInstanceState) {
42 super.onCreate(savedInstanceState);
43 setContentView(R.layout.activity_test);
44
45 AccountManager am = AccountManager.get(this);
46
47 Account[] ocAccounts = am.getAccountsByType(mAccountType);
48 for (Account ac : ocAccounts) {
49 if (ac.name.equals(mAccountName)) {
50 mAccount = ac;
51 break;
52 }
53 }
54
55 // if (mAccount == null) {
56 // mAccount = new Account(accountName, mAccountType);
57 // am.addAccountExplicitly(mAccount, mAccountPass, null);
58 // am.setUserData(mAccount, "oc_version", "5.0.14");
59 // am.setUserData(mAccount, "oc_base_url", "http://beta.owncloud.com/owncloud");
60 // } else {
61 Log.d(TAG, "oc_version --->"+ am.getUserData(mAccount, "oc_version") );
62 Log.d(TAG, "oc_base_url --->"+ am.getUserData(mAccount, "oc_base_url") );
63 // }
64
65
66 try {
67 mClient = OwnCloudClientFactory.createOwnCloudClient(mAccount, this.getApplicationContext());
68 } catch (OperationCanceledException e) {
69 Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
70 e.printStackTrace();
71 } catch (AuthenticatorException e) {
72 Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
73 e.printStackTrace();
74 } catch (AccountNotFoundException e) {
75 Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
76 e.printStackTrace();
77 } catch (IOException e) {
78 Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
79 e.printStackTrace();
80 }
81 }
82
83 @Override
84 public boolean onCreateOptionsMenu(Menu menu) {
85 // Inflate the menu; this adds items to the action bar if it is present.
86 getMenuInflater().inflate(R.menu.test, menu);
87 return true;
88 }
89
90 /**
91 * Access to the library method to Create a Folder
92 * @param folderName
93 * @param remotePath
94 * @param createFullPath
95 *
96 * @return
97 */
98 public RemoteOperationResult createFolder(String folderName, String remotePath, boolean createFullPath) {
99
100 CreateRemoteFolderOperation createOperation = new CreateRemoteFolderOperation(folderName, remotePath, createFullPath);
101 RemoteOperationResult result = createOperation.execute(mClient);
102
103 return result;
104 }
105
106 /**
107 * Access to the library method to Rename a File or Folder
108 * @param oldName Old name of the file.
109 * @param oldRemotePath Old remote path of the file. For folders it starts and ends by "/"
110 * @param newName New name to set as the name of file.
111 * @param newRemotePath New remote path to move the file, for folders it starts and ends by "/"
112 *
113 * @return
114 */
115
116 public RemoteOperationResult renameFile(String oldName, String oldRemotePath, String newName, String newRemotePath) {
117
118 RenameRemoteFileOperation renameOperation = new RenameRemoteFileOperation(oldName, oldRemotePath, newName, newRemotePath);
119 RemoteOperationResult result = renameOperation.execute(mClient);
120
121 return result;
122 }
123 }