Skip to content

Commit 7e183b1

Browse files
authored
Merge pull request #89 from cientopolis/development
Development
2 parents c315b0c + ed17c29 commit 7e183b1

38 files changed

Lines changed: 892 additions & 211 deletions

SamplersConfig.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88
"title" : "Samplers Hello World App",
99
"welcomeMessage" : "Welcome to your first Samplers App!",
1010
"networkConfiguration" : {
11-
"url" : "http://192.168.1.10/samplers/upload.php",
12-
"paramName" : "sample"
11+
"url" : "http://192.168.0.10/samplers/upload.php",
12+
"paramName" : "sample",
13+
"paramNameUserId" : "user_id",
14+
"paramNameAuthenticationType" : "authentication_type"
15+
1316
},
17+
"authenticationEnabled" : true,
18+
"authenticationOptional" : true,
1419
"googleMaps_API_KEY" : "AIzaSyBSdAX453WDiPJmPotF5akUQNQw_W_MzJ4",
1520
"mainHelpFileName" : "mainhelp.html"
1621
} ,

sampleApplication/src/main/java/org/cientopolis/sampleapplication/MyMainSamplersActivity.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package org.cientopolis.sampleapplication;
22

3-
import android.os.Bundle;
43

4+
import android.os.Bundle;
5+
import org.cientopolis.samplers.authentication.AuthenticationManager;
56
import org.cientopolis.samplers.framework.information.InformationStep;
67
import org.cientopolis.samplers.framework.insertDate.InsertDateStep;
78
import org.cientopolis.samplers.framework.insertText.InsertTextStep;
@@ -33,9 +34,13 @@ protected void onCreate(Bundle savedInstanceState) {
3334

3435
// Set the network configuration
3536
NetworkConfiguration.setURL("http://192.168.0.10/samplers/upload.php");
36-
NetworkConfiguration.setPARAM_NAME("sample");
37+
NetworkConfiguration.setPARAM_NAME_SAMPLE("sample");
38+
NetworkConfiguration.setPARAM_NAME_USER_ID("user_id");
39+
NetworkConfiguration.setPARAM_NAME_AUTHENTICATION_TYPE("authentication_type");
3740

38-
lb_main_welcome_message.setText("Bienvenido a la Aplicacion de Prueba!");
41+
// Set the authentication configuration
42+
AuthenticationManager.setAuthenticationEnabled(true);
43+
AuthenticationManager.setAuthenticationOptional(true);
3944
}
4045

4146

