Skip to content

Commit

Permalink
Merge pull request #1 from suitetecsa/use-rx-java
Browse files Browse the repository at this point in the history
Use rx java
  • Loading branch information
Alessandro authored Apr 5, 2024
2 parents f2a8513 + 8b17067 commit 36a5cdc
Show file tree
Hide file tree
Showing 21 changed files with 714 additions and 135 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/app-build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: App / Build

on:
push:
branches:
- master
pull_request:

permissions:
contents: read

jobs:
build:
name: Build
runs-on: ubuntu-22.04
steps:
- name: Checkout repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Setup Java
uses: actions/setup-java@0ab4596768b603586c0de567f2430c30f5b0d2b0 # v3.13.0
with:
distribution: temurin
java-version: 17
- name: Setup Gradle
uses: gradle/gradle-build-action@ef76a971e2fa3f867b617efd72f2fbd72cf6f8bc # v2.8.0
- name: Assemble debug APKs
run: ./gradlew assembleDebug
- name: Create publish bundle
run: mkdir -p build/gh-app-publish/; find app/build/ -iname "*.apk" -exec mv "{}" build/gh-app-publish/ \;
- name: Upload artifacts
uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3
with:
name: build-artifacts
retention-days: 14
if-no-files-found: error
path: build/gh-app-publish/
38 changes: 38 additions & 0 deletions .github/workflows/app-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: App / Lint

on:
push:
branches:
- master
- release-*
pull_request:

permissions:
contents: read
security-events: write

jobs:
lint:
name: Lint
runs-on: ubuntu-22.04
continue-on-error: true
strategy:
matrix:
task: [ lint ]
steps:
- name: Checkout repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Setup Java
uses: actions/setup-java@387ac29b308b003ca37ba93a6cab5eb57c8f5f93 # v4.0.0
with:
distribution: temurin
java-version: 17
- name: Setup Gradle
uses: gradle/gradle-build-action@87a9a15658c426a54dd469d4fc7dc1a73ca9d4a6 # v2.10.0
- name: Run ${{ matrix.task }} task
run: ./gradlew ${{ matrix.task }}
- name: Upload SARIF files
uses: github/codeql-action/upload-sarif@c0d1daa7f7e14667747d73a7dbbe8c074bc8bfe2 # v2.22.9
if: ${{ always() }}
with:
sarif_file: .
28 changes: 28 additions & 0 deletions .github/workflows/app-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: APP / Test

on:
push:
branches:
- master
pull_request:

jobs:
test:
name: Test
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
java: [ 17 ]
steps:
- name: Checkout repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Setup Java
uses: actions/setup-java@0ab4596768b603586c0de567f2430c30f5b0d2b0 # v3.13.0
with:
distribution: temurin
java-version: ${{ matrix.java }}
- name: Setup Gradle
uses: gradle/gradle-build-action@87a9a15658c426a54dd469d4fc7dc1a73ca9d4a6 # v2.10.0
- name: Run test task
run: ./gradlew test
22 changes: 22 additions & 0 deletions .github/workflows/gradlew-validate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Gradle / Validate wrapper

on:
push:
branches:
- master
pull_request:
paths:
- '**/gradlе-wrapper.jar'

permissions:
contents: read

jobs:
validate:
name: Validate
runs-on: ubuntu-22.04
steps:
- name: Checkout repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@56b90f209b02bf6d1deae490e9ef18b21a389cd4 # v1.1.0
15 changes: 11 additions & 4 deletions apklis-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ android {
targetCompatibility = JavaVersion.VERSION_17
}

lint {
lintConfig = file("$rootDir/android-lint.xml")
abortOnError = false
sarifReport = true
}

