Browse Source

locations add/get

tags/0.0.5
Robin Thoni 9 years ago
parent
commit
1206242a32

+ 4
- 2
app/src/main/AndroidManifest.xml View File

15
         android:supportsRtl="true"
15
         android:supportsRtl="true"
16
         android:theme="@style/AppTheme">
16
         android:theme="@style/AppTheme">
17
         <activity
17
         <activity
18
-            android:name=".MainActivity"
18
+            android:name=".ui.MainActivity"
19
             android:label="@string/app_name"
19
             android:label="@string/app_name"
20
             android:theme="@style/AppTheme.NoActionBar">
20
             android:theme="@style/AppTheme.NoActionBar">
21
             <intent-filter>
21
             <intent-filter>
28
         <service
28
         <service
29
             android:name=".business.CamotionAccountBusiness"
29
             android:name=".business.CamotionAccountBusiness"
30
             android:exported="true"
30
             android:exported="true"
31
+            android:label="@string/app_name"
31
             android:process=":auth">
32
             android:process=":auth">
32
             <intent-filter>
33
             <intent-filter>
33
                 <action android:name="android.accounts.AccountAuthenticator"/>
34
                 <action android:name="android.accounts.AccountAuthenticator"/>
38
                 android:resource="@xml/authenticator"/>
39
                 android:resource="@xml/authenticator"/>
39
         </service>
40
         </service>
40
 
41
 
41
-        <activity android:name=".AddAccountActivity">
42
+        <activity android:name=".ui.AddAccountActivity"
43
+            android:label="@string/add_location_title">
42
         </activity>
44
         </activity>
43
 
45
 
44
     </application>
46
     </application>

+ 0
- 13
app/src/main/java/com/rthoni/camotion/AddAccountActivity.java View File

1
-package com.rthoni.camotion;
2
-
3
-import android.support.v7.app.AppCompatActivity;
4
-import android.os.Bundle;
5
-
6
-public class AddAccountActivity extends AppCompatActivity {
7
-
8
-    @Override
9
-    protected void onCreate(Bundle savedInstanceState) {
10
-        super.onCreate(savedInstanceState);
11
-        setContentView(R.layout.activity_add_account);
12
-    }
13
-}

+ 1
- 1
app/src/main/java/com/rthoni/camotion/business/CamotionAccountBusiness.java View File

12
 import android.os.IBinder;
12
 import android.os.IBinder;
13
 import android.util.Log;
13
 import android.util.Log;
14
 
14
 
15
-import com.rthoni.camotion.AddAccountActivity;
15
+import com.rthoni.camotion.ui.AddAccountActivity;
16
 
16
 
17
 /**
17
 /**
18
  *
18
  *

+ 31
- 2
app/src/main/java/com/rthoni/camotion/business/CamotionBusiness.java View File

1
 package com.rthoni.camotion.business;
1
 package com.rthoni.camotion.business;
2
 
2
 
3
+import android.accounts.Account;
4
+import android.accounts.AccountManager;
5
+import android.content.Context;
6
+
3
 import com.luticate.utils.dbo.LuDataAccessConfigDbo;
7
 import com.luticate.utils.dbo.LuDataAccessConfigDbo;
8
+import com.rthoni.camotion.R;
9
+import com.rthoni.camotion.dbo.LocationDbo;
10
+
11
+import java.util.List;
12
+import java.util.Vector;
4
 
13
 
5
 /**
14
 /**
15
+ *
6
  * Created by robin on 11/28/15.
16
  * Created by robin on 11/28/15.
7
  */
17
  */
