Disable companion and car mode role in role manager adapter
am: 8bfba76cf2

Change-Id: I6587d77a65b8e1bb0031a9b3ec586fa447a19739
diff --git a/PREUPLOAD.cfg b/PREUPLOAD.cfg
new file mode 100644
index 0000000..9874044
--- /dev/null
+++ b/PREUPLOAD.cfg
@@ -0,0 +1,2 @@
+[Hook Scripts]
+aosp_hook = ${REPO_ROOT}/packages/services/Telecomm/scripts/aosp_tag_preupload.py ${PREUPLOAD_COMMIT}
diff --git a/res/values/config.xml b/res/values/config.xml
index 540bece..5b9636d 100644
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -66,4 +66,12 @@
          classified as being on a user's ear. If the Y-component is less than this negative value,
          the device is probably upside-down and therefore not on a ear -->
     <item name="device_on_ear_y_gravity_negative_threshold" format="float" type="dimen">-1</item>
+
+    <!-- When true, an option is shown in the call blocking screen which allows the user to block
+         all incoming calls from callers not in their contacts. -->
+    <bool name="show_option_to_block_callers_not_in_contacts">false</bool>
+
+    <!-- When true, the options in the call blocking settings to block restricted and unknown
+         callers are combined into a single toggle. -->
+    <bool name="combine_options_to_block_restricted_and_unknown_callers">true</bool>
 </resources>
diff --git a/scripts/aosp_tag_preupload.py b/scripts/aosp_tag_preupload.py
new file mode 100755
index 0000000..8ec013e
--- /dev/null
+++ b/scripts/aosp_tag_preupload.py
@@ -0,0 +1,65 @@
+#!/usr/bin/python
+
+import re
+import subprocess
+import sys
+
+# Looks for a string of the form [aosp/branch-name]
+AOSP_BRANCH_REGEX = "\[aosp/[^\]]+\]"
+
+AOSP_COMMIT_TAG_REGEX = "AOSP:"
+AOSP_COMMIT_LINK_REGEX = "aosp/\d+"
+AOSP_INFEASIBLE_REGEX = "Infeasible[ ]?\S+"
+
+ERROR_MESSAGE = """
+The source of truth for this project is AOSP. If you are uploading something to
+a non-AOSP branch first, please provide a link in your commit message to the
+corresponding patch in AOSP. The link should be formatted as follows:
+
+  AOSP: aosp/<patch number>
+
+If it's infeasible for the change to be included in AOSP (for example, if a
+change contains confidential or security-sensitive information), please state
+that it's infeasible and provide reasoning as follows:
+
+  AOSP: Infeasible <your reasoning here>
+
+If you need to cherry-pick your change from an internal branch to AOSP before
+uploading, you can do so locally by adding the internal branch as a remote in
+AOSP:
+  git remote add goog-master /path/to/your/remote/branch/.git
+starting a new branch in AOSP:
+  repo start <your-branch-name>-cp
+then fetching and cherry-picking the change:
+  git fetch goog-master your-branch-name && git cherry-pick FETCH_HEAD
+"""
+
+def main():
+  if _is_in_aosp():
+    sys.exit(0)
+
+  commit_msg = subprocess.check_output(["git", "show",
+                                        sys.argv[1], "--no-notes"])
+  for commit_line in commit_msg.splitlines():
+    if re.search(AOSP_COMMIT_TAG_REGEX, commit_line, re.IGNORECASE):
+      _check_aosp_message(commit_line)
+
+  print(ERROR_MESSAGE)
+  sys.exit(1)
+
+def _is_in_aosp():
+  branch_info = subprocess.check_output(["git", "branch", "-vv"])
+  return re.search(AOSP_BRANCH_REGEX, branch_info) is not None
+
+def _check_aosp_message(aosp_line):
+  if re.search(AOSP_COMMIT_LINK_REGEX, aosp_line):
+    sys.exit(0)
+
+  if re.search(AOSP_INFEASIBLE_REGEX, aosp_line, re.IGNORECASE):
+    sys.exit(0)
+
+  print(ERROR_MESSAGE)
+  sys.exit(1)
+
+if __name__ == '__main__':
+  main()
diff --git a/src/com/android/server/telecom/RoleManagerAdapter.java b/src/com/android/server/telecom/RoleManagerAdapter.java
index 647d7bf..f1eb455 100644
--- a/src/com/android/server/telecom/RoleManagerAdapter.java
+++ b/src/com/android/server/telecom/RoleManagerAdapter.java
@@ -65,15 +65,12 @@
     void setTestDefaultCallScreeningApp(String packageName);
 
     /**
-     * Retrieves a list of package names of the app(s) which fill the
-     * {@link android.app.role.RoleManager} companion device role.
-     * @return List of package names filling the role, or empty list if there are none.
+     * @return List of package names of companion apps, or empty list if there are none.
      */
     List<String> getCallCompanionApps();
 
     /**
-     * Set a package to be added to the list of the {@link android.app.role.RoleManager} companion
-     * apps.  Used for testing purposes only.
+     * Set a package to be added to the list of the companion apps. Used for testing purposes only.
      * @param packageName Package name of the app to be added or removed as an override call
      *                    companion app.
      * @param isAdded {@code true} if the specified package should be added, {@code false} if it
@@ -82,17 +79,13 @@
     void addOrRemoveTestCallCompanionApp(String packageName, boolean isAdded);
 
     /**
-     * Returns the package name of the app which fills the {@link android.app.role.RoleManager}
-     * projection mode role.
-     * @return Package name of the car more app or {@code null} if there are no apps that fill this
-     * role.
+     * @return Package name of the car more app or {@code null} if there are no apps that match.
      */
     String getCarModeDialerApp();
 
     /**
-     * Override the {@link android.app.role.RoleManager} automotive app with another value.
-     * Used for testing purposes only.
-     * @param packageName Package name of the app to fill the automotive app role.  Where
+     * Override the automotive app with another value. Used for testing purposes only.
+     * @param packageName Package name of the automotive app. Where
      *                    {@code null}, the override is removed.
      */
     void setTestAutoModeApp(String packageName);
