(SHIFT) Squashed revert of restricted network changes

-----

Revert "POLICY_REJECT_ALL restricts all network access"

    This reverts commit 406607e0c16ed23d918c68f14eb4576ce411bb73.

Revert "AppDataUsage: Handle apps without INTERNET permission"

    This reverts commit 25f4ac0ef4de90e828d37510735349d974f762e6.

Revert "Settings: Add support for allowing/disallowing apps on cellular, vpn and wifi networks"

    This reverts commit 415fa88d200423a589a1aa456cfcf8b12adeac97.

Revert "Settings: Add support for allowing/disallowing apps on restricted networks"

    This reverts commit cba7424311ee089e140066d8e0abfaafa76f7198.

Revert "Settings: Move SIM restrictions currently applied to per-app data usage"

    This reverts commit 9e2147d8089549f1f17f8a58118edddc70b49b9e.

-----

Change-Id: I58f6f315e9f88851176717e8ddc74e1500072cc1
Signed-off-by: Alexander Martinz <amartinz@shiftphones.com>
diff --git a/res/values/cm_strings.xml b/res/values/cm_strings.xml
index 37f5034..35e2810 100644
--- a/res/values/cm_strings.xml
+++ b/res/values/cm_strings.xml
@@ -144,12 +144,6 @@
     <!-- Per-app data restrictions -->
     <string name="data_usage_app_restrict_all">Allow network access</string>
     <string name="data_usage_app_restrict_all_summary">Enable network usage</string>
-    <string name="data_usage_app_restrict_mobile">Mobile data</string>
-    <string name="data_usage_app_restrict_mobile_summary">Enable usage of mobile data</string>
-    <string name="data_usage_app_restrict_vpn">VPN data</string>
-    <string name="data_usage_app_restrict_vpn_summary">Enable usage of VPN data</string>
-    <string name="data_usage_app_restrict_wifi">Wi\u2011Fi data</string>
-    <string name="data_usage_app_restrict_wifi_summary">Enable usage of Wi\u2011Fi data</string>
 
     <!-- PIN scramble -->
     <string name="unlock_scramble_pin_layout_title">Scramble layout</string>
diff --git a/res/xml/app_data_usage.xml b/res/xml/app_data_usage.xml
index 9b98391..aacc1f6 100644
--- a/res/xml/app_data_usage.xml
+++ b/res/xml/app_data_usage.xml
@@ -51,6 +51,7 @@
 
     <PreferenceCategory
         android:key="app_data_usage_settings_category"
+        settings:isPreferenceVisible="@bool/config_show_sim_info"
         android:layout="@layout/preference_category_no_label">
 
         <Preference
@@ -58,39 +59,16 @@
             android:title="@string/data_usage_app_settings" />
 
         <com.android.settingslib.RestrictedSwitchPreference
-            android:key="restrict_all"
-            android:title="@string/data_usage_app_restrict_all"
-            android:summary="@string/data_usage_app_restrict_all_summary" />
-
-        <com.android.settingslib.RestrictedSwitchPreference
-            android:key="restrict_wifi"
-            android:title="@string/data_usage_app_restrict_wifi"
-            android:summary="@string/data_usage_app_restrict_wifi_summary" />
-
-        <com.android.settingslib.RestrictedSwitchPreference
-            android:key="restrict_cellular"
-            android:title="@string/data_usage_app_restrict_mobile"
-            android:summary="@string/data_usage_app_restrict_mobile_summary"
-            settings:isPreferenceVisible="@bool/config_show_sim_info" />
-
-        <com.android.settingslib.RestrictedSwitchPreference
             android:key="restrict_background"
             android:title="@string/data_usage_app_restrict_background"
             android:summary="@string/data_usage_app_restrict_background_summary"
-            settings:isPreferenceVisible="@bool/config_show_sim_info"
             settings:useAdditionalSummary="true"
             settings:restrictedSwitchSummary="@string/disabled_by_admin" />
 
         <com.android.settingslib.RestrictedSwitchPreference
