Skip to content

Commit

Permalink
fix: make android file closer to ios one
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed May 4, 2022
1 parent c6a0ca4 commit c584e9d
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package ee.forgr.capacitor_updater;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import com.android.volley.AuthFailureError;
Expand All @@ -14,7 +9,6 @@
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.getcapacitor.plugin.WebView;

import org.json.JSONException;
Expand Down Expand Up @@ -42,7 +36,6 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.ArrayList;
import android.provider.Settings.Secure;

interface Callback {
void callback(JSONObject jsonObject);
Expand All @@ -58,34 +51,21 @@ public class CapacitorUpdater {

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


private final Context context;

private final String bundleDirectory = "versions";

private final SharedPreferences prefs;
private final SharedPreferences.Editor editor;
private RequestQueue requestQueue;
private String documentsDir = "";
private String versionBuild = "";
private String versionCode = "";
private String versionOs = "";

public final String TAG = "Capacitor-updater";
public final String pluginVersion = "3.3.2";
public static final String TAG = "Capacitor-updater";
public static final String pluginVersion = "3.3.2";
public String statsUrl = "";
public String appId = "";
public String deviceID = "";

public CapacitorUpdater (final Context context) throws PackageManager.NameNotFoundException {
this.context = context;
this.prefs = this.context.getSharedPreferences("CapWebViewSettings", Activity.MODE_PRIVATE);
this.editor = this.prefs.edit();
this.versionOs = Build.VERSION.RELEASE;
this.deviceID = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
final PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
this.versionBuild = pInfo.versionName;
this.versionCode = Integer.toString(pInfo.versionCode);
}
private SharedPreferences prefs;
private SharedPreferences.Editor editor;

private final FilenameFilter filter = new FilenameFilter() {
@Override
Expand All @@ -111,7 +91,7 @@ private String randomString(final int len){
}

private File unzip(final File zipFile, final String dest) throws IOException {
final File targetDirectory = new File(this.context.getFilesDir() + "/" + dest);
final File targetDirectory = new File(this.getDocumentsDir() + "/" + dest);
final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
try {
int count;
Expand Down Expand Up @@ -170,7 +150,7 @@ private void flattenAssets(final File sourceFile, final String dest) throws IOEx
if (!sourceFile.exists()) {
throw new FileNotFoundException("Source file not found: " + sourceFile.getPath());
}
final File destinationFile = new File(this.context.getFilesDir() + "/" + dest);
final File destinationFile = new File(this.getDocumentsDir() + "/" + dest);
destinationFile.getParentFile().mkdirs();
final String[] entries = sourceFile.list(this.filter);
if (entries == null || entries.length == 0) {
Expand All @@ -192,7 +172,7 @@ private File downloadFile(final String url, final String dest) throws IOExceptio
final InputStream is = u.openStream();
final DataInputStream dis = new DataInputStream(is);

final File target = new File(this.context.getFilesDir() + "/" + dest);
final File target = new File(this.getDocumentsDir() + "/" + dest);
target.getParentFile().mkdirs();
target.createNewFile();
final FileOutputStream fos = new FileOutputStream(target);
Expand Down Expand Up @@ -240,7 +220,7 @@ private void setCurrentBundle(final File bundle) {
public VersionInfo download(final String url, final String versionName) throws IOException {
this.notifyDownload(0);
final String path = this.randomString(10);
final File zipFile = new File(this.context.getFilesDir() + "/" + path);
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;
Expand All @@ -260,7 +240,7 @@ 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.context.getFilesDir() + "/" + this.bundleDirectory);
final File destHot = new File(this.getDocumentsDir() + "/" + this.bundleDirectory);
Log.i(this.TAG, "list File : " + destHot.getPath());
if (destHot.exists()) {
for (final File i : destHot.listFiles()) {
Expand All @@ -275,7 +255,7 @@ public ArrayList<VersionInfo> list() {

public Boolean delete(final String version) throws IOException {
final VersionInfo deleted = this.getVersionInfo(version);
final File bundle = new File(this.context.getFilesDir() + "/" + this.bundleDirectory + "/" + version);
final File bundle = new File(this.getDocumentsDir() + "/" + this.bundleDirectory + "/" + version);
if (bundle.exists()) {
this.deleteDirectory(bundle);
this.removeVersionInfo(version);
Expand All @@ -287,7 +267,7 @@ public Boolean delete(final String version) throws IOException {
}

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

private boolean bundleExists(final File bundle) {
Expand Down Expand Up @@ -379,8 +359,7 @@ public Map<String, String> getHeaders() throws AuthFailureError {
return params;
}
};
final RequestQueue requestQueue = Volley.newRequestQueue(this.context);
requestQueue.add(stringRequest);
this.requestQueue.add(stringRequest);
}

public void sendStats(final String action, final VersionInfo version) {
Expand Down Expand Up @@ -587,4 +566,36 @@ public String getDeviceID() {
public void setDeviceID(final String deviceID) {
this.deviceID = deviceID;
}

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

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

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

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

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

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

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

public String getDocumentsDir() {
return documentsDir;
}
}

0 comments on commit c584e9d

Please sign in to comment.