Skip to content

Commit

Permalink
feat: add setChannel method
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Nov 3, 2022
1 parent 41b2be0 commit 4cbd77d
Show file tree
Hide file tree
Showing 7 changed files with 428 additions and 226 deletions.
120 changes: 91 additions & 29 deletions android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.getcapacitor.JSObject;
import com.getcapacitor.plugin.WebView;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
Expand All @@ -22,6 +23,7 @@
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
Expand All @@ -33,6 +35,10 @@ interface Callback {
void callback(JSONObject jsonObject);
}

interface CallbackChannel {
void callback(JSObject jsoObject);
}

public class CapacitorUpdater {

private static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Expand All @@ -58,6 +64,7 @@ public class CapacitorUpdater {
public String versionOs = "";

public String statsUrl = "";
public String channelUrl = "";
public String appId = "";
public String deviceID = "";

Expand Down Expand Up @@ -104,7 +111,7 @@ private File unzip(final String id, final File zipFile, final String dest) throw

if (!canonicalPath.startsWith(canonicalDir)) {
throw new FileNotFoundException(
"SecurityException, Failed to ensure directory is the start path : " + canonicalDir + " of " + canonicalPath
"SecurityException, Failed to ensure directory is the start path : " + canonicalDir + " of " + canonicalPath
);
}

Expand Down Expand Up @@ -372,21 +379,21 @@ public void getLatest(final String updateUrl, final Callback callback) {
Log.i(CapacitorUpdater.TAG, "Auto-update parameters: " + json);
// Building a request
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST,
updateUrl,
json,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
callback.callback(response);
Request.Method.POST,
updateUrl,
json,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
callback.callback(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error getting Latest", error);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error getting Latest", error);
}
}
);
this.requestQueue.add(request);
} catch (JSONException ex) {
Expand All @@ -395,6 +402,61 @@ public void onErrorResponse(VolleyError error) {
}
}

public void setChannel(final String channel, final CallbackChannel callback) {
String channelUrl = this.channelUrl;
if (channelUrl == null || "".equals(channelUrl) || channelUrl.length() == 0) {
return;
}
try {
JSONObject json = new JSONObject();
json.put("platform", "android");
json.put("device_id", this.deviceID);
json.put("app_id", this.appId);
json.put("version_build", this.versionBuild);
json.put("version_code", this.versionCode);
json.put("version_os", this.versionOs);
json.put("version_name", this.getCurrentBundle().getVersionName());
json.put("plugin_version", pluginVersion);
json.put("channel", channel);

// Building a request
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST,
channelUrl,
json,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject res) {
final JSObject ret = new JSObject();
Iterator<String> keys = res.keys();
while (keys.hasNext()) {
String key = keys.next();
if (res.has(key)) {
try {
ret.put(key, res.get(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Log.i(TAG, "Channel set to \"" + channel);
callback.callback(ret);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error set channel: " + error);
}
}
);
this.requestQueue.add(request);
} catch (JSONException ex) {
// Catch if something went wrong with the params
Log.e(TAG, "Error setChannel JSONException", ex);
}
}

public void sendStats(final String action, final String versionName) {
String statsUrl = this.statsUrl;
if (statsUrl == null || "".equals(statsUrl) || statsUrl.length() == 0) {
Expand All @@ -414,21 +476,21 @@ public void sendStats(final String action, final String versionName) {

// Building a request
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST,
statsUrl,
json,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(TAG, "Stats send for \"" + action + "\", version " + versionName);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error sending stats: " + error);
Request.Method.POST,
statsUrl,
json,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(TAG, "Stats send for \"" + action + "\", version " + versionName);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error sending stats: " + error);
}
}
}
);
this.requestQueue.add(request);
} catch (JSONException ex) {
Expand Down

0 comments on commit 4cbd77d

Please sign in to comment.