Skip to content

Commit

Permalink
fix: android stats method
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Feb 13, 2022
1 parent 4e14de0 commit bf2fb46
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.SecureRandom;
Expand Down Expand Up @@ -190,13 +190,14 @@ public ArrayList<String> list() {
return res;
}

public Boolean delete(String version) throws IOException {
public Boolean delete(String version, String versionName) throws IOException {
File destHot = new File(this.context.getFilesDir() + "/" + basePathHot + "/" + version);
if (destHot.exists()) {
deleteDirectory(destHot);
return true;
}
Log.i(TAG, "Directory not removed: " + destHot.getPath());
this.sendStats("delete", versionName);
return false;
}

Expand Down Expand Up @@ -254,36 +255,51 @@ public void reset() {

public void sendStats(String action, String version) {
if (statsUrl == "") { return; }

String statsUrl = this.statsUrl;
Context context = this.context;
URL url;
JSONObject json = new JSONObject();
String jsonString;
try {
url = new URL(statsUrl);
String android_id = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
json.put("platform", "android");
json.put("action", action);
json.put("device_id", android_id);
json.put("version_name", version);
json.put("version_build", pInfo.versionName);
json.put("app_id", pInfo.packageName);
jsonString = json.toString();
} catch (Exception ex) {
Log.e(TAG, "Error get stats", ex);
return;
}
new Thread(new Runnable(){
@Override
public void run() {
HttpURLConnection con = null;
try {
URL url = new URL(statsUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Content-Length", Integer.toString(jsonString.getBytes().length));
con.setDoOutput(true);
JSONObject json = new JSONObject();
String android_id = Secure.getString(context.getContentResolver(),
Secure.ANDROID_ID);
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
json.put("platform", "android");
json.put("action", action);
json.put("device_id", android_id);
json.put("version_name", version);
json.put("version_build", pInfo.versionName);
json.put("app_id", "");
con.setConnectTimeout(500);
try(OutputStream os = con.getOutputStream()) {
byte[] input = json.toString().getBytes("utf-8");
os.write(input, 0, input.length);
DataOutputStream wr = new DataOutputStream (con.getOutputStream());
wr.writeBytes(jsonString);
wr.close();
int responseCode = con.getResponseCode();
if (responseCode != 200) {
Log.e(TAG, "stats responseCode: " + responseCode);
} else {
Log.i(TAG, "Stats send for \"" + action + "\", version " + version + " in " + statsUrl);
}
} catch (Exception ex) {
Log.e(TAG, "Error post stats");
Log.e(TAG, "Error post stats", ex);
} finally {
if (con != null) {
con.disconnect();
}
}
}
}).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void load() {
this.editor = prefs.edit();
implementation = new CapacitorUpdater(this.getContext());
String statsUrl = getConfig().getString("statsUrl");
implementation.statsUrl = statsUrl == null ? statsUrl : "https://capgo.app/api/stats";
implementation.statsUrl = statsUrl != null ? statsUrl : "https://capgo.app/api/stats";
this.autoUpdateUrl = getConfig().getString("autoUpdateUrl");
if (this.autoUpdateUrl == null || this.autoUpdateUrl.equals("")) return;
Application application = (Application) this.getContext().getApplicationContext();
Expand Down Expand Up @@ -246,7 +246,7 @@ public void onActivityStopped(@NonNull Activity activity) {
Log.i(TAG, "Version: " + curVersionName + ", is considered broken");
Log.i(TAG, "Will downgraded to " + pastVersionName + " for next start");
Log.i(TAG, "Don't forget to trigger 'notifyAppReady()' in js code to validate a version.");
implementation.sendStats("revert",curVersionName);
implementation.sendStats("revert", curVersionName);
if (!pastVersion.equals("") && !pastVersionName.equals("")) {
Boolean res = implementation.set(pastVersion, pastVersionName);
if (res) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "capacitor-updater",
"version": "1.3.0",
"version": "1.3.1",
"license": "AGPL-3.0-only",
"description": "Download app update from url",
"main": "dist/plugin.cjs.js",
Expand Down

0 comments on commit bf2fb46

Please sign in to comment.