8
 public class CamotionBusiness {
18
 public class CamotionBusiness {
9
 
19
 
10
-    public static LuDataAccessConfigDbo getConfig()
20
+    public static LuDataAccessConfigDbo getConfig(LocationDbo location)
11
     {
21
     {
12
         LuDataAccessConfigDbo config = new LuDataAccessConfigDbo();
22
         LuDataAccessConfigDbo config = new LuDataAccessConfigDbo();
13
-        config.setBaseUrl("http://camotion.gigi.rthoni.com");
23
+        config.setBaseUrl(location.getBaseUrl());
14
         return config;
24
         return config;
15
     }
25
     }
26
+
27
+    public static List<LocationDbo> getLocations(Context context)
28
+    {
29
+        List<LocationDbo> locations = new Vector<>();
30
+        AccountManager accountManager = AccountManager.get(context);
31
+        for (Account account : accountManager.getAccountsByType(context.getString(R.string.account_type))) {
32
+            LocationDbo location = new LocationDbo();
33
+            String[] split = account.name.split("@");
34
+
35
+            location.setPassword(accountManager.getPassword(account));
36
+            location.setHttps(accountManager.getUserData(account, "Https").equals("true"));
37
+            location.setDomain(accountManager.getUserData(account, "Domain"));
38
+            location.setUsername(split[0]);
39
+            location.setName(split[1]);
40
+
41
+            locations.add(location);
42
+        }
43
+        return locations;
44
+    }
16
 }
45
 }

+ 48
- 1
app/src/main/java/com/rthoni/camotion/dbo/LocationDbo.java View File

14
 
14
 
15
     private String _name;
15
     private String _name;
16
 
16
 
17
-    private String _baseUrl;
17
+    private String _domain;
18
 
18
 
19
     private String _username;
19
     private String _username;
20
 
20
 
21
     private String _password;
21
     private String _password;
22
 
22
 
23
+    private boolean _https;
24
+
25
+    public String getName() {
26
+        return _name;
27
+    }
28
+
29
+    public void setName(String name) {
30
+        _name = name;
31
+    }
32
+
33
+    public String getDomain() {
34
+        return _domain;
35
+    }
36
+
37
+    public void setDomain(String domain) {
38
+        _domain = domain;
39
+    }
40
+
41
+    public String getUsername() {
42
+        return _username;
43
+    }
44
+
45
+    public void setUsername(String username) {
46
+        _username = username;
47
+    }
48
+
49
+    public String getPassword() {
50
+        return _password;
51
+    }
52
+
53
+    public void setPassword(String password) {
54
+        _password = password;
55
+    }
56
+
57
+    public boolean isHttps() {
58
+        return _https;
59
+    }
60
+
61
+    public void setHttps(boolean https) {
62
+        _https = https;
63
+    }
64
+
65
+    public String getBaseUrl()
66
+    {
67
+        return (_https ? "https" : "http") + "://" + _domain;
68
+    }
69
+
23
     @Override
70
     @Override
