Snap for 10453563 from 5859b263ee189e2fc7153fa9d8ef550a698637b5 to mainline-permission-release

Change-Id: I9e1ff9ddd0799d548017441faf445b5d5af0417a
diff --git a/java/AndroidManifest.xml b/java/AndroidManifest.xml
index ac84c33..633c68b 100644
--- a/java/AndroidManifest.xml
+++ b/java/AndroidManifest.xml
@@ -54,6 +54,13 @@
     <permission android:name="com.android.inputmethod.latin.HIDE_SOFT_INPUT"
          android:protectionLevel="signature"/>
 
+    <!-- To query enabled input methods. -->
+    <queries>
+        <intent>
+            <action android:name="android.view.InputMethod" />
+        </intent>
+    </queries>
+
     <application android:label="@string/english_ime_name"
          android:icon="@drawable/ic_launcher_keyboard"
          android:supportsRtl="true"
@@ -65,7 +72,8 @@
         <service android:name="LatinIME"
              android:label="@string/english_ime_name"
              android:permission="android.permission.BIND_INPUT_METHOD"
-             android:exported="true">
+             android:exported="true"
+             android:visibleToInstantApps="true">
             <intent-filter>
                 <action android:name="android.view.InputMethod"/>
             </intent-filter>
@@ -149,7 +157,7 @@
         <activity android:name="com.android.inputmethod.dictionarypack.DownloadOverMeteredDialog"
              android:theme="@style/platformActivityTheme"
              android:label="@string/dictionary_install_over_metered_network_prompt"
-             android:exported="true">
+             android:exported="false">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN"/>
             </intent-filter>
diff --git a/java/lint-baseline.xml b/java/lint-baseline.xml
index 02f41f4..dbb81ca 100644
--- a/java/lint-baseline.xml
+++ b/java/lint-baseline.xml
@@ -26,50 +26,6 @@
     <issue
         id="NewApi"
         message="Call requires API level 23 (current min is 21): `android.content.Context#getSystemService`"
-        errorLine1="        final WindowManager wm = getSystemService(WindowManager.class);"
-        errorLine2="                                 ~~~~~~~~~~~~~~~~">
-        <location
-            file="packages/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/LatinIME.java"
-            line="606"
-            column="34"/>
-    </issue>
-
-    <issue
-        id="NewApi"
-        message="Call requires API level 23 (current min is 21): `android.content.Context#getSystemService`"
-        errorLine1="        final WindowManager wm = getSystemService(WindowManager.class);"
-        errorLine2="                                 ~~~~~~~~~~~~~~~~">
-        <location
-            file="packages/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/LatinIME.java"
-            line="804"
-            column="34"/>
-    </issue>
-
-    <issue
-        id="NewApi"
-        message="Call requires API level 23 (current min is 21): `android.app.ActivityOptions#makeBasic`"
-        errorLine1="                ActivityOptions.makeBasic().setLaunchDisplayId(currentDisplayId).toBundle());"
-        errorLine2="                                ~~~~~~~~~">
-        <location
-            file="packages/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/LatinIME.java"
-            line="1823"
-            column="33"/>
-    </issue>
-
-    <issue
-        id="NewApi"
-        message="Call requires API level 26 (current min is 21): `android.app.ActivityOptions#setLaunchDisplayId`"
-        errorLine1="                ActivityOptions.makeBasic().setLaunchDisplayId(currentDisplayId).toBundle());"
-        errorLine2="                                            ~~~~~~~~~~~~~~~~~~">
-        <location
-            file="packages/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/LatinIME.java"
-            line="1823"
-            column="45"/>
-    </issue>
-
-    <issue
-        id="NewApi"
-        message="Call requires API level 23 (current min is 21): `android.content.Context#getSystemService`"
         errorLine1="        final UserManager userManager = context.getSystemService(UserManager.class);"
         errorLine2="                                                ~~~~~~~~~~~~~~~~">
         <location
