summaryrefslogtreecommitdiff
path: root/src/com/android/settings/bluetooth/GroupBluetoothSettingsPreference.java
blob: f55d6cfe698d4d464d208846378097d7368cf6d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/******************************************************************************
 *  Copyright (c) 2020, The Linux Foundation. All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are
 *  met:
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials provided
 *        with the distribution.
 *      * Neither the name of The Linux Foundation nor the names of its
 *        contributors may be used to endorse or promote products derived
 *        from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 *  ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 *  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/

package com.android.settings.bluetooth;

import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH;

import com.android.settings.bluetooth.BluetoothDevicePreference.SortType;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.widget.GearPreference;

import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;


import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.UserManager;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Pair;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;

import com.android.settings.R;


public class GroupBluetoothSettingsPreference extends GearPreference {

    private static final String TAG = "GroupBluetoothSettingsPreference";

    private static int sDimAlpha = Integer.MIN_VALUE;

    private int mGroupId = -1;

    private final UserManager mUserManager;

    private String contentDescription = null;
    private boolean mHideSecondTarget = false;
    private Resources mResources;
    private int mVisibleCount  = 0;

    public GroupBluetoothSettingsPreference(Context context, int groupId) {
        super(context, null);
        mGroupId = groupId;
        mResources = getContext().getResources();
        mUserManager = context.getSystemService(UserManager.class);
        if (sDimAlpha == Integer.MIN_VALUE) {
            TypedValue outValue = new TypedValue();
            context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, outValue, true);
            sDimAlpha = (int) (outValue.getFloat() * 255);
        }
        mVisibleCount = 0;
        onDeviceAttributesChanged();
    }

    @Override
    protected boolean shouldHideSecondTarget() {
        return mUserManager.hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH)
                || mHideSecondTarget;
    }

    public void hideSecondTarget(boolean hideSecondTarget) {
        mHideSecondTarget = hideSecondTarget;
    }

    public void onDeviceAttributesChanged() {
        /*
         * The preference framework takes care of making sure the value has
         * changed before proceeding. It will also call notifyChanged() if
         * any preference info has changed from the previous value.
         */
        Context context = getContext();
        String title  = context.getString(R.string.group_settings);
        setTitle(title + " " + (mGroupId + GroupUtils.GROUP_START_VAL));

        // Used to gray out the item
        setEnabled(true); // Change dynamically if required

        setVisible(true); // Change to dynamic if required
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder view) {
        // Disable this view if the bluetooth enable/disable preference view is off
        if (null != findPreferenceInHierarchy("bt_checkbox")) {
            setDependency("bt_checkbox");
        }

            ImageView deviceDetails = (ImageView) view.findViewById(R.id.settings_button);

            if (deviceDetails != null) {
                deviceDetails.setOnClickListener(this);
            }
        final ImageView imageView = (ImageView) view.findViewById(android.R.id.icon);
        if (imageView != null) {
            imageView.setContentDescription(contentDescription);
            // Set property to prevent Talkback from reading out.
            imageView.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
            imageView.setVisibility(ImageView.VISIBLE);
        }
        super.onBindViewHolder(view);
    }


    @Override
    protected int getSecondTargetResId() {
        return R.layout.preference_widget_gear;
    }

    public int getGroupId() {
        return mGroupId;
    }

    void onClicked() {
    }

    public int getVisibleCount() {
        return mVisibleCount;
    }

    public int incrementChildCound() {
        return ++mVisibleCount;
    }

    public int decrementChildCount() {
        return --mVisibleCount;
    }

    public boolean isRemovePref() {
        if (mVisibleCount == 0) {
            return true;
        } else {
            return  false;
        }
    }
}