Sudheer Shanka | 7ff866d | 2016-01-12 17:22:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.graphics.drawable.Drawable; |
| 21 | import android.support.v7.preference.DropDownPreference; |
| 22 | import android.support.v7.preference.PreferenceViewHolder; |
| 23 | import android.util.AttributeSet; |
| 24 | import android.view.View; |
| 25 | import android.view.ViewGroup; |
| 26 | import android.widget.AdapterView; |
| 27 | import android.widget.AdapterView.OnItemSelectedListener; |
| 28 | import android.widget.ArrayAdapter; |
| 29 | import android.widget.Spinner; |
| 30 | import android.widget.TextView; |
| 31 | |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.List; |
| 34 | |
| 35 | import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; |
| 36 | |
| 37 | public class RestrictedDropDownPreference extends DropDownPreference { |
| 38 | private Spinner mSpinner; |
| 39 | private final Drawable mRestrictedPadlock; |
| 40 | private final int mRestrictedPadlockPadding; |
| 41 | private List<RestrictedItem> mRestrictedItems = new ArrayList<>(); |
| 42 | |
| 43 | public RestrictedDropDownPreference(Context context) { |
| 44 | this(context, null); |
| 45 | } |
| 46 | |
| 47 | public RestrictedDropDownPreference(Context context, AttributeSet attrs) { |
| 48 | super(context, attrs); |
| 49 | |
| 50 | mRestrictedPadlock = RestrictedLockUtils.getRestrictedPadlock(context); |
| 51 | mRestrictedPadlockPadding = context.getResources().getDimensionPixelSize( |
Sudheer Shanka | 40400a6 | 2016-02-23 11:42:11 +0000 | [diff] [blame] | 52 | R.dimen.restricted_icon_padding); |
Sudheer Shanka | 7ff866d | 2016-01-12 17:22:06 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | private final OnItemSelectedListener mItemSelectedListener = new OnItemSelectedListener() { |
| 56 | @Override |
| 57 | public void onItemSelected(AdapterView<?> parent, View v, int position, long id) { |
| 58 | if (position >= 0) { |
| 59 | String value = getEntryValues()[position].toString(); |
| 60 | RestrictedItem item = getRestrictedItemForEntryValue(value); |
| 61 | if (item != null) { |
| 62 | RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(), |
| 63 | item.enforcedAdmin); |
| 64 | mSpinner.setSelection(findIndexOfValue(getValue())); |
| 65 | } else if (!value.equals(getValue()) && callChangeListener(value)) { |
| 66 | setValue(value); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void onNothingSelected(AdapterView<?> parent) { |
| 73 | // noop |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | @Override |
| 78 | protected ArrayAdapter createAdapter() { |
| 79 | return new RestrictedArrayItemAdapter(getContext()); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void setValue(String value) { |
| 84 | if (getRestrictedItemForEntryValue(value) != null) { |
| 85 | return; |
| 86 | } |
| 87 | super.setValue(value); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void onBindViewHolder(PreferenceViewHolder view) { |
| 92 | super.onBindViewHolder(view); |
| 93 | mSpinner = (Spinner) view.itemView.findViewById(R.id.spinner); |
| 94 | mSpinner.setOnItemSelectedListener(mItemSelectedListener); |
| 95 | } |
| 96 | |
| 97 | private class RestrictedArrayItemAdapter extends ArrayAdapter<String> { |
| 98 | public RestrictedArrayItemAdapter(Context context) { |
| 99 | super(context, R.layout.spinner_dropdown_restricted_item); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public View getDropDownView(int position, View convertView, ViewGroup parent) { |
| 104 | TextView view = (TextView) super.getView(position, convertView, parent); |
| 105 | CharSequence entry = getItem(position); |
| 106 | boolean isEntryRestricted = isRestrictedForEntry(entry); |
| 107 | RestrictedLockUtils.setTextViewPadlock(getContext(), view, isEntryRestricted); |
| 108 | view.setEnabled(!isEntryRestricted); |
| 109 | return view; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private boolean isRestrictedForEntry(CharSequence entry) { |
| 114 | if (entry == null) { |
| 115 | return false; |
| 116 | } |
| 117 | for (RestrictedItem item : mRestrictedItems) { |
| 118 | if (entry.equals(item.entry)) { |
| 119 | return true; |
| 120 | } |
| 121 | } |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | private RestrictedItem getRestrictedItemForEntryValue(CharSequence entryValue) { |
| 126 | if (entryValue == null) { |
| 127 | return null; |
| 128 | } |
| 129 | for (RestrictedItem item : mRestrictedItems) { |
| 130 | if (entryValue.equals(item.entryValue)) { |
| 131 | return item; |
| 132 | } |
| 133 | } |
| 134 | return null; |
| 135 | } |
| 136 | |
| 137 | public void addRestrictedItem(RestrictedItem item) { |
| 138 | mRestrictedItems.add(item); |
| 139 | } |
| 140 | |
| 141 | public static class RestrictedItem { |
| 142 | public CharSequence entry; |
| 143 | public CharSequence entryValue; |
| 144 | public EnforcedAdmin enforcedAdmin; |
| 145 | |
| 146 | public RestrictedItem(CharSequence entry, CharSequence entryValue, |
| 147 | EnforcedAdmin enforcedAdmin) { |
| 148 | this.entry = entry; |
| 149 | this.entryValue = entryValue; |
| 150 | this.enforcedAdmin = enforcedAdmin; |
| 151 | } |
| 152 | } |
| 153 | } |