Do not restrict downloads to WiFi networks.

Bug 21900337.

Change-Id: I82991b492ea4c02a28a1ccc898d6f12741f1b793
diff --git a/java/src/com/android/inputmethod/compat/DownloadManagerCompatUtils.java b/java/src/com/android/inputmethod/compat/DownloadManagerCompatUtils.java
deleted file mode 100644
index 6209b60..0000000
--- a/java/src/com/android/inputmethod/compat/DownloadManagerCompatUtils.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.inputmethod.compat;
-
-import android.app.DownloadManager;
-
-import java.lang.reflect.Method;
-
-public final class DownloadManagerCompatUtils {
-    // DownloadManager.Request#setAllowedOverMetered() has been introduced
-    // in API level 16 (Build.VERSION_CODES.JELLY_BEAN).
-    private static final Method METHOD_setAllowedOverMetered = CompatUtils.getMethod(
-            DownloadManager.Request.class, "setAllowedOverMetered", boolean.class);
-
-    public static DownloadManager.Request setAllowedOverMetered(
-            final DownloadManager.Request request, final boolean allowOverMetered) {
-        return (DownloadManager.Request)CompatUtils.invoke(request,
-                request /* default return value */, METHOD_setAllowedOverMetered, allowOverMetered);
-    }
-
-    public static final boolean hasSetAllowedOverMetered() {
-        return null != METHOD_setAllowedOverMetered;
-    }
-}
diff --git a/java/src/com/android/inputmethod/dictionarypack/ActionBatch.java b/java/src/com/android/inputmethod/dictionarypack/ActionBatch.java
index ee5106b..6feef9e 100644
--- a/java/src/com/android/inputmethod/dictionarypack/ActionBatch.java
+++ b/java/src/com/android/inputmethod/dictionarypack/ActionBatch.java
@@ -25,7 +25,6 @@
 import android.text.TextUtils;
 import android.util.Log;
 
-import com.android.inputmethod.compat.DownloadManagerCompatUtils;
 import com.android.inputmethod.latin.BinaryDictionaryFileDumper;
 import com.android.inputmethod.latin.R;
 import com.android.inputmethod.latin.common.LocaleUtils;
@@ -86,7 +85,7 @@
          * Execute this action NOW.
          * @param context the context to get system services, resources, databases
          */
-        public void execute(final Context context);
+        void execute(final Context context);
     }
 
     /**
@@ -98,13 +97,10 @@
         private final String mClientId;
         // The data to download. May not be null.
         final WordListMetadata mWordList;
-        final boolean mForceStartNow;
-        public StartDownloadAction(final String clientId,
-                final WordListMetadata wordList, final boolean forceStartNow) {
+        public StartDownloadAction(final String clientId, final WordListMetadata wordList) {
             DebugLogUtils.l("New download action for client ", clientId, " : ", wordList);
             mClientId = clientId;
             mWordList = wordList;
-            mForceStartNow = forceStartNow;
         }
 
         @Override
@@ -143,28 +139,7 @@
             final Request request = new Request(uri);
 
             final Resources res = context.getResources();
-            if (!mForceStartNow) {
-                if (DownloadManagerCompatUtils.hasSetAllowedOverMetered()) {
-                    final boolean allowOverMetered;
-                    switch (UpdateHandler.getDownloadOverMeteredSetting(context)) {
-                    case UpdateHandler.DOWNLOAD_OVER_METERED_DISALLOWED:
-                        // User said no: don't allow.
-                        allowOverMetered = false;
-                        break;
-                    case UpdateHandler.DOWNLOAD_OVER_METERED_ALLOWED:
-                        // User said yes: allow.
-                        allowOverMetered = true;
-                        break;
-                    default: // UpdateHandler.DOWNLOAD_OVER_METERED_SETTING_UNKNOWN
-                        // Don't know: use the default value from configuration.
-                        allowOverMetered = res.getBoolean(R.bool.allow_over_metered);
-                    }
-                    DownloadManagerCompatUtils.setAllowedOverMetered(request, allowOverMetered);
-                } else {
-                    request.setAllowedNetworkTypes(Request.NETWORK_WIFI);
-                }
-                request.setAllowedOverRoaming(res.getBoolean(R.bool.allow_over_roaming));
-            } // if mForceStartNow, then allow all network types and roaming, which is the default.
+            request.setAllowedNetworkTypes(Request.NETWORK_WIFI | Request.NETWORK_MOBILE);
             request.setTitle(mWordList.mDescription);
             request.setNotificationVisibility(
                     res.getBoolean(R.bool.display_notification_for_auto_update)
diff --git a/java/src/com/android/inputmethod/dictionarypack/CommonPreferences.java b/java/src/com/android/inputmethod/dictionarypack/CommonPreferences.java
index 3cd822a..3d0e29e 100644
--- a/java/src/com/android/inputmethod/dictionarypack/CommonPreferences.java
+++ b/java/src/com/android/inputmethod/dictionarypack/CommonPreferences.java
@@ -22,8 +22,6 @@
 public final class CommonPreferences {
     private static final String COMMON_PREFERENCES_NAME = "LatinImeDictPrefs";
 
-    public static final String PREF_FORCE_DOWNLOAD_DICT = "pref_key_force_download_dict";
-
     public static SharedPreferences getCommonPreferences(final Context context) {
         return context.getSharedPreferences(COMMON_PREFERENCES_NAME, 0);
     }
@@ -39,14 +37,4 @@
         editor.putBoolean(id, false);
         editor.apply();
     }
-
-    public static boolean isForceDownloadDict(Context context) {
-        return getCommonPreferences(context).getBoolean(PREF_FORCE_DOWNLOAD_DICT, false);
-    }
-
-    public static void setForceDownloadDict(Context context, boolean forceDownload) {
-        SharedPreferences.Editor editor = getCommonPreferences(context).edit();
-        editor.putBoolean(PREF_FORCE_DOWNLOAD_DICT, forceDownload);
-        editor.apply();
-    }
 }
diff --git a/java/src/com/android/inputmethod/dictionarypack/DictionaryService.java b/java/src/com/android/inputmethod/dictionarypack/DictionaryService.java
index bbdf2a3..fe988ac 100644
--- a/java/src/com/android/inputmethod/dictionarypack/DictionaryService.java
+++ b/java/src/com/android/inputmethod/dictionarypack/DictionaryService.java
@@ -192,27 +192,22 @@
     }
 
     static void dispatchBroadcast(final Context context, final Intent intent) {
-        if (DATE_CHANGED_INTENT_ACTION.equals(intent.getAction())) {
-            // Do not force download dictionaries on date change updates.
-            CommonPreferences.setForceDownloadDict(context, false);
+        final String action = intent.getAction();
+        if (DATE_CHANGED_INTENT_ACTION.equals(action)) {
             // This happens when the date of the device changes. This normally happens
             // at midnight local time, but it may happen if the user changes the date
             // by hand or something similar happens.
             checkTimeAndMaybeSetupUpdateAlarm(context);
-        } else if (DictionaryPackConstants.UPDATE_NOW_INTENT_ACTION.equals(intent.getAction())) {
+        } else if (DictionaryPackConstants.UPDATE_NOW_INTENT_ACTION.equals(action)) {
             // Intent to trigger an update now.
-            UpdateHandler.tryUpdate(context, CommonPreferences.isForceDownloadDict(context));
-        } else if (DictionaryPackConstants.INIT_AND_UPDATE_NOW_INTENT_ACTION.equals(
-                intent.getAction())) {
-            // Enable force download of dictionaries irrespective of wifi or metered connection.
-            CommonPreferences.setForceDownloadDict(context, true);
-
+            UpdateHandler.tryUpdate(context);
+        } else if (DictionaryPackConstants.INIT_AND_UPDATE_NOW_INTENT_ACTION.equals(action)) {
             // Initialize the client Db.
             final String mClientId = context.getString(R.string.dictionary_pack_client_id);
             BinaryDictionaryFileDumper.initializeClientRecordHelper(context, mClientId);
 
             // Updates the metadata and the download the dictionaries.
-            UpdateHandler.tryUpdate(context, true);
+            UpdateHandler.tryUpdate(context);
         } else {
             UpdateHandler.downloadFinished(context, intent);
         }
@@ -263,7 +258,7 @@
      */
     public static void updateNowIfNotUpdatedInAVeryLongTime(final Context context) {
         if (!isLastUpdateAtLeastThisOld(context, VERY_LONG_TIME_MILLIS)) return;
-        UpdateHandler.tryUpdate(context, CommonPreferences.isForceDownloadDict(context));
+        UpdateHandler.tryUpdate(context);
     }
 
     /**
diff --git a/java/src/com/android/inputmethod/dictionarypack/DictionarySettingsFragment.java b/java/src/com/android/inputmethod/dictionarypack/DictionarySettingsFragment.java
index 88ea4e6..35b46a9 100644
--- a/java/src/com/android/inputmethod/dictionarypack/DictionarySettingsFragment.java
+++ b/java/src/com/android/inputmethod/dictionarypack/DictionarySettingsFragment.java
@@ -384,7 +384,7 @@
                 // We call tryUpdate(), which returns whether we could successfully start an update.
                 // If we couldn't, we'll never receive the end callback, so we stop the loading
                 // animation and return to the previous screen.
-                if (!UpdateHandler.tryUpdate(activity, true)) {
+                if (!UpdateHandler.tryUpdate(activity)) {
                     stopLoadingAnimation();
                 }
             }
diff --git a/java/src/com/android/inputmethod/dictionarypack/MetadataDbHelper.java b/java/src/com/android/inputmethod/dictionarypack/MetadataDbHelper.java
index 003929d..78d13eb 100644
--- a/java/src/com/android/inputmethod/dictionarypack/MetadataDbHelper.java
+++ b/java/src/com/android/inputmethod/dictionarypack/MetadataDbHelper.java
@@ -266,8 +266,6 @@
      */
     @Override
     public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
-        // Allow automatic download of dictionaries on upgrading the database.
-        CommonPreferences.setForceDownloadDict(mContext, true);
         if (METADATA_DATABASE_INITIAL_VERSION == oldVersion
                 && METADATA_DATABASE_VERSION_WITH_CLIENTID <= newVersion
                 && CURRENT_METADATA_DATABASE_VERSION >= newVersion) {
diff --git a/java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java b/java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java
index f30f248..d1e3c91 100644
--- a/java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java
+++ b/java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java
@@ -36,7 +36,6 @@
 import android.util.Log;
 
 import com.android.inputmethod.compat.ConnectivityManagerCompatUtils;
-import com.android.inputmethod.compat.DownloadManagerCompatUtils;
 import com.android.inputmethod.compat.NotificationCompatUtils;
 import com.android.inputmethod.latin.R;
 import com.android.inputmethod.latin.common.LocaleUtils;
@@ -106,9 +105,9 @@
      * This is chiefly used by the dictionary manager UI.
      */
     public interface UpdateEventListener {
-        public void downloadedMetadata(boolean succeeded);
-        public void wordListDownloadFinished(String wordListId, boolean succeeded);
-        public void updateCycleCompleted();
+        void downloadedMetadata(boolean succeeded);
+        void wordListDownloadFinished(String wordListId, boolean succeeded);
+        void updateCycleCompleted();
     }
 
     /**
@@ -179,10 +178,9 @@
     /**
      * 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 boolean tryUpdate(final Context context, final boolean updateNow) {
+    public static boolean tryUpdate(final Context context) {
         // TODO: loop through all clients instead of only doing the default one.
         final TreeSet<String> uris = new TreeSet<>();
         final Cursor cursor = MetadataDbHelper.queryClientIds(context);
@@ -208,7 +206,7 @@
                 // it should have been rejected at the time of client registration; if there
                 // 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);
+                updateClientsWithMetadataUri(context, metadataUri);
                 started = true;
             }
         }
@@ -219,11 +217,10 @@
      * Download latest metadata from the server through DownloadManager for all relevant clients
      *
      * @param context The context for retrieving resources
-     * @param updateNow Whether we should update NOW, or respect bandwidth policies
      * @param metadataUri The client to update
      */
-    private static void updateClientsWithMetadataUri(final Context context,
-            final boolean updateNow, final String metadataUri) {
+    private static void updateClientsWithMetadataUri(
+            final Context context, final String metadataUri) {
         PrivateLog.log("Update for metadata URI " + DebugLogUtils.s(metadataUri));
         // Adding a disambiguator to circumvent a bug in older versions of DownloadManager.
         // DownloadManager also stupidly cuts the extension to replace with its own that it
@@ -234,19 +231,7 @@
         DebugLogUtils.l("Request =", metadataRequest);
 
         final Resources res = context.getResources();
-        // By default, download over roaming is allowed and all network types are allowed too.
-        if (!updateNow) {
-            final boolean allowedOverMetered = res.getBoolean(R.bool.allow_over_metered);
-            // If we don't have to update NOW, then only do it over non-metered connections.
-            if (DownloadManagerCompatUtils.hasSetAllowedOverMetered()) {
-                DownloadManagerCompatUtils.setAllowedOverMetered(metadataRequest,
-                        allowedOverMetered);
-            } else if (!allowedOverMetered) {
-                metadataRequest.setAllowedNetworkTypes(Request.NETWORK_WIFI);
-            }
-            metadataRequest.setAllowedOverRoaming(res.getBoolean(R.bool.allow_over_roaming));
-        }
-
+        metadataRequest.setAllowedNetworkTypes(Request.NETWORK_WIFI | Request.NETWORK_MOBILE);
         metadataRequest.setTitle(res.getString(R.string.download_description));
         // Do not show the notification when downloading the metadata.
         metadataRequest.setNotificationVisibility(Request.VISIBILITY_HIDDEN);
@@ -448,8 +433,6 @@
         // download, so we are pretty sure it's alive. It's theoretically possible that it's
         // disabled right inbetween the firing of the intent and the control reaching here.
 
-        boolean dictionaryDownloaded = false;
-
         for (final DownloadRecord record : recordList) {
             // downloadSuccessful is not final because we may still have exceptions from now on
             boolean downloadSuccessful = false;
@@ -464,15 +447,9 @@
                     final SQLiteDatabase db = MetadataDbHelper.getDb(context, record.mClientId);
                     publishUpdateWordListCompleted(context, downloadSuccessful, fileId,
                             db, record.mAttributes, record.mClientId);
-                    dictionaryDownloaded = true;
                 }
             }
         }
-
-        if (dictionaryDownloaded) {
-            // Disable the force download after downloading the dictionaries.
-            CommonPreferences.setForceDownloadDict(context, false);
-        }
         // Now that we're done using it, we can remove this download from DLManager
         manager.remove(fileId);
     }