diff --git a/src/com/android/server/telecom/settings/EnhancedCallBlockingFragment.java b/src/com/android/server/telecom/settings/EnhancedCallBlockingFragment.java
index b475ec6..ecc019f 100644
--- a/src/com/android/server/telecom/settings/EnhancedCallBlockingFragment.java
+++ b/src/com/android/server/telecom/settings/EnhancedCallBlockingFragment.java
@@ -26,6 +26,7 @@
 import android.provider.BlockedNumberContract.SystemContract;
 import android.telephony.CarrierConfigManager;
 import android.telephony.SubscriptionManager;
+import android.telecom.Log;
 import android.view.LayoutInflater;
 import android.view.MenuItem;
 import android.view.View;
@@ -35,12 +36,21 @@
 
 public class EnhancedCallBlockingFragment extends PreferenceFragment
         implements Preference.OnPreferenceChangeListener {
+    private static final String BLOCK_NUMBERS_NOT_IN_CONTACTS_KEY =
+            "block_numbers_not_in_contacts_setting";
+    private static final String BLOCK_RESTRICTED_NUMBERS_KEY =
+            "block_private_number_calls_setting";
+    private static final String BLOCK_UNKNOWN_NUMBERS_KEY =
+            "block_unknown_calls_setting";
+    private boolean mIsCombiningRestrictedAndUnknownOption = false;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         addPreferencesFromResource(R.xml.enhanced_call_blocking_settings);
 
+        maybeConfigureCallBlockingOptions();
+
         setOnPreferenceChangeListener(SystemContract.ENHANCED_SETTING_KEY_BLOCK_UNREGISTERED);
         setOnPreferenceChangeListener(SystemContract.ENHANCED_SETTING_KEY_BLOCK_PRIVATE);
         setOnPreferenceChangeListener(SystemContract.ENHANCED_SETTING_KEY_BLOCK_PAYPHONE);
@@ -67,6 +77,25 @@
         return b.getBoolean(CarrierConfigManager.KEY_SHOW_BLOCKING_PAY_PHONE_OPTION_BOOL);
     }
 
+    private void maybeConfigureCallBlockingOptions() {
+        PreferenceScreen screen = getPreferenceScreen();
+        boolean isShowingNotInContactsOption =
+                getResources().getBoolean(R.bool.show_option_to_block_callers_not_in_contacts);
+        if (!isShowingNotInContactsOption) {
+            Preference pref = findPreference(BLOCK_NUMBERS_NOT_IN_CONTACTS_KEY);
+            screen.removePreference(pref);
+            Log.i(this, "onCreate: removed block not in contacts preference.");
+        }
+
+        mIsCombiningRestrictedAndUnknownOption = getResources().getBoolean(
+                        R.bool.combine_options_to_block_restricted_and_unknown_callers);
+        if (mIsCombiningRestrictedAndUnknownOption) {
+            Preference pref = findPreference(BLOCK_RESTRICTED_NUMBERS_KEY);
+            screen.removePreference(pref);
+            Log.i(this, "onCreate: removed block restricted preference.");
+        }
+    }
+
     /**
      * Set OnPreferenceChangeListener for the preference.
      */
@@ -101,6 +130,13 @@
 
     @Override
     public boolean onPreferenceChange(Preference preference, Object objValue) {
+        if (mIsCombiningRestrictedAndUnknownOption
+                && preference.getKey().equals(BLOCK_UNKNOWN_NUMBERS_KEY)) {
+            Log.i(this, "onPreferenceChange: changing %s and %s to %b",
+                    preference.getKey(), BLOCK_RESTRICTED_NUMBERS_KEY, (boolean) objValue);
+            BlockedNumbersUtil.setEnhancedBlockSetting(getActivity(), BLOCK_RESTRICTED_NUMBERS_KEY,
+                    (boolean) objValue);
+        }
         BlockedNumbersUtil.setEnhancedBlockSetting(getActivity(), preference.getKey(),
                 (boolean) objValue);
         return true;