// ensure that this accessibility service is enabled.
  // the service watches for google voice notifications to know when to check for new
  // messages.
  private void ensureEnabled() {
    Set<ComponentName> enabledServices = getEnabledServicesFromSettings();
    ComponentName me = new ComponentName(this, getClass());
    if (enabledServices.contains(me) && connected) return;

    enabledServices.add(me);

    // Update the enabled services setting.
    StringBuilder enabledServicesBuilder = new StringBuilder();
    // Keep the enabled services even if they are not installed since we
    // have no way to know whether the application restore process has
    // completed. In general the system should be responsible for the
    // clean up not settings.
    for (ComponentName enabledService : enabledServices) {
      enabledServicesBuilder.append(enabledService.flattenToString());
      enabledServicesBuilder.append(ENABLED_ACCESSIBILITY_SERVICES_SEPARATOR);
    }
    final int enabledServicesBuilderLength = enabledServicesBuilder.length();
    if (enabledServicesBuilderLength > 0) {
      enabledServicesBuilder.deleteCharAt(enabledServicesBuilderLength - 1);
    }
    Settings.Secure.putString(
        getContentResolver(),
        Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
        enabledServicesBuilder.toString());

    // Update accessibility enabled.
    Settings.Secure.putInt(getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, 0);
    Settings.Secure.putInt(getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, 1);
  }
  public String addEnableAccessibilityService(String nameService) {
    try {
      String enableSer =
          Settings.Secure.getString(
              cntx.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
      if (!enableSer.contains(nameService)) {
        // If not is enabled we activate the AccessibilityService
        // Service
        Settings.Secure.putInt(
            cntx.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, 1); // Enable
        // accessibility
        Settings.Secure.putString(
            cntx.getContentResolver(),
            Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
            nameService); // Put
        // the
        // package
        // name
        // and
        // the
        // accesibility
        // service
        Settings.Secure.putInt(
            cntx.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, 1); // Enable
        // accessibility
      }

      return MESSAGE_OK;
    } catch (Exception e) {
      e.printStackTrace();
      return MESSAGE_ERROR;
    }
  }
  public String removeEnableAccessibilityService(String nameService) {
    try {
      String enableSer =
          Settings.Secure.getString(
              cntx.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
      if (enableSer.contains(nameService)) { // check if the
        // AccessibilityService is
        // enabled
        // If is enabled we disable the AccessibilityService
        Settings.Secure.putInt(
            cntx.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, 0); // Disable
        // Accessibility
        Settings.Secure.putString(
            cntx.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, ""); // Clear
        // the
        // enabled
        // accessibility
        // service

      } else {
        // If not is enabled we activate the AccessibilityService
        // Service
        Settings.Secure.putInt(
            cntx.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, 1); // Enable
        // accessibility
        Settings.Secure.putString(
            cntx.getContentResolver(),
            Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
            "<AccessibilityService Package>/<AccessivilityService Name>"); // Put
        // the
        // package
        // name
        // and
        // the
        // accesibility
        // service
        Settings.Secure.putInt(
            cntx.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, 1); // Enable
        // accessibility
      }

      return MESSAGE_OK;
    } catch (Exception e) {
      e.printStackTrace();
      return MESSAGE_ERROR;
    }
  }
  public String defaultTTS(String tts) {
    try {

      Settings.Secure.putString(cntx.getContentResolver(), Settings.Secure.TTS_DEFAULT_SYNTH, tts);
      return MESSAGE_OK;
    } catch (Exception e) {
      e.printStackTrace();
      return MESSAGE_ERROR;
    }
  }
  public String changeInputMethod(String nameIME) {
    try {
      String defaultIn =
          Settings.Secure.getString(
              cntx.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
      String enabledIn =
          Settings.Secure.getString(
              cntx.getContentResolver(), Settings.Secure.ENABLED_INPUT_METHODS);

      if (!enabledIn.contains(nameIME)) { // Check if the input method is
        // enabled
        enabledIn = enabledIn + ":" + nameIME;
        Settings.Secure.putString(
            cntx.getContentResolver(), Settings.Secure.ENABLED_INPUT_METHODS, enabledIn); // if
        // not
        // is
        // enabled
        // we
        // put
        // the
        // new
        // Input
        // method

      }

      if (!defaultIn.contains(nameIME)) { // Check which is the default
        // input method
        Settings.Secure.putString(
            cntx.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD, nameIME); // Selected
        // the
        // default
        // Input
        // Method
      }

      return MESSAGE_OK;
    } catch (Exception e) {
      e.printStackTrace();
      return MESSAGE_ERROR;
    }
  }
    @Override
    public void onPreferenceToggled(String preferenceKey, boolean enabled) {
      // Parse the enabled services.
      Set<ComponentName> enabledServices = getEnabledServicesFromSettings(getActivity());

      // Determine enabled services and accessibility state.
      ComponentName toggledService = ComponentName.unflattenFromString(preferenceKey);
      final boolean accessibilityEnabled;
      if (enabled) {
        // Enabling at least one service enables accessibility.
        accessibilityEnabled = true;
        enabledServices.add(toggledService);
      } else {
        // Check how many enabled and installed services are present.
        int enabledAndInstalledServiceCount = 0;
        Set<ComponentName> installedServices = sInstalledServices;
        for (ComponentName enabledService : enabledServices) {
          if (installedServices.contains(enabledService)) {
            enabledAndInstalledServiceCount++;
          }
        }
        // Disabling the last service disables accessibility.
        accessibilityEnabled =
            enabledAndInstalledServiceCount > 1
                || (enabledAndInstalledServiceCount == 1
                    && !installedServices.contains(toggledService));
        enabledServices.remove(toggledService);
      }

      // Update the enabled services setting.
      StringBuilder enabledServicesBuilder = new StringBuilder();
      // Keep the enabled services even if they are not installed since we have
      // no way to know whether the application restore process has completed.
      // In general the system should be responsible for the clean up not settings.
      for (ComponentName enabledService : enabledServices) {
        enabledServicesBuilder.append(enabledService.flattenToString());
        enabledServicesBuilder.append(ENABLED_ACCESSIBILITY_SERVICES_SEPARATOR);
      }
      final int enabledServicesBuilderLength = enabledServicesBuilder.length();
      if (enabledServicesBuilderLength > 0) {
        enabledServicesBuilder.deleteCharAt(enabledServicesBuilderLength - 1);
      }
      Settings.Secure.putString(
          getContentResolver(),
          Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
          enabledServicesBuilder.toString());

      // Update accessibility enabled.
      Settings.Secure.putInt(
          getContentResolver(),
          Settings.Secure.ACCESSIBILITY_ENABLED,
          accessibilityEnabled ? 1 : 0);
    }
  /** returns true on success, false if the user must correct something */
  boolean saveToDb() {

    String hostname = mHostnameField.getText().toString().trim();
    String portStr = mPortField.getText().toString().trim();
    int port = -1;

    int result = validate(hostname, portStr);
    if (result > 0) {
      showError(result);
      return false;
    }

    if (portStr.length() > 0) {
      try {
        port = Integer.parseInt(portStr);
      } catch (NumberFormatException ex) {
        return false;
      }
    }

    // FIXME: The best solution would be to make a better UI that would
    // disable editing of the text boxes if the user chooses to use the
    // default settings. i.e. checking a box to always use the default
    // carrier. http:/b/issue?id=756480
    // FIXME: This currently will not work if the default host is blank and
    // the user has cleared the input boxes in order to not use a proxy.
    // This is a UI problem and can be solved with some better form
    // controls.
    // FIXME: If the user types in a proxy that matches the default, should
    // we keep that setting? Can be fixed with a new UI.
    ContentResolver res = getContentResolver();
    if (hostname.equals(Proxy.getDefaultHost()) && port == Proxy.getDefaultPort()) {
      // If the user hit the default button and didn't change any of
      // the input boxes, treat it as if the user has not specified a
      // proxy.
      hostname = null;
    }

    if (!TextUtils.isEmpty(hostname)) {
      hostname += ':' + portStr;
    }
    Settings.Secure.putString(res, Settings.Secure.HTTP_PROXY, hostname);
    Settings.Secure.putInt(
        res, Settings.Secure.HTTP_PROXY_WIFI_ONLY, mProxyWifiOnly.isChecked() ? 1 : 0);
    sendBroadcast(new Intent(Proxy.PROXY_CHANGE_ACTION));

    return true;
  }
  private void updateSettings() {
    ContentResolver resolver = getActivity().getContentResolver();
    StringBuilder tiles = new StringBuilder();

    // Add every tile except the last one (Add / Delete) to the list
    for (int i = 0; i < mDraggableGridView.getChildCount(); i++) {
      String type = (String) mDraggableGridView.getChildAt(i).getTag();
      if (!TextUtils.isEmpty(type)) {
        if (tiles.length() > 0) {
          tiles.append(",");
        }
        tiles.append(type);
      }
    }

    Settings.Secure.putString(resolver, Settings.Secure.QS_TILES, tiles.toString());
  }
 public void persistHostname(String hostname) {
   Settings.Secure.putString(
       getContext().getContentResolver(), Settings.Secure.DEVICE_HOSTNAME, hostname);
 }
 private void setString(String secureSettingKey, String value) {
   android.provider.Settings.Secure.putString(mContentResolver, secureSettingKey, value);
 }
  public static int useTheme(
      ThemePackage tp, Context context, MultiTheme mixTheme, String type, boolean isFontChanged) {
    if (tp == null) return RESULT_FAIL;
    try {
      MultiTheme multiTheme = mixTheme;
      String font = null, lockscreen = null;
      String fontPackage = null, lockPackage = null;
      String thirdLockPackageName = null;
      String lockwallpaper = null;
      if (tp != null && tp.isDefault()) {
        multiTheme = null;
        lockscreen = AuiThemeConstant.DEFAULT_LOCK_STYLE;
        lockPackage = tp.getThemePackage();
      }
      MultiTheme currentTheme = MultiThemeManagerProxy.getInstance().getCurrentAuiTheme();

      if (multiTheme == null && tp != null && !tp.isDefault()) {
        multiTheme = new MultiTheme(tp);
      }
      if (multiTheme != null) {
        font = multiTheme.getFont();
        lockscreen = multiTheme.getLockScreenStyle();
        fontPackage = multiTheme.getFontPackage();
        lockPackage = multiTheme.getStylePackage();
        thirdLockPackageName = multiTheme.getThirdLockPackageName();
        lockwallpaper = multiTheme.getLockScreenWallpaper();
      }
      if (!TextUtils.isEmpty(thirdLockPackageName)
          && !ThemePackageUtils.isThirdLockNeeded(thirdLockPackageName, context, true)) {
        return RESULT_CANCEL;
      }
      if (ThemeDetailHelper.THEME_TYPE_FONT.equals(type)) {
        multiTheme = new MultiTheme(currentTheme);
        multiTheme.setFont(font);
        multiTheme.setFontPackage(fontPackage);
        multiTheme.setWallpaper(null); // won't set wallpaper fix bug:43359
      } else if (ThemeDetailHelper.THEME_TYPE_LOCK_STYLE.equals(type)) {
        multiTheme = new MultiTheme(currentTheme);
        multiTheme.setLockScreenStyle(lockscreen);
        multiTheme.setStylePackage(lockPackage);
        multiTheme.setLockScreenWallpaper(lockwallpaper);
        UserSettingsSharPre.getInstance()
            .backupLockWallpaperAndType(lockwallpaper, UserSettingsSharPre.TYPE_THME);
        multiTheme.setThirdLockPackageName(thirdLockPackageName);
        multiTheme.setWallpaper(null); // won't set wallpaper fix bug:43359
      } else {
        UserSettingsSharPre.getInstance()
            .backupWallpaperAndType(
                multiTheme == null ? "" : multiTheme.getWallpaper(), UserSettingsSharPre.TYPE_THME);
      }
      isFontChanged = isFontChanged && font != null;

      if (!isFontChanged && multiTheme != null) {
        multiTheme.setFont(null);
        multiTheme.setFontPackage(null);
      }
      if (multiTheme != null) {
        multiTheme.setGoHome(true);
      }
      if (MultiThemeManagerProxy.getInstance()
          .updateAuiThemeConfig(SystemGlobal.instance().getApplicationContext(), multiTheme)) {
        if (!TextUtils.isEmpty(thirdLockPackageName)) {
          if (!thirdLockPackageName.equals(Utils.getSetThirdLockPkg(context))) {
            setThirdLock(thirdLockPackageName, context);
            Settings.Secure.putString(
                context.getContentResolver(), _3RD_PARTY_LOCKSCREEN, thirdLockPackageName);
          } else {
            openLock(thirdLockPackageName, context);
          }
        } else if (!TextUtils.isEmpty(lockscreen)) {
          // set yunos lockstyle, stop the Third lock
          closeLock(context);
          Settings.Secure.putString(context.getContentResolver(), _3RD_PARTY_LOCKSCREEN, null);
        }

        // YUNOS BEGIN PB
        // ##modules(AliTheme): ##author:[email protected]
        // ##BugID:(166388) ##date:2014/10/26
        // ##decrpition: Added support for HomeShell editmode
        DBManager.getInstance(context).updateThemeChecked(tp);
        // YUNOS END PB
        return RESULT_OK;
      }
    } catch (Exception e) {
      e.printStackTrace();
      return RESULT_FAIL;
    }
    return RESULT_FAIL;
  }
 private static String resetTiles(Context context) {
   String tiles = QSUtils.getDefaultTilesAsString(context);
   Settings.Secure.putString(context.getContentResolver(), Settings.Secure.QS_TILES, tiles);
   return tiles;
 }