Skip to content

Commit f776632

Browse files
committed
New activity verify that the installer has a valid token
1 parent 57439b6 commit f776632

File tree

9 files changed

+201
-1
lines changed

9 files changed

+201
-1
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@
178178
android:noHistory="true"
179179
/>
180180

181+
<!-- New Activity for validate token -->
182+
<activity
183+
android:name=".activities.SplashBatchActivity"
184+
android:configChanges="keyboardHidden|orientation"
185+
android:noHistory="true"
186+
/>
187+
181188
<activity
182189
android:name="com.prey.activities.CheckPasswordActivity"
183190
android:launchMode="singleInstance"

app/src/main/java/com/prey/PreyBatch.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,12 @@ public boolean isAskForNameBatch() {
5757
return Boolean.parseBoolean(properties.getProperty("ask-for-name-batch"));
5858
}
5959

60+
/**
61+
* Method get token
62+
* @return
63+
*/
64+
public String getToken() {
65+
return properties.getProperty("token");
66+
}
67+
6068
}

app/src/main/java/com/prey/activities/LoginActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private void showLogin() {
118118

119119
private void showLoginBatch() {
120120
Intent intent = null;
121-
intent = new Intent(LoginActivity.this, WelcomeBatchActivity.class);
121+
intent = new Intent(LoginActivity.this, SplashBatchActivity.class);
122122
startActivity(intent);
123123
finish();
124124
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*******************************************************************************
2+
* Created by Orlando Aliaga
3+
* Copyright 2022 Prey Inc. All rights reserved.
4+
* License: GPLv3
5+
* Full license at "/LICENSE"
6+
******************************************************************************/
7+
package com.prey.activities;
8+
9+
import android.app.ProgressDialog;
10+
import android.content.Context;
11+
import android.content.Intent;
12+
import android.content.pm.ActivityInfo;
13+
import android.os.AsyncTask;
14+
import android.os.Bundle;
15+
import android.view.Window;
16+
import android.widget.TextView;
17+
18+
import androidx.fragment.app.FragmentActivity;
19+
20+
import com.prey.PreyBatch;
21+
import com.prey.PreyLogger;
22+
import com.prey.R;
23+
import com.prey.net.PreyWebServices;
24+
25+
/****
26+
* This activity verify that the installer has a valid token
27+
*/
28+
public class SplashBatchActivity extends FragmentActivity {
29+
30+
private String error = null;
31+
private TextView textSplash = null;
32+
33+
@Override
34+
public void onResume() {
35+
PreyLogger.d("onResume of SplashBatchActivity");
36+
super.onResume();
37+
new SplashBatchActivity.TokenBatchTask().execute();
38+
}
39+
40+
@Override
41+
public void onPause() {
42+
PreyLogger.d("onPause of SplashBatchActivity");
43+
super.onPause();
44+
}
45+
46+
@Override
47+
protected void onCreate(Bundle savedInstanceState) {
48+
PreyLogger.d("onCreate of SplashBatchActivity");
49+
super.onCreate(savedInstanceState);
50+
requestWindowFeature(Window.FEATURE_NO_TITLE);
51+
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
52+
setContentView(R.layout.splash_batch);
53+
textSplash = (TextView) findViewById(R.id.text_splash);
54+
}
55+
56+
/****
57+
* This asyncTask verify that the installer has a valid token
58+
*/
59+
private class TokenBatchTask extends AsyncTask<String, Void, Void> {
60+
ProgressDialog progressDialog = null;
61+
62+
@Override
63+
protected void onPreExecute() {
64+
try {
65+
textSplash.setText("");
66+
progressDialog = new ProgressDialog(SplashBatchActivity.this);
67+
progressDialog.setMessage(SplashBatchActivity.this.getText(R.string.loading).toString());
68+
progressDialog.setIndeterminate(true);
69+
progressDialog.setCancelable(false);
70+
progressDialog.show();
71+
} catch (Exception e) {
72+
PreyLogger.e("Error:" + e.getMessage(), e);
73+
}
74+
}
75+
76+
@Override
77+
protected Void doInBackground(String... data) {
78+
Context ctx = getApplicationContext();
79+
try {
80+
error = null;
81+
String token = PreyBatch.getInstance(ctx).getToken();
82+
if (token == null || "".equals(token)) {
83+
error = ctx.getString(R.string.error_token);
84+
} else {
85+
boolean validToken = PreyWebServices.getInstance().validToken(ctx, PreyBatch.getInstance(ctx).getToken());
86+
if (!validToken) {
87+
error = ctx.getString(R.string.error_token);
88+
}
89+
}
90+
} catch (Exception e) {
91+
error = e.getMessage();
92+
}
93+
return null;
94+
}
95+
96+
@Override
97+
protected void onPostExecute(Void unused) {
98+
if (progressDialog != null)
99+
progressDialog.dismiss();
100+
if (error == null) {
101+
Intent intentPermission = new Intent(SplashBatchActivity.this, WelcomeBatchActivity.class);
102+
startActivity(intentPermission);
103+
finish();
104+
} else {
105+
textSplash.setText(error);
106+
}
107+
}
108+
109+
}
110+
111+
}

app/src/main/java/com/prey/net/PreyRestHttpClient.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,18 @@ public PreyHttpResponse sendHelp(Context ctx, String uri, Map<String, String> pa
195195
String contentType = CONTENT_TYPE_URL_ENCODED;
196196
return UtilConnection.connection(PreyConfig.getPreyConfig(ctx), uri, params, requestMethod, contentType, authorization, status, entityFiles, correlationId);
197197
}
198+
199+
/**
200+
* Method to valid token
201+
*
202+
* @param ctx
203+
* @param uri
204+
* @param json
205+
* @return result
206+
* @throws Exception
207+
*/
208+
public PreyHttpResponse validToken(Context ctx, String uri, JSONObject json) throws Exception {
209+
HttpURLConnection connection = UtilConnection.connectionJson(PreyConfig.getPreyConfig(ctx), uri, UtilConnection.REQUEST_METHOD_POST, json);
210+
return new PreyHttpResponse(connection);
211+
}
198212
}

app/src/main/java/com/prey/net/PreyWebServices.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,4 +1210,27 @@ public PreyHttpResponse sendHelp(Context ctx, String subject, String message) th
12101210
return response;
12111211
}
12121212

