1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.health.connect.exportimport; 18 19 import static android.health.connect.Constants.DEFAULT_INT; 20 21 import android.annotation.IntRange; 22 import android.annotation.NonNull; 23 import android.annotation.Nullable; 24 import android.net.Uri; 25 import android.os.Parcel; 26 import android.os.Parcelable; 27 28 import java.util.Objects; 29 30 /** 31 * Settings for configuring the scheduled export service. 32 * 33 * @hide 34 */ 35 public final class ScheduledExportSettings implements Parcelable { 36 @NonNull 37 public static final Creator<ScheduledExportSettings> CREATOR = 38 new Creator<>() { 39 @Override 40 public ScheduledExportSettings createFromParcel(Parcel in) { 41 return new ScheduledExportSettings(in); 42 } 43 44 @Override 45 public ScheduledExportSettings[] newArray(int size) { 46 return new ScheduledExportSettings[size]; 47 } 48 }; 49 50 @Nullable private final Uri mUri; 51 private final int mPeriodInDays; 52 53 @Override equals(Object o)54 public boolean equals(Object o) { 55 if (this == o) return true; 56 if (!(o instanceof ScheduledExportSettings that)) return false; 57 return mPeriodInDays == that.mPeriodInDays && Objects.equals(mUri, that.mUri); 58 } 59 60 @Override hashCode()61 public int hashCode() { 62 return Objects.hash(mUri, mPeriodInDays); 63 } 64 65 /** 66 * Returns a {@link ScheduledExportSettings} to update the URI to write to when exporting data. 67 */ withUri(@onNull Uri uri)68 public static ScheduledExportSettings withUri(@NonNull Uri uri) { 69 Objects.requireNonNull(uri); 70 71 return new ScheduledExportSettings(uri, DEFAULT_INT); 72 } 73 74 /** 75 * Returns a {@link ScheduledExportSettings} to update the period in days between scheduled 76 * exports. 77 */ withPeriodInDays( @ntRangefrom = 0, to = 30) int periodInDays)78 public static ScheduledExportSettings withPeriodInDays( 79 @IntRange(from = 0, to = 30) int periodInDays) { 80 if (periodInDays < 0 || periodInDays > 30) { 81 throw new IllegalArgumentException("periodInDays should be between 0 and 30"); 82 } 83 84 return new ScheduledExportSettings(null, periodInDays); 85 } 86 87 /** 88 * Returns a {@link ScheduledExportSettings} to update the period in days between scheduled 89 * exports. 90 */ withUriAndPeriodInDays( @onNull Uri uri, @IntRange(from = 0, to = 30) int periodInDays)91 public static ScheduledExportSettings withUriAndPeriodInDays( 92 @NonNull Uri uri, @IntRange(from = 0, to = 30) int periodInDays) { 93 // TODO: b/345152760 - change to builder pattern so we can avoid duplicated argument check 94 Objects.requireNonNull(uri); 95 if (periodInDays < 0 || periodInDays > 30) { 96 throw new IllegalArgumentException("periodInDays should be between 0 and 30"); 97 } 98 99 return new ScheduledExportSettings(uri, periodInDays); 100 } 101 ScheduledExportSettings(@onNull Parcel in)102 private ScheduledExportSettings(@NonNull Parcel in) { 103 boolean hasUri = in.readBoolean(); 104 mUri = hasUri ? Uri.parse(in.readString()) : null; 105 106 mPeriodInDays = in.readInt(); 107 } 108 ScheduledExportSettings(@ullable Uri uri, int periodInDays)109 private ScheduledExportSettings(@Nullable Uri uri, int periodInDays) { 110 mUri = uri; 111 mPeriodInDays = periodInDays; 112 } 113 114 /** Returns the URI to write to when exporting data or null to keep the existing URI. */ 115 @Nullable getUri()116 public Uri getUri() { 117 return mUri; 118 } 119 120 /** 121 * Returns the period between scheduled exports in days or {@code DEFAULT_INT} to keep the 122 * existing period. 123 */ getPeriodInDays()124 public int getPeriodInDays() { 125 return mPeriodInDays; 126 } 127 128 @Override describeContents()129 public int describeContents() { 130 return 0; 131 } 132 133 @Override writeToParcel(@onNull Parcel dest, int flags)134 public void writeToParcel(@NonNull Parcel dest, int flags) { 135 dest.writeBoolean(mUri != null); 136 if (mUri != null) { 137 dest.writeString(mUri.toString()); 138 } 139 140 dest.writeInt(mPeriodInDays); 141 } 142 } 143