-            android:key="restrict_vpn"
-            android:title="@string/data_usage_app_restrict_vpn"
-            android:summary="@string/data_usage_app_restrict_vpn_summary" />
-
-        <com.android.settingslib.RestrictedSwitchPreference
             android:key="unrestricted_data_saver"
             android:title="@string/unrestricted_app_title"
             android:summary="@string/unrestricted_app_summary"
-            settings:isPreferenceVisible="@bool/config_show_sim_info"
             settings:useAdditionalSummary="true"
             settings:restrictedSwitchSummary="@string/disabled_by_admin" />
 
diff --git a/src/com/android/settings/datausage/AppDataUsage.java b/src/com/android/settings/datausage/AppDataUsage.java
index 574ed1b..e15cead 100644
--- a/src/com/android/settings/datausage/AppDataUsage.java
+++ b/src/com/android/settings/datausage/AppDataUsage.java
@@ -15,12 +15,7 @@
 package com.android.settings.datausage;
 
 import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
-import static android.net.NetworkPolicyManager.POLICY_REJECT_ALL;
-import static android.net.NetworkPolicyManager.POLICY_REJECT_CELLULAR;
-import static android.net.NetworkPolicyManager.POLICY_REJECT_VPN;
-import static android.net.NetworkPolicyManager.POLICY_REJECT_WIFI;
 
-import android.Manifest;
 import android.app.Activity;
 import android.app.settings.SettingsEnums;
 import android.content.Context;
@@ -28,7 +23,6 @@
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 import android.graphics.drawable.Drawable;
-import android.net.NetworkPolicyManager;
 import android.net.NetworkTemplate;
 import android.os.Bundle;
 import android.os.Process;
