Ejemplo n.º 1
0
 private Condition parseExistingTimeCondition(Condition condition) {
   if (condition == null) return null;
   final long time = ZenModeConfig.tryParseCountdownConditionId(condition.id);
   if (time == 0) return null;
   final long now = System.currentTimeMillis();
   final long span = time - now;
   if (span <= 0 || span > MAX_BUCKET_MINUTES * MINUTES_MS) return null;
   return ZenModeConfig.toTimeCondition(
       mContext,
       time,
       Math.round(span / (float) MINUTES_MS),
       now,
       ActivityManager.getCurrentUser());
 }
Ejemplo n.º 2
0
 private void onClickTimeButton(View row, ConditionTag tag, boolean up) {
   Condition newCondition = null;
   final int N = MINUTE_BUCKETS.length;
   if (mBucketIndex == -1) {
     // not on a known index, search for the next or prev bucket by time
     final Uri conditionId = getConditionId(tag.condition);
     final long time = ZenModeConfig.tryParseCountdownConditionId(conditionId);
     final long now = System.currentTimeMillis();
     for (int i = 0; i < N; i++) {
       int j = up ? i : N - 1 - i;
       final int bucketMinutes = MINUTE_BUCKETS[j];
       final long bucketTime = now + bucketMinutes * MINUTES_MS;
       if (up && bucketTime > time || !up && bucketTime < time) {
         mBucketIndex = j;
         newCondition =
             ZenModeConfig.toTimeCondition(
                 mContext, bucketTime, bucketMinutes, now, ActivityManager.getCurrentUser());
         break;
       }
     }
     if (newCondition == null) {
       mBucketIndex = DEFAULT_BUCKET_INDEX;
       newCondition =
           ZenModeConfig.toTimeCondition(
               mContext, MINUTE_BUCKETS[mBucketIndex], ActivityManager.getCurrentUser());
     }
   } else {
     // on a known index, simply increment or decrement
     mBucketIndex = Math.max(0, Math.min(N - 1, mBucketIndex + (up ? 1 : -1)));
     newCondition =
         ZenModeConfig.toTimeCondition(
             mContext, MINUTE_BUCKETS[mBucketIndex], ActivityManager.getCurrentUser());
   }
   mTimeCondition = newCondition;
   bind(mTimeCondition, row);
   tag.rb.setChecked(true);
   select(mTimeCondition);
   announceConditionSelection(tag);
 }
Ejemplo n.º 3
0
  private void bind(final Condition condition, final View row) {
    if (condition == null) throw new IllegalArgumentException("condition must not be null");
    final boolean enabled = condition.state == Condition.STATE_TRUE;
    final ConditionTag tag =
        row.getTag() != null ? (ConditionTag) row.getTag() : new ConditionTag();
    row.setTag(tag);
    final boolean first = tag.rb == null;
    if (tag.rb == null) {
      tag.rb = (RadioButton) row.findViewById(android.R.id.checkbox);
    }
    tag.condition = condition;
    final Uri conditionId = getConditionId(tag.condition);
    if (DEBUG)
      Log.d(
          mTag,
          "bind i="
              + mZenConditions.indexOfChild(row)
              + " first="
              + first
              + " condition="
              + conditionId);
    tag.rb.setEnabled(enabled);
    final boolean checked =
        (mSessionExitCondition != null || mAttachedZen != Global.ZEN_MODE_OFF)
            && (sameConditionId(mSessionExitCondition, tag.condition)
                || isCountdown(mSessionExitCondition) && isCountdown(tag.condition));
    if (checked != tag.rb.isChecked()) {
      if (DEBUG) Log.d(mTag, "bind checked=" + checked + " condition=" + conditionId);
      tag.rb.setChecked(checked);
    }
    tag.rb.setOnCheckedChangeListener(
        new OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (mExpanded && isChecked) {
              if (DEBUG) Log.d(mTag, "onCheckedChanged " + conditionId);
              final int N = getVisibleConditions();
              for (int i = 0; i < N; i++) {
                final ConditionTag childTag = getConditionTagAt(i);
                if (childTag == null || childTag == tag) continue;
                childTag.rb.setChecked(false);
              }
              select(tag.condition);
              announceConditionSelection(tag);
            }
          }
        });

    if (tag.lines == null) {
      tag.lines = row.findViewById(android.R.id.content);
    }
    if (tag.line1 == null) {
      tag.line1 = (TextView) row.findViewById(android.R.id.text1);
    }
    if (tag.line2 == null) {
      tag.line2 = (TextView) row.findViewById(android.R.id.text2);
    }
    final String line1 = !TextUtils.isEmpty(condition.line1) ? condition.line1 : condition.summary;
    final String line2 = condition.line2;
    tag.line1.setText(line1);
    if (TextUtils.isEmpty(line2)) {
      tag.line2.setVisibility(GONE);
    } else {
      tag.line2.setVisibility(VISIBLE);
      tag.line2.setText(line2);
    }
    tag.lines.setEnabled(enabled);
    tag.lines.setAlpha(enabled ? 1 : .4f);

    final ImageView button1 = (ImageView) row.findViewById(android.R.id.button1);
    button1.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            onClickTimeButton(row, tag, false /*down*/);
          }
        });

    final ImageView button2 = (ImageView) row.findViewById(android.R.id.button2);
    button2.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            onClickTimeButton(row, tag, true /*up*/);
          }
        });
    tag.lines.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            tag.rb.setChecked(true);
          }
        });

    final long time = ZenModeConfig.tryParseCountdownConditionId(conditionId);
    if (time > 0) {
      button1.setVisibility(VISIBLE);
      button2.setVisibility(VISIBLE);
      if (mBucketIndex > -1) {
        button1.setEnabled(mBucketIndex > 0);
        button2.setEnabled(mBucketIndex < MINUTE_BUCKETS.length - 1);
      } else {
        final long span = time - System.currentTimeMillis();
        button1.setEnabled(span > MIN_BUCKET_MINUTES * MINUTES_MS);
        final Condition maxCondition =
            ZenModeConfig.toTimeCondition(
                mContext, MAX_BUCKET_MINUTES, ActivityManager.getCurrentUser());
        button2.setEnabled(!Objects.equals(condition.summary, maxCondition.summary));
      }

      button1.setAlpha(button1.isEnabled() ? 1f : .5f);
      button2.setAlpha(button2.isEnabled() ? 1f : .5f);
    } else {
      button1.setVisibility(GONE);
      button2.setVisibility(GONE);
    }
    // wire up interaction callbacks for newly-added condition rows
    if (first) {
      Interaction.register(tag.rb, mInteractionCallback);
      Interaction.register(tag.lines, mInteractionCallback);
      Interaction.register(button1, mInteractionCallback);
      Interaction.register(button2, mInteractionCallback);
    }
    row.setVisibility(VISIBLE);
  }