@Override
  protected View onCreateDialogView() {
    Resources res = getContext().getResources();
    final Context context = getContext();
    final View root =
        new DialogBuilder(context)
            .setIcon(mIcon)
            .setTitle(mTitle)
            .setContentView(R.layout.preference_dialog_timeout)
            .createView();

    Config config = Config.getInstance();

    mProgresses = new int[1];
    mGroups = new Group[mProgresses.length];
    mGroups[0] =
        new Group(
            (SeekBar) root.findViewById(R.id.normal_timeout_seekbar),
            (TextView) root.findViewById(R.id.normal_timeout_value),
            config.getOption(Config.KEY_NOTIFY_DECAY_TIME));

    final int max = res.getInteger(R.integer.config_timeout_maxDurationMillis) / MULTIPLIER;
    mMin = res.getInteger(R.integer.config_timeout_minDurationMillis) / MULTIPLIER;
    mSoftStoredLabels = new SoftReference[max + 1];

    for (Group group : mGroups) {
      int progress = (int) group.option.read(config);
      group.seekBar.setOnSeekBarChangeListener(this);
      group.seekBar.setMax(max);
      group.seekBar.setProgress(progress / MULTIPLIER);
    }

    return root;
  }
 private void updateTimeoutSummary(Config config) {
   mTimeoutPreference.setSummary(
       ResUtils.getString(
           getResources(),
           R.string.settings_timeout_summary,
           Float.toString(config.getTimeoutNormal() / 1000f),
           Float.toString(config.getTimeoutShort() / 1000f)));
 }
  @Override
  public void onResume() {
    super.onResume();
    Config config = getConfig();
    config.registerListener(this);

    updateInactiveHoursSummary(config);
    updateTimeoutSummary(config);
  }
  public Enabler(Context context, Switch switch_, String key) {
    mContext = context;

    mKey = key;
    mConfig = Config.getInstance();
    mOption = mConfig.getHashMap().get(mKey);

    mSwitch = switch_;
  }
  private void onEnabledInternal(Context context) {
    if (mConfig != null) {
      return; // already initialized
    }

    mConfig = Config.getInstance();
    mConfig.registerListener(this);

    if (DEBUG) Log.d(TAG, "Toggle widget enabled");
  }
 private void updateInactiveHoursSummary(Config config) {
   if (config.isInactiveTimeEnabled()) {
     int from = config.getInactiveTimeFrom();
     int to = config.getInactiveTimeTo();
     mInactiveHoursPreference.setSummary(
         ResUtils.getString(
             getResources(),
             R.string.settings_inactive_hours_enabled,
             DateUtils.formatTime(getActivity(), from / 60, from % 60),
             DateUtils.formatTime(getActivity(), to / 60, to % 60)));
   } else {
     mInactiveHoursPreference.setSummary(getString(R.string.settings_inactive_hours_disabled));
   }
 }
  private void onDisabledInternal(Context context) {
    if (mConfig == null) {
      return; // not initialized
    }

    mConfig.unregisterListener(this);
    mConfig = null;

    if (DEBUG) Log.d(TAG, "Toggle widget disabled");
  }
  @Override
  protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);

    if (!positiveResult) {
      return;
    }

    // Save changes to config.
    Config config = Config.getInstance();
    for (Group group : mGroups) {
      int value = group.seekBar.getProgress() * MULTIPLIER;
      group.option.write(config, getContext(), value, null);
    }
  }
  private void updateWidgets(Context context) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

    ComponentName cn = new ComponentName(context, ToggleWidgetProvider.class);
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(cn);
    for (int appWidgetId : appWidgetIds) {

      // Create an Intent to launch ReceiverActivity to toggle AcDisplay.
      // Probably doing same using BroadcastReceiver would be better solution.
      Intent intent = new Intent(App.ACTION_TOGGLE);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
      Resources res = context.getResources();

      // Get the layout for the App Widget and attach an on-click listener
      // to the button
      RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.appwidget_toggle_layout);
      rv.setOnClickPendingIntent(R.id.container, pendingIntent);
      rv.setTextViewText(
          R.id.title, res.getString(mConfig.isEnabled() ? R.string.enabled : R.string.disabled));

      // Tell the AppWidgetManager to perform an update on the current app widget
      appWidgetManager.updateAppWidget(appWidgetId, rv);
    }
  }
 @Override
 public void onPause() {
   super.onPause();
   Config config = getConfig();
   config.unregisterListener(this);
 }
 @Override
 public Config getConfig() {
   return Config.getInstance();
 }
 public void pause() {
   mConfig.unregisterListener(this);
   mSwitch.setOnCheckedChangeListener(null);
 }
 public void resume() {
   mConfig.registerListener(this);
   mSwitch.setOnCheckedChangeListener(this);
   updateState();
 }