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