mirror of
https://github.com/keymanapp/keyman.git
synced 2026-08-27 02:37:42 +00:00
Merge branch 'feature/downloadManager' into feature/keyboard-download-manager
This commit is contained in:
commit
1fba50a611
9 changed files with 55 additions and 58 deletions
|
|
@ -58,16 +58,16 @@ public class CloudApiTypes {
|
|||
public static class SingleCloudDownload
|
||||
{
|
||||
private DownloadManager.Request request;
|
||||
private boolean downloadfinished =false;
|
||||
private boolean downloadFinished =false;
|
||||
private long downloadId;
|
||||
private File destiniationFile;
|
||||
private File destinationFile;
|
||||
private CloudApiTypes.JSONType type;
|
||||
private CloudApiTypes.ApiTarget target;
|
||||
|
||||
public SingleCloudDownload(DownloadManager.Request aRequest,File aDestinationFile)
|
||||
{
|
||||
request = aRequest;
|
||||
destiniationFile=aDestinationFile;
|
||||
destinationFile = aDestinationFile;
|
||||
}
|
||||
public SingleCloudDownload setDownloadId(long downloadId) {
|
||||
this.downloadId = downloadId;
|
||||
|
|
@ -92,8 +92,8 @@ public class CloudApiTypes {
|
|||
return downloadId;
|
||||
}
|
||||
|
||||
public File getDestiniationFile() {
|
||||
return destiniationFile;
|
||||
public File getDestinationFile() {
|
||||
return destinationFile;
|
||||
}
|
||||
|
||||
public JSONType getType() {
|
||||
|
|
@ -107,22 +107,22 @@ public class CloudApiTypes {
|
|||
|
||||
/**
|
||||
* Typed Download sets for cloud download.
|
||||
* @param <M> the model objects type
|
||||
* @param <R> the result type of the download
|
||||
* @param <ModelType> the model objects type
|
||||
* @param <ResultType> the result type of the download
|
||||
*/
|
||||
public static class CloudDownloadSet<M,R> {
|
||||
public static class CloudDownloadSet<ModelType,ResultType> {
|
||||
private String downloadIdentifier;
|
||||
private M targetModel;
|
||||
private ModelType targetModel;
|
||||
private LinkedList<SingleCloudDownload> downloads = new LinkedList<>();
|
||||
|
||||
private ICloudDownloadCallback<M,R> callback;
|
||||
private ICloudDownloadCallback<ModelType,ResultType> callback;
|
||||
|
||||
private boolean resultsAreProcessing = false;
|
||||
private boolean resultsReady = false;
|
||||
|
||||
//maybe implement a max lifetime for downloads
|
||||
//TODO: maybe implement a max lifetime for downloads
|
||||
//private long startingTime = System.currentTimeMillis();
|
||||
|
||||
public CloudDownloadSet(@NonNull String aDownloadIdentifier, M theTargetObject)
|
||||
public CloudDownloadSet(@NonNull String aDownloadIdentifier, ModelType theTargetObject)
|
||||
{
|
||||
targetModel = theTargetObject;
|
||||
downloadIdentifier = aDownloadIdentifier;
|
||||
|
|
@ -131,7 +131,7 @@ public class CloudApiTypes {
|
|||
protected boolean hasOpenDownloads() {
|
||||
synchronized (downloads) {
|
||||
for (SingleCloudDownload _d : downloads) {
|
||||
if (!_d.downloadfinished)
|
||||
if (!_d.downloadFinished)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
@ -140,7 +140,7 @@ public class CloudApiTypes {
|
|||
|
||||
void addDownload(SingleCloudDownload aDownload) {
|
||||
synchronized (downloads) {
|
||||
if (resultsAreProcessing)
|
||||
if (resultsReady)
|
||||
throw new IllegalStateException("Could not add download to an allready processed download set");
|
||||
|
||||
downloads.add(aDownload);
|
||||
|
|
@ -155,30 +155,30 @@ public class CloudApiTypes {
|
|||
this.callback = callback;
|
||||
}
|
||||
|
||||
public boolean isResultsAreProcessing() {
|
||||
return resultsAreProcessing;
|
||||
public boolean isResultsReady() {
|
||||
return resultsReady;
|
||||
}
|
||||
|
||||
public void setResultsAreProcessing(boolean resultsAreProcessing) {
|
||||
this.resultsAreProcessing = resultsAreProcessing;
|
||||
public void setResultsReady() {
|
||||
this.resultsReady = true;
|
||||
}
|
||||
|
||||
public String getDownloadIdentifier() {
|
||||
return downloadIdentifier;
|
||||
}
|
||||
|
||||
public M getTargetModel() {
|
||||
public ModelType getTargetModel() {
|
||||
return targetModel;
|
||||
}
|
||||
|
||||
void setDone(long aDownload) {
|
||||
synchronized (downloads) {
|
||||
if (resultsAreProcessing)
|
||||
throw new IllegalStateException("Download is already processed");
|
||||
if (resultsReady)
|
||||
throw new IllegalStateException("Download is already ready");
|
||||
|
||||
for (SingleCloudDownload _d : downloads) {
|
||||
if (_d.downloadId == aDownload) {
|
||||
_d.downloadfinished = true;
|
||||
_d.downloadFinished = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import java.util.List;
|
|||
*/
|
||||
public class CloudCatalogDownloadCallback implements ICloudDownloadCallback<Dataset, CloudCatalogDownloadReturns>{
|
||||
|
||||
private static final String TAG = "CloudCatalogDownloadCallback";
|
||||
private static final boolean DEBUG_SIMULATE_UPDATES = false;
|
||||
|
||||
private final Context context;
|
||||
|
|
@ -215,7 +216,7 @@ public class CloudCatalogDownloadCallback implements ICloudDownloadCallback<Data
|
|||
|
||||
if (updateBundles.size() > 0) {
|
||||
// Time for updates!
|
||||
Log.v(CloudRepository.TAG, "Performing keyboard and model updates for " + updateBundles.size() + " resources.");
|
||||
Log.v(TAG, "Performing keyboard and model updates for " + updateBundles.size() + " resources.");
|
||||
|
||||
updateHandler.onUpdateDetection(updateBundles);
|
||||
}
|
||||
|
|
@ -250,19 +251,19 @@ public class CloudCatalogDownloadCallback implements ICloudDownloadCallback<Data
|
|||
JSONArray dataArray = null;
|
||||
JSONObject dataObject = null;
|
||||
|
||||
if (_d.getDestiniationFile() != null && _d.getDestiniationFile().length() > 0) {
|
||||
if (_d.getDestinationFile() != null && _d.getDestinationFile().length() > 0) {
|
||||
try {
|
||||
|
||||
//Object _o = jsonParser.getJSONObjectFromFile(_d.getDestiniationFile());
|
||||
if (_d.getType() == CloudApiTypes.JSONType.Array) {
|
||||
dataArray = jsonParser.getJSONObjectFromFile(_d.getDestiniationFile(),JSONArray.class);//(JSONArray) _o;
|
||||
dataArray = jsonParser.getJSONObjectFromFile(_d.getDestinationFile(),JSONArray.class);//(JSONArray) _o;
|
||||
} else {
|
||||
dataObject = jsonParser.getJSONObjectFromFile(_d.getDestiniationFile(),JSONObject.class);//(JSONObject) _o;
|
||||
dataObject = jsonParser.getJSONObjectFromFile(_d.getDestinationFile(),JSONObject.class);//(JSONObject) _o;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.d(CloudRepository.TAG, e.getMessage());
|
||||
Log.d(TAG, e.getMessage());
|
||||
} finally {
|
||||
_d.getDestiniationFile().delete();
|
||||
_d.getDestinationFile().delete();
|
||||
}
|
||||
} else {
|
||||
// Offline trouble! That said, we can't get anything, so we simply shouldn't add anything.
|
||||
|
|
|
|||
|
|
@ -39,20 +39,8 @@ class CloudCatalogDownloadReturns {
|
|||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
boolean emptyKbd = false;
|
||||
boolean emptyLex = false;
|
||||
|
||||
if (keyboardJSON == null) {
|
||||
emptyKbd = true;
|
||||
} else if (keyboardJSON.length() == 0) {
|
||||
emptyKbd = true;
|
||||
}
|
||||
|
||||
if(lexicalModelJSON == null) {
|
||||
emptyLex = true;
|
||||
} else if(lexicalModelJSON.length() == 0) {
|
||||
emptyLex = true;
|
||||
}
|
||||
boolean emptyKbd = keyboardJSON == null || keyboardJSON.length() == 0;
|
||||
boolean emptyLex = lexicalModelJSON == null || lexicalModelJSON.length() == 0;
|
||||
|
||||
return emptyKbd && emptyLex;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import java.util.List;
|
|||
|
||||
class CloudCatalogDownloadTask extends AsyncTask<CloudApiTypes.CloudApiParam, Integer, CloudCatalogDownloadReturns> {
|
||||
|
||||
private static final String TAG = "CloudCatalogDownloadTask";
|
||||
|
||||
private final boolean hasConnection;
|
||||
private ProgressDialog progressDialog;
|
||||
|
|
@ -114,7 +115,7 @@ class CloudCatalogDownloadTask extends AsyncTask<CloudApiTypes.CloudApiParam, In
|
|||
dataObject = jsonParser.getJSONObjectFromUrl(remoteUrl, JSONObject.class);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.d(CloudRepository.TAG, e.getMessage());
|
||||
Log.d(TAG, e.getMessage());
|
||||
}
|
||||
} else {
|
||||
// Offline trouble! That said, we can't get anything, so we simply shouldn't add anything.
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import java.util.List;
|
|||
|
||||
public class CloudDataJsonUtil {
|
||||
|
||||
private static final String TAG = "CloudDataJsonUtil";
|
||||
private CloudDataJsonUtil()
|
||||
{
|
||||
//no instances
|
||||
|
|
@ -70,7 +71,7 @@ public class CloudDataJsonUtil {
|
|||
}
|
||||
}
|
||||
} catch (JSONException | NullPointerException e) {
|
||||
Log.e(CloudRepository.TAG, "JSONParse Error: " + e);
|
||||
Log.e(TAG, "JSONParse Error: " + e);
|
||||
return new ArrayList<>(); // Is this ideal?
|
||||
}
|
||||
|
||||
|
|
@ -131,7 +132,7 @@ public class CloudDataJsonUtil {
|
|||
modelList.add(new LexicalModel(hashMap));
|
||||
}
|
||||
} catch (JSONException | NullPointerException e) {
|
||||
Log.e(CloudRepository.TAG, "JSONParse Error: " + e);
|
||||
Log.e(TAG, "JSONParse Error: " + e);
|
||||
return new ArrayList<>(); // Is this ideal?
|
||||
}
|
||||
|
||||
|
|
@ -148,7 +149,7 @@ public class CloudDataJsonUtil {
|
|||
objInput.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(CloudRepository.TAG, "Failed to read from cache file. Error: " + e);
|
||||
Log.e(TAG, "Failed to read from cache file. Error: " + e);
|
||||
lmData = null;
|
||||
}
|
||||
|
||||
|
|
@ -165,7 +166,7 @@ public class CloudDataJsonUtil {
|
|||
objInput.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(CloudRepository.TAG, "Failed to read from cache file. Error: " + e);
|
||||
Log.e(TAG, "Failed to read from cache file. Error: " + e);
|
||||
kbData = null;
|
||||
}
|
||||
|
||||
|
|
@ -186,7 +187,7 @@ public class CloudDataJsonUtil {
|
|||
objOutput.writeObject(json.toString());
|
||||
objOutput.close();
|
||||
} catch (Exception e) {
|
||||
Log.e(CloudRepository.TAG, "Failed to save to cache file. Error: " + e);
|
||||
Log.e(TAG, "Failed to save to cache file. Error: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -204,7 +205,7 @@ public class CloudDataJsonUtil {
|
|||
objOutput.writeObject(json.toString());
|
||||
objOutput.close();
|
||||
} catch (Exception e) {
|
||||
Log.e(CloudRepository.TAG, "Failed to save to cache file. Error: " + e);
|
||||
Log.e(TAG, "Failed to save to cache file. Error: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ public class CloudDownloadMgr{
|
|||
*/
|
||||
private void processDownloadSet(Context aContext, CloudApiTypes.CloudDownloadSet aDownloadSet)
|
||||
{
|
||||
aDownloadSet.setResultsAreProcessing(true);
|
||||
aDownloadSet.setResultsReady();
|
||||
|
||||
ICloudDownloadCallback _callback = aDownloadSet.getCallback();
|
||||
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ import java.util.List;
|
|||
|
||||
public class CloudRepository {
|
||||
static public final CloudRepository shared = new CloudRepository();
|
||||
static final String TAG = "CloudRepository";
|
||||
private static final String TAG = "CloudRepository";
|
||||
|
||||
public static final boolean USE_DOWNLOAD_MANAGER = true;
|
||||
public static final String DOWNLOAD_IDENTIFIER_CATALOGUE = "catalogue";
|
||||
|
|
|
|||
|
|
@ -4,10 +4,10 @@ import android.content.Context;
|
|||
|
||||
/**
|
||||
* Interface for {@link CloudDownloadMgr} as callback to do use case specific task.
|
||||
* @param <M> the model objects type
|
||||
* @param <R> the result type of the download
|
||||
* @param <ModelType> the model objects type
|
||||
* @param <ResultType> the result type of the download
|
||||
*/
|
||||
public interface ICloudDownloadCallback<M,R> {
|
||||
public interface ICloudDownloadCallback<ModelType,ResultType> {
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -15,7 +15,13 @@ public interface ICloudDownloadCallback<M,R> {
|
|||
* @param aDownload the download
|
||||
* @return the result
|
||||
*/
|
||||
R extractCloudResultFromDownloadSet(CloudApiTypes.CloudDownloadSet<M,R> aDownload);
|
||||
ResultType extractCloudResultFromDownloadSet(CloudApiTypes.CloudDownloadSet<ModelType,ResultType> aDownload);
|
||||
|
||||
void applyCloudDownloadToModel(Context aContext, M aModel, R aCloudResult);
|
||||
/**
|
||||
* Apply download results to target model.
|
||||
* @param aContext the context
|
||||
* @param aModel the model
|
||||
* @param aCloudResult the result
|
||||
*/
|
||||
void applyCloudDownloadToModel(Context aContext, ModelType aModel, ResultType aCloudResult);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@
|
|||
<string name="downloading_model" translatable="false">Downloading dictionary…</string>
|
||||
<string name="catalog_unavailable" translatable="false">The resource catalog is unavailable\n</string>
|
||||
<string name="catalog_download_start_in_background" translatable="false">Catalog update started in background.\n</string>
|
||||
<string name="catalog_download_is_running_in_background" translatable="false">The download of catalog is running. Please, try again later!\n</string>
|
||||
<string name="catalog_download_is_running_in_background" translatable="false">The catalog is still downloading; please try again in a moment!\n</string>
|
||||
|
||||
<!-- General Updates -->
|
||||
<string name="update_check_unavailable" translatable="false">Failed to access server!\n</string>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue