Initial conversion of SearchBaseUrlHelper.java to SearchBaseUrlHelper.kt in AOSP QuickSearchBox App using automatic conversion tool.

Test: built against Kotlin 1.7, did not compile successfully but still commiting to showcase git history of conversion
Change-Id: I9dc49d9da65a74f09c9d3a57cb730d2ef4a3f3ea
diff --git a/src/com/android/quicksearchbox/google/SearchBaseUrlHelper.kt b/src/com/android/quicksearchbox/google/SearchBaseUrlHelper.kt
index d95214f..19f8b09 100644
--- a/src/com/android/quicksearchbox/google/SearchBaseUrlHelper.kt
+++ b/src/com/android/quicksearchbox/google/SearchBaseUrlHelper.kt
@@ -13,55 +13,20 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+package com.android.quicksearchbox.google
 
-package com.android.quicksearchbox.google;
-
-import com.android.quicksearchbox.R;
-import com.android.quicksearchbox.SearchSettings;
-import com.android.quicksearchbox.SearchSettingsImpl;
-import com.android.quicksearchbox.util.HttpHelper;
-
-import android.content.Context;
-import android.content.SharedPreferences;
-import android.os.AsyncTask;
-import android.text.TextUtils;
-import android.util.Log;
-
-import java.util.Locale;
+import com.android.quicksearchbox.R
 
 /**
  * Helper to build the base URL for all search requests.
  */