@@ -64,7 +58,6 @@
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Set;
 
 public class AppDataUsage extends DataUsageBaseFragment implements OnPreferenceChangeListener,
         DataSaverBackend.Listener {
@@ -80,11 +73,7 @@
     private static final String KEY_FOREGROUND_USAGE = "foreground_usage";
     private static final String KEY_BACKGROUND_USAGE = "background_usage";
     private static final String KEY_APP_SETTINGS = "app_settings";
-    private static final String KEY_RESTRICT_ALL = "restrict_all";
     private static final String KEY_RESTRICT_BACKGROUND = "restrict_background";
-    private static final String KEY_RESTRICT_CELLULAR = "restrict_cellular";
-    private static final String KEY_RESTRICT_VPN = "restrict_vpn";
-    private static final String KEY_RESTRICT_WIFI = "restrict_wifi";
     private static final String KEY_APP_LIST = "app_list";
     private static final String KEY_CYCLE = "cycle";
     private static final String KEY_UNRESTRICTED_DATA = "unrestricted_data_saver";
@@ -98,11 +87,7 @@
     private Preference mForegroundUsage;
     private Preference mBackgroundUsage;
     private Preference mAppSettings;
-    private RestrictedSwitchPreference mRestrictAll;
     private RestrictedSwitchPreference mRestrictBackground;
-    private RestrictedSwitchPreference mRestrictCellular;
-    private RestrictedSwitchPreference mRestrictVpn;
-    private RestrictedSwitchPreference mRestrictWifi;
     private PreferenceCategory mAppList;
 
     private Drawable mIcon;
@@ -125,6 +110,10 @@
     private long mSelectedCycle;
     private boolean mIsLoading;
 
+    public boolean isSimHardwareVisible(Context context) {
+        return SubscriptionUtil.isSimHardwareVisible(context);
+    }
+
     @Override
     public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
@@ -177,16 +166,12 @@
         final UidDetailProvider uidDetailProvider = getUidDetailProvider();
 
         if (mAppItem.key > 0) {
-            if (!UserHandle.isApp(mAppItem.key)) {
+            if ((!isSimHardwareVisible(mContext)) || !UserHandle.isApp(mAppItem.key)) {
                 final UidDetail uidDetail = uidDetailProvider.getUidDetail(mAppItem.key, true);
                 mIcon = uidDetail.icon;
                 mLabel = uidDetail.label;
                 removePreference(KEY_UNRESTRICTED_DATA);
-                removePreference(KEY_RESTRICT_ALL);
                 removePreference(KEY_RESTRICT_BACKGROUND);
-                removePreference(KEY_RESTRICT_CELLULAR);
-                removePreference(KEY_RESTRICT_VPN);
-                removePreference(KEY_RESTRICT_WIFI);
             } else {
                 if (mPackages.size() != 0) {
                     try {
@@ -198,16 +183,8 @@
                     } catch (PackageManager.NameNotFoundException e) {
                     }
                 }
-                mRestrictAll = findPreference(KEY_RESTRICT_ALL);
-                mRestrictAll.setOnPreferenceChangeListener(this);
                 mRestrictBackground = findPreference(KEY_RESTRICT_BACKGROUND);
                 mRestrictBackground.setOnPreferenceChangeListener(this);
-                mRestrictCellular = findPreference(KEY_RESTRICT_CELLULAR);
-                mRestrictCellular.setOnPreferenceChangeListener(this);
-                mRestrictVpn = findPreference(KEY_RESTRICT_VPN);
-                mRestrictVpn.setOnPreferenceChangeListener(this);
-                mRestrictWifi = findPreference(KEY_RESTRICT_WIFI);
-                mRestrictWifi.setOnPreferenceChangeListener(this);
                 mUnrestrictedData = findPreference(KEY_UNRESTRICTED_DATA);
                 mUnrestrictedData.setOnPreferenceChangeListener(this);
             }
@@ -247,11 +224,7 @@
 
             removePreference(KEY_UNRESTRICTED_DATA);
             removePreference(KEY_APP_SETTINGS);
-            removePreference(KEY_RESTRICT_ALL);
             removePreference(KEY_RESTRICT_BACKGROUND);
-            removePreference(KEY_RESTRICT_CELLULAR);
-            removePreference(KEY_RESTRICT_VPN);
-            removePreference(KEY_RESTRICT_WIFI);
             removePreference(KEY_APP_LIST);
         }
 
@@ -293,22 +266,6 @@
             mDataSaverBackend.setIsDenylisted(mAppItem.key, mPackageName, !(Boolean) newValue);
             updatePrefs();
             return true;
-        } else if (preference == mRestrictAll) {
-            setAppRestrictAll(!(Boolean) newValue);
-            updatePrefs();
-            return true;
-        } else if (preference == mRestrictCellular) {
-            setAppRestrictCellular(!(Boolean) newValue);
-            updatePrefs();
-            return true;
-        } else if (preference == mRestrictVpn) {
-            setAppRestrictVpn(!(Boolean) newValue);
-            updatePrefs();
-            return true;
-        } else if (preference == mRestrictWifi) {
-            setAppRestrictWifi(!(Boolean) newValue);
-            updatePrefs();
-            return true;
         } else if (preference == mUnrestrictedData) {
             mDataSaverBackend.setIsAllowlisted(mAppItem.key, mPackageName, (Boolean) newValue);
             return true;
@@ -339,9 +296,7 @@
 
     @VisibleForTesting
     void updatePrefs() {
-        updatePrefs(getAppRestrictBackground(), getUnrestrictData(), getAppRestrictAll(),
-                getAppRestrictCellular(), getAppRestrictVpn(), getAppRestrictWifi(),
-                hasInternetPermission());
+        updatePrefs(getAppRestrictBackground(), getUnrestrictData());
     }
 
     @VisibleForTesting
@@ -377,43 +332,25 @@
         }
     }
 