@@ -78,4 +34,28 @@
             column="49"/>
     </issue>
 
-</issues>
+    <issue
+        id="NewApi"
+        message="Call requires API level 23 (current min is 21): `android.app.ActivityOptions#makeBasic`">
+        <location
+            file="packages/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/LatinIME.java"
+            line="1842"/>
+    </issue>
+
+    <issue
+        id="NewApi"
+        message="Call requires API level 23 (current min is 21): `android.content.Context#getSystemService`">
+        <location
+            file="packages/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/LatinIME.java"
+            line="605"/>
+    </issue>
+
+    <issue
+        id="NewApi"
+        message="Call requires API level 26 (current min is 21): `android.app.ActivityOptions#setLaunchDisplayId`">
+        <location
+            file="packages/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/LatinIME.java"
+            line="1842"/>
+    </issue>
+
+</issues>
\ No newline at end of file
diff --git a/java/src/com/android/inputmethod/dictionarypack/DownloadManagerWrapper.java b/java/src/com/android/inputmethod/dictionarypack/DownloadManagerWrapper.java
index 6f6b026..e2e9a7e 100644
--- a/java/src/com/android/inputmethod/dictionarypack/DownloadManagerWrapper.java
+++ b/java/src/com/android/inputmethod/dictionarypack/DownloadManagerWrapper.java
@@ -26,6 +26,7 @@
 import android.util.Log;
 
 import java.io.FileNotFoundException;
+import java.util.Arrays;
 
 import javax.annotation.Nullable;
 
@@ -62,7 +63,8 @@
             // We couldn't remove the file from DownloadManager. Apparently, the database can't
             // be opened. It may be a problem with file system corruption. In any case, there is
             // not much we can do apart from avoiding crashing.
-            Log.e(TAG, "Can't remove files with ID " + ids + " from download manager", e);
+            Log.e(TAG, "Can't remove files with ID " + Arrays.toString(ids) +
+                    " from download manager", e);
         }
     }
 
diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
index 9764cb3..c0ac1c0 100644
--- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java
+++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
@@ -281,7 +281,7 @@
         if (DEBUG_LISTENER) {
             final String output = code == Constants.CODE_OUTPUT_TEXT
                     ? key.getOutputText() : Constants.printableCode(code);
-            Log.d(TAG, String.format("[%d] onCodeInput: %4d %4d %s%s%s", mPointerId, x, y,
+            Log.d(TAG, String.format("[%d] onCodeInput: %4d %4d %s%s%s%s", mPointerId, x, y,
                     output, ignoreModifierKey ? " ignoreModifier" : "",
                     altersCode ? " altersCode" : "", key.isEnabled() ? "" : " disabled"));
         }
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 8ed3a59..e68b43b 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -628,16 +628,31 @@
 
         final IntentFilter newDictFilter = new IntentFilter();
         newDictFilter.addAction(DictionaryPackConstants.NEW_DICTIONARY_INTENT_ACTION);
-        registerReceiver(mDictionaryPackInstallReceiver, newDictFilter);
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+            registerReceiver(mDictionaryPackInstallReceiver, newDictFilter,
+                    Context.RECEIVER_NOT_EXPORTED);
+        } else {
+            registerReceiver(mDictionaryPackInstallReceiver, newDictFilter);
+        }
 
         final IntentFilter dictDumpFilter = new IntentFilter();
         dictDumpFilter.addAction(DictionaryDumpBroadcastReceiver.DICTIONARY_DUMP_INTENT_ACTION);
-        registerReceiver(mDictionaryDumpBroadcastReceiver, dictDumpFilter);
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+            registerReceiver(mDictionaryDumpBroadcastReceiver, dictDumpFilter,
+                    Context.RECEIVER_NOT_EXPORTED);
+        } else {
+            registerReceiver(mDictionaryDumpBroadcastReceiver, dictDumpFilter);
+        }
 
         final IntentFilter hideSoftInputFilter = new IntentFilter();
         hideSoftInputFilter.addAction(ACTION_HIDE_SOFT_INPUT);
