Add show setup wizard icon preference settings

Bug: 8239067
Change-Id: If6106a3632d3abad3e22ce29f1351525a5152ec7
diff --git a/java/res/values/strings.xml b/java/res/values/strings.xml
index 2a47d7a..68fa8fd 100644
--- a/java/res/values/strings.xml
+++ b/java/res/values/strings.xml
@@ -434,4 +434,6 @@
     <string name="language_settings">Language &amp; input</string>
     <!-- Title of the Input method picker. This should be aligned with msgid="4653387336791222978" -->
     <string name="select_input_method">Choose input method</string>
+    <!-- Option to show setup wizard icon. [CHAR LIMIT=30]-->
+    <string name="show_setup_wizard_icon" translatable="false">Show setup wizard icon</string>
 </resources>
diff --git a/java/res/xml/prefs.xml b/java/res/xml/prefs.xml
index 276395b..e5fef88 100644
--- a/java/res/xml/prefs.xml
+++ b/java/res/xml/prefs.xml
@@ -145,7 +145,7 @@
                 android:fragment="com.android.inputmethod.latin.AdditionalSubtypeSettings"
                 android:key="custom_input_styles"
                 android:title="@string/custom_input_styles_title" />
-            <!-- Values for popup dismiss delay are added programatically -->
+            <!-- Values for popup dismiss delay are added programmatically -->
             <CheckBoxPreference
                 android:key="pref_sliding_key_input_preview"
                 android:title="@string/sliding_key_input_preview"
@@ -171,6 +171,11 @@
                 android:key="pref_keypress_sound_volume"
                 android:title="@string/prefs_keypress_sound_volume_settings"
                 latin:maxValue="100" /> <!-- percent -->
+            <!-- The show setup wizard icon settings shouldn't be persistent and the default value
+                 is added programmatically. -->
+            <CheckBoxPreference
+                android:key="pref_show_setup_wizard_icon"
+                android:title="@string/show_setup_wizard_icon" />
         </PreferenceScreen>
         <PreferenceScreen
             android:key="debug_settings"
diff --git a/java/src/com/android/inputmethod/latin/Settings.java b/java/src/com/android/inputmethod/latin/Settings.java
index 02b44c7..435074b 100644
--- a/java/src/com/android/inputmethod/latin/Settings.java
+++ b/java/src/com/android/inputmethod/latin/Settings.java
@@ -18,6 +18,7 @@
 
 import android.content.Context;
 import android.content.SharedPreferences;
+import android.content.pm.ApplicationInfo;
 import android.content.res.Resources;
 import android.preference.PreferenceManager;
 
@@ -64,6 +65,7 @@
     public static final String PREF_GESTURE_PREVIEW_TRAIL = "pref_gesture_preview_trail";
     public static final String PREF_GESTURE_FLOATING_PREVIEW_TEXT =
             "pref_gesture_floating_preview_text";
+    public static final String PREF_SHOW_SETUP_WIZARD_ICON = "pref_show_setup_wizard_icon";
 
     public static final String PREF_INPUT_LANGUAGE = "input_language";
     public static final String PREF_SELECTED_LANGUAGES = "selected_languages";
@@ -260,4 +262,16 @@
     public static boolean readUseFullscreenMode(final Resources res) {
         return res.getBoolean(R.bool.config_use_fullscreen_mode);
     }
+
+    public static boolean readShowSetupWizardIcon(final SharedPreferences prefs,
+            final Context context) {
+        if (!prefs.contains(Settings.PREF_SHOW_SETUP_WIZARD_ICON)) {
+            final ApplicationInfo appInfo = context.getApplicationInfo();
+            final boolean isApplicationInSystemImage =
+                    (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
+            // Default value
+            return !isApplicationInSystemImage;
+        }
+        return prefs.getBoolean(Settings.PREF_SHOW_SETUP_WIZARD_ICON, false);
+    }
 }
diff --git a/java/src/com/android/inputmethod/latin/SettingsFragment.java b/java/src/com/android/inputmethod/latin/SettingsFragment.java
index edd064c..4c90e48 100644
--- a/java/src/com/android/inputmethod/latin/SettingsFragment.java
+++ b/java/src/com/android/inputmethod/latin/SettingsFragment.java
@@ -31,6 +31,7 @@
 import android.view.inputmethod.InputMethodSubtype;
 
 import com.android.inputmethod.latin.define.ProductionFlag;
+import com.android.inputmethod.latin.setup.LauncherIconVisibilityManager;
 import com.android.inputmethodcommon.InputMethodSettingsFragment;
 
 public final class SettingsFragment extends InputMethodSettingsFragment
@@ -155,6 +156,10 @@
             removePreference(Settings.PREF_GESTURE_SETTINGS, getPreferenceScreen());
         }
 