-    private void updatePrefs(boolean restrictBackground, boolean unrestrictData,
-            boolean restrictAll, boolean restrictCellular, boolean restrictVpn,
-            boolean restrictWifi, boolean hasInternetPermission) {
+    private void updatePrefs(boolean restrictBackground, boolean unrestrictData) {
+        if (!isSimHardwareVisible(mContext)) {
+            return;
+        }
         setBackPreferenceListAnimatorIfLoaded();
         final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfMeteredDataRestricted(
                 mContext, mPackageName, UserHandle.getUserId(mAppItem.key));
-        if (mRestrictAll != null) {
-            mRestrictAll.setEnabled(hasInternetPermission);
-            mRestrictAll.setChecked(!restrictAll);
-        }
         if (mRestrictBackground != null) {
+            mRestrictBackground.setChecked(!restrictBackground);
             mRestrictBackground.setDisabledByAdmin(admin);
-            mRestrictBackground.setEnabled(hasInternetPermission &&
-                    !mRestrictBackground.isDisabledByAdmin() && !restrictAll &&
-                    !restrictCellular);
-            mRestrictBackground.setChecked(!restrictBackground && !restrictAll &&
-                    !restrictCellular);
-        }
-        if (mRestrictCellular != null) {
-            mRestrictCellular.setEnabled(hasInternetPermission && !restrictAll);
-            mRestrictCellular.setChecked(!restrictAll && !restrictCellular);
-        }
-        if (mRestrictVpn != null) {
-            mRestrictVpn.setEnabled(hasInternetPermission && !restrictAll);
-            mRestrictVpn.setChecked(!restrictAll && !restrictVpn);
-        }
-        if (mRestrictWifi != null) {
-            mRestrictWifi.setEnabled(hasInternetPermission && !restrictAll);
-            mRestrictWifi.setChecked(!restrictAll && !restrictWifi);
         }
         if (mUnrestrictedData != null) {
-            mUnrestrictedData.setDisabledByAdmin(admin);
-            mUnrestrictedData.setEnabled(hasInternetPermission &&
-                    !mUnrestrictedData.isDisabledByAdmin() && !restrictBackground && !restrictAll &&
-                    !restrictCellular);
-            mUnrestrictedData.setChecked(unrestrictData && !restrictBackground && !restrictAll &&
-                    !restrictCellular);
+            if (restrictBackground) {
+                mUnrestrictedData.setVisible(false);
+            } else {
+                mUnrestrictedData.setVisible(true);
+                mUnrestrictedData.setChecked(unrestrictData);
+                mUnrestrictedData.setDisabledByAdmin(admin);
+            }
         }
     }
 
@@ -450,23 +387,9 @@
     }
 
     private boolean getAppRestrictBackground() {
-        return getAppRestriction(POLICY_REJECT_METERED_BACKGROUND);
-    }
-
-    private boolean getAppRestrictCellular() {
-        return getAppRestriction(POLICY_REJECT_CELLULAR);
-    }
-
-    private boolean getAppRestrictVpn() {
-        return getAppRestriction(POLICY_REJECT_VPN);
-    }
-
-    private boolean getAppRestrictWifi() {
-        return getAppRestriction(POLICY_REJECT_WIFI);
-    }
-
-    private boolean getAppRestrictAll() {
-        return getAppRestriction(POLICY_REJECT_ALL);
+        final int uid = mAppItem.key;
+        final int uidPolicy = services.mPolicyManager.getUidPolicy(uid);
+        return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
     }
 
     private boolean getUnrestrictData() {
@@ -476,42 +399,6 @@
         return false;
     }
 
