Skip to content

Commit

Permalink
fix(android): code style
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed May 5, 2022
1 parent b0befb1 commit 13475a4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 42 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Do not password encrypt this file, or it will fail to unpack.

* [`download(...)`](#download)
* [`next(...)`](#next)
* [`isAutoUpdateEnabled()`](#isautoupdateenabled)
* [`set(...)`](#set)
* [`getId()`](#getid)
* [`getPluginVersion()`](#getpluginversion)
Expand Down Expand Up @@ -175,6 +176,19 @@ Set the next bundle version to be used when the app is reloaded.
--------------------


### isAutoUpdateEnabled()

```typescript
isAutoUpdateEnabled() => Promise<{ enabled: boolean; }>
```

Get the state of auto update config.

**Returns:** <code>Promise&lt;{ enabled: boolean; }&gt;</code>

--------------------


### set(...)

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,32 @@ interface Callback {

public class CapacitorUpdater {
private static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private static SecureRandom rnd = new SecureRandom();
private static final SecureRandom rnd = new SecureRandom();

private static final String DOWNLOADED_SUFFIX = "_downloaded";
private static final String NAME_SUFFIX = "_name";
private static final String STATUS_SUFFIX = "_status";

private static final String FALLBACK_VERSION = "pastVersion";
private static final String NEXT_VERSION = "nextVersion";
private final String bundleDirectory = "versions";
private static final String bundleDirectory = "versions";

public static final String TAG = "Capacitor-updater";
public static final String pluginVersion = "4.0.0";

private SharedPreferences.Editor editor;
private SharedPreferences prefs;

private RequestQueue requestQueue;

private String documentsDir = "";
private String versionBuild = "";
private String versionCode = "";
private String versionOs = "";

public static final String TAG = "Capacitor-updater";
public static final String pluginVersion = "3.3.2";
public String statsUrl = "";
public String appId = "";
public String deviceID = "";
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
private String statsUrl = "";
private String appId = "";
private String deviceID = "";

private final FilenameFilter filter = new FilenameFilter() {
@Override
Expand Down Expand Up @@ -141,7 +144,7 @@ private File unzip(final File zipFile, final String dest) throws IOException {
try {
zis.close();
} catch (final IOException e) {
Log.e(this.TAG, "Failed to close zip input stream", e);
Log.e(TAG, "Failed to close zip input stream", e);
}
}
}
Expand Down Expand Up @@ -213,7 +216,7 @@ private void deleteDirectory(final File file) throws IOException {

private void setCurrentBundle(final File bundle) {
this.editor.putString(WebView.CAP_SERVER_PATH, bundle.getPath());
Log.i(this.TAG, "Current bundle set to: " + bundle);
Log.i(TAG, "Current bundle set to: " + bundle);
this.editor.commit();
}

Expand All @@ -223,7 +226,7 @@ public VersionInfo download(final String url, final String versionName) throws I
final File zipFile = new File(this.getDocumentsDir() + "/" + path);
final String folderNameUnZip = this.randomString(10);
final String version = this.randomString(10);
final String folderName = this.bundleDirectory + "/" + version;
final String folderName = bundleDirectory + "/" + version;
this.notifyDownload(5);
final File downloaded = this.downloadFile(url, path);
this.notifyDownload(71);
Expand All @@ -240,34 +243,34 @@ public VersionInfo download(final String url, final String versionName) throws I

public ArrayList<VersionInfo> list() {
final ArrayList<VersionInfo> res = new ArrayList<>();
final File destHot = new File(this.getDocumentsDir() + "/" + this.bundleDirectory);
Log.i(this.TAG, "list File : " + destHot.getPath());
final File destHot = new File(this.getDocumentsDir() + "/" + bundleDirectory);
Log.i(TAG, "list File : " + destHot.getPath());
if (destHot.exists()) {
for (final File i : destHot.listFiles()) {
final String version = i.getName();
res.add(this.getVersionInfo(version));
}
} else {
Log.i(this.TAG, "No version available" + destHot);
Log.i(TAG, "No version available" + destHot);
}
return res;
}

public Boolean delete(final String version) throws IOException {
final VersionInfo deleted = this.getVersionInfo(version);
final File bundle = new File(this.getDocumentsDir() + "/" + this.bundleDirectory + "/" + version);
final File bundle = new File(this.getDocumentsDir() + "/" + bundleDirectory + "/" + version);
if (bundle.exists()) {
this.deleteDirectory(bundle);
this.removeVersionInfo(version);
return true;
}
Log.i(this.TAG, "Directory not removed: " + bundle.getPath());
Log.i(TAG, "Directory not removed: " + bundle.getPath());
this.sendStats("delete", deleted);
return false;
}

private File getBundleDirectory(final String version) {
return new File(this.getDocumentsDir() + "/" + this.bundleDirectory + "/" + version);
return new File(this.getDocumentsDir() + "/" + bundleDirectory + "/" + version);
}

private boolean bundleExists(final File bundle) {
Expand All @@ -287,7 +290,7 @@ public Boolean set(final String version) {
final VersionInfo existing = this.getVersionInfo(version);
final File bundle = this.getBundleDirectory(version);

Log.i(this.TAG, "Setting next active bundle " + existing);
Log.i(TAG, "Setting next active bundle " + existing);
if (this.bundleExists(bundle)) {
this.setCurrentBundle(bundle);
this.setVersionStatus(version, VersionStatus.PENDING);
Expand Down Expand Up @@ -326,7 +329,7 @@ public void getLatest(final String url, final Callback callback) {
final String versionBuild = this.versionBuild;
final String versionCode = this.versionCode;
final String versionOs = this.versionOs;
final String pluginVersion = this.pluginVersion;
final String pluginVersion = CapacitorUpdater.pluginVersion;
final String versionName = this.getCurrentBundle().getName();
final StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
Expand All @@ -336,13 +339,13 @@ public void onResponse(final String response) {
final JSONObject jsonObject = new JSONObject(response);
callback.callback(jsonObject);
} catch (final JSONException e) {
Log.e(CapacitorUpdater.this.TAG, "Error parsing JSON", e);
Log.e(TAG, "Error parsing JSON", e);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(final VolleyError error) {
Log.e(CapacitorUpdater.this.TAG, "Error getting Latest" + error);
Log.e(TAG, "Error getting Latest" + error);
}
}) {
@Override
Expand Down Expand Up @@ -376,11 +379,11 @@ public void sendStats(final String action, final VersionInfo version) {
json.put("version_build", this.versionBuild);
json.put("version_code", this.versionCode);
json.put("version_os", this.versionOs);
json.put("plugin_version", this.pluginVersion);
json.put("plugin_version", pluginVersion);
json.put("app_id", this.getAppId());
jsonString = json.toString();
} catch (final Exception ex) {
Log.e(this.TAG, "Error get stats", ex);
Log.e(TAG, "Error get stats", ex);
return;
}
new Thread(new Runnable(){
Expand All @@ -400,12 +403,12 @@ public void run() {
wr.close();
final int responseCode = con.getResponseCode();
if (responseCode != 200) {
Log.e(CapacitorUpdater.this.TAG, "Stats error responseCode: " + responseCode);
Log.e(TAG, "Stats error responseCode: " + responseCode);
} else {
Log.i(CapacitorUpdater.this.TAG, "Stats send for \"" + action + "\", version " + version);
Log.i(TAG, "Stats send for \"" + action + "\", version " + version);
}
} catch (final Exception ex) {
Log.e(CapacitorUpdater.this.TAG, "Error post stats", ex);
Log.e(TAG, "Error post stats", ex);
} finally {
if (con != null) {
con.disconnect();
Expand Down Expand Up @@ -437,7 +440,7 @@ private String getVersionDownloadedTimestamp(final String version) {

private void setVersionDownloadedTimestamp(final String version, final Date time) {
if(version != null) {
Log.i(this.TAG, "Setting version download timestamp " + version + " to " + time);
Log.i(TAG, "Setting version download timestamp " + version + " to " + time);
if(time == null) {
this.editor.remove(version + DOWNLOADED_SUFFIX);
} else {
Expand All @@ -459,7 +462,7 @@ private String getVersionName(final String version) {

public void setVersionName(final String version, final String name) {
if(version != null) {
Log.i(this.TAG, "Setting version name " + version + " to " + name);
Log.i(TAG, "Setting version name " + version + " to " + name);
if(name == null) {
this.editor.remove(version + NAME_SUFFIX);
} else {
Expand All @@ -475,7 +478,7 @@ private VersionStatus getVersionStatus(final String version) {

private void setVersionStatus(final String version, final VersionStatus status) {
if(version != null) {
Log.i(this.TAG, "Setting version status " + version + " to " + status);
Log.i(TAG, "Setting version status " + version + " to " + status);
if(status == null) {
this.editor.remove(version + STATUS_SUFFIX);
} else {
Expand Down Expand Up @@ -567,35 +570,35 @@ public void setDeviceID(final String deviceID) {
this.deviceID = deviceID;
}

public void setVersionBuild(String versionBuild) {
public void setVersionBuild(final String versionBuild) {
this.versionBuild = versionBuild;
}

public void setVersionCode(String versionCode) {
public void setVersionCode(final String versionCode) {
this.versionCode = versionCode;
}

public void setVersionOs(String versionOs) {
public void setVersionOs(final String versionOs) {
this.versionOs = versionOs;
}

public void setPrefs(SharedPreferences prefs) {
public void setPrefs(final SharedPreferences prefs) {
this.prefs = prefs;
}

public void setEditor(SharedPreferences.Editor editor) {
public void setEditor(final SharedPreferences.Editor editor) {
this.editor = editor;
}

public void setDocumentsDir(String documentsDir) {
public void setDocumentsDir(final String documentsDir) {
this.documentsDir = documentsDir;
}

public void setRequestQueue(RequestQueue requestQueue) {
public void setRequestQueue(final RequestQueue requestQueue) {
this.requestQueue = requestQueue;
}

public String getDocumentsDir() {
return documentsDir;
return this.documentsDir;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@ public class CapacitorUpdaterPlugin extends Plugin implements Application.Activi
private static final String autoUpdateUrlDefault = "https://capgo.app/api/auto_update";
private static final String statsUrlDefault = "https://capgo.app/api/stats";
private static final String DELAY_UPDATE = "delayUpdate";
private CapacitorUpdater implementation;

private SharedPreferences prefs;

private SharedPreferences.Editor editor;
private SharedPreferences prefs;
private CapacitorUpdater implementation;

private Integer appReadyTimeout = 10000;
private String autoUpdateUrl = "";
private Version currentVersionNative;
private Boolean autoDeleteFailed = true;
private Boolean autoDeletePrevious = true;
private Boolean autoUpdate = false;
private String autoUpdateUrl = "";
private Version currentVersionNative;
private Boolean resetWhenUpdate = true;

private volatile Thread appReadyCheck;
Expand Down

0 comments on commit 13475a4

Please sign in to comment.