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

Commit 7477c225 authored by Haijie Hong's avatar Haijie Hong
Browse files

make device setting be able to use either Intent or PendingIntent

BUG: 343317785
Test: local tested
Flag: com.android.settings.flags.enable_bluetooth_device_details_polish
Change-Id: I42583b358255c6d0226b739ba33cadc1d67d860a
parent 7590443d
Loading
Loading
Loading
Loading
+16 −17
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.settingslib.bluetooth.devicesettings;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Parcel;
@@ -36,7 +35,7 @@ public class ActionSwitchPreference extends DeviceSettingPreference implements P
    private final String mTitle;
    private final String mSummary;
    private final Bitmap mIcon;
    private final Intent mIntent;
    private final DeviceSettingAction mAction;
    private final boolean mHasSwitch;
    private final boolean mChecked;
    private final boolean mIsAllowedChangingState;
@@ -46,7 +45,7 @@ public class ActionSwitchPreference extends DeviceSettingPreference implements P
            String title,
            @Nullable String summary,
            @Nullable Bitmap icon,
            @Nullable Intent intent,
            @NonNull DeviceSettingAction action,
            boolean hasSwitch,
            boolean checked,
            boolean allowChangingState,
@@ -56,7 +55,7 @@ public class ActionSwitchPreference extends DeviceSettingPreference implements P
        mTitle = title;
        mSummary = summary;
        mIcon = icon;
        mIntent = intent;
        mAction = action;
        mHasSwitch = hasSwitch;
        mChecked = checked;
        mIsAllowedChangingState = allowChangingState;
@@ -79,13 +78,13 @@ public class ActionSwitchPreference extends DeviceSettingPreference implements P
        String title = in.readString();
        String summary = in.readString();
        Bitmap icon = in.readParcelable(Bitmap.class.getClassLoader());
        Intent intent = in.readParcelable(Intent.class.getClassLoader());
        DeviceSettingAction action = DeviceSettingAction.readFromParcel(in);
        boolean hasSwitch = in.readBoolean();
        boolean checked = in.readBoolean();
        boolean allowChangingState = in.readBoolean();
        Bundle extras = in.readBundle(Bundle.class.getClassLoader());
        return new ActionSwitchPreference(
                title, summary, icon, intent, hasSwitch, checked, allowChangingState, extras);
                title, summary, icon, action, hasSwitch, checked, allowChangingState, extras);
    }

    public static final Creator<ActionSwitchPreference> CREATOR =
@@ -115,7 +114,7 @@ public class ActionSwitchPreference extends DeviceSettingPreference implements P
        dest.writeString(mTitle);
        dest.writeString(mSummary);
        dest.writeParcelable(mIcon, flags);
        dest.writeParcelable(mIntent, flags);
        mAction.writeToParcel(dest, flags);
        dest.writeBoolean(mHasSwitch);
        dest.writeBoolean(mChecked);
        dest.writeBoolean(mIsAllowedChangingState);
@@ -127,7 +126,7 @@ public class ActionSwitchPreference extends DeviceSettingPreference implements P
        private String mTitle;
        private String mSummary;
        private Bitmap mIcon;
        private Intent mIntent;
        private DeviceSettingAction mAction = DeviceSettingAction.EMPTY_ACTION;
        private boolean mHasSwitch;
        private boolean mChecked;
        private boolean mIsAllowedChangingState;