24
     public void fromJson(JSONObject json) throws JSONException {
71
     public void fromJson(JSONObject json) throws JSONException {
25
 
72
 

+ 140
- 0
app/src/main/java/com/rthoni/camotion/ui/AddAccountActivity.java View File

1
+package com.rthoni.camotion.ui;
2
+
3
+import android.accounts.Account;
4
+import android.accounts.AccountAuthenticatorResponse;
5
+import android.accounts.AccountManager;
6
+import android.app.Activity;
7
+import android.support.v7.app.AppCompatActivity;
8
+import android.os.Bundle;
9
+import android.view.View;
10
+import android.widget.CheckBox;
11
+import android.widget.EditText;
12
+import android.widget.Toast;
13
+
14
+import com.luticate.auth.dbo.LuLoginDbo;
15
+import com.luticate.utils.business.LuPromise;
16
+import com.rthoni.camotion.R;
17
+import com.rthoni.camotion.dbo.LocationDbo;
18
+import com.rthoni.camotion.ui.dialogs.LoginDialog;
19
+
20
+public class AddAccountActivity extends AppCompatActivity {
21
+
22
+    private EditText _nameInput;
23
+    private EditText _usernameInput;
24
+    private EditText _passwordInput;
25
+    private EditText _domainInput;
26
+    private CheckBox _checkBoxHttps;
27
+
28
+    private AccountAuthenticatorResponse _response;
29
+
30
+    @Override
31
+    protected void onCreate(Bundle savedInstanceState) {
32
+        super.onCreate(savedInstanceState);
33
+        setContentView(R.layout.activity_add_account);
34
+        _nameInput = (EditText) findViewById(R.id.name_input);
35
+        _usernameInput = (EditText) findViewById(R.id.username_input);
36
+        _passwordInput = (EditText) findViewById(R.id.password_input);
37
+        _domainInput = (EditText) findViewById(R.id.domain_input);
38
+        _checkBoxHttps = (CheckBox) findViewById(R.id.checkBoxHttps);
39
+
40
+        findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
41
+            @Override
42
+            public void onClick(View v) {
43
+                check();
44
+            }
45
+        });
46
+        findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
47
+            @Override
48
+            public void onClick(View v) {
49
+                setResult(Activity.RESULT_CANCELED);
50
+                finish();
51
+            }
52
+        });
53
+
54
+        Bundle extras = getIntent().getExtras();
55
+
56
+        if (extras != null) {
57
+            _response = extras.getParcelable(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
58
+        }
59
+    }
60
+
61
+    private LocationDbo validate()
62
+    {
63
+        LocationDbo location = new LocationDbo();
64
+        location.setName(_nameInput.getText().toString());
65
+        location.setUsername(_usernameInput.getText().toString());
66
+        location.setPassword(_passwordInput.getText().toString());
67
+        location.setDomain(_domainInput.getText().toString());
68
+        location.setHttps(_checkBoxHttps.isChecked());
69
+
70
+        _nameInput.setError(null);
71
+        _usernameInput.setError(null);
72
+        _passwordInput.setError(null);
73
+        _domainInput.setError(null);
74
+
75
+        if (location.getName().isEmpty()) {
76
+            _nameInput.setError(getResources().getString(R.string.error_name));
77
+            _nameInput.requestFocus();
78
+            return null;
79
+        }
80
+
81
+        if (location.getUsername().isEmpty()) {
82
+            _usernameInput.setError(getResources().getString(R.string.error_username));
83
+            _usernameInput.requestFocus();
84
+            return null;
85
+        }
86
+
87
+        if (location.getPassword().isEmpty()) {
88
+            _passwordInput.setError(getResources().getString(R.string.error_password));
89
+            _passwordInput.requestFocus();
90
+            return null;
91
+        }
92
+
93
+        if (location.getDomain().isEmpty()) {
94
+            _domainInput.setError(getResources().getString(R.string.error_domain));
95
+            _domainInput.requestFocus();
96
+            return null;
97
+        }
98
+
99
+        return location;
100
+    }
101
+
102
+    private void addAccount(LocationDbo location)
103
+    {
104
+        String username = location.getUsername() + "@" + location.getName();
105
+
106
+        AccountManager accountManager = AccountManager.get(this);
107
+        Account newUserAccount = new Account(username, getResources().getString(R.string.account_type));
108
+        boolean accountCreated = accountManager.addAccountExplicitly(newUserAccount, location.getPassword(), null);
109
+
110
+        if (accountCreated && _response != null) {
111
+            accountManager.setUserData(newUserAccount, "Https", location.isHttps() ? "true" : "false");
112
+            accountManager.setUserData(newUserAccount, "Domain", location.getDomain());
113
+            Bundle result = new Bundle();
114
+            result.putString(AccountManager.KEY_ACCOUNT_NAME, username);
115
+            result.putString(AccountManager.KEY_ACCOUNT_TYPE, getString(R.string.account_type));
116
+            _response.onResult(result);
117
+            finish();
118
+        }
119
+    }
120
+
121
+    private void check()
122
+    {
123
+        final LocationDbo location = validate();
124
+        if (location == null) {
125
+            return;
126
+        }
127
+        LoginDialog dlg = new LoginDialog(this);
128
+        dlg.login(location).then(new LuPromise.LuConsumer<LuLoginDbo>() {
129
+            @Override
130
+            public void execute(LuLoginDbo data) {
131
+                addAccount(location);
132
+            }
133
+        }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
134
+            @Override
135
+            public void execute(LuPromise.LuPromiseError error) {
136
+                Toast.makeText(AddAccountActivity.this, error.toString(), Toast.LENGTH_LONG).show();
137
+            }
138
+        });
139
+    }
140
+}

