1 /*
2  * Copyright (C) 2020 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 com.android.settingslib.schedulesprovider;
18 
19 import android.app.PendingIntent;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 import android.text.TextUtils;
25 
26 import androidx.annotation.NonNull;
27 
28 /**
29  * Schedule data item containing the schedule title text, the summary text which is displayed on the
30  * summary of the Settings preference and a {@link PendingIntent} which Settings will launch
31  * when the user clicks on the preference.
32  */
33 public class ScheduleInfo implements Parcelable {
34     private static final String TAG = "ScheduleInfo";
35     private final String mTitle;
36     private final String mSummary;
37     private final PendingIntent mPendingIntent;
38 
ScheduleInfo(Builder builder)39     public ScheduleInfo(Builder builder) {
40         mTitle = builder.mTitle;
41         mSummary = builder.mSummary;
42         mPendingIntent = builder.mPendingIntent;
43     }
44 
ScheduleInfo(Parcel in)45     private ScheduleInfo(Parcel in) {
46         mTitle = in.readString();
47         mSummary = in.readString();
48         mPendingIntent = in.readParcelable(PendingIntent.class.getClassLoader());
49     }
50 
51     /**
52      * Returns the title text.
53      */
getTitle()54     public String getTitle() {
55         return mTitle;
56     }
57 
58     /**
59      * Returns the summary text.
60      */
getSummary()61     public String getSummary() {
62         return mSummary;
63     }
64 
65     /**
66      * Returns a {@link PendingIntent} which Settings will launch when the user clicks on a
67      * schedule preference.
68      */
getPendingIntent()69     public PendingIntent getPendingIntent() {
70         return mPendingIntent;
71     }
72 
73     /**
74      * Verify the member variables are valid.
75      *
76      * @return {@code true} if all member variables are valid.
77      */
isValid()78     public boolean isValid() {
79         return !TextUtils.isEmpty(mTitle) && !TextUtils.isEmpty(mSummary)
80                 && (mPendingIntent != null);
81     }
82 
83     @Override
writeToParcel(Parcel dest, int flags)84     public void writeToParcel(Parcel dest, int flags) {
85         dest.writeString(mTitle);
86         dest.writeString(mSummary);
87         dest.writeParcelable(mPendingIntent, flags);
88     }
89 
90     @Override
describeContents()91     public int describeContents() {
92         return 0;
93     }
94 
95     public static final Creator<ScheduleInfo> CREATOR = new Creator<ScheduleInfo>() {
96         @Override
97         public ScheduleInfo createFromParcel(Parcel in) {
98             return new ScheduleInfo(in);
99         }
100 
101         @Override
102         public ScheduleInfo[] newArray(int size) {
103             return new ScheduleInfo[size];
104         }
105     };
106 
107     @NonNull
108     @Override
toString()109     public String toString() {
110         return "title: " + mTitle + ", summary: " + mSummary + ", pendingIntent: " + mPendingIntent;
111     }
112 
113     /**
114      * A simple builder for {@link ScheduleInfo}.
115      */
116     public static class Builder {
117         private String mTitle;
118         private String mSummary;
119         private PendingIntent mPendingIntent;
120 
121         /**
122          * Sets the title.
123          *
124          * @param title The title of the preference item.
125          * @return This instance.
126          */
setTitle(@onNull String title)127         public Builder setTitle(@NonNull String title) {
128             mTitle = title;
129             return this;
130         }
131 
132         /**
133          * Sets the summary.
134          *
135          * @param summary The summary of the preference summary.
136          * @return This instance.
137          */
setSummary(@onNull String summary)138         public Builder setSummary(@NonNull String summary) {
139             mSummary = summary;
140             return this;
141         }
142 
143         /**
144          * Sets the {@link PendingIntent}.
145          * <p>The {@link PendingIntent} should be created with
146          * {@link PendingIntent#getActivity(Context, int, Intent, int)}.
147          *
148          * @param pendingIntent The pending intent to send when the user clicks the preference.
149          * @return This instance.
150          */
setPendingIntent(@onNull PendingIntent pendingIntent)151         public Builder setPendingIntent(@NonNull PendingIntent pendingIntent) {
152             mPendingIntent = pendingIntent;
153             return this;
154         }
155 
156         /**
157          * Creates an instance of {@link ScheduleInfo}.
158          *
159          * @return The instance of {@link ScheduleInfo}.
160          */
build()161         public ScheduleInfo build() {
162             return new ScheduleInfo(this);
163         }
164     }
165 }
166