1213+
/**
1214+
* Method to valid token
1215+
*
1216+
* @param ctx
1217+
* @param token
1218+
* @return result
1219+
* @throws Exception
1220+
*/
1221+
public boolean validToken(Context ctx, String token) throws Exception {
1222+
JSONObject json = new JSONObject();
1223+
json.put("token", token);
1224+
json.put("action", "deploy");
1225+
String uri = PreyConfig.getPreyConfig(ctx).getPreyUrl().concat("token/v2/check");
1226+
PreyHttpResponse response = PreyRestHttpClient.getInstance(ctx).validToken(ctx, uri, json);
1227+
int statusCode = -1;
1228+
try {
1229+
statusCode = response.getStatusCode();
1230+
} catch (Exception e) {
1231+
PreyLogger.e("Error validateToken:" + e.getMessage(), e);
1232+
}
1233+
return statusCode == 200;
1234+
}
1235+
12131236
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:orientation="vertical"
8+
android:background="#1e2938"
9+
>
10+
<ImageView
11+
android:id="@+id/imageView"
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:src="@drawable/prey_logo_b_mono2"
15+
app:layout_constraintBottom_toBottomOf="parent"
16+
app:layout_constraintEnd_toEndOf="parent"
17+
app:layout_constraintHorizontal_bias="0.496"
18+
app:layout_constraintStart_toStartOf="parent"
19+
app:layout_constraintTop_toTopOf="parent"
20+
app:layout_constraintVertical_bias="0.179" />
21+
<TextView
22+
android:id="@+id/text_splash"
23+
android:layout_width="330dp"
24+
android:layout_height="300dp"
25+
android:fontFamily="Avenir Next LT Pro"
26+
android:gravity="center"
27+
android:text=""
28+
android:textColor="#ffffff"
29+
android:textSize="18sp"
30+
android:textStyle="bold"
31+
app:layout_constraintBottom_toBottomOf="parent"
32+
app:layout_constraintEnd_toEndOf="parent"
33+
app:layout_constraintStart_toStartOf="parent"
34+
app:layout_constraintTop_toBottomOf="@+id/imageView" />
35+
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/values-es/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,5 +369,6 @@
369369
<string name="help_error_message">El mensaje debe tener mín. 10 caracteres.</string>
370370
<string name="help_error_attachment">El archivo seleccionado excede el tamaño máximo.</string>
371371
<string name="help_no_file_chosen">No se eligió archivo</string>
372+
<string name="error_token">La identificación de cuenta ha expirado o ha sido revocado, por favor contacta a tu administrador.</string>
372373

373374
</resources>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,5 +375,6 @@
375375
<string name="help_error_message">Message must have 10 characters minimum.</string>
376376
<string name="help_error_attachment">File size exceeds maximum limit.</string>
377377
<string name="help_no_file_chosen">No file chosen</string>
378+
<string name="error_token">The account identification has expired or has been revoked, please contact your administrator.</string>
378379

379380
</resources>

0 commit comments

Comments
 (0)