-public class SearchBaseUrlHelper implements SharedPreferences.OnSharedPreferenceChangeListener {
-    private static final boolean DBG = false;
-    private static final String TAG = "QSB.SearchBaseUrlHelper";
-
-    private static final String DOMAIN_CHECK_URL =
-            "https://www.google.com/searchdomaincheck?format=domain";
-
-    private static final long SEARCH_BASE_URL_EXPIRY_MS = 24 * 3600 * 1000L;
-
-    private final HttpHelper mHttpHelper;
-    private final Context mContext;
-    private final SearchSettings mSearchSettings;
-
-    /**
-     * Note that this constructor will spawn a thread to issue a HTTP
-     * request if shouldUseGoogleCom is false.
-     */
-    public SearchBaseUrlHelper(Context context, HttpHelper helper,
-            SearchSettings searchSettings, SharedPreferences prefs) {
-        mHttpHelper = helper;
-        mContext = context;
-        mSearchSettings = searchSettings;
-
-        // Note: This earlier used an inner class, but that causes issues
-        // because SharedPreferencesImpl uses a WeakHashMap< > and the listener
-        // will be GC'ed unless we keep a reference to it here.
-        prefs.registerOnSharedPreferenceChangeListener(this);
-
-        maybeUpdateBaseUrlSetting(false);
-    }
+class SearchBaseUrlHelper(
+    context: Context, helper: HttpHelper,
+    searchSettings: SearchSettings, prefs: SharedPreferences
+) : SharedPreferences.OnSharedPreferenceChangeListener {
+    private val mHttpHelper: HttpHelper
+    private val mContext: Context
+    private val mSearchSettings: SearchSettings
 
     /**
      * Update the base search url, either:
@@ -70,18 +35,16 @@
      * (c) if the caller forces an update by setting the "force" parameter.
      *
      * @param force if true, then the URL is reset whether or not it has
-     *     expired.
+     * expired.
      */
-    public void maybeUpdateBaseUrlSetting(boolean force) {
-        long lastUpdateTime = mSearchSettings.getSearchBaseDomainApplyTime();
-        long currentTime = System.currentTimeMillis();
-
-        if (force || lastUpdateTime == -1 ||
-                currentTime - lastUpdateTime >= SEARCH_BASE_URL_EXPIRY_MS) {
+    fun maybeUpdateBaseUrlSetting(force: Boolean) {
+        val lastUpdateTime: Long = mSearchSettings.getSearchBaseDomainApplyTime()
+        val currentTime: Long = System.currentTimeMillis()
+        if (force || lastUpdateTime == -1L || currentTime - lastUpdateTime >= SearchBaseUrlHelper.Companion.SEARCH_BASE_URL_EXPIRY_MS) {
             if (mSearchSettings.shouldUseGoogleCom()) {
-                setSearchBaseDomain(getDefaultBaseDomain());
+                setSearchBaseDomain(defaultBaseDomain)
             } else {
-                checkSearchDomain();
+                checkSearchDomain()
             }
         }
     }
@@ -89,88 +52,132 @@
     /**
      * @return the base url for searches.
      */
-    public String getSearchBaseUrl() {
-        return mContext.getResources().getString(R.string.google_search_base_pattern,
-                getSearchDomain(), GoogleSearch.getLanguage(Locale.getDefault()));
-    }
-
+    val searchBaseUrl: String
+        get() = mContext.getResources().getString(
+            R.string.google_search_base_pattern,
+            searchDomain, GoogleSearch.getLanguage(Locale.getDefault())
+        )// This is required to deal with the case wherein getSearchDomain
+    // is called before checkSearchDomain returns a valid URL. This will
+    // happen *only* on the first run of the app when the "use google.com"
+    // option is unchecked. In other cases, the previously set domain (or
+    // the default) will be returned.
+    //
+    // We have no choice in this case but to use the default search domain.
     /**
      * @return the search domain. This is of the form "google.co.xx" or "google.com",
-     *     used by UI code.
+     * used by UI code.
      */
-    public String getSearchDomain() {
-        String domain = mSearchSettings.getSearchBaseDomain();
+    val searchDomain: String?
+        get() {
+            var domain: String = mSearchSettings.getSearchBaseDomain()
+            if (domain == null) {
+                if (SearchBaseUrlHelper.Companion.DBG) {
+                    Log.w(
+                        SearchBaseUrlHelper.Companion.TAG,
+                        "Search base domain was null, last apply time=" +
+                                mSearchSettings.getSearchBaseDomainApplyTime()
+                    )
+                }
 
-        if (domain == null) {
-            if (DBG) {
-                Log.w(TAG, "Search base domain was null, last apply time=" +
-                        mSearchSettings.getSearchBaseDomainApplyTime());
+                // This is required to deal with the case wherein getSearchDomain
+                // is called before checkSearchDomain returns a valid URL. This will
+                // happen *only* on the first run of the app when the "use google.com"
+                // option is unchecked. In other cases, the previously set domain (or
+                // the default) will be returned.
+                //
+                // We have no choice in this case but to use the default search domain.
+                domain = defaultBaseDomain
             }
-
-            // This is required to deal with the case wherein getSearchDomain
-            // is called before checkSearchDomain returns a valid URL. This will
-            // happen *only* on the first run of the app when the "use google.com"
-            // option is unchecked. In other cases, the previously set domain (or
-            // the default) will be returned.
-            //
-            // We have no choice in this case but to use the default search domain.
-            domain = getDefaultBaseDomain();
+            if (domain.startsWith(".")) {
+                if (SearchBaseUrlHelper.Companion.DBG) Log.d(
+                    SearchBaseUrlHelper.Companion.TAG,
+                    "Prepending www to $domain"
+                )
+                domain = "www$domain"
+            }
+            return domain
         }
 
-        if (domain.startsWith(".")) {
-            if (DBG) Log.d(TAG, "Prepending www to " + domain);
-            domain = "www" + domain;
-        }
-        return domain;
-    }
-
     /**
      * Issue a request to google.com/searchdomaincheck to retrieve the base
      * URL for search requests.
      */
-    private void checkSearchDomain() {
-        final HttpHelper.GetRequest request = new HttpHelper.GetRequest(DOMAIN_CHECK_URL);
-
-        new AsyncTask<Void, Void, Void>() {
+    private fun checkSearchDomain() {
+        val request = GetRequest(SearchBaseUrlHelper.Companion.DOMAIN_CHECK_URL)
+        object : AsyncTask<Void?, Void?, Void?>() {
             @Override
-            protected Void doInBackground(Void ... params) {
-                if (DBG) Log.d(TAG, "Starting request to /searchdomaincheck");
-                String domain;
+            protected fun doInBackground(vararg params: Void?): Void? {
+                if (SearchBaseUrlHelper.Companion.DBG) Log.d(
+                    SearchBaseUrlHelper.Companion.TAG,
+                    "Starting request to /searchdomaincheck"
+                )
+                var domain: String
                 try {
-                    domain = mHttpHelper.get(request);
-                } catch (Exception e) {
-                    if (DBG) Log.d(TAG, "Request to /searchdomaincheck failed : " + e);
+                    domain = mHttpHelper.get(request)
+                } catch (e: Exception) {
+                    if (SearchBaseUrlHelper.Companion.DBG) Log.d(
+                        SearchBaseUrlHelper.Companion.TAG,
+                        "Request to /searchdomaincheck failed : $e"
+                    )
                     // Swallow any exceptions thrown by the HTTP helper, in
                     // this rare case, we just use the default URL.
-                    domain = getDefaultBaseDomain();
-
-                    return null;
+                    domain = defaultBaseDomain
+                    return null
                 }
-
-                if (DBG) Log.d(TAG, "Request to /searchdomaincheck succeeded");
-                setSearchBaseDomain(domain);
-
-                return null;
+                if (SearchBaseUrlHelper.Companion.DBG) Log.d(
+                    SearchBaseUrlHelper.Companion.TAG,
+                    "Request to /searchdomaincheck succeeded"
+                )
+                setSearchBaseDomain(domain)
+                return null
             }
-        }.execute();
+        }.execute()
     }
 
-    private String getDefaultBaseDomain() {
-        return mContext.getResources().getString(R.string.default_search_domain);
-    }
+    private val defaultBaseDomain: String
+        private get() = mContext.getResources().getString(R.string.default_search_domain)
 
-    private void setSearchBaseDomain(String domain) {
-        if (DBG) Log.d(TAG, "Setting search domain to : " + domain);
-
-        mSearchSettings.setSearchBaseDomain(domain);
+    private fun setSearchBaseDomain(domain: String) {
+        if (SearchBaseUrlHelper.Companion.DBG) Log.d(
+            SearchBaseUrlHelper.Companion.TAG,
+            "Setting search domain to : $domain"
+        )
+        mSearchSettings.setSearchBaseDomain(domain)
     }
 
     @Override
-    public void onSharedPreferenceChanged(SharedPreferences pref, String key) {
+    fun onSharedPreferenceChanged(pref: SharedPreferences?, key: String) {
         // Listen for changes only to the SEARCH_BASE_URL preference.
-        if (DBG) Log.d(TAG, "Handling changed preference : " + key);
+        if (SearchBaseUrlHelper.Companion.DBG) Log.d(
+            SearchBaseUrlHelper.Companion.TAG,
+            "Handling changed preference : $key"
+        )
         if (SearchSettingsImpl.USE_GOOGLE_COM_PREF.equals(key)) {
-            maybeUpdateBaseUrlSetting(true);
+            maybeUpdateBaseUrlSetting(true)
         }
     }
+
+    companion object {
+        private const val DBG = false
+        private const val TAG = "QSB.SearchBaseUrlHelper"
+        private const val DOMAIN_CHECK_URL =
+            "https://www.google.com/searchdomaincheck?format=domain"
+        private const val SEARCH_BASE_URL_EXPIRY_MS = 24 * 3600 * 1000L
+    }
+
+    /**
+     * Note that this constructor will spawn a thread to issue a HTTP
+     * request if shouldUseGoogleCom is false.
+     */
+    init {
+        mHttpHelper = helper
+        mContext = context
+        mSearchSettings = searchSettings
+
+        // Note: This earlier used an inner class, but that causes issues
+        // because SharedPreferencesImpl uses a WeakHashMap< > and the listener
+        // will be GC'ed unless we keep a reference to it here.
+        prefs.registerOnSharedPreferenceChangeListener(this)
+        maybeUpdateBaseUrlSetting(false)
+    }
 }
\ No newline at end of file