aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java')
-rw-r--r--java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java38
1 files changed, 21 insertions, 17 deletions
diff --git a/java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java b/java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java
index 719f24e59..0e7c3bb7e 100644
--- a/java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java
+++ b/java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java
@@ -38,8 +38,8 @@ import android.util.Log;
import com.android.inputmethod.compat.ConnectivityManagerCompatUtils;
import com.android.inputmethod.compat.DownloadManagerCompatUtils;
import com.android.inputmethod.latin.R;
+import com.android.inputmethod.latin.utils.ApplicationUtils;
import com.android.inputmethod.latin.utils.DebugLogUtils;
-import com.android.inputmethod.latin.utils.Utils;
import java.io.File;
import java.io.FileInputStream;
@@ -173,14 +173,15 @@ public final class UpdateHandler {
* Download latest metadata from the server through DownloadManager for all known clients
* @param context The context for retrieving resources
* @param updateNow Whether we should update NOW, or respect bandwidth policies
+ * @return true if an update successfully started, false otherwise.
*/
- public static void update(final Context context, final boolean updateNow) {
+ public static boolean tryUpdate(final Context context, final boolean updateNow) {
// TODO: loop through all clients instead of only doing the default one.
final TreeSet<String> uris = new TreeSet<String>();
final Cursor cursor = MetadataDbHelper.queryClientIds(context);
- if (null == cursor) return;
+ if (null == cursor) return false;
try {
- if (!cursor.moveToFirst()) return;
+ if (!cursor.moveToFirst()) return false;
do {
final String clientId = cursor.getString(0);
final String metadataUri =
@@ -192,6 +193,7 @@ public final class UpdateHandler {
} finally {
cursor.close();
}
+ boolean started = false;
for (final String metadataUri : uris) {
if (!TextUtils.isEmpty(metadataUri)) {
// If the metadata URI is empty, that means we should never update it at all.
@@ -200,8 +202,10 @@ public final class UpdateHandler {
// is a bug and it happens anyway, doing nothing is the right thing to do.
// For more information, {@see DictionaryProvider#insert(Uri, ContentValues)}.
updateClientsWithMetadataUri(context, updateNow, metadataUri);
+ started = true;
}
}
+ return started;
}
/**
@@ -218,7 +222,7 @@ public final class UpdateHandler {
// DownloadManager also stupidly cuts the extension to replace with its own that it
// gets from the content-type. We need to circumvent this.
final String disambiguator = "#" + System.currentTimeMillis()
- + Utils.getVersionName(context) + ".json";
+ + ApplicationUtils.getVersionName(context) + ".json";
final Request metadataRequest = new Request(Uri.parse(metadataUri + disambiguator));
DebugLogUtils.l("Request =", metadataRequest);
@@ -268,23 +272,22 @@ public final class UpdateHandler {
}
/**
- * Cancels a pending update, if there is one.
+ * Cancels downloading a file, if there is one for this URI.
*
- * If none, this is a no-op.
+ * If we are not currently downloading the file at this URI, this is a no-op.
*
* @param context the context to open the database on
- * @param clientId the id of the client
+ * @param metadataUri the URI to cancel
* @param manager an instance of DownloadManager
*/
private static void cancelUpdateWithDownloadManager(final Context context,
- final String clientId, final DownloadManager manager) {
+ final String metadataUri, final DownloadManager manager) {
synchronized (sSharedIdProtector) {
final long metadataDownloadId =
- MetadataDbHelper.getMetadataDownloadIdForClient(context, clientId);
+ MetadataDbHelper.getMetadataDownloadIdForURI(context, metadataUri);
if (NOT_AN_ID == metadataDownloadId) return;
manager.remove(metadataDownloadId);
- writeMetadataDownloadId(context,
- MetadataDbHelper.getMetadataUriAsString(context, clientId), NOT_AN_ID);
+ writeMetadataDownloadId(context, metadataUri, NOT_AN_ID);
}
// Consider a cancellation as a failure. As such, inform listeners that the download
// has failed.
@@ -294,10 +297,10 @@ public final class UpdateHandler {
}
/**
- * Cancels a pending update, if there is one.
+ * Cancels a pending update for this client, if there is one.
*
- * If there is none, this is a no-op. This is a helper method that gets the
- * download manager service.
+ * If we are not currently updating metadata for this client, this is a no-op. This is a helper
+ * method that gets the download manager service and the metadata URI for this client.
*
* @param context the context, to get an instance of DownloadManager
* @param clientId the ID of the client we want to cancel the update of
@@ -305,7 +308,8 @@ public final class UpdateHandler {
public static void cancelUpdate(final Context context, final String clientId) {
final DownloadManager manager =
(DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
- if (null != manager) cancelUpdateWithDownloadManager(context, clientId, manager);
+ final String metadataUri = MetadataDbHelper.getMetadataUriAsString(context, clientId);
+ if (null != manager) cancelUpdateWithDownloadManager(context, metadataUri, manager);
}
/**
@@ -769,7 +773,7 @@ public final class UpdateHandler {
// We may come here if there is a new word list that we can't handle.
Log.i(TAG, "Can't handle word list with id '" + id + "' because it has format"
+ " version " + metadataInfo.mFormatVersion + " and the maximum version"
- + "we can handle is " + MAXIMUM_SUPPORTED_FORMAT_VERSION);
+ + " we can handle is " + MAXIMUM_SUPPORTED_FORMAT_VERSION);
}
continue;
} else if (null == currentInfo) {