@@ -829,8 +806,7 @@
                     actions.add(new ActionBatch.MakeAvailableAction(clientId, newInfo));
                     if (status == MetadataDbHelper.STATUS_INSTALLED
                             || status == MetadataDbHelper.STATUS_DISABLED) {
-                        actions.add(new ActionBatch.StartDownloadAction(
-                                clientId, newInfo, CommonPreferences.isForceDownloadDict(context)));
+                        actions.add(new ActionBatch.StartDownloadAction(clientId, newInfo));
                     } else {
                         // Pass true to ForgetAction: this is indeed an update to a non-installed
                         // word list, so activate status == AVAILABLE check
@@ -984,9 +960,7 @@
         // auto-installed once to get auto-installed again, and that's what we want.
         final ActionBatch actions = new ActionBatch();
         actions.add(new ActionBatch.StartDownloadAction(
-                clientId,
-                WordListMetadata.createFromContentValues(installCandidate),
-                CommonPreferences.isForceDownloadDict(context)));
+                clientId, WordListMetadata.createFromContentValues(installCandidate)));
         final String localeString = installCandidate.getAsString(MetadataDbHelper.LOCALE_COLUMN);
         // We are in a content provider: we can't do any UI at all. We have to defer the displaying
         // itself to the service. Also, we only display this when the user does not have a
@@ -1032,9 +1006,7 @@
                 || MetadataDbHelper.STATUS_DELETING == status) {
             actions.add(new ActionBatch.EnableAction(clientId, wordListMetaData));
         } else if (MetadataDbHelper.STATUS_AVAILABLE == status) {
-            boolean forceDownloadDict = CommonPreferences.isForceDownloadDict(context);
-            actions.add(new ActionBatch.StartDownloadAction(clientId, wordListMetaData,
-                    forceDownloadDict || allowDownloadOnMeteredData));
+            actions.add(new ActionBatch.StartDownloadAction(clientId, wordListMetaData));
         } else {
             Log.e(TAG, "Unexpected state of the word list for markAsUsed : " + status);
         }
@@ -1149,8 +1121,7 @@
             }
 
             final ActionBatch actions = new ActionBatch();
-            actions.add(new ActionBatch.StartDownloadAction(
-                    clientId, wordListMetaData, CommonPreferences.isForceDownloadDict(context)));
+            actions.add(new ActionBatch.StartDownloadAction(clientId, wordListMetaData));
             actions.execute(context, new LogProblemReporter(TAG));
         } else {
             if (DEBUG) {