app/src/main/java/com/rthoni/camotion/MainActivity.java → app/src/main/java/com/rthoni/camotion/ui/MainActivity.java View File

1
-package com.rthoni.camotion;
1
+package com.rthoni.camotion.ui;
2
 
2
 
3
 import android.os.Bundle;
3
 import android.os.Bundle;
4
 import android.support.design.widget.FloatingActionButton;
4
 import android.support.design.widget.FloatingActionButton;
16
 import com.luticate.auth.business.LuticateUsersBusiness;
16
 import com.luticate.auth.business.LuticateUsersBusiness;
17
 import com.luticate.auth.dbo.LuFullLoginDbo;
17
 import com.luticate.auth.dbo.LuFullLoginDbo;
18
 import com.luticate.utils.business.LuPromise;
18
 import com.luticate.utils.business.LuPromise;
19
+import com.rthoni.camotion.R;
19
 import com.rthoni.camotion.business.CamotionBusiness;
20
 import com.rthoni.camotion.business.CamotionBusiness;
21
+import com.rthoni.camotion.dbo.LocationDbo;
22
+
23
+import java.util.List;
20
 
24
 
21
 public class MainActivity extends AppCompatActivity
25
 public class MainActivity extends AppCompatActivity
22
         implements NavigationView.OnNavigationItemSelectedListener {
26
         implements NavigationView.OnNavigationItemSelectedListener {
32
         fab.setOnClickListener(new View.OnClickListener() {
36
         fab.setOnClickListener(new View.OnClickListener() {
33
             @Override
37
             @Override
34
             public void onClick(View view) {
38
             public void onClick(View view) {
35
-                LuticateUsersBusiness.loginFull(CamotionBusiness.getConfig(), "user", "test42")
39
+                /*LuticateUsersBusiness.loginFull(CamotionBusiness.getConfig(), "user", "test42")
36
                         .then(new LuPromise.LuConsumer<LuFullLoginDbo>() {
40
                         .then(new LuPromise.LuConsumer<LuFullLoginDbo>() {
37
                             @Override
41
                             @Override
38
                             public void execute(LuFullLoginDbo data) {
42
                             public void execute(LuFullLoginDbo data) {
43
                             public void execute(LuPromise.LuPromiseError error) {
47
                             public void execute(LuPromise.LuPromiseError error) {
44
                                 Log.e("Login", error.toString());
48
                                 Log.e("Login", error.toString());
45
                             }
49
                             }
46
-                        });
50
+                        });*/
51
+                //List<LocationDbo> locations = CamotionBusiness.getLocations(MainActivity.this);
47
             }
52
             }
48
         });
53
         });
49
 
54
 
50
         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
55
         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
51
-        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
52
-                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
56
+        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
57
+                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
53
         drawer.setDrawerListener(toggle);
58
         drawer.setDrawerListener(toggle);
54
         toggle.syncState();
59
         toggle.syncState();
55
 
60
 

+ 48
- 0
app/src/main/java/com/rthoni/camotion/ui/dialogs/LoginDialog.java View File

1
+package com.rthoni.camotion.ui.dialogs;
2
+
3
+import android.app.ProgressDialog;
4
+import android.content.Context;
5
+
6
+import com.luticate.auth.business.LuticateUsersBusiness;
7
+import com.luticate.auth.dbo.LuLoginDbo;
8
+import com.luticate.utils.business.LuPromise;
9
+import com.luticate.utils.dataaccess.LuDataAccess;
10
+import com.luticate.utils.dbo.LuDataAccessConfigDbo;
11
+import com.rthoni.camotion.R;
12
+import com.rthoni.camotion.dbo.LocationDbo;
13
+
14
+/**
15
+ *
16
+ * Created by robin on 11/28/15.
17
+ */
18
+public class LoginDialog extends ProgressDialog {
19
+    public LoginDialog(Context context) {
20
+        super(context);
21
+        setTitle(R.string.loading);
22
+        setIndeterminate(true);
23
+        setMessage(context.getString(R.string.login_progress));
24
+    }
25
+
26
+    public LuPromise<LuLoginDbo> login(LocationDbo location)
27
+    {
28
+        final LuPromise<LuLoginDbo> promise = new LuPromise<>();
29
+        show();
30
+        LuDataAccessConfigDbo config = new LuDataAccessConfigDbo();
31
+        config.setBaseUrl(location.getBaseUrl());
32
+        LuticateUsersBusiness.login(config, location.getUsername(), location.getPassword())
33
+                .then(new LuPromise.LuConsumer<LuLoginDbo>() {
34
+                    @Override
35
+                    public void execute(LuLoginDbo data) {
36
+                        hide();
37
+                        promise.resolve(data);
38
+                    }
39
+                }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
40
+                    @Override
41
+                    public void execute(LuPromise.LuPromiseError data) {
42
+                        hide();
43
+                        promise.reject(data);
44
+                    }
45
+                });
46
+        return promise;
47
+    }
48
+}

+ 16
- 28
app/src/main/res/layout/activity_add_account.xml View File

3
                 android:layout_width="match_parent"
3
                 android:layout_width="match_parent"
4
                 android:layout_height="match_parent"
4
                 android:layout_height="match_parent"
5
                 android:orientation="vertical"
5
                 android:orientation="vertical"
6
-                tools:context="com.rob57530.camotion.ui.AddLocationActivity">
6
+                tools:context=".ui.AddAccountActivity">
7
 
7
 
8
 
8
 
9
     <ScrollView
9
     <ScrollView
33
                 android:layout_width="match_parent"
33
                 android:layout_width="match_parent"
34
                 android:layout_height="wrap_content"
34
                 android:layout_height="wrap_content"
35
                 android:hint="@string/name"
35
                 android:hint="@string/name"
36
+                android:nextFocusDown="@+id/username_input"
37
+                android:singleLine="true"
36
                 android:id="@+id/name_input"
38
                 android:id="@+id/name_input"
37
                 />
39
                 />
38
 
40
 
40
                 android:layout_width="match_parent"
42
                 android:layout_width="match_parent"
41
                 android:layout_height="wrap_content"
43
                 android:layout_height="wrap_content"
42
                 android:hint="@string/username"
44
                 android:hint="@string/username"
43
-                android:id="@+id/username_input"
44
-                />
45
+                android:singleLine="true"
46
+                android:id="@+id/username_input"/>
45
 
47
 
46
             <EditText
48
             <EditText
47
                 android:layout_width="match_parent"
49
                 android:layout_width="match_parent"
48
                 android:layout_height="wrap_content"
50
                 android:layout_height="wrap_content"
51
+                android:singleLine="true"
49
                 android:id="@+id/password_input"
52
                 android:id="@+id/password_input"
50
                 android:inputType="textPassword"
53
                 android:inputType="textPassword"
51
                 android:hint="@string/password"
54
                 android:hint="@string/password"
55
             <EditText
58
             <EditText
56
                 android:layout_width="match_parent"
59
                 android:layout_width="match_parent"
57
                 android:layout_height="wrap_content"
60
                 android:layout_height="wrap_content"
58
-                android:id="@+id/api_entrypoint_input"
59
-                android:hint="@string/api_entrypoint"
60
-                android:layout_gravity="center_horizontal"
61
-                />
61
+                android:hint="@string/domain"
62
+                android:singleLine="true"
63
+                android:id="@+id/domain_input"
64
+                android:inputType="textUri"/>
62
 
65
 
63
-            <RelativeLayout
64
-                android:layout_width="match_parent"
65
-                android:layout_height="match_parent">
66
-
67
-                <Button
68
-                    android:layout_width="wrap_content"
69
-                    android:layout_height="wrap_content"
70
-                    android:text="@string/check_account"
71
-                    android:id="@+id/test"
72
-                    android:layout_gravity="center_horizontal"
73
-                    android:layout_alignParentTop="true"
74
-                    android:layout_centerHorizontal="true"/>
75
-
76
-                <ImageView
77
-                    android:layout_width="wrap_content"
78
-                    android:layout_height="wrap_content"
79
-                    android:id="@+id/imageView"
80
-                    android:layout_alignParentTop="true"
81
-                    android:layout_toRightOf="@+id/test"
82
-                    android:layout_toEndOf="@+id/test"/>
83
-            </RelativeLayout>
66
+            <CheckBox
67
+                android:layout_width="wrap_content"
68
+                android:layout_height="wrap_content"
69
+                android:text="HTTPS"
70
+                android:checked="true"
71
+                android:id="@+id/checkBoxHttps"/>
84
 
72
 
85
         </LinearLayout>
73
         </LinearLayout>
86
     </ScrollView>
74
     </ScrollView>

+ 1
- 1
app/src/main/res/layout/app_bar_main.xml View File

5
     android:layout_width="match_parent"
5
     android:layout_width="match_parent"
6
     android:layout_height="match_parent"
6
     android:layout_height="match_parent"
7
     android:fitsSystemWindows="true"
7
     android:fitsSystemWindows="true"
8
-    tools:context="com.rthoni.camotion.MainActivity">
8
+    tools:context=".ui.MainActivity">
9
 
9
 
10
     <android.support.design.widget.AppBarLayout
10
     <android.support.design.widget.AppBarLayout
11
         android:layout_width="match_parent"
11
         android:layout_width="match_parent"

+ 1
- 1
app/src/main/res/layout/content_main.xml View File

9
     android:paddingRight="@dimen/activity_horizontal_margin"
9
     android:paddingRight="@dimen/activity_horizontal_margin"
10
     android:paddingTop="@dimen/activity_vertical_margin"
10
     android:paddingTop="@dimen/activity_vertical_margin"
11
     app:layout_behavior="@string/appbar_scrolling_view_behavior"
11
     app:layout_behavior="@string/appbar_scrolling_view_behavior"
12
-    tools:context="com.rthoni.camotion.MainActivity"
12
+    tools:context=".ui.MainActivity"
13
     tools:showIn="@layout/app_bar_main">
13
     tools:showIn="@layout/app_bar_main">
14
 
14
 
15
     <TextView
15
     <TextView

+ 12
- 1
app/src/main/res/values/strings.xml View File

1
 <resources>
1
 <resources>
2
     <string name="app_name">Camotion</string>
2
     <string name="app_name">Camotion</string>
3
 
3
 
4
+    <string name="add_location_title">Add Camotion Account</string>
5
+
4
     <string name="navigation_drawer_open">Open navigation drawer</string>
6
     <string name="navigation_drawer_open">Open navigation drawer</string>
5
     <string name="navigation_drawer_close">Close navigation drawer</string>
7
     <string name="navigation_drawer_close">Close navigation drawer</string>
6
 
8
 
11
     <string name="name">Location name</string>
13
     <string name="name">Location name</string>
12
     <string name="username">Username</string>
14
     <string name="username">Username</string>
13
     <string name="password">Password</string>
15
     <string name="password">Password</string>
16
+    <string name="domain">Domain</string>
14
     <string name="check_account">Check account</string>
17
     <string name="check_account">Check account</string>
15
-    <string name="api_entrypoint">API entry point</string>
18
+
19
+    <string name="error_name">Location name is required</string>
20
+    <string name="error_username">Username is required</string>
21
+    <string name="error_password">Password is required</string>
22
+    <string name="error_domain">Domain is required</string>
23
+    <string name="account_type">com.rthoni.luticate.account</string>
24
+
25
+    <string name="loading">Loading…</string>
26
+    <string name="login_progress">Logging in…</string>
16
 </resources>
27
 </resources>

+ 9
- 0
luticateauth/src/main/java/com/luticate/auth/dbo/LuVoidPromise.java View File

1
+package com.luticate.auth.dbo;
2
+
3
+import com.luticate.utils.business.LuPromise;
4
+
5
+/**
6
+ * Created by robin on 11/28/15.
7
+ */
8
+public class LuVoidPromise extends LuPromise<Void> {
9
+}

Loading…
Cancel
Save