@@ -45,7 +50,7 @@ protected Workflow getWorkflow() {
4550
Step step;
4651

4752
// InformationStep
48-
workflow.addStep(new InformationStep(1, "Informacion de prueba para ver que se muestra bien",55));
53+
workflow.addStep(new InformationStep(1, "Informacion de prueba para ver que se muestra bien",3));
4954

5055
// Insert Time
5156
workflow.addStep(new InsertTimeStep(101, "Seleccione la Hora de la muestra",102));
@@ -63,7 +68,7 @@ protected Workflow getWorkflow() {
6368
workflow.addStep(new SelectOneStep(2, optionsToSelect2, "¿Quiere sacar una foto?"));
6469

6570
// PhotoStep
66-
PhotoStep photoStep = new PhotoStep(3, "Saque una foto de su gato","",4);
71+
PhotoStep photoStep = new PhotoStep(3, "Saque una foto de su gato",null);
6772
// set help resource
6873
photoStep.setHelpResourseId(R.raw.photohelp);
6974
workflow.addStep(photoStep);

samplers.gradle

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ task samplers_config {
2727
println json.application.mainHelpFileName
2828
println json.application.networkConfiguration.url
2929
println json.application.networkConfiguration.paramName
30+
println json.application.networkConfiguration.paramNameUserId
31+
println json.application.networkConfiguration.paramNameAuthenticationType
3032

3133
// WORKFLOW
3234
println json.workflow.actionLabel
@@ -55,10 +57,10 @@ task samplers_config {
5557
if (!main_activity_file.exists()) {
5658

5759

58-
def class_generator = new org.cientopolis.samplers.SamplersClassGenerator(package_name, manifest_path, strings_path, raw_path);
60+
def class_generator = new org.cientopolis.samplers.SamplersClassGenerator(package_name, manifest_path, strings_path, raw_path)
5961
// Set Main Help
6062
if (json.application.mainHelpFileName != null) {
61-
class_generator.setMainHelpFileName(json.application.mainHelpFileName);
63+
class_generator.setMainHelpFileName(json.application.mainHelpFileName)
6264
}
6365

6466
// Set Google Maps API Key
@@ -69,7 +71,7 @@ task samplers_config {
6971
println "No google maps API KEY found"
7072

7173
// Workflow
72-
def stepClassGenerator;
74+
def stepClassGenerator
7375
json.workflow.steps.eachWithIndex { step, i ->
7476
switch (step.type) {
7577
case "Information":
@@ -95,8 +97,7 @@ task samplers_config {
9597
break
9698

9799
case "Photo":
98-
// TODO add functionality to overlay image
99-
stepClassGenerator = new org.cientopolis.samplers.PhotoStepClassGenerator(step.id, step.nextStepId, step.text, "")
100+
stepClassGenerator = new org.cientopolis.samplers.PhotoStepClassGenerator(step.id, step.nextStepId, step.text)
100101
break
101102

102103
case "Location":
@@ -145,8 +146,18 @@ task samplers_config {
145146
def welcomeMessage = json.application.welcomeMessage
146147
def net_config_url = json.application.networkConfiguration.url
147148
def net_config_paramName = json.application.networkConfiguration.paramName
149+
def net_config_paramNameUserId = json.application.networkConfiguration.paramNameUserId
150+
def net_config_paramNameAuthenticationType = json.application.networkConfiguration.paramNameAuthenticationType
148151

149-
class_generator.generateMainActivity(app_path, activity_name, welcomeMessage, net_config_url, net_config_paramName)
152+
def net_config = new org.cientopolis.samplers.NetworkConfiguration(net_config_url,net_config_paramName,net_config_paramNameUserId,net_config_paramNameAuthenticationType)
153+
154+
if (json.application.authenticationEnabled != null)
155+
class_generator.setAuthenticationEnabled(json.application.authenticationEnabled)
156+
157+
if (json.application.authenticationOptional != null)
158+
class_generator.setAuthenticationOptional(json.application.authenticationOptional)
159+
160+
class_generator.generateMainActivity(app_path, activity_name, welcomeMessage, net_config)
150161
}
151162
else {
152163
println "MyMainSamplersActivity.java already exists."

samplersFramework/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ dependencies {
3939

4040
// Libraries dependencies
4141
// Location
42-
compile ('com.google.android.gms:play-services-location:10.2.1')
42+
compile ('com.google.android.gms:play-services-location:12.0.1')
4343
// Maps
44-
compile ('com.google.android.gms:play-services-maps:10.2.1')
44+
compile ('com.google.android.gms:play-services-maps:12.0.1')
45+
// Authentication with Google
46+
compile ('com.google.android.gms:play-services-auth:12.0.1')
4547

4648
// Added manually as libraries (.jar in libs folder)
4749
//compile 'com.google.code.gson:gson:2.8.0'

samplersFramework/src/main/AndroidManifest.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
android:name=".ui.SamplersMainActivity"
2020
android:label="@string/app_name"
2121
android:theme="@style/SamplersFrameworkAppTheme">
22-
<intent-filter>
22+
<!-- <intent-filter>
2323
<action android:name="android.intent.action.MAIN" />
2424
2525
<category android:name="android.intent.category.LAUNCHER" />
26-
</intent-filter>
26+
</intent-filter> -->
2727
</activity>
2828
<activity
2929
android:name=".ui.TakeSampleActivity"
@@ -37,6 +37,10 @@
3737
android:name=".ui.HelpActivity"
3838
android:label="@string/title_activity_help"
3939
android:theme="@style/SamplersFrameworkAppTheme" />
40+
<activity
41+
android:name=".authentication.LoginActivity"
42+
android:label="@string/title_activity_login"
43+
android:theme="@style/SamplersFrameworkAppTheme" />
4044

4145
<service
4246
android:name=".framework.soundRecord.service.RecordingService"
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package org.cientopolis.samplers.authentication;
2+
3+
4+
import android.content.Context;
5+
import android.content.SharedPreferences;
6+
import com.google.gson.Gson;
7+
import org.cientopolis.samplers.bus.BusProvider;
8+
import org.cientopolis.samplers.bus.LoginEvent;
9+
10+
/**
11+
* Created by Xavier on 04/04/2018.
12+
*/
13+
14+
public class AuthenticationManager {
15+
16+
private static final String PREFERENCES_NAME = "org.cientopolis.samplers.PREFERENCES_NAME";
17+
private static final String PREFERENCES_KEY_USER = "org.cientopolis.samplers.PREFERENCES_KEY_USER";
18+
private static final String PREFERENCES_KEY_USER_CLASS = "org.cientopolis.samplers.PREFERENCES_KEY_USER_CLASS";
19+
20+
private static boolean authenticationEnabled = false;
21+
private static boolean authenticationOptional = true;
22+
private static Class loginFragmentClass = StandardLoginFragment.class;
23+
24+
public static <T extends LoginFragment> Class<T> getLoginFragmentClass() {
25+
return loginFragmentClass;
26+
}
27+
28+
public static <T extends LoginFragment> void setLoginFragmentClass(Class<T> type) {
29+
AuthenticationManager.loginFragmentClass = type;
30+
}
31+
32+
public static User getUser(Context context) {
33+
34+
return retrieveUser(context);
35+
}
36+
37+
public static void login(User user, Context context) {
38+
39+
saveUser(user, context);
40+
41+
BusProvider.getInstance().post(new LoginEvent(user));
42+
}
43+
44+
public static void logout(Context context) {
45+
46+
removeUser(context);
47+
48+
BusProvider.getInstance().post(new LoginEvent(null));
49+
}
50+
51+
private static void saveUser(User user, Context context) {
52+
53+
SharedPreferences sharedPref = context.getSharedPreferences (PREFERENCES_NAME,Context.MODE_PRIVATE);
54+
SharedPreferences.Editor editor = sharedPref.edit();
55+
56+
Gson gson = new Gson();
57+
String jsonUser = gson.toJson(user);
58+
59+
editor.putString(PREFERENCES_KEY_USER,jsonUser);
60+
editor.putString(PREFERENCES_KEY_USER_CLASS,user.getClass().getCanonicalName());
61+
editor.commit();
62+
63+
}
64+
65+
private static void removeUser(Context context) {
66+
SharedPreferences sharedPref = context.getSharedPreferences (PREFERENCES_NAME,Context.MODE_PRIVATE);
67+
SharedPreferences.Editor editor = sharedPref.edit();
68+
69+
editor.remove(PREFERENCES_KEY_USER);
70+
editor.remove(PREFERENCES_KEY_USER_CLASS);
71+
editor.commit();
72+
}
73+
74+
private static User retrieveUser(Context context) {
75+
SharedPreferences sharedPref = context.getSharedPreferences (PREFERENCES_NAME,Context.MODE_PRIVATE);
76+
77+
Gson gson = new Gson();
78+
String jsonUser = sharedPref.getString(PREFERENCES_KEY_USER, "");
79+
String className = sharedPref.getString(PREFERENCES_KEY_USER_CLASS, "");
80+
User obj = null;
81+
82+
if (!jsonUser.equals("")) {
83+
84+
try {
85+
obj = (User) gson.fromJson(jsonUser, Class.forName(className));
86+
} catch (ClassNotFoundException e) {
87+
e.printStackTrace();
88+
throw new RuntimeException("");
89+
}
90+
}
91+
92+
return obj;
93+
}
94+
95+
96+
public static boolean isAuthenticationEnabled() {
97+
return authenticationEnabled;
98+
}
99+
100+
public static void setAuthenticationEnabled(boolean authenticationEnabled) {
101+
AuthenticationManager.authenticationEnabled = authenticationEnabled;
102+
}
103+
104+
public static boolean isAuthenticationOptional() {
105+
return authenticationOptional;
106+
}
107+
108+
public static void setAuthenticationOptional(boolean authenticationOptional) {
109+
AuthenticationManager.authenticationOptional = authenticationOptional;
110+
}
111+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.cientopolis.samplers.authentication;
2+
3+
/**
4+
* Created by Xavier on 04/04/2018.
5+
*/
6+
7+
public class GoogleUser implements User {
8+
9+
public static final String AUTHENTICATION_TYPE = "google";
10+
11+
private String userName;
12+
private String userId;
13+
14+
public GoogleUser(String userName, String userId) {
15+
this.userName = userName;
16+
this.userId = userId;
17+
}
18+
19+
@Override
20+
public String getAuthenticationType() {
21+
return AUTHENTICATION_TYPE;
22+
}
23+
24+
@Override
25+
public String getUserName() {
26+
return userName;
27+
}
28+
29+
@Override
30+
public String getUserId() {
31+
return userId;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return "GoogleUser{" +
37+
"userName='" + userName + '\'' +
38+
'}';
39+
}
40+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.cientopolis.samplers.authentication;
2+
3+
import android.app.Activity;
4+
import android.os.Bundle;
5+
import android.support.annotation.Nullable;
6+
import org.cientopolis.samplers.R;
7+
8+
public class LoginActivity extends Activity implements LoginFragmentInteractionListener {
9+
10+
@Override
11+
protected void onCreate(Bundle savedInstanceState) {
12+
super.onCreate(savedInstanceState);
13+
setContentView(R.layout.activity_samples_list);
14+
15+
if (savedInstanceState == null) {
16+
try {
17+
LoginFragment loginFragment = AuthenticationManager.getLoginFragmentClass().newInstance();
18+
19+
getFragmentManager().beginTransaction()
20+
.add(R.id.activity_samples_list, loginFragment).commit();
21+
22+
} catch (Exception e) {
23+
e.printStackTrace();
24+
throw new RuntimeException("Error while instantiating LoginFragment");
25+
}
26+
}
27+
28+
}
29+
30+
31+
@Override
32+
public void onLogin(@Nullable User user) {
33+
this.finish();
34+
}
35+
36+
}
37+
38+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.cientopolis.samplers.authentication;
2+
3+
import android.app.Activity;
4+
import android.app.Fragment;
5+
import android.content.Context;
6+
7+
8+
public abstract class LoginFragment extends Fragment {
9+
10+
protected LoginFragmentInteractionListener mListener;
11+
12+
13+
private void myOnAttach(Context context) {
14+
/*
15+
* ATTENTION:
16+
* this method is called twice on API 23-25 because both onAttach(Activity) and onAttach(Context) are executed
17+
* Don't put creation code here (e.g. new SomeClass())
18+
*/
19+
20+
if (context instanceof LoginFragmentInteractionListener) {
21+
mListener = (LoginFragmentInteractionListener) context;
22+
} else {
23+
throw new RuntimeException(context.toString()
24+
+ " must implement LoginFragmentInteractionListener");
25+
}
26+
}
27+
28+
@SuppressWarnings("deprecation") // This method is needed when running on API Levels < 23
29+
@Override
30+
public void onAttach(Activity activity) {
31+
super.onAttach(activity);
32+
myOnAttach(activity);
33+
}
34+
35+
@Override
36+
public void onAttach(Context context) {
37+
super.onAttach(context);
38+
myOnAttach(context);
39+
}
40+
41+
42+
43+
44+
}

0 commit comments

Comments
 (0)