Skip to content

Commit

Permalink
fix: android build
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Apr 8, 2022
1 parent f8272f2 commit 885ff7b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
Expand All @@ -22,12 +25,15 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.ArrayList;
Expand All @@ -37,19 +43,18 @@ interface Callback {
void callback(JSONObject jsonObject);
}

FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File f, String name) {
// ignore directories generated by mac os x
return !name.startsWith("__MACOSX") && !name.startsWith(".") && !name.startsWith(".DS_Store");
}
};

public class CapacitorUpdater {
public String statsUrl = "";
public String appId = "";
public String deviceID = "";

private FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File f, String name) {
// ignore directories generated by mac os x
return !name.startsWith("__MACOSX") && !name.startsWith(".") && !name.startsWith(".DS_Store");
}
};
private final CapacitorUpdaterPlugin plugin;
private String versionBuild = "";
private String TAG = "Capacitor-updater";
Expand All @@ -71,8 +76,7 @@ private String randomString(int len){
sb.append(AB.charAt(rnd.nextInt(AB.length())));
return sb.toString();
}

public CapacitorUpdater (Context context) {
public CapacitorUpdater (Context context) throws PackageManager.NameNotFoundException {
this.context = context;
this.plugin = new CapacitorUpdaterPlugin();
this.prefs = context.getSharedPreferences("CapWebViewSettings", Activity.MODE_PRIVATE);
Expand All @@ -81,7 +85,7 @@ public CapacitorUpdater (Context context) {
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
this.versionBuild = pInfo.versionName;
}
public CapacitorUpdater (Context context, CapacitorUpdaterPlugin plugin) {
public CapacitorUpdater (Context context, CapacitorUpdaterPlugin plugin) throws PackageManager.NameNotFoundException {
this.context = context;
this.plugin = plugin;
this.prefs = context.getSharedPreferences("CapWebViewSettings", Activity.MODE_PRIVATE);
Expand Down Expand Up @@ -288,6 +292,10 @@ public Boolean set(String version, String versionName) {
}

public void getLatest(String url, Callback callback) {
String deviceID = this.deviceID;
String appId = this.appId;
String versionBuild = this.versionBuild;
String versionName = getVersionName().equals("") ? "builtin" : getVersionName();
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
Expand All @@ -306,13 +314,13 @@ public void onErrorResponse(VolleyError error) {
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("cap_platform", "android");
params.put("cap_device_id", this.deviceID);
params.put("cap_app_id", this.appId);
params.put("cap_version_build", this.versionBuild);
params.put("cap_version_name", prefs.getString("versionName", "builtin"));
params.put("cap_device_id", deviceID);
params.put("cap_app_id", appId);
params.put("cap_version_build", versionBuild);
params.put("cap_version_name", versionName);
return params;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import android.app.Application;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.getcapacitor.CapConfig;
import com.getcapacitor.JSArray;
Expand Down Expand Up @@ -43,7 +46,12 @@ public void load() {
super.load();
this.prefs = this.getContext().getSharedPreferences("CapWebViewSettings", Activity.MODE_PRIVATE);
this.editor = prefs.edit();
implementation = new CapacitorUpdater(this.getContext(), this);
try {
implementation = new CapacitorUpdater(this.getContext(), this);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return;
}
CapConfig config = CapConfig.loadDefault(getActivity());
implementation.appId = config.getString("appId", "");
implementation.statsUrl = getConfig().getString("statsUrl", statsUrlDefault);
Expand All @@ -60,7 +68,13 @@ public void load() {
editor.putString("LatestVersionAutoUpdate", "");
editor.putString("LatestVersionNameAutoUpdate", "");
ArrayList<String> res = implementation.list();
res.forEach((version) -> implementation.delete(version, ""));
for (int i = 0; i < res.size(); i++) {
try {
implementation.delete(res.get(i), "");
} catch (IOException e) {
e.printStackTrace();
}
}
}
editor.putString("LatestVersionNative", currentVersionNative.toString());
editor.commit();
Expand Down Expand Up @@ -249,12 +263,12 @@ public void run() {
String newVersion = (String) res.get("version");
JSObject ret = new JSObject();
ret.put("newVersion", newVersion);
if (res.get('message')) {
Log.i(TAG, "Capacitor-updater: " + (res.get('message') || "Unknow error"));
if (res.get('major') == true) {
if (res.has("message")) {
Log.i(TAG, "Capacitor-updater: " + res.get("message"));
if (res.getBoolean("major")) {
notifyListeners("majorAvailable", ret);
}
return
return;
}
String failingVersion = prefs.getString("failingVersion", "");
if (!newVersion.equals("") && !newVersion.equals(failingVersion)) {
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": "3.0.0-beta.7",
"version": "3.0.0-beta.8",
"license": "AGPL-3.0-only",
"description": "OTA update for capacitor apps",
"main": "dist/plugin.cjs.js",
Expand Down

0 comments on commit 885ff7b

Please sign in to comment.