Add a SchedulesProvider

- Provide an abstract api named getScheduleInfoList.
- Implement a ScheduleInfo class.

Fixes: 148995400
Test: compilation
Change-Id: I236627daa478fb2d2d138a8832680b1ec2464b3b
diff --git a/packages/SettingsLib/Android.bp b/packages/SettingsLib/Android.bp
index 6212493..3f42ad4 100644
--- a/packages/SettingsLib/Android.bp
+++ b/packages/SettingsLib/Android.bp
@@ -27,6 +27,7 @@
         "SettingsLibRadioButtonPreference",
         "WifiTrackerLib",
         "SettingsLibDisplayDensityUtils",
+        "SettingsLibSchedulesProvider",
     ],
 
     // ANDROIDMK TRANSLATION ERROR: unsupported assignment to LOCAL_SHARED_JAVA_LIBRARIES
diff --git a/packages/SettingsLib/SchedulesProvider/Android.bp b/packages/SettingsLib/SchedulesProvider/Android.bp
new file mode 100644
index 0000000..ef59252
--- /dev/null
+++ b/packages/SettingsLib/SchedulesProvider/Android.bp
@@ -0,0 +1,12 @@
+android_library {
+    name: "SettingsLibSchedulesProvider",
+
+    srcs: ["src/**/*.java"],
+
+    static_libs: [
+          "androidx.annotation_annotation",
+    ],
+
+    sdk_version: "system_current",
+    min_sdk_version: "21",
+}
diff --git a/packages/SettingsLib/SchedulesProvider/AndroidManifest.xml b/packages/SettingsLib/SchedulesProvider/AndroidManifest.xml
new file mode 100644
index 0000000..1b0e4bf
--- /dev/null
+++ b/packages/SettingsLib/SchedulesProvider/AndroidManifest.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2020 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License
+  -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+          package="com.android.settingslib.schedulesprovider">
+
+    <uses-sdk android:minSdkVersion="21" />
+
+</manifest>
diff --git a/packages/SettingsLib/SchedulesProvider/src/com/android/settingslib/schedulesprovider/ScheduleInfo.java b/packages/SettingsLib/SchedulesProvider/src/com/android/settingslib/schedulesprovider/ScheduleInfo.java
new file mode 100644
index 0000000..7d2b8e2
--- /dev/null
+++ b/packages/SettingsLib/SchedulesProvider/src/com/android/settingslib/schedulesprovider/ScheduleInfo.java
@@ -0,0 +1,167 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settingslib.schedulesprovider;
+
+import android.content.Intent;
+import android.os.Parcel;
+import android.os.Parcelable;
+import android.text.TextUtils;
+
+import androidx.annotation.NonNull;
+
+/**
+ * This is a schedule data item. It contains the schedule title text, the summary text which
+ * displays on the summary of the Settings preference and an {@link Intent}. Intent is able to
+ * launch the editing page of the schedule data when user clicks this item (preference).
+ */
+public class ScheduleInfo implements Parcelable {
+    private static final String TAG = "ScheduleInfo";
+    private final String mTitle;
+    private final String mSummary;
+    private final Intent mIntent;
+
+    public ScheduleInfo(Builder builder) {
+        mTitle = builder.mTitle;
+        mSummary = builder.mSummary;
+        mIntent = builder.mIntent;
+    }
+
+    protected ScheduleInfo(Parcel in) {
+        mTitle = in.readString();
+        mSummary = in.readString();
+        mIntent = in.readParcelable(Intent.class.getClassLoader());
+    }
+
+    /**
+     * Returns the title text.
+     *
+     * @return The title.
+     */
+    public String getTitle() {
+        return mTitle;
+    }
+
+    /**
+     * Returns the summary text.
+     *
+     * @return The summary.
+     */
+    public String getSummary() {
+        return mSummary;
+    }
+
+    /**
+     * Returns an {@link Intent}.
+     */
+    public Intent getIntent() {
+        return mIntent;
+    }
+
+    /**
+     * Verify the member variables are valid.
+     *
+     * @return {@code true} if all member variables are valid.
+     */
+    public boolean isValid() {
+        return !TextUtils.isEmpty(mTitle) && !TextUtils.isEmpty(mSummary) && (mIntent != null);
+    }
+
+    @Override
+    public void writeToParcel(Parcel dest, int flags) {
+        dest.writeString(mTitle);
+        dest.writeString(mSummary);
+        dest.writeParcelable(mIntent, flags);
+    }
+
+    @Override
+    public int describeContents() {
+        return 0;
+    }
+
+    public static final Creator<ScheduleInfo> CREATOR = new Creator<ScheduleInfo>() {
+        @Override
+        public ScheduleInfo createFromParcel(Parcel in) {
+            return new ScheduleInfo(in);
+        }
+
+        @Override
+        public ScheduleInfo[] newArray(int size) {
+            return new ScheduleInfo[size];
+        }
+    };
+
+    @NonNull
+    @Override
+    public String toString() {
+        return "title : " + mTitle + " summary : " + mSummary + (mIntent == null
+                ? " and intent is null." : ".");
+    }
+
+    /**
+     * A simple builder for {@link ScheduleInfo}.
+     */
+    public static class Builder {
+        @NonNull
+        private String mTitle;
+        @NonNull
+        private String mSummary;
+        @NonNull
+        private Intent mIntent;
+
+        /**
+         * Sets the title.
+         *
+         * @param title The title of the preference item.
+         * @return This instance.
+         */
+        public Builder setTitle(@NonNull String title) {
+            mTitle = title;
+            return this;
+        }
+
+        /**
+         * Sets the summary.
+         *
+         * @param summary The summary of the preference summary.
+         * @return This instance.
+         */
+        public Builder setSummary(@NonNull String summary) {
+            mSummary = summary;
+            return this;
+        }
+
+        /**
+         * Sets the {@link Intent}.
+         *
+         * @param intent The action when user clicks the preference.
+         * @return This instance.
+         */
+        public Builder setIntent(@NonNull Intent intent) {
+            mIntent = intent;
+            return this;
+        }
+
+        /**
+         * Creates an instance of {@link ScheduleInfo}.
+         *
+         * @return The instance of {@link ScheduleInfo}.
+         */
+        public ScheduleInfo build() {
+            return new ScheduleInfo(this);
+        }
+    }
+}
diff --git a/packages/SettingsLib/SchedulesProvider/src/com/android/settingslib/schedulesprovider/SchedulesProvider.java b/packages/SettingsLib/SchedulesProvider/src/com/android/settingslib/schedulesprovider/SchedulesProvider.java
new file mode 100644
index 0000000..a423e47
--- /dev/null
+++ b/packages/SettingsLib/SchedulesProvider/src/com/android/settingslib/schedulesprovider/SchedulesProvider.java
@@ -0,0 +1,133 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.settingslib.schedulesprovider;
+
+import android.content.ContentProvider;
+import android.content.ContentValues;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.Bundle;
+import android.os.SystemProperties;
+import android.util.Log;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * This provider is a bridge for client apps to provide the schedule data.
+ * Client provider needs to implement their {@link #getScheduleInfoList()} and returns a list of
+ * {@link ScheduleInfo}.
+ */
+public abstract class SchedulesProvider extends ContentProvider {
+    public static final String METHOD_GENERATE_SCHEDULE_INFO_LIST = "generateScheduleInfoList";
+    public static final String BUNDLE_SCHEDULE_INFO_LIST = "scheduleInfoList";
+    private static final String TAG = "SchedulesProvider";
+
+    @Override
+    public boolean onCreate() {
+        return true;
+    }
+
+    @Override
+    public final Cursor query(
+            Uri uri, String[] projection, String selection, String[] selectionArgs,
+            String sortOrder) {
+        throw new UnsupportedOperationException("Query operation is not supported currently.");
+    }
+
+    @Override
+    public final String getType(Uri uri) {
+        throw new UnsupportedOperationException("GetType operation is not supported currently.");
+    }
+
+    @Override
+    public final Uri insert(Uri uri, ContentValues values) {
+        throw new UnsupportedOperationException("Insert operation is not supported currently.");
+    }
+
+    @Override
+    public final int delete(Uri uri, String selection, String[] selectionArgs) {
+        throw new UnsupportedOperationException("Delete operation not supported currently.");
+    }
+
+    @Override
+    public final int update(Uri uri, ContentValues values, String selection,
+            String[] selectionArgs) {
+        throw new UnsupportedOperationException("Update operation is not supported currently.");
+    }
+
+    /**
+     * Return the list of the schedule information.
+     *
+     * @return a list of the {@link ScheduleInfo}.
+     */
+    public abstract ArrayList<ScheduleInfo> getScheduleInfoList();
+
+    /**
+     * Returns a bundle which contains a list of {@link ScheduleInfo} and data types:
+     * scheduleInfoList : ArrayList<ScheduleInfo>
+     */
+    @Override
+    public Bundle call(@NonNull String method, @Nullable String arg, @Nullable Bundle extras) {
+        final Bundle bundle = new Bundle();
+        if (METHOD_GENERATE_SCHEDULE_INFO_LIST.equals(method)) {
+            final ArrayList<ScheduleInfo> scheduleInfoList = filterInvalidData(
+                    getScheduleInfoList());
+            if (scheduleInfoList != null) {
+                bundle.putParcelableArrayList(BUNDLE_SCHEDULE_INFO_LIST, scheduleInfoList);
+            }
+        }
+        return bundle;
+    }
+
+    /**
+     * To filter the invalid schedule info.
+     *
+     * @param scheduleInfoList The list of the {@link ScheduleInfo}.
+     * @return The valid list of the {@link ScheduleInfo}.
+     */
+    private ArrayList<ScheduleInfo> filterInvalidData(ArrayList<ScheduleInfo> scheduleInfoList) {
+        if (scheduleInfoList == null) {
+            Log.d(TAG, "package : " + getContext().getPackageName() + " has no scheduling data.");
+            return null;
+        }
+        // Dump invalid data in debug mode.
+        if (SystemProperties.getInt("ro.debuggable", 0) == 1) {
+            new Thread(() -> {
+                dumpInvalidData(scheduleInfoList);
+            }).start();
+        }
+        final List<ScheduleInfo> filteredList = scheduleInfoList
+                .stream()
+                .filter(scheduleInfo -> scheduleInfo.isValid())
+                .collect(Collectors.toList());
+
+        return new ArrayList<>(filteredList);
+    }
+
+    private void dumpInvalidData(ArrayList<ScheduleInfo> scheduleInfoList) {
+        Log.d(TAG, "package : " + getContext().getPackageName()
+                + " provided some scheduling data are invalid.");
+        scheduleInfoList
+                .stream()
+                .filter(scheduleInfo -> !scheduleInfo.isValid())
+                .forEach(scheduleInfo -> Log.d(TAG, scheduleInfo.toString()));
+    }
+}