protected void setupQuickSettings() {
   shutdown();
   mReceiver = new QSBroadcastReceiver();
   mReceiverMap.clear();
   mObserver = new QuickSettingsObserver(mHandler);
   mObserverMap.clear();
   mTileStatusUris.clear();
   loadTiles();
   setupBroadcastReceiver();
   setupContentObserver();
   ContentResolver resolver = mContext.getContentResolver();
   boolean smallIcons =
       Settings.System.getIntForUser(
               resolver, Settings.System.QUICK_SETTINGS_SMALL_ICONS, 0, UserHandle.USER_CURRENT)
           == 1;
   if (mRibbonMode || smallIcons) {
     for (QuickSettingsTile t : mQuickSettingsTiles) {
       if (mRibbonMode) {
         t.switchToRibbonMode();
       } else {
         t.switchToSmallIcons();
       }
     }
   }
   updateResources();
 }
 public void updateResources() {
   updateSize();
   mContainerView.updateResources();
   for (QuickSettingsTile t : mQuickSettingsTiles) {
     t.updateResources();
   }
 }
 public void onReceive(Context context, Intent intent) {
   String action = intent.getAction();
   if (action != null) {
     for (QuickSettingsTile t : mReceiverMap.get(action)) {
       t.onReceive(context, intent);
     }
   }
 }
 public void shutdown() {
   if (mObserver != null) {
     mContext.getContentResolver().unregisterContentObserver(mObserver);
   }
   if (mReceiver != null) {
     mContext.unregisterReceiver(mReceiver);
   }
   for (QuickSettingsTile qs : mQuickSettingsTiles) {
     qs.onDestroy();
   }
   mQuickSettingsTiles.clear();
   mContainerView.removeAllViews();
 }
 @Override
 public void onChange(boolean selfChange, Uri uri) {
   if (mTileStatusUris.contains(uri)) {
     mHandler.removeMessages(MSG_UPDATE_TILES);
     mHandler.sendEmptyMessage(MSG_UPDATE_TILES);
   } else {
     ContentResolver resolver = mContext.getContentResolver();
     if (mObserverMap != null && mObserverMap.get(uri) != null) {
       for (QuickSettingsTile tile : mObserverMap.get(uri)) {
         tile.onChangeUri(resolver, uri);
       }
     }
   }
 }
  private void loadDockBatteryTile(final ContentResolver resolver, final LayoutInflater inflater) {
    if (!QSUtils.deviceSupportsDockBattery(mContext)) {
      return;
    }
    if (Settings.System.getIntForUser(
            resolver, Settings.System.QS_DYNAMIC_DOCK_BATTERY, 1, UserHandle.USER_CURRENT)
        == 0) {
      return;
    }

    QuickSettingsTile qs =
        new DockBatteryTile(mContext, this, mStatusBarService.mDockBatteryController);
    qs.setupQuickSettingsTile(inflater, mContainerView);
    mQuickSettingsTiles.add(qs);
  }
  private void updateSize() {
    if (mContainerView == null || !mRibbonMode) return;

    QSSize size = mContainerView.getRibbonSize();
    int height, margin;
    if (size == QSSize.AutoNarrow || size == QSSize.Narrow) {
      height = R.dimen.qs_ribbon_height_small;
      margin = R.dimen.qs_tile_ribbon_icon_margin_small;
    } else {
      height = R.dimen.qs_ribbon_height_big;
      margin = R.dimen.qs_tile_ribbon_icon_margin_big;
    }
    Resources res = mContext.getResources();
    height = res.getDimensionPixelSize(height);
    margin = res.getDimensionPixelSize(margin);

    View parent = (View) mContainerView.getParent();
    LayoutParams lp = parent.getLayoutParams();
    lp.height = height;
    parent.setLayoutParams(lp);
    for (QuickSettingsTile t : mQuickSettingsTiles) {
      t.setImageMargins(margin);
    }
  }
  void loadTiles() {
    // Reset reference tiles
    mIMETile = null;

    // Filter items not compatible with device
    boolean cameraSupported = QSUtils.deviceSupportsCamera();
    boolean bluetoothSupported = QSUtils.deviceSupportsBluetooth();
    boolean mobileDataSupported = QSUtils.deviceSupportsMobileData(mContext);
    boolean lteSupported = QSUtils.deviceSupportsLte(mContext);
    boolean gpsSupported = QSUtils.deviceSupportsGps(mContext);
    boolean torchSupported = QSUtils.deviceSupportsTorch(mContext);

    if (!bluetoothSupported) {
      TILES_DEFAULT.remove(TILE_BLUETOOTH);
    }

    if (!mobileDataSupported) {
      TILES_DEFAULT.remove(TILE_WIFIAP);
      TILES_DEFAULT.remove(TILE_MOBILEDATA);
      TILES_DEFAULT.remove(TILE_NETWORKMODE);
    }

    if (!lteSupported) {
      TILES_DEFAULT.remove(TILE_LTE);
    }

    if (!gpsSupported) {
      TILES_DEFAULT.remove(TILE_GPS);
    }

    if (!torchSupported) {
      TILES_DEFAULT.remove(TILE_TORCH);
    }

    // Read the stored list of tiles
    ContentResolver resolver = mContext.getContentResolver();
    LayoutInflater inflater = LayoutInflater.from(mContext);
    String tiles =
        Settings.System.getStringForUser(resolver, mSettingsKey, UserHandle.USER_CURRENT);
    if (tiles == null) {
      Log.i(TAG, "Default tiles being loaded");
      tiles = TextUtils.join(TILE_DELIMITER, TILES_DEFAULT);
    }

    Log.i(TAG, "Tiles list: " + tiles);

    // Split out the tile names and add to the list
    boolean dockBatteryLoaded = false;
    NetworkController networkController =
        MSimTelephonyManager.getDefault().isMultiSimEnabled()
            ? mStatusBarService.mMSimNetworkController
            : mStatusBarService.mNetworkController;

    for (String tile : tiles.split("\\|")) {
      QuickSettingsTile qs = null;
      if (tile.equals(TILE_USER)) {
        qs = new UserTile(mContext, this, mHandler);
      } else if (tile.equals(TILE_BATTERY)) {
        qs = new BatteryTile(mContext, this, mStatusBarService.mBatteryController);
      } else if (tile.equals(TILE_SETTINGS)) {
        qs = new PreferencesTile(mContext, this);
      } else if (tile.equals(TILE_WIFI)) {
        qs = new WiFiTile(mContext, this, networkController);
      } else if (tile.equals(TILE_GPS)) {
        qs = new GPSTile(mContext, this, mStatusBarService.mLocationController);
      } else if (tile.equals(TILE_BLUETOOTH) && bluetoothSupported) {
        qs = new BluetoothTile(mContext, this, mStatusBarService.mBluetoothController);
      } else if (tile.equals(TILE_BRIGHTNESS)) {
        qs = new BrightnessTile(mContext, this);
      } else if (tile.equals(TILE_CAMERA) && cameraSupported) {
        qs = new CameraTile(mContext, this, mHandler);
      } else if (tile.equals(TILE_RINGER)) {
        qs = new RingerModeTile(mContext, this);
      } else if (tile.equals(TILE_SYNC)) {
        qs = new SyncTile(mContext, this, mHandler);
      } else if (tile.equals(TILE_WIFIAP) && mobileDataSupported) {
        qs = new WifiAPTile(mContext, this);
      } else if (tile.equals(TILE_SCREENTIMEOUT)) {
        qs = new ScreenTimeoutTile(mContext, this);
      } else if (tile.equals(TILE_MOBILEDATA) && mobileDataSupported) {
        qs = new MobileNetworkTile(mContext, this, networkController);
      } else if (tile.equals(TILE_LOCKSCREEN)) {
        qs = new ToggleLockscreenTile(mContext, this);
      } else if (tile.equals(TILE_NETWORKMODE) && mobileDataSupported) {
        qs = new MobileNetworkTypeTile(mContext, this, networkController);
      } else if (tile.equals(TILE_AUTOROTATE)) {
        qs = new AutoRotateTile(mContext, this);
      } else if (tile.equals(TILE_AIRPLANE)) {
        qs = new AirplaneModeTile(mContext, this, networkController);
      } else if (tile.equals(TILE_TORCH)) {
        qs = new TorchTile(mContext, this);
      } else if (tile.equals(TILE_SLEEP)) {
        qs = new SleepScreenTile(mContext, this);
      } else if (tile.equals(TILE_PROFILE)) {
        mTileStatusUris.add(Settings.System.getUriFor(Settings.System.SYSTEM_PROFILES_ENABLED));
        if (QSUtils.systemProfilesEnabled(resolver)) {
          qs = new ProfileTile(mContext, this);
        }
      } else if (tile.equals(TILE_PERFORMANCE_PROFILE)) {
        if (QSUtils.deviceSupportsPerformanceProfiles(mContext)) {
          qs = new PerformanceProfileTile(mContext, this);
        }
      } else if (tile.equals(TILE_NFC)) {
        // User cannot add the NFC tile if the device does not support it
        // No need to check again here
        qs = new NfcTile(mContext, this);
      } else if (tile.equals(TILE_WIMAX)) {
        // Not available yet
      } else if (tile.equals(TILE_LTE)) {
        qs = new LteTile(mContext, this);
      } else if (tile.equals(TILE_QUIETHOURS)) {
        qs = new QuietHoursTile(mContext, this);
      } else if (tile.equals(TILE_VOLUME)) {
        qs = new VolumeTile(mContext, this);
      } else if (tile.equals(TILE_EXPANDEDDESKTOP)) {
        mTileStatusUris.add(Settings.System.getUriFor(Settings.System.EXPANDED_DESKTOP_STYLE));
        if (QSUtils.expandedDesktopEnabled(resolver)) {
          qs = new ExpandedDesktopTile(mContext, this);
        }
      } else if (tile.equals(TILE_NETWORKADB)) {
        mTileStatusUris.add(Settings.Global.getUriFor(Settings.Global.ADB_ENABLED));
        if (QSUtils.adbEnabled(resolver)) {
          qs = new NetworkAdbTile(mContext, this);
        }
      } else if (tile.equals(TILE_COMPASS)) {
        qs = new CompassTile(mContext, this);
      } else if (tile.equals(TILE_HEADS_UP)) {
        qs = new HeadsUpTile(mContext, this);
      } else if (tile.equals(TILE_THEMES)) {
        qs = new ThemesTile(mContext, this);
      }

      if (qs != null) {
        qs.setupQuickSettingsTile(inflater, mContainerView);
        mQuickSettingsTiles.add(qs);

        // Add dock battery beside main battery when possible
        if (qs instanceof BatteryTile) {
          loadDockBatteryTile(resolver, inflater);
          dockBatteryLoaded = true;
        }
      }
    }

    if (mRibbonMode) {
      return;
    }

    // Load the dynamic tiles
    // These toggles must be the last ones added to the view, as they will show
    // only when they are needed
    if (Settings.System.getIntForUser(
            resolver, Settings.System.QS_DYNAMIC_ALARM, 1, UserHandle.USER_CURRENT)
        == 1) {
      QuickSettingsTile qs = new AlarmTile(mContext, this);
      qs.setupQuickSettingsTile(inflater, mContainerView);
      mQuickSettingsTiles.add(qs);
    }
    if (Settings.System.getIntForUser(
            resolver, Settings.System.QS_DYNAMIC_BUGREPORT, 1, UserHandle.USER_CURRENT)
        == 1) {
      QuickSettingsTile qs = new BugReportTile(mContext, this, mHandler);
      qs.setupQuickSettingsTile(inflater, mContainerView);
      mQuickSettingsTiles.add(qs);
    }
    if (!dockBatteryLoaded) {
      loadDockBatteryTile(resolver, inflater);
    }
    if (Settings.System.getIntForUser(
            resolver, Settings.System.QS_DYNAMIC_WIFI, 1, UserHandle.USER_CURRENT)
        == 1) {
      QuickSettingsTile qs = new RemoteDisplayTile(mContext, this);
      qs.setupQuickSettingsTile(inflater, mContainerView);
      mQuickSettingsTiles.add(qs);
    }
    if (QSUtils.deviceSupportsImeSwitcher(mContext)
        && Settings.System.getIntForUser(
                resolver, Settings.System.QS_DYNAMIC_IME, 1, UserHandle.USER_CURRENT)
            == 1) {
      mIMETile = new InputMethodTile(mContext, this);
      mIMETile.setupQuickSettingsTile(inflater, mContainerView);
      mQuickSettingsTiles.add(mIMETile);
    }
    if (QSUtils.deviceSupportsUsbTether(mContext)
        && Settings.System.getIntForUser(
                resolver, Settings.System.QS_DYNAMIC_USBTETHER, 1, UserHandle.USER_CURRENT)
            == 1) {
      QuickSettingsTile qs = new UsbTetherTile(mContext, this);
      qs.setupQuickSettingsTile(inflater, mContainerView);
      mQuickSettingsTiles.add(qs);
    }
    if (Settings.System.getIntForUser(
            resolver, Settings.System.QS_DYNAMIC_EQUALIZER, 1, UserHandle.USER_CURRENT)
        == 1) {
      QuickSettingsTile qs = new EqualizerTile(mContext, this);
      qs.setupQuickSettingsTile(inflater, mContainerView);
      mQuickSettingsTiles.add(qs);
    }
  }