private View buildQSTile(String tileType) {
    QSTileHolder item = QSTileHolder.from(getActivity(), tileType);
    if (item == null) {
      return null;
    }
    ColoringCardView qsTile =
        (ColoringCardView) getLayoutInflater(null).inflate(R.layout.qs_item, null);
    int defaultColor = getResources().getColor(R.color.qs_tile_default_background_color);
    qsTile.setColor(defaultColor);
    if (item.name != null) {
      ImageView icon = (ImageView) qsTile.findViewById(android.R.id.icon);
      Drawable d = Utils.getNamedDrawable(getSystemUIContext(getActivity()), item.resourceName);
      d.setColorFilter(
          getResources().getColor(R.color.qs_tile_tint_color), PorterDuff.Mode.SRC_ATOP);
      icon.setImageDrawable(d);
      TextView title = (TextView) qsTile.findViewById(android.R.id.title);
      title.setText(item.name);

      ImageView type = (ImageView) qsTile.findViewById(R.id.type);
      d =
          getActivity()
              .getDrawable(
                  QSUtils.isDynamicQsTile(tileType)
                      ? R.drawable.ic_qs_tile_dynamic_type
                      : R.drawable.ic_qs_tile_static_type);
      type.setImageDrawable(d);
    }
    qsTile.setTag(tileType);

    return qsTile;
  }
 public static int determineTileCount(Context context) {
   String order =
       Settings.Secure.getString(context.getContentResolver(), Settings.Secure.QS_TILES);
   if (order == null) {
     order = QSUtils.getDefaultTilesAsString(context);
   }
   if (TextUtils.isEmpty(order)) {
     return 0;
   }
   return order.split(",").length;
 }
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
      ContentResolver resolver = getActivity().getContentResolver();

      // We load the added tiles and compare it to the list of available tiles.
      // We only show the tiles that aren't already on the grid.
      String order = Settings.Secure.getString(resolver, Settings.Secure.QS_TILES);

      List<String> savedTiles = Arrays.asList(order.split(","));

      final List<QSTileHolder> tilesList = new ArrayList<QSTileHolder>();
      for (String tile : QSUtils.getAvailableTiles(getActivity())) {
        // Don't count the already added tiles
        if (!savedTiles.contains(tile)) {
          QSTileHolder holder = QSTileHolder.from(getActivity(), tile);
          if (holder != null) {
            tilesList.add(holder);
          }
        }
      }

      final DialogInterface.OnClickListener selectionListener =
          new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
              dialog.dismiss();
              final QSTiles tiles = (QSTiles) getTargetFragment();
              if (tiles != null) {
                tiles.addTile(tilesList.get(which).value);
              }
            }
          };

      Collections.sort(
          tilesList,
          new Comparator<QSTileHolder>() {
            @Override
            public int compare(QSTileHolder lhs, QSTileHolder rhs) {
              return lhs.name.compareTo(rhs.name);
            }
          });

      final QSListAdapter adapter =
          new QSListAdapter(getActivity(), getSystemUIContext(getActivity()), tilesList);
      return new AlertDialog.Builder(getActivity())
          .setTitle(R.string.add_qs)
          .setSingleChoiceItems(adapter, -1, selectionListener)
          .setNegativeButton(R.string.cancel, null)
          .create();
    }
  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 updateAddDeleteState() {
    int activeTiles = mDraggableGridView.getChildCount() - (mDraggingActive ? 2 : 1);
    boolean limitReached = activeTiles >= QSUtils.getAvailableTiles(getActivity()).size();
    int iconResId = mDraggingActive ? R.drawable.ic_menu_delete : R.drawable.ic_menu_add_dark;
    int titleResId =
        mDraggingActive
            ? R.string.qs_action_delete
            : limitReached ? R.string.qs_action_no_more_tiles : R.string.qs_action_add;

    TextView title = (TextView) mAddDeleteTile.findViewById(android.R.id.title);
    ImageView icon = (ImageView) mAddDeleteTile.findViewById(android.R.id.icon);

    title.setText(titleResId);
    title.setEnabled(!limitReached);

    icon.setImageResource(iconResId);
    icon.setEnabled(!limitReached);
  }
 private static String resetTiles(Context context) {
   String tiles = QSUtils.getDefaultTilesAsString(context);
   Settings.Secure.putString(context.getContentResolver(), Settings.Secure.QS_TILES, tiles);
   return tiles;
 }
  private QSTile<?> createTile(String tileSpec) {
    // Ensure tile is supported on this device
    if (!QSUtils.getAvailableTiles(mContext).contains(tileSpec)) {
      return null;
    }
    if (QSUtils.isDynamicQsTile(tileSpec)) {
      return null;
    }

    switch (tileSpec) {
      case QSConstants.TILE_WIFI:
        return new WifiTile(this);
      case QSConstants.TILE_BLUETOOTH:
        return new BluetoothTile(this);
      case QSConstants.TILE_INVERSION:
        return new ColorInversionTile(this);
      case QSConstants.TILE_CELLULAR:
        return new CellularTile(this);
      case QSConstants.TILE_AIRPLANE:
        return new AirplaneModeTile(this);
      case QSConstants.TILE_ROTATION:
        return new RotationLockTile(this);
      case QSConstants.TILE_FLASHLIGHT:
        return new FlashlightTile(this);
      case QSConstants.TILE_LOCATION:
        return new LocationTile(this);
      case QSConstants.TILE_CAST:
        return new CastTile(this);
      case QSConstants.TILE_HOTSPOT:
        return new HotspotTile(this);
      case QSConstants.TILE_NOTIFICATIONS:
        return new NotificationsTile(this);
      case QSConstants.TILE_DATA:
        return new DataTile(this);
      case QSConstants.TILE_ROAMING:
        return new RoamingTile(this);
      case QSConstants.TILE_DDS:
        return new DdsTile(this);
      case QSConstants.TILE_COMPASS:
        return new CompassTile(this);
      case QSConstants.TILE_APN:
        return new ApnTile(this);
      case QSConstants.TILE_PROFILES:
        return new ProfilesTile(this);
      case QSConstants.TILE_PERFORMANCE:
        return new PerfProfileTile(this);
      case QSConstants.TILE_ADB_NETWORK:
        return new AdbOverNetworkTile(this);
      case QSConstants.TILE_NFC:
        return new NfcTile(this);
      case QSConstants.TILE_LOCKSCREEN:
        return new LockscreenToggleTile(this);
      case QSConstants.TILE_LTE:
        return new LteTile(this);
      case QSConstants.TILE_VISUALIZER:
        return new VisualizerTile(this);
      case QSConstants.TILE_SCREEN_TIMEOUT:
        return new ScreenTimeoutTile(this);
      case QSConstants.TILE_LIVE_DISPLAY:
        return new LiveDisplayTile(this);
      case QSConstants.TILE_USB_TETHER:
        return new UsbTetherTile(this);
      case QSConstants.TILE_HEADS_UP:
        return new HeadsUpTile(this);
      case QSConstants.TILE_AMBIENT_DISPLAY:
        return new AmbientDisplayTile(this);
      case QSConstants.TILE_SYNC:
        return new SyncTile(this);
      default:
        throw new IllegalArgumentException("Bad tile spec: " + tileSpec);
    }
  }
  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);
    }
  }