-    private boolean getAppRestriction(int policy) {
-        final int uid = mAppItem.key;
-        final int uidPolicy = services.mPolicyManager.getUidPolicy(uid);
-        return (uidPolicy & policy) != 0;
-    }
-
-    private boolean hasInternetPermission() {
-        return mPackageManager.checkPermission(Manifest.permission.INTERNET, mPackageName)
-                == PackageManager.PERMISSION_GRANTED;
-    }
-
-    private void setAppRestrictAll(boolean restrict) {
-        setAppRestriction(POLICY_REJECT_ALL, restrict);
-    }
-
-    private void setAppRestrictCellular(boolean restrict) {
-        setAppRestriction(POLICY_REJECT_CELLULAR, restrict);
-    }
-
-    private void setAppRestrictVpn(boolean restrict) {
-        setAppRestriction(POLICY_REJECT_VPN, restrict);
-    }
-
-    private void setAppRestrictWifi(boolean restrict) {
-        setAppRestriction(POLICY_REJECT_WIFI, restrict);
-    }
-
-    private void setAppRestriction(int policy, boolean restrict) {
-        final int uid = mAppItem.key;
-        if (restrict) {
-            services.mPolicyManager.addUidPolicy(uid, policy);
-        } else {
-            services.mPolicyManager.removeUidPolicy(uid, policy);
-        }
-    }
-
     @VisibleForTesting
     void addEntityHeader() {
         String pkg = mPackages.size() != 0 ? mPackages.valueAt(0) : null;
@@ -638,18 +525,14 @@
     @Override
     public void onAllowlistStatusChanged(int uid, boolean isAllowlisted) {
         if (mAppItem.uids.get(uid, false)) {
-            updatePrefs(getAppRestrictBackground(), isAllowlisted, getAppRestrictAll(),
-                    getAppRestrictCellular(), getAppRestrictVpn(), getAppRestrictWifi(),
-                    hasInternetPermission());
+            updatePrefs(getAppRestrictBackground(), isAllowlisted);
         }
     }
 
     @Override
     public void onDenylistStatusChanged(int uid, boolean isDenylisted) {
         if (mAppItem.uids.get(uid, false)) {
-            updatePrefs(isDenylisted, getUnrestrictData(), getAppRestrictAll(),
-                    getAppRestrictCellular(), getAppRestrictVpn(), getAppRestrictWifi(),
-                    hasInternetPermission());
+            updatePrefs(isDenylisted, getUnrestrictData());
         }
     }
 }
diff --git a/src/com/android/settings/datausage/DataSaverBackend.java b/src/com/android/settings/datausage/DataSaverBackend.java
index 98d909f..e39e41a 100644
--- a/src/com/android/settings/datausage/DataSaverBackend.java
+++ b/src/com/android/settings/datausage/DataSaverBackend.java
@@ -80,15 +80,12 @@
 
     public void setIsAllowlisted(int uid, String packageName, boolean allowlisted) {
         final int policy = allowlisted ? POLICY_ALLOW_METERED_BACKGROUND : POLICY_NONE;
+        mPolicyManager.setUidPolicy(uid, policy);
         mUidPolicies.put(uid, policy);
         if (allowlisted) {
-            mPolicyManager.addUidPolicy(uid, POLICY_ALLOW_METERED_BACKGROUND);
             mMetricsFeatureProvider.action(
                     mContext, SettingsEnums.ACTION_DATA_SAVER_WHITELIST, packageName);
-        } else {
-            mPolicyManager.removeUidPolicy(uid, POLICY_ALLOW_METERED_BACKGROUND);
         }
-        mPolicyManager.removeUidPolicy(uid, POLICY_REJECT_METERED_BACKGROUND);
     }
 
     public boolean isAllowlisted(int uid) {
@@ -113,15 +110,12 @@
 
     public void setIsDenylisted(int uid, String packageName, boolean denylisted) {
         final int policy = denylisted ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE;
+        mPolicyManager.setUidPolicy(uid, policy);
         mUidPolicies.put(uid, policy);
         if (denylisted) {
-            mPolicyManager.addUidPolicy(uid, POLICY_REJECT_METERED_BACKGROUND);
             mMetricsFeatureProvider.action(
                     mContext, SettingsEnums.ACTION_DATA_SAVER_BLACKLIST, packageName);
-        } else {
-            mPolicyManager.removeUidPolicy(uid, POLICY_REJECT_METERED_BACKGROUND);
         }
-        mPolicyManager.removeUidPolicy(uid, POLICY_ALLOW_METERED_BACKGROUND);
     }
 
     public boolean isDenylisted(int uid) {
@@ -161,9 +155,6 @@
         loadAllowlist();
         loadDenylist();
 
-        // We only care about allow/reject metered background policy here.
-        newPolicy &= POLICY_ALLOW_METERED_BACKGROUND | POLICY_REJECT_METERED_BACKGROUND;
-
         final int oldPolicy = mUidPolicies.get(uid, POLICY_NONE);
         if (newPolicy == POLICY_NONE) {
             mUidPolicies.delete(uid);