summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/DropDownPreference.java
diff options
context:
space:
mode:
authorJason Monk <jmonk@google.com>2015-02-26 11:33:22 -0500
committerJason Monk <jmonk@google.com>2015-03-19 15:28:40 -0400
commit60a09ea2388db01a661beeb9995a7ecf770a4200 (patch)
tree52e1c8071aedb4a90befc966eddce971ab2d752b /src/com/android/settings/DropDownPreference.java
parentad99996ffb93a699cde3c27f13c3dea3028a4046 (diff)
downloadpackages_apps_Settings-60a09ea2388db01a661beeb9995a7ecf770a4200.tar.gz
packages_apps_Settings-60a09ea2388db01a661beeb9995a7ecf770a4200.tar.bz2
packages_apps_Settings-60a09ea2388db01a661beeb9995a7ecf770a4200.zip
Start implementation of new app info storage page
Things are mostly in the right places, may need some minor location adjustment on everything and styling for the buttons. Bug: 19511439 Change-Id: If7730285d6ddc36e32cc8bc119885a8e215c0eb5
Diffstat (limited to 'src/com/android/settings/DropDownPreference.java')
-rw-r--r--src/com/android/settings/DropDownPreference.java131
1 files changed, 131 insertions, 0 deletions
diff --git a/src/com/android/settings/DropDownPreference.java b/src/com/android/settings/DropDownPreference.java
new file mode 100644
index 000000000..8f8ec78c1
--- /dev/null
+++ b/src/com/android/settings/DropDownPreference.java
@@ -0,0 +1,131 @@
+/*
+ * Copyright (C) 2014 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.settings;
+
+import android.content.Context;
+import android.preference.Preference;
+import android.util.AttributeSet;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.AdapterView;
+import android.widget.ArrayAdapter;
+import android.widget.Spinner;
+import android.widget.AdapterView.OnItemSelectedListener;
+
+import java.util.ArrayList;
+
+public class DropDownPreference extends Preference {
+ private final Context mContext;
+ private final ArrayAdapter<String> mAdapter;
+ private final Spinner mSpinner;
+ private final ArrayList<Object> mValues = new ArrayList<Object>();
+
+ private Callback mCallback;
+
+ public DropDownPreference(Context context) {
+ this(context, null);
+ }
+
+ public DropDownPreference(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ mContext = context;
+ mAdapter = new ArrayAdapter<String>(mContext,
+ android.R.layout.simple_spinner_dropdown_item);
+
+ mSpinner = new Spinner(mContext);
+
+ mSpinner.setVisibility(View.INVISIBLE);
+ mSpinner.setAdapter(mAdapter);
+ mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
+ @Override
+ public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
+ setSelectedItem(position);
+ }
+
+ @Override
+ public void onNothingSelected(AdapterView<?> parent) {
+ // noop
+ }
+ });
+ setPersistent(false);
+ setOnPreferenceClickListener(new OnPreferenceClickListener() {
+ @Override
+ public boolean onPreferenceClick(Preference preference) {
+ mSpinner.performClick();
+ return true;
+ }
+ });
+ }
+
+ public void setDropDownWidth(int dimenResId) {
+ mSpinner.setDropDownWidth(mContext.getResources().getDimensionPixelSize(dimenResId));
+ }
+
+ public void setCallback(Callback callback) {
+ mCallback = callback;
+ }
+
+ public void setSelectedItem(int position) {
+ final Object value = mValues.get(position);
+ if (mCallback != null && !mCallback.onItemSelected(position, value)) {
+ return;
+ }
+ mSpinner.setSelection(position);
+ setSummary(mAdapter.getItem(position));
+ final boolean disableDependents = value == null;
+ notifyDependencyChange(disableDependents);
+ }
+
+ public void setSelectedValue(Object value) {
+ final int i = mValues.indexOf(value);
+ if (i > -1) {
+ setSelectedItem(i);
+ }
+ }
+
+ public void addItem(int captionResid, Object value) {
+ addItem(mContext.getResources().getString(captionResid), value);
+ }
+
+ public void addItem(String caption, Object value) {
+ mAdapter.add(caption);
+ mValues.add(value);
+ }
+
+ public void clearItems(){
+ mAdapter.clear();
+ mValues.clear();
+ }
+
+ @Override
+ protected void onBindView(View view) {
+ super.onBindView(view);
+ if (view.equals(mSpinner.getParent())) return;
+ if (mSpinner.getParent() != null) {
+ ((ViewGroup)mSpinner.getParent()).removeView(mSpinner);
+ }
+ final ViewGroup vg = (ViewGroup)view;
+ vg.addView(mSpinner, 0);
+ final ViewGroup.LayoutParams lp = mSpinner.getLayoutParams();
+ lp.width = 0;
+ mSpinner.setLayoutParams(lp);
+ }
+
+ public interface Callback {
+ boolean onItemSelected(int pos, Object value);
+ }
+}