@@ -170,14 +169,14 @@ public class ActionSwitchPreference extends DeviceSettingPreference implements P
        }

        /**
         * Sets the Intent to launch when the preference is clicked, optional.
         * Sets the action to trigger when the preference is clicked, optional.
         *
         * @param intent The Intent.
         * @param action The action to trigger.
         * @return Returns the Builder object.
         */
        @NonNull
        public Builder setIntent(@Nullable Intent intent) {
            mIntent = intent;
        public Builder setAction(@Nullable DeviceSettingAction action) {
            mAction = action == null ? DeviceSettingAction.EMPTY_ACTION : action;
            return this;
        }

@@ -239,7 +238,7 @@ public class ActionSwitchPreference extends DeviceSettingPreference implements P
                    mTitle,
                    mSummary,
                    mIcon,
                    mIntent,
                    mAction,
                    mHasSwitch,
                    mChecked,
                    mIsAllowedChangingState,
@@ -278,13 +277,13 @@ public class ActionSwitchPreference extends DeviceSettingPreference implements P
    }

    /**
     * Gets the Intent to launch when the preference is clicked.
     * Gets the action to trigger when the preference is clicked.
     *
     * @return Returns the intent to launch.
     */
    @Nullable
    public Intent getIntent() {
        return mIntent;
    @NonNull
    public DeviceSettingAction getAction() {
        return mAction;
    }

    /**
+61 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.bluetooth.devicesettings;

import android.os.Parcel;

import androidx.annotation.NonNull;

/** An abstract class representing a device setting action. */
public abstract class DeviceSettingAction {
    @DeviceSettingActionType private final int mActionType;

    public static final DeviceSettingAction EMPTY_ACTION =
            new DeviceSettingAction(DeviceSettingActionType.DEVICE_SETTING_ACTION_TYPE_UNKNOWN) {};

    protected DeviceSettingAction(@DeviceSettingActionType int actionType) {
        mActionType = actionType;
    }

    /** Read a {@link DeviceSettingPreference} instance from {@link Parcel} */
    @NonNull
    public static DeviceSettingAction readFromParcel(@NonNull Parcel in) {
        int type = in.readInt();
        return switch (type) {
            case DeviceSettingActionType.DEVICE_SETTING_ACTION_TYPE_INTENT ->
                    DeviceSettingIntentAction.readFromParcel(in);
            case DeviceSettingActionType.DEVICE_SETTING_ACTION_TYPE_PENDING_INTENT ->
                    DeviceSettingPendingIntentAction.readFromParcel(in);
            default -> EMPTY_ACTION;
        };
    }

    /** Writes the instance to {@link Parcel}. */
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeInt(mActionType);
    }

    /**
     * Gets the setting action type, as defined by IntDef {@link DeviceSettingActionType}.
     *
     * @return the setting action type.
     */
    @DeviceSettingType
    public int getActionType() {
        return mActionType;
    }
}
+41 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.bluetooth.devicesettings;

import androidx.annotation.IntDef;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.SOURCE)
@IntDef(
        value = {
            DeviceSettingActionType.DEVICE_SETTING_ACTION_TYPE_UNKNOWN,
            DeviceSettingActionType.DEVICE_SETTING_ACTION_TYPE_INTENT,
            DeviceSettingActionType.DEVICE_SETTING_ACTION_TYPE_PENDING_INTENT,
        },
        open = true)
public @interface DeviceSettingActionType {
    /** Device setting action type is unknown. */
    int DEVICE_SETTING_ACTION_TYPE_UNKNOWN = 0;

    /** Device setting action is an intent to start an activity. */
    int DEVICE_SETTING_ACTION_TYPE_INTENT = 1;

    /** Device setting action is a pending intent. */
    int DEVICE_SETTING_ACTION_TYPE_PENDING_INTENT = 2;
}
+132 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.bluetooth.devicesettings;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;

import java.util.Objects;

/** An abstract class representing a device setting action. */
public class DeviceSettingIntentAction extends DeviceSettingAction implements Parcelable {
    private final Intent mIntent;
    private final Bundle mExtras;

    DeviceSettingIntentAction(@NonNull Intent intent, @NonNull Bundle extras) {
        super(DeviceSettingActionType.DEVICE_SETTING_ACTION_TYPE_INTENT);
        validate(intent);
        mIntent = intent;
        mExtras = extras;
    }

    private static void validate(Intent intent) {
        if (Objects.isNull(intent)) {
            throw new IllegalArgumentException("Intent must be set");
        }
    }

    /** Read a {@link DeviceSettingIntentAction} instance from {@link Parcel} */
    @NonNull
    public static DeviceSettingIntentAction readFromParcel(@NonNull Parcel in) {
        Intent intent = in.readParcelable(Intent.class.getClassLoader());
        Bundle extras = in.readBundle(Bundle.class.getClassLoader());
        return new DeviceSettingIntentAction(intent, extras);
    }

    public static final Creator<DeviceSettingIntentAction> CREATOR =
            new Creator<>() {
                @Override
                @NonNull
                public DeviceSettingIntentAction createFromParcel(@NonNull Parcel in) {
                    in.readInt();
                    return readFromParcel(in);
                }

                @Override
                @NonNull
                public DeviceSettingIntentAction[] newArray(int size) {
                    return new DeviceSettingIntentAction[size];
                }
            };

    @Override
    public int describeContents() {
        return 0;
    }

