Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d4c62b02 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add a SchedulesProvider"

parents 7c1f1342 9de5cca3
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ android_library {
        "SettingsLibRadioButtonPreference",
        "WifiTrackerLib",
        "SettingsLibDisplayDensityUtils",
        "SettingsLibSchedulesProvider",
    ],

    // ANDROIDMK TRANSLATION ERROR: unsupported assignment to LOCAL_SHARED_JAVA_LIBRARIES
+12 −0
Original line number Diff line number Diff line
android_library {
    name: "SettingsLibSchedulesProvider",

    srcs: ["src/**/*.java"],

    static_libs: [
          "androidx.annotation_annotation",
    ],

    sdk_version: "system_current",
    min_sdk_version: "21",
}
+23 −0
Original line number Diff line number Diff line
<?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>
+167 −0
Original line number Diff line number Diff line
/*
 * 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);
        }
    }
}
+133 −0
Original line number Diff line number Diff line
/*
 * 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()));
    }
}