Hide quicksetting tile for dev options when it's turned off

Merged-In: I3e11700a59c8a88bb586c77a876963b5e6a62c89
Change-Id: I3e11700a59c8a88bb586c77a876963b5e6a62c89
Fixes: 78652607
Bug: 117770924
Test: robotests
(cherry picked from commit 04f1e38dc3af5b79849be07f31235ad8c1b20574)
diff --git a/src/com/android/settings/development/DevelopmentSettingsEnabler.java b/src/com/android/settings/development/DevelopmentSettingsEnabler.java
index ed750e4..297a21d 100644
--- a/src/com/android/settings/development/DevelopmentSettingsEnabler.java
+++ b/src/com/android/settings/development/DevelopmentSettingsEnabler.java
@@ -18,6 +18,8 @@
 
 import android.content.Context;
 import android.content.SharedPreferences;
+import android.os.Build;
+import android.os.UserManager;
 import android.provider.Settings;
 
 import com.android.settingslib.core.lifecycle.Lifecycle;
@@ -53,6 +55,17 @@
                 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
     }
 
+    public static boolean isDevelopmentSettingsEnabled(Context context) {
+        final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
+        final boolean settingEnabled = Settings.Global.getInt(context.getContentResolver(),
+                Settings.Global.DEVELOPMENT_SETTINGS_ENABLED,
+                Build.TYPE.equals("eng") ? 1 : 0) != 0;
+        final boolean hasRestriction = um.hasUserRestriction(
+                UserManager.DISALLOW_DEBUGGING_FEATURES);
+        final boolean isAdminOrDemo = um.isAdminUser() || um.isDemoUser();
+        return isAdminOrDemo && !hasRestriction && settingEnabled;
+    }
+
     private void updateEnabledState() {
         mLastEnabledState = Settings.Global.getInt(mContext.getContentResolver(),
                 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
diff --git a/src/com/android/settings/qstile/DevelopmentTiles.java b/src/com/android/settings/qstile/DevelopmentTiles.java
index ad60827..2c82283 100644
--- a/src/com/android/settings/qstile/DevelopmentTiles.java
+++ b/src/com/android/settings/qstile/DevelopmentTiles.java
@@ -16,20 +16,29 @@
 
 package com.android.settings.qstile;
 
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.pm.PackageManager;
 import android.os.RemoteException;
+import android.os.ServiceManager;
 import android.os.SystemProperties;
 import android.provider.Settings;
 import android.service.quicksettings.Tile;
 import android.service.quicksettings.TileService;
+import android.util.EventLog;
+import android.util.Log;
 import android.view.IWindowManager;
 import android.view.ThreadedRenderer;
 import android.view.View;
 import android.view.WindowManagerGlobal;
 
 import com.android.internal.app.LocalePicker;
+import com.android.internal.statusbar.IStatusBarService;
+import com.android.settings.development.DevelopmentSettingsEnabler;
 import com.android.settings.development.DevelopmentSettings;
 
 public abstract class DevelopmentTiles extends TileService {
+    private static final String TAG = "DevelopmentTiles";
 
     protected abstract boolean isEnabled();
 
@@ -42,7 +51,33 @@
     }
 
     public void refresh() {
-        getQsTile().setState(isEnabled() ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE);
+        final int state;
+        if (!DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)) {
+            // Reset to disabled state if dev option is off.
+            if (isEnabled()) {
+                setIsEnabled(false);
+                new DevelopmentSettings.SystemPropPoker().execute();
+            }
+            final ComponentName cn = new ComponentName(getPackageName(), getClass().getName());
+            try {
+                getPackageManager().setComponentEnabledSetting(
+                        cn, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
+                        PackageManager.DONT_KILL_APP);
+                final IStatusBarService statusBarService = IStatusBarService.Stub.asInterface(
+                        ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
+                if (statusBarService != null) {
+                    EventLog.writeEvent(0x534e4554, "117770924");  // SaftyNet
+                    statusBarService.remTile(cn);
+                }
+            } catch (RemoteException e) {
+                Log.e(TAG, "Failed to modify QS tile for component " +
+                        cn.toString(), e);
+            }
+            state = Tile.STATE_UNAVAILABLE;
+        } else {
+            state = isEnabled() ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;
+        }
+        getQsTile().setState(state);
         getQsTile().updateTile();
     }
 
@@ -131,4 +166,4 @@
             } catch (RemoteException e) { }
         }
     }
-}
\ No newline at end of file
+}