/**
  * Returns true if user preference with the given user handle is set to 24-hour format.
  *
  * @param context the context to use for the content resolver
  * @param userHandle the user handle of the user to query.
  * @return true if 24 hour time format is selected, false otherwise.
  * @hide
  */
 public static boolean is24HourFormat(Context context, int userHandle) {
   String setting =
       Settings.System.getStringForUser(
           context.getContentResolver(), Settings.System.TIME_12_24, userHandle);
   Locale locale = context.getResources().getConfiguration().locale;
   return is24HourFormat(setting, locale);
 }
  /** Remove favorite if current app was uninstalled. */
  private void mayBeRemoveFavoriteEntry(String packageName) {
    ContentResolver resolver = mContext.getContentResolver();
    final String favorites =
        Settings.System.getStringForUser(
            resolver, Settings.System.RECENT_PANEL_FAVORITES, UserHandle.USER_CURRENT);
    String entryToSave = "";

    if (favorites == null || favorites != null && favorites.isEmpty()) {
      return;
    }
    for (String favorite : favorites.split("\\|")) {
      if (favorite.toLowerCase().contains(packageName.toLowerCase())) {
        continue;
      }
      entryToSave += favorite + "|";
    }
    if (!entryToSave.isEmpty()) {
      entryToSave = entryToSave.substring(0, entryToSave.length() - 1);
    }

    Settings.System.putStringForUser(
        resolver, Settings.System.RECENT_PANEL_FAVORITES, entryToSave, UserHandle.USER_CURRENT);
  }
  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);
    }
  }