buildTypes {
release {
isMinifyEnabled = false
Expand All @@ -38,19 +44,20 @@ dependencies {


implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("com.google.android.material:material:1.9.0")
implementation("com.google.android.material:material:1.11.0")
implementation("androidx.appcompat:appcompat:1.6.1")

implementation("io.reactivex.rxjava2:rxjava:2.2.19")
implementation("io.reactivex.rxjava2:rxkotlin:2.4.0")
implementation("io.reactivex.rxjava2:rxandroid:2.1.1")
implementation("com.squareup.retrofit2:adapter-rxjava2:2.8.1")

implementation("com.squareup.okhttp3:okhttp:4.5.0")
implementation("com.squareup.retrofit2:retrofit:2.8.1")
implementation("com.squareup.retrofit2:converter-gson:2.8.1")
implementation("com.squareup.okhttp3:okhttp:4.12.0")
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")

testImplementation("junit:junit:4.13.2")
testImplementation("com.squareup.okhttp3:mockwebserver:4.9.1")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")

Expand Down
22 changes: 13 additions & 9 deletions apklis-api/src/main/java/com/arr/apklislib/payments/ApklisPay.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

public class ApklisPay {
private static final String TAG = "ApklisPay";
private static final String APKLIS_URL = "content://cu.uci.android.apklis.payment.provider/app/";
private static final String APK_PAID = "paid";
private static final String USER = "user_name";

private String APKLIS_URL = "content://cu.uci.android.apklis.payment.provider/app/";
private String APK_PAID = "paid";
private String USER = "user_name";

private Context mContext;
private String packageName;
private final Context mContext;
private final String packageName;

private boolean isPaid = false;
private String usuario = null;
Expand All @@ -34,8 +35,10 @@ private void checkPayment() {
Cursor cursor = content.query(provider, null, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
isPaid = cursor.getInt(cursor.getColumnIndex(APK_PAID)) > 0;
usuario = cursor.getString(cursor.getColumnIndex(USER));
int paidColumnIndex = cursor.getColumnIndex(APK_PAID);
int userColumnIndex = cursor.getColumnIndex(USER);
isPaid = cursor.getInt(paidColumnIndex) > 0;
usuario = cursor.getString(userColumnIndex);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
content.close();
Expand All @@ -46,7 +49,8 @@ private void checkPayment() {
}
}
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "checkPayment: Fail", e);
;
} finally {
if (content != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
content.release();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.arr.apklislib.update;

import com.arr.apklislib.update.model.PackageResponse;

import io.reactivex.Single;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.Query;

public interface APKLisService {
@Headers({"User-Agent: ApplifyCU/1.0.0", "Content-Type: application/json"})
@GET("/application/")
public Single<PackageResponse> getPackageResponse(@Query("package_name") String packageName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.arr.apklislib.update;

public class ApklisException extends Exception {
public ApklisException(String message) {
super(message);
}
}
132 changes: 65 additions & 67 deletions apklis-api/src/main/java/com/arr/apklislib/update/ApklisUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,92 @@

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import androidx.annotation.NonNull;

import com.arr.apklislib.update.callback.UpdateCallback;
import com.arr.apklislib.update.model.ApiResponse;
import com.arr.apklislib.update.model.LastRelease;
import com.arr.apklislib.update.model.UpdateInfo;
import com.google.gson.Gson;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.util.List;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApklisUpdate {
private static final String TAG = "ApklisUpdate";
private final APKLisService service;

private static final String API_URL = "https://api.apklis.cu/v1/application/?package_name=";
private Context context;
private OkHttpClient httpClient;
private Gson gson;
private Handler handler;

public ApklisUpdate(Context context) {
this.context = context;
httpClient = new OkHttpClient();
gson = new Gson();
handler = new Handler(Looper.getMainLooper());
ApklisUpdate(APKLisService service) {
this.service = service;
}

public void checkLastUpdate(String package_name, final UpdateCallback callback) {
Request request = new Request.Builder().url(API_URL + package_name).build();
httpClient
.newCall(request)
.enqueue(
new Callback() {
@Override
public void onResponse(Call call, Response response)
throws IOException {
if (response.isSuccessful()) {
String result = response.body().string();
Log.e("Result: ", "" + result.toString());

//
ApiResponse api = gson.fromJson(result, ApiResponse.class);
List<UpdateInfo> info = api.appUpdateInfo();

if (info != null && !info.isEmpty()) {
UpdateInfo updateInfo = info.get(0);
LastRelease release = updateInfo.lastRelease();

// compribar las version code
int serverVersionCode = release.versionCode();
int appVersionCode = getCurrentVersionCode();
if (serverVersionCode > appVersionCode) {
handler.post(() -> callback.onLastUpdate(release));
}
}

} else {
handler.post(
() ->
callback.onError(
new Exception(
"Error occurred during network request")));
}
}
service.getPackageResponse(package_name)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(response -> {
if (response.getCount() == 0) {
callback.onError(new ApklisException("No existen apps publicadas con este nombre de paquete en apklis"));
} else {
callback.onLastUpdate(response.getResults().get(0).getLastRelease());
}
}, throwable -> {
callback.onError((Exception) throwable);
}).dispose();
}

@Override
public void onFailure(Call call, IOException e) {
callback.onError(e);
}
});
public void hasAppUpdate(@NonNull Context context, UpdateCallback callback) {
service.getPackageResponse(context.getPackageName())
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(response -> {
if (response.getCount() == 0) {
callback.onError(new ApklisException("No existen apps publicadas con este nombre de paquete en apklis"));
} else {
LastRelease lastRelease = response.getResults().get(0).getLastRelease();
if (getCurrentVersionCode(context) < lastRelease.getVersionCode()) {
callback.onLastUpdate(lastRelease);
}
}
}, throwable -> {
callback.onError((Exception) throwable);
}).dispose();
}

private int getCurrentVersionCode() {
private int getCurrentVersionCode(@NonNull Context context) {
// Obtener el versionCode actual de la aplicación
try {
return context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0)
.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "getCurrentVersionCode: " + e.getMessage(), e);
;
}
return 0;
}

public static class Builder {
private APKLisService service;

public Builder setService(APKLisService service) {
this.service = service;
return this;
}

public ApklisUpdate build() {
if (service == null) {
String API_URL = "https://api.apklis.cu/v2";
service = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
.create(APKLisService.class);
}
return new ApklisUpdate(service);
}
}
}
Loading

0 comments on commit 36a5cdc

Please sign in to comment.