1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.ui
.activity
;
21 import android
.accounts
.AccountAuthenticatorActivity
;
22 import android
.app
.Activity
;
23 import android
.os
.Bundle
;
24 import android
.view
.View
;
25 import android
.view
.View
.OnClickListener
;
27 public class PreferencesNewSession
extends AccountAuthenticatorActivity
28 implements OnClickListener
{
30 public void onCreate(Bundle savedInstanceState
) {
31 super.onCreate(savedInstanceState
);
32 // setContentView(R.layout.add_new_session);
34 * EditText et;// = (EditText)
35 * findViewById(R.id.newSession_sessionName);
37 * et = (EditText) findViewById(R.id.newSession_URL); if
38 * (getIntent().hasExtra("sessionURL")) { try { URI uri = new
39 * URI(getIntent().getStringExtra("sessionURL")); String url =
40 * uri.getHost(); if (uri.getPort() != -1) { url += ":" +
41 * String.valueOf(uri.getPort()); } if (uri.getPath() != null) { url +=
42 * uri.getPath(); } else { url += "/"; } et.setText(url); et =
43 * (EditText) findViewById(R.id.newSession_username); if
44 * (uri.getAuthority() != null) { if (uri.getUserInfo().indexOf(':') !=
45 * -1) { et.setText(uri.getUserInfo().substring(0,
46 * uri.getUserInfo().indexOf(':'))); et = (EditText)
47 * findViewById(R.id.newSession_password);
48 * et.setText(uri.getUserInfo().substring
49 * (uri.getUserInfo().indexOf(':')+1)); } else {
50 * et.setText(uri.getUserInfo()); } }
52 * } catch (URISyntaxException e) { Log.e(TAG, "Incorrect URI syntax " +
53 * e.getLocalizedMessage()); } }
55 * mReturnData = new Intent(); setResult(Activity.RESULT_OK,
56 * mReturnData); ((Button)
57 * findViewById(R.id.button1)).setOnClickListener(this); ((Button)
58 * findViewById(R.id.button2)).setOnClickListener(this);
63 protected void onResume() {
67 public void onClick(View v
) {
69 * switch (v.getId()) { case R.id.button1: Intent intent = new Intent();
70 * if (getIntent().hasExtra("sessionId")) { intent.putExtra("sessionId",
71 * getIntent().getIntExtra("sessionId", -1)); } //String sessionName =
73 * findViewById(R.id.newSession_sessionName)).getText().toString(); //
74 * if (sessionName.trim().equals("") || !isNameValid(sessionName)) { //
75 * Toast.makeText(this, R.string.new_session_session_name_error,
76 * Toast.LENGTH_LONG).show(); // break; // } URI uri = prepareURI(); if
77 * (uri != null) { //intent.putExtra("sessionName", sessionName);
78 * intent.putExtra("sessionURL", uri.toString());
79 * setResult(Activity.RESULT_OK, intent); AccountManager accMgr =
80 * AccountManager.get(this); Account a = new Account("OwnCloud",
81 * AccountAuthenticatorService.ACCOUNT_TYPE);
82 * accMgr.addAccountExplicitly(a, "asd", null); finish(); } break; case
83 * R.id.button2: setResult(Activity.RESULT_CANCELED); finish(); break; }
88 * private URI prepareURI() { URI uri = null; String url = ""; try { String
89 * username = ((EditText)
90 * findViewById(R.id.newSession_username)).getText().toString().trim();
91 * String password = ((EditText)
92 * findViewById(R.id.newSession_password)).getText().toString().trim();
93 * String hostname = ((EditText)
94 * findViewById(R.id.newSession_URL)).getText().toString().trim(); String
95 * scheme; if (hostname.matches("[A-Za-z]://")) { scheme =
96 * hostname.substring(0, hostname.indexOf("://")+3); hostname =
97 * hostname.substring(hostname.indexOf("://")+3); } else { scheme =
98 * "http://"; } if (!username.equals("")) { if (!password.equals("")) {
99 * username += ":" + password + "@"; } else { username += "@"; } } url =
100 * scheme + username + hostname; Log.i(TAG, url); uri = new URI(url); }
101 * catch (URISyntaxException e) { Log.e(TAG, "Incorrect URI syntax " +
102 * e.getLocalizedMessage()); Toast.makeText(this,
103 * R.string.new_session_uri_error, Toast.LENGTH_LONG).show(); } return uri;
106 * private boolean isNameValid(String string) { return
107 * string.matches("[A-Za-z0-9 _-]*"); }
111 public void onBackPressed() {
112 setResult(Activity
.RESULT_CANCELED
);
113 super.onBackPressed();