+        final CheckBoxPreference showSetupWizardIcon =
+                (CheckBoxPreference)findPreference(Settings.PREF_SHOW_SETUP_WIZARD_ICON);
+        showSetupWizardIcon.setChecked(Settings.readShowSetupWizardIcon(prefs, context));
+
         setupKeyLongpressTimeoutSettings(prefs, res);
         setupKeypressVibrationDurationSettings(prefs, res);
         setupKeypressSoundVolumeSettings(prefs, res);
@@ -196,6 +201,8 @@
             final boolean gestureInputEnabled = Settings.readGestureInputEnabled(prefs, res);
             setPreferenceEnabled(Settings.PREF_GESTURE_PREVIEW_TRAIL, gestureInputEnabled);
             setPreferenceEnabled(Settings.PREF_GESTURE_FLOATING_PREVIEW_TEXT, gestureInputEnabled);
+        } else if (key.equals(Settings.PREF_SHOW_SETUP_WIZARD_ICON)) {
+            LauncherIconVisibilityManager.updateSetupWizardIconVisibility(getActivity());
         }
         ensureConsistencyOfAutoCorrectionSettings();
         updateVoiceModeSummary();
diff --git a/java/src/com/android/inputmethod/latin/setup/LauncherIconVisibilityManager.java b/java/src/com/android/inputmethod/latin/setup/LauncherIconVisibilityManager.java
index ad34011..1b893a6 100644
--- a/java/src/com/android/inputmethod/latin/setup/LauncherIconVisibilityManager.java
+++ b/java/src/com/android/inputmethod/latin/setup/LauncherIconVisibilityManager.java
@@ -16,18 +16,19 @@
 
 package com.android.inputmethod.latin.setup;
 
-import android.app.Activity;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
-import android.content.pm.ApplicationInfo;
+import android.content.SharedPreferences;
 import android.content.pm.PackageManager;
 import android.os.Process;
+import android.preference.PreferenceManager;
 import android.util.Log;
 
 import com.android.inputmethod.compat.IntentCompatUtils;
 import com.android.inputmethod.latin.RichInputMethodManager;
+import com.android.inputmethod.latin.Settings;
 
 /**
  * This class detects the {@link Intent#ACTION_MY_PACKAGE_REPLACED} broadcast intent when this IME
@@ -60,11 +61,7 @@
     @Override
     public void onReceive(final Context context, final Intent intent) {
         if (shouldHandleThisIntent(intent, context)) {
-            if (isInSystemImage(context)) {
-                disableActivity(context, SetupActivity.class);
-            } else {
-                Log.i(TAG, "This package isn't in system image: " + context.getPackageName());
-            }
+            updateSetupWizardIconVisibility(context);
         }
 
         // The process that hosts this broadcast receiver is invoked and remains alive even after
@@ -94,32 +91,32 @@
         return false;
     }
 
-    /**
-     * Disable an activity of the specified package. Disabling an activity will also hide its
-     * icon from the launcher.
-     *
-     * @param context package context of an activity to be disabled
-     * @param activityClass activity class to be disabled
-     */
-    private static void disableActivity(final Context context,
-            final Class<? extends Activity> activityClass) {
-        final ComponentName activityComponent = new ComponentName(context, activityClass);
-        final PackageManager pm = context.getPackageManager();
-        final int activityComponentState = pm.getComponentEnabledSetting(activityComponent);
-        if (activityComponentState == PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
-            // This activity is already disabled.
-            Log.i(TAG, "Activity has already been disabled: " + activityComponent);
-            return;
+    public static void updateSetupWizardIconVisibility(final Context context) {
+        final ComponentName setupWizardActivity = new ComponentName(context, SetupActivity.class);
+        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
+        final boolean stateHasSet;
+        if (Settings.readShowSetupWizardIcon(prefs, context)) {
+            stateHasSet = setActivityState(context, setupWizardActivity,
+                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
+            Log.i(TAG, (stateHasSet ? "Enable activity: " : "Activity has already been enabled: ")
+                    + setupWizardActivity);
+        } else {
+            stateHasSet = setActivityState(context, setupWizardActivity,
+                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
+            Log.i(TAG, (stateHasSet ? "Disable activity: " : "Activity has already been disabled: ")
+                    + setupWizardActivity);
         }
-        // Disabling an activity will also hide its icon from the launcher.
-        pm.setComponentEnabledSetting(activityComponent,
-                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
-                PackageManager.DONT_KILL_APP);
-        Log.i(TAG, "Disable activity: " + activityComponent);
     }
 
-    private static boolean isInSystemImage(final Context context) {
-        final ApplicationInfo appInfo = context.getApplicationInfo();
-        return (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
+    private static boolean setActivityState(final Context context,
+            final ComponentName activityComponent, final int activityState) {
+        final PackageManager pm = context.getPackageManager();
+        final int activityComponentState = pm.getComponentEnabledSetting(activityComponent);
+        if (activityComponentState == activityState) {
+            return false;
+        }
+        pm.setComponentEnabledSetting(
+                activityComponent, activityState, PackageManager.DONT_KILL_APP);
+        return true;
     }
 }