From 74702e16945e5e89d916307593bcaae5fa55bceb Mon Sep 17 00:00:00 2001
From: M66B <M66B@users.noreply.github.com>
Date: Tue, 31 Dec 2019 12:07:45 +0100
Subject: [PATCH] Allow disabling external search

---
 app/src/main/AndroidManifest.xml              |  2 +-
 .../faircode/email/FragmentOptionsMisc.java   | 21 +++++++++++++++++++
 .../java/eu/faircode/email/FragmentSetup.java |  8 -------
 .../main/res/layout/fragment_options_misc.xml | 14 ++++++++++++-
 app/src/main/res/values/strings.xml           |  1 +
 5 files changed, 36 insertions(+), 10 deletions(-)

diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 1027f5a875..783605dd37 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -110,7 +110,7 @@
 
         <activity
             android:name=".ActivitySearch"
-            android:enabled="false"
+            android:enabled="true"
             android:excludeFromRecents="true"
             android:exported="true"
             android:icon="@mipmap/ic_launcher"
diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java
index 9a293c32f3..eeaab8c3fd 100644
--- a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java
+++ b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java
@@ -21,8 +21,10 @@ package eu.faircode.email;
 
 import android.app.ActivityManager;
 import android.app.NotificationManager;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.SharedPreferences;
+import android.content.pm.PackageManager;
 import android.graphics.Paint;
 import android.os.Bundle;
 import android.view.LayoutInflater;
@@ -44,6 +46,7 @@ import androidx.lifecycle.Lifecycle;
 import androidx.preference.PreferenceManager;
 
 public class FragmentOptionsMisc extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
+    private SwitchCompat swExternalSearch;
     private SwitchCompat swEnglish;
     private SwitchCompat swWatchdog;
     private SwitchCompat swUpdates;
@@ -83,6 +86,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
 
         // Get controls
 
+        swExternalSearch = view.findViewById(R.id.swExternalSearch);
         swEnglish = view.findViewById(R.id.swEnglish);
         swWatchdog = view.findViewById(R.id.swWatchdog);
         swUpdates = view.findViewById(R.id.swUpdates);
@@ -108,6 +112,19 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
 
         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
 
+        swExternalSearch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+            @Override
+            public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
+                PackageManager pm = getContext().getPackageManager();
+                pm.setComponentEnabledSetting(
+                        new ComponentName(getContext(), ActivitySearch.class),
+                        checked
+                                ? PackageManager.COMPONENT_ENABLED_STATE_DEFAULT
+                                : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
+                        PackageManager.DONT_KILL_APP);
+            }
+        });
+
         swEnglish.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
             @Override
             public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
@@ -278,6 +295,10 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
     private void setOptions() {
         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
 
+        PackageManager pm = getContext().getPackageManager();
+        int state = pm.getComponentEnabledSetting(new ComponentName(getContext(), ActivitySearch.class));
+
+        swExternalSearch.setChecked(state != PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
         swEnglish.setChecked(prefs.getBoolean("english", false));
         swWatchdog.setChecked(prefs.getBoolean("watchdog", true));
         swUpdates.setChecked(prefs.getBoolean("updates", true));
diff --git a/app/src/main/java/eu/faircode/email/FragmentSetup.java b/app/src/main/java/eu/faircode/email/FragmentSetup.java
index 30d0f8c250..4a38e9b2a4 100644
--- a/app/src/main/java/eu/faircode/email/FragmentSetup.java
+++ b/app/src/main/java/eu/faircode/email/FragmentSetup.java
@@ -21,7 +21,6 @@ package eu.faircode.email;
 
 import android.Manifest;
 import android.app.Dialog;
-import android.content.ComponentName;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
@@ -404,13 +403,6 @@ public class FragmentSetup extends FragmentBase {
                 tvNoIdentities.setVisibility(done ? View.GONE : View.VISIBLE);
             }
         });
-
-        // Backward compatibility
-        PackageManager pm = getContext().getPackageManager();
-        pm.setComponentEnabledSetting(
-                new ComponentName(getContext(), ActivitySearch.class),
-                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
-                PackageManager.DONT_KILL_APP);
     }
 
     @Override
diff --git a/app/src/main/res/layout/fragment_options_misc.xml b/app/src/main/res/layout/fragment_options_misc.xml
index 7546832bc2..62d12e7b3f 100644
--- a/app/src/main/res/layout/fragment_options_misc.xml
+++ b/app/src/main/res/layout/fragment_options_misc.xml
@@ -24,6 +24,18 @@
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintTop_toTopOf="parent" />
 
+        <androidx.appcompat.widget.SwitchCompat
+            android:id="@+id/swExternalSearch"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_marginTop="12dp"
+            android:enabled="true"
+            android:text="@string/title_advanced_external_search"
+            app:layout_constraintEnd_toEndOf="parent"
+            app:layout_constraintStart_toStartOf="parent"
+            app:layout_constraintTop_toBottomOf="@id/tvAdvancedHint"
+            app:switchPadding="12dp" />
+
         <androidx.appcompat.widget.SwitchCompat
             android:id="@+id/swEnglish"
             android:layout_width="0dp"
@@ -32,7 +44,7 @@
             android:text="@string/title_advanced_english"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintStart_toStartOf="parent"
-            app:layout_constraintTop_toBottomOf="@id/tvAdvancedHint"
+            app:layout_constraintTop_toBottomOf="@id/swExternalSearch"
             app:switchPadding="12dp" />
 
         <TextView
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 695ca2c20c..a2de0afb9f 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -363,6 +363,7 @@
     <string name="title_advanced_manage_keys">Manage private keys</string>
     <string name="title_advanced_aes_key_size" translatable="false">Max AES key size: %1$d</string>
 
+    <string name="title_advanced_external_search">Allow other apps to search in messages</string>
     <string name="title_advanced_english">Force English language</string>
     <string name="title_advanced_watchdog">Periodically check if FairEmail is still active</string>
     <string name="title_advanced_updates">Check for updates</string>