-        registerReceiver(mHideSoftInputReceiver, hideSoftInputFilter, PERMISSION_HIDE_SOFT_INPUT,
-                null /* scheduler */);
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+            registerReceiver(mHideSoftInputReceiver, hideSoftInputFilter,
+                    PERMISSION_HIDE_SOFT_INPUT, null /* scheduler */, Context.RECEIVER_EXPORTED);
+        } else {
+            registerReceiver(mHideSoftInputReceiver, hideSoftInputFilter,
+                    PERMISSION_HIDE_SOFT_INPUT, null /* scheduler */);
+        }
 
         StatsUtils.onCreate(mSettings.getCurrent(), mRichImm);
     }
diff --git a/java/src/com/android/inputmethod/latin/utils/CursorAnchorInfoUtils.java b/java/src/com/android/inputmethod/latin/utils/CursorAnchorInfoUtils.java
index c90d30c..41090c0 100644
--- a/java/src/com/android/inputmethod/latin/utils/CursorAnchorInfoUtils.java
+++ b/java/src/com/android/inputmethod/latin/utils/CursorAnchorInfoUtils.java
@@ -216,7 +216,7 @@
                         if (isTopLeftVisible || isBottomRightVisible) {
                             characterBoundsFlags |= CursorAnchorInfo.FLAG_HAS_VISIBLE_REGION;
                         }