    /** Writes the instance to {@link Parcel}. */
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeParcelable(mIntent, flags);
        dest.writeBundle(mExtras);
    }

    /** Builder class for {@link DeviceSettingFooterPreference}. */
    public static final class Builder {
        private Intent mIntent = null;
        private Bundle mExtras = Bundle.EMPTY;

        /**
         * Sets the intent for the action.
         *
         * @param intent The intent.
         * @return Returns the Builder object.
         */
        @NonNull
        public Builder setIntent(@NonNull Intent intent) {
            mIntent = intent;
            return this;
        }

        /**
         * Sets the extras bundle.
         *
         * @return Returns the Builder object.
         */
        @NonNull
        public Builder setExtras(@NonNull Bundle extras) {
            mExtras = extras;
            return this;
        }

        /**
         * Builds the {@link DeviceSettingIntentAction} object.
         *
         * @return Returns the built {@link DeviceSettingIntentAction} object.
         */
        @NonNull
        public DeviceSettingIntentAction build() {
            return new DeviceSettingIntentAction(mIntent, mExtras);
        }
    }

    /** Gets the intent. */
    @NonNull
    public Intent getIntent() {
        return mIntent;
    }

    /** Gets the extra bundle. */
    @NonNull
    public Bundle getExtras() {
        return mExtras;
    }
}
+133 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.bluetooth.devicesettings;

import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;

import java.util.Objects;

/** An abstract class representing a device setting action. */
public class DeviceSettingPendingIntentAction extends DeviceSettingAction implements Parcelable {
    private final PendingIntent mPendingIntent;
    private final Bundle mExtras;

    DeviceSettingPendingIntentAction(@NonNull PendingIntent pendingIntent, @NonNull Bundle extras) {
        super(DeviceSettingActionType.DEVICE_SETTING_ACTION_TYPE_PENDING_INTENT);
        validate(pendingIntent);
        mPendingIntent = pendingIntent;
        mExtras = extras;
    }

    private static void validate(PendingIntent pendingIntent) {
        if (Objects.isNull(pendingIntent)) {
            throw new IllegalArgumentException("PendingIntent must be set");
        }
    }

    /** Read a {@link DeviceSettingPendingIntentAction} instance from {@link Parcel} */
    @NonNull
    public static DeviceSettingPendingIntentAction readFromParcel(@NonNull Parcel in) {
        PendingIntent pendingIntent = in.readParcelable(Intent.class.getClassLoader());
        Bundle extras = in.readBundle(Bundle.class.getClassLoader());
        return new DeviceSettingPendingIntentAction(pendingIntent, extras);
    }

    public static final Creator<DeviceSettingPendingIntentAction> CREATOR =
            new Creator<>() {
                @Override
                @NonNull
                public DeviceSettingPendingIntentAction createFromParcel(@NonNull Parcel in) {
                    in.readInt();
                    return readFromParcel(in);
                }

                @Override
                @NonNull
                public DeviceSettingPendingIntentAction[] newArray(int size) {
                    return new DeviceSettingPendingIntentAction[size];
                }
            };

    @Override
    public int describeContents() {
        return 0;
    }

    /** Writes the instance to {@link Parcel}. */
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeParcelable(mPendingIntent, flags);
        dest.writeBundle(mExtras);
    }

    /** Builder class for {@link DeviceSettingFooterPreference}. */
    public static final class Builder {
        private PendingIntent mPendingIntent = null;
        private Bundle mExtras = Bundle.EMPTY;

        /**
         * Sets the intent for the action.
         *
         * @param pendingIntent The pending intent.
         * @return Returns the Builder object.
         */
        @NonNull
        public Builder setPendingIntent(@NonNull PendingIntent pendingIntent) {
            mPendingIntent = pendingIntent;
            return this;
        }

        /**
         * Sets the extras bundle.
         *
         * @return Returns the Builder object.
         */
        @NonNull
        public Builder setExtras(@NonNull Bundle extras) {
            mExtras = extras;
            return this;
        }

        /**
         * Builds the {@link DeviceSettingPendingIntentAction} object.
         *
         * @return Returns the built {@link DeviceSettingPendingIntentAction} object.
         */
        @NonNull
        public DeviceSettingPendingIntentAction build() {
            return new DeviceSettingPendingIntentAction(mPendingIntent, mExtras);
        }
    }

    /** Gets the pending intent. */
    @NonNull
    public PendingIntent getPendingIntent() {
        return mPendingIntent;
    }

    /** Gets the extra bundle. */
    @NonNull
    public Bundle getExtras() {
        return mExtras;
    }
}
Loading