-                        if (!isTopLeftVisible || !isTopLeftVisible) {
+                        if (!isTopLeftVisible || !isBottomRightVisible) {
                             characterBoundsFlags |= CursorAnchorInfo.FLAG_HAS_INVISIBLE_REGION;
                         }
                         if (isRtl) {
diff --git a/native/jni/src/dictionary/structure/backward/v402/content/terminal_position_lookup_table.cpp b/native/jni/src/dictionary/structure/backward/v402/content/terminal_position_lookup_table.cpp
index 30b72bb..317710c 100644
--- a/native/jni/src/dictionary/structure/backward/v402/content/terminal_position_lookup_table.cpp
+++ b/native/jni/src/dictionary/structure/backward/v402/content/terminal_position_lookup_table.cpp
@@ -82,14 +82,12 @@
 }
 
 bool TerminalPositionLookupTable::runGCTerminalIds(TerminalIdMap *const terminalIdMap) {
-    int removedEntryCount = 0;
     int nextNewTerminalId = 0;
     for (int i = 0; i < mSize; ++i) {
         const int terminalPos = getBuffer()->readUint(
                 Ver4DictConstants::TERMINAL_ADDRESS_TABLE_ADDRESS_SIZE, getEntryPos(i));
         if (terminalPos == Ver4DictConstants::NOT_A_TERMINAL_ADDRESS) {
             // This entry is a garbage.
-            removedEntryCount++;
         } else {
             // Give a new terminal id to the entry.
             if (!getWritableBuffer()->writeUint(terminalPos,
diff --git a/native/jni/src/dictionary/structure/v4/content/terminal_position_lookup_table.cpp b/native/jni/src/dictionary/structure/v4/content/terminal_position_lookup_table.cpp
index 5503151..e4d7c2d 100644
--- a/native/jni/src/dictionary/structure/v4/content/terminal_position_lookup_table.cpp
+++ b/native/jni/src/dictionary/structure/v4/content/terminal_position_lookup_table.cpp
@@ -71,14 +71,12 @@
 }
 
 bool TerminalPositionLookupTable::runGCTerminalIds(TerminalIdMap *const terminalIdMap) {
-    int removedEntryCount = 0;
     int nextNewTerminalId = 0;
     for (int i = 0; i < mSize; ++i) {
         const int terminalPos = getBuffer()->readUint(
                 Ver4DictConstants::TERMINAL_ADDRESS_TABLE_ADDRESS_SIZE, getEntryPos(i));
         if (terminalPos == Ver4DictConstants::NOT_A_TERMINAL_ADDRESS) {
             // This entry is a garbage.
-            removedEntryCount++;
         } else {
             // Give a new terminal id to the entry.
             if (!getWritableBuffer()->writeUint(terminalPos,
diff --git a/native/jni/src/dictionary/utils/trie_map.cpp b/native/jni/src/dictionary/utils/trie_map.cpp
index 0bef8c7..19ad9c5 100644
--- a/native/jni/src/dictionary/utils/trie_map.cpp
+++ b/native/jni/src/dictionary/utils/trie_map.cpp
@@ -53,7 +53,7 @@
     for (int i = from; i < to; ++i) {
         AKLOGI("Entry[%d]: %x, %x", i, readField0(i), readField1(i));
     }
-    int unusedRegionSize = 0;
+    [[maybe_unused]] int unusedRegionSize = 0;
     for (int i = 1; i <= MAX_NUM_OF_ENTRIES_IN_ONE_LEVEL; ++i) {
         int index = readEmptyTableLink(i);
         while (index != ROOT_BITMAP_ENTRY_INDEX) {
diff --git a/native/jni/src/suggest/core/layout/proximity_info_state_utils.cpp b/native/jni/src/suggest/core/layout/proximity_info_state_utils.cpp
index 0aeb36a..0aa6caf 100644
--- a/native/jni/src/suggest/core/layout/proximity_info_state_utils.cpp
+++ b/native/jni/src/suggest/core/layout/proximity_info_state_utils.cpp
@@ -490,12 +490,10 @@
     const int x0 = (*sampledInputXs)[id];
     const int y0 = (*sampledInputYs)[id];
     const int actualInputIndex = (*sampledInputIndices)[id];
-    int tempTime = 0;
     int tempBeelineDistance = 0;
     int start = actualInputIndex;
     // lookup forward
     while (start > 0 && tempBeelineDistance < lookupRadius) {
-        tempTime += times[start] - times[start - 1];
         --start;
         tempBeelineDistance = GeometryUtils::getDistanceInt(x0, y0, xCoordinates[start],
                 yCoordinates[start]);
@@ -504,12 +502,10 @@
     if (start > 0 && start < actualInputIndex) {
         ++start;
     }
-    tempTime= 0;
     tempBeelineDistance = 0;
     int end = actualInputIndex;
     // lookup backward
     while (end < (inputSize - 1) && tempBeelineDistance < lookupRadius) {
-        tempTime += times[end + 1] - times[end];
         ++end;
         tempBeelineDistance = GeometryUtils::getDistanceInt(x0, y0, xCoordinates[end],
                 yCoordinates[end]);
diff --git a/native/jni/src/suggest/core/layout/proximity_info_utils.h b/native/jni/src/suggest/core/layout/proximity_info_utils.h
index 79d0615..0037961 100644
--- a/native/jni/src/suggest/core/layout/proximity_info_utils.h
+++ b/native/jni/src/suggest/core/layout/proximity_info_utils.h
@@ -72,7 +72,7 @@
             for (int i = 0; i < inputSize; ++i) {
                 AKLOGI("---");
                 for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE; ++j) {
-                    int proximityChar =
+                    [[maybe_unused]] int proximityChar =
                             inputProximities[i * MAX_PROXIMITY_CHARS_SIZE + j];
                     proximityChar += 0;
                     AKLOGI("--- (%d)%c", i, proximityChar);
diff --git a/native/jni/src/suggest/core/result/suggestion_results.cpp b/native/jni/src/suggest/core/result/suggestion_results.cpp
index 3756d10..312c9e7 100644
--- a/native/jni/src/suggest/core/result/suggestion_results.cpp
+++ b/native/jni/src/suggest/core/result/suggestion_results.cpp
@@ -97,7 +97,7 @@
         suggestedWords.push_back(copyOfSuggestedWords.top());
         copyOfSuggestedWords.pop();
     }
-    int index = 0;
+    [[maybe_unused]] int index = 0;
     for (auto it = suggestedWords.rbegin(); it != suggestedWords.rend(); ++it) {
         DUMP_SUGGESTION(it->getCodePoint(), it->getCodePointCount(), index, it->getScore());
         index++;
diff --git a/tests/src/com/android/inputmethod/keyboard/layout/expected/ExpectedKeyOutput.java b/tests/src/com/android/inputmethod/keyboard/layout/expected/ExpectedKeyOutput.java
index e7b0f09..e2b98bc 100644
--- a/tests/src/com/android/inputmethod/keyboard/layout/expected/ExpectedKeyOutput.java
+++ b/tests/src/com/android/inputmethod/keyboard/layout/expected/ExpectedKeyOutput.java
@@ -131,7 +131,7 @@
 
         @Override
         boolean hasSameKeyOutput(final String text) {
-            return text.equals(text);
+            return mText.equals(text);
         }
 
         @Override
diff --git a/tests/src/com/android/inputmethod/latin/makedict/Ver2DictEncoder.java b/tests/src/com/android/inputmethod/latin/makedict/Ver2DictEncoder.java
index c63b972..b5694ca 100644
--- a/tests/src/com/android/inputmethod/latin/makedict/Ver2DictEncoder.java
+++ b/tests/src/com/android/inputmethod/latin/makedict/Ver2DictEncoder.java
@@ -34,6 +34,7 @@
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map.Entry;
+import java.util.Objects;
 
 /**
  * An implementation of DictEncoder for version 2 binary dictionary.
@@ -100,7 +101,7 @@
         Collections.sort(codePointOccurrenceArray, new Comparator<Entry<Integer, Integer>>() {
             @Override
             public int compare(final Entry<Integer, Integer> a, final Entry<Integer, Integer> b) {
-                if (a.getValue() != b.getValue()) {
+                if (!Objects.equals(a.getValue(), b.getValue())) {
                     return b.getValue().compareTo(a.getValue());
                 }
                 return b.getKey().compareTo(a.getKey());
diff --git a/tools/dicttool/tests/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtilsTests.java b/tools/dicttool/tests/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtilsTests.java
index 84d36a0..d3ecc91 100644
--- a/tools/dicttool/tests/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtilsTests.java
+++ b/tools/dicttool/tests/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtilsTests.java
@@ -162,7 +162,7 @@
         options.put("locale", "en_US");
         options.put("version", Integer.toString(mRandom.nextInt()));
         // Add some random options for test
-        final int numberOfOptionsToAdd = mRandom.nextInt() % (MAX_NUMBER_OF_OPTIONS_TO_ADD + 1);
+        final int numberOfOptionsToAdd = mRandom.nextInt(MAX_NUMBER_OF_OPTIONS_TO_ADD + 1);
         for (int i = 0; i < numberOfOptionsToAdd; ++i) {
             options.put(sWords.get(2 * i), sWords.get(2 * 1 + 1));
         }
diff --git a/tools/dicttool/tests/com/android/inputmethod/latin/makedict/FusionDictionaryTest.java b/tools/dicttool/tests/com/android/inputmethod/latin/makedict/FusionDictionaryTest.java
index 6260239..901d0ce 100644
--- a/tools/dicttool/tests/com/android/inputmethod/latin/makedict/FusionDictionaryTest.java
+++ b/tools/dicttool/tests/com/android/inputmethod/latin/makedict/FusionDictionaryTest.java
@@ -47,7 +47,7 @@
      */
     private String generateWord(final Random random) {
         StringBuilder builder = new StringBuilder("a");
-        int count = random.nextInt() % 30;
+        int count = random.nextInt(30);
         while (count > 0) {
             final long r = Math.abs(random.nextInt